// Serve a built Netlify site on an exe.dev VM: static files, redirects and // headers, Functions, Edge Functions, Blobs, Forms, and Image CDN URLs. // From https://exe.dev/deploy/migrate-netlify // // node site.mjs prepare DIR [CONTEXT] after the build: bundle functions, read the rules, // find the forms; writes DIR/.exe/manifest.json // node site.mjs serve production|preview // node site.mjs import-data DIR Blobs and form submissions from the hand-over // // It runs from ~/netlify, next to its node_modules. Data that outlives a // deploy (Blobs, form submissions) lives in ~/site/data. import fs from 'node:fs'; import fsp from 'node:fs/promises'; import path from 'node:path'; import http from 'node:http'; import crypto from 'node:crypto'; import net from 'node:net'; import { Readable } from 'node:stream'; import { AsyncLocalStorage } from 'node:async_hooks'; import { pathToFileURL } from 'node:url'; const HOME = process.env.HOME; const SITE = path.join(HOME, 'site'); const DATA = path.join(SITE, 'data'); const [cmd, arg, arg2] = process.argv.slice(2); // ---------------------------------------------------------------- prepare const exts = ['.mts', '.ts', '.mjs', '.js', '.cjs', '.cts', '.tsx', '.jsx']; async function readConfig(dir, context) { const { parse } = await import('smol-toml'); const file = path.join(dir, 'netlify.toml'); const toml = fs.existsSync(file) ? parse(fs.readFileSync(file, 'utf8')) : {}; const site = readJSON(path.join(HOME, 'netlify', 'site.json')) || {}; const bs = site.build_settings || {}; const build = { ...(toml.build || {}) }; const ctx = toml.context || {}; for (const c of [context, process.env.BRANCH]) if (c && ctx[c]) { Object.assign(build, ctx[c], { environment: { ...build.environment, ...ctx[c].environment } }); } const base = path.join(dir, build.base ?? bs.base ?? ''); const rel = (p, d) => (p == null || p === '' ? d : path.resolve(base, p)); return { toml, file, base, command: build.command ?? bs.cmd ?? '', publish: rel(build.publish ?? bs.dir, base), functions: rel(build.functions ?? toml.functions?.directory ?? bs.functions_dir, path.join(base, 'netlify/functions')), edge: rel(build.edge_functions, path.join(base, 'netlify/edge-functions')), server: path.join(base, 'netlify/server'), migrations: rel(toml.db?.migrations?.path, path.join(base, 'netlify/database/migrations')), environment: build.environment || {}, }; } function entries(dir) { // a functions directory: NAME -> file const out = {}; if (!fs.existsSync(dir)) return out; for (const e of fs.readdirSync(dir, { withFileTypes: true })) { const ext = path.extname(e.name); if (e.isFile() && exts.includes(ext)) out[path.basename(e.name, ext)] = path.join(dir, e.name); if (e.isDirectory()) for (const x of exts) for (const f of [e.name, 'index']) { const p = path.join(dir, e.name, f + x); if (!out[e.name] && fs.existsSync(p)) out[e.name] = p; } } return out; } async function bundle(file, outfile, edge) { const esbuild = await import('esbuild'); await esbuild.build({ entryPoints: [file], outfile, bundle: true, platform: 'node', format: 'esm', target: 'node22', packages: 'external', logLevel: 'error', sourcemap: 'inline', // CommonJS functions call require(); give the ES module one. banner: { js: "import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);" }, plugins: edge ? [{ name: 'deno-imports', // npm:pkg@1 -> pkg; URL imports stay and fail at load if used setup(b) { b.onResolve({ filter: /^npm:/ }, a => ({ path: a.path.slice(4).replace(/(?<=.)@[^/]*$/, '').replace(/^(@[^/]+\/[^@/]+)@[^/]*/, '$1'), external: true })); b.onResolve({ filter: /^https?:/ }, a => ({ path: a.path, external: true })); }, }] : [], }); } function findForms(publish) { const forms = {}, files = []; const walk = d => { for (const e of fs.readdirSync(d, { withFileTypes: true })) { const p = path.join(d, e.name); if (e.isDirectory()) walk(p); else if (/\.html?$/.test(e.name)) files.push(p); } }; if (fs.existsSync(publish)) walk(publish); for (const f of files) { let html = fs.readFileSync(f, 'utf8'), changed = false; html = html.replace(/]*>/gi, tag => { if (!/\s(data-)?netlify(=["']?true["']?)?[\s>\/]/i.test(tag)) return tag; const attr = n => (tag.match(new RegExp(`\\s${n}=["']([^"']*)["']`, 'i')) || [])[1]; const name = attr('name') || attr('id') || 'form'; forms[name] = { action: attr('action') || '', honeypot: attr('netlify-honeypot') || attr('data-netlify-honeypot') || '' }; changed = true; // Netlify adds this field when it processes the HTML; so do we return `${tag}`; }); if (changed) fs.writeFileSync(f, html.replace(/()(\s*]*>)/g, '$1')); } return forms; } async function prepare(dir, context = 'production') { dir = path.resolve(dir); const cfg = await readConfig(dir, context); const out = path.join(dir, '.exe'); fs.rmSync(out, { recursive: true, force: true }); fs.mkdirSync(path.join(out, 'functions'), { recursive: true }); fs.mkdirSync(path.join(out, 'edge'), { recursive: true }); const warnings = []; const tomlFns = cfg.toml.functions || {}; const functions = {}; for (const [name, file] of Object.entries(entries(cfg.functions))) { const outfile = path.join(out, 'functions', name + '.mjs'); await bundle(file, outfile, false); const mod = await import(pathToFileURL(outfile)); const v2 = typeof mod.default === 'function' || typeof mod.default?.fetch === 'function'; if (!v2 && !mod.handler && !mod.default?.handler) { warnings.push(`function ${name}: no default export or handler; skipped`); continue; } const c = (v2 && (mod.config || mod.default.config)) || {}; const schedule = c.schedule || tomlFns[name]?.schedule || ''; functions[name] = { file: outfile, v2, paths: [].concat(c.path || []), excluded: [].concat(c.excludedPath || []), method: [].concat(c.method || []), preferStatic: !!c.preferStatic, background: name.endsWith('-background') || !!c.background || !!tomlFns[name]?.background, schedule, }; } const edge = []; const files = entries(cfg.edge); const declared = (cfg.toml.edge_functions || []).map(d => ({ ...d, inline: false })); for (const [name, file] of Object.entries(files).sort()) { const outfile = path.join(out, 'edge', name + '.mjs'); await bundle(file, outfile, true); let mod; try { mod = await import(pathToFileURL(outfile)); } catch (e) { warnings.push(`edge function ${name}: ${e.message.split('\n')[0]}; port it to plain imports`); continue; } const c = mod.config || {}; for (const p of [].concat(c.path || [])) declared.push({ function: name, path: p, excludedPath: c.excludedPath, inline: true }); for (const p of [].concat(c.pattern || [])) declared.push({ function: name, pattern: p, excludedPattern: c.excludedPattern, inline: true }); files[name] = outfile; } for (const d of declared) { if (!files[d.function] || !files[d.function].endsWith('.mjs') || !files[d.function].includes('/.exe/')) { warnings.push(`edge function ${d.function}: declared but not built`); continue; } edge.push({ name: d.function, file: files[d.function], path: d.path || '', pattern: d.pattern || '', excluded: [].concat(d.excludedPath || []), excludedPattern: [].concat(d.excludedPattern || []) }); } const { parseAllRedirects } = await import('@netlify/redirect-parser'); const { parseAllHeaders } = await import('@netlify/headers-parser'); const toml = fs.existsSync(cfg.file) ? cfg.file : undefined; const r = await parseAllRedirects({ redirectsFiles: [path.join(cfg.publish, '_redirects')], netlifyConfigPath: toml, configRedirects: [], minimal: false }); const h = await parseAllHeaders({ headersFiles: [path.join(cfg.publish, '_headers')], netlifyConfigPath: toml, configHeaders: [], minimal: false }); for (const e of [...r.errors, ...h.errors]) warnings.push(String(e.message || e)); for (const x of r.redirects) { for (const k of Object.keys(x.conditions || {})) if (!['Language', 'Cookie'].includes(k)) warnings.push(`redirect ${x.from}: condition ${k} is never true here`); if (x.signed) warnings.push(`redirect ${x.from}: signed proxy headers aren't sent`); } // Netlify Server: one entry file, netlify/server/index.{ts,mts,js,mjs}. let server = null; const sfile = ['index.ts', 'index.mts', 'index.js', 'index.mjs'].map(f => path.join(cfg.server, f)).find(f => fs.existsSync(f)); if (sfile) { const outfile = path.join(out, 'server.mjs'); await bundle(sfile, outfile, false); // [server] in netlify.toml; serve adds the entry's own config, which wins setting by setting. // (Not loaded here: loading a server can start it.) server = { file: outfile, config: cfg.toml.server || {} }; } // Netlify Database: migrations, applied by deploy before the switch. const migrations = []; if (fs.existsSync(cfg.migrations)) { for (const e of fs.readdirSync(cfg.migrations, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : 1))) { const mm = e.name.match(/^(\d+)_.+?(\.sql)?$/); if (!mm) continue; if (e.isDirectory() && fs.existsSync(path.join(cfg.migrations, e.name, 'migration.sql'))) migrations.push({ name: e.name, file: path.join(cfg.migrations, e.name, 'migration.sql') }); else if (e.isFile() && mm[2]) migrations.push({ name: e.name.replace(/\.sql$/, ''), file: path.join(cfg.migrations, e.name) }); } } const forms = findForms(cfg.publish); const images = { remote: [].concat(cfg.toml.images?.remote_images || []) }; const manifest = { context, publish: cfg.publish, functions, edge, server, migrations, redirects: r.redirects, headers: h.headers, forms, images, warnings }; fs.writeFileSync(path.join(out, 'manifest.json'), JSON.stringify(manifest, null, 2)); for (const w of warnings) console.error(`warning: ${w}`); console.log(`${Object.keys(functions).length} functions, ${edge.length} edge function routes, ${server ? 'a server, ' : ''}${migrations.length ? migrations.length + ' database migrations, ' : ''}${r.redirects.length} redirects, ${h.headers.length} header rules, ${Object.keys(forms).length} forms`); } // ---------------------------------------------------------------- import-data // What the hand-over copied from Netlify, into ~/site/data, replacing what's there. // DIR/blobs/STORE/KEY.data and KEY.meta.json (names URI-encoded); DIR/forms/NAME.json. async function importData(dir) { const { BlobsServer } = await import('@netlify/blobs/server'); const { getStore } = await import('@netlify/blobs'); fs.rmSync(path.join(DATA, 'blobs'), { recursive: true, force: true }); const token = crypto.randomBytes(16).toString('hex'); const server = new BlobsServer({ directory: path.join(DATA, 'blobs'), token, port: 0 }); const { port } = await server.start(); let n = 0; const bdir = path.join(dir, 'blobs'); for (const store of fs.existsSync(bdir) ? fs.readdirSync(bdir) : []) { const st = getStore({ name: decodeURIComponent(store), siteID: 'exe-dev', token, edgeURL: `http://127.0.0.1:${port}` }); for (const f of fs.readdirSync(path.join(bdir, store)).filter(f => f.endsWith('.data'))) { const metadata = readJSON(path.join(bdir, store, f.replace(/\.data$/, '.meta.json'))) || {}; await st.set(decodeURIComponent(f.replace(/\.data$/, '')), new Blob([fs.readFileSync(path.join(bdir, store, f))]), { metadata }); n++; } } await server.stop(); console.log(`blobs: ${n} entries`); const fdir = path.join(dir, 'forms'); for (const f of fs.existsSync(fdir) ? fs.readdirSync(fdir) : []) { const name = path.basename(f, '.json'); const subs = (readJSON(path.join(fdir, f)) || []).sort((a, b) => (a.created_at < b.created_at ? -1 : 1)); const lines = subs.map(s => { const { ip, user_agent, referrer, ...data } = s.data || {}; return JSON.stringify({ id: s.id, form_name: name, created_at: s.created_at, data, ip, user_agent, referrer, from: 'netlify' }) + '\n'; }); fs.mkdirSync(path.join(DATA, 'forms'), { recursive: true }); fs.writeFileSync(path.join(DATA, 'forms', `${name}.jsonl`), lines.join('')); console.log(`form ${name}: ${subs.length} submissions`); } } // ---------------------------------------------------------------- serve function readJSON(f) { try { return JSON.parse(fs.readFileSync(f, 'utf8')); } catch { return null; } } const types = { html: 'text/html; charset=utf-8', htm: 'text/html; charset=utf-8', css: 'text/css; charset=utf-8', js: 'text/javascript; charset=utf-8', mjs: 'text/javascript; charset=utf-8', json: 'application/json', map: 'application/json', txt: 'text/plain; charset=utf-8', xml: 'application/xml', svg: 'image/svg+xml', png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp', avif: 'image/avif', ico: 'image/x-icon', woff: 'font/woff', woff2: 'font/woff2', ttf: 'font/ttf', otf: 'font/otf', pdf: 'application/pdf', mp4: 'video/mp4', webm: 'video/webm', mp3: 'audio/mpeg', wasm: 'application/wasm', webmanifest: 'application/manifest+json', rss: 'application/rss+xml', atom: 'application/atom+xml', csv: 'text/csv', zip: 'application/zip', }; async function serve(instance) { const port = Number(process.env.PORT || (instance === 'preview' ? 8001 : 8000)); const root = fs.realpathSync(path.join(SITE, instance)); const m = readJSON(path.join(root, '.exe', 'manifest.json')); const site = readJSON(path.join(HOME, 'netlify', 'site.json')) || {}; const env = readJSON(path.join(SITE, instance === 'preview' ? 'env.preview.json' : 'env.json')) || {}; const host = fs.existsSync(path.join(SITE, 'domain')) ? fs.readFileSync(path.join(SITE, 'domain'), 'utf8').trim() : ''; const deployID = path.basename(root); Object.assign(process.env, env, { SITE_ID: site.id || 'exe-dev', SITE_NAME: site.name || '', CONTEXT: m.context, DEPLOY_ID: deployID, URL: process.env.URL || `https://${host || process.env.VM_HOST}`, }); // Blobs: Netlify's own local Blobs server, on files in ~/site/data/blobs. const { BlobsServer } = await import('@netlify/blobs/server'); const token = crypto.randomBytes(16).toString('hex'); const blobs = new BlobsServer({ directory: path.join(DATA, 'blobs'), token, port: 0 }); const { port: bport } = await blobs.start(); process.env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify({ edgeURL: `http://127.0.0.1:${bport}`, uncachedEdgeURL: `http://127.0.0.1:${bport}`, token, siteID: 'exe-dev', deployID, })).toString('base64'); // Netlify Database: Postgres on this VM, a database per instance. if (fs.existsSync(path.join(SITE, 'postgres')) && !process.env.NETLIFY_DB_URL) { process.env.NETLIFY_DB_URL = `postgresql:///${instance === 'preview' ? 'site_preview' : 'site'}?host=/var/run/postgresql`; process.env.NETLIFY_DB_DRIVER = 'server'; // node-postgres, not the HTTP driver Netlify uses } // The request context, where Netlify's libraries look for it (getContext() in @netlify/server). const als = new AsyncLocalStorage(); globalThis[Symbol.for('@netlify/functions/request-context-store')] = als; globalThis.Netlify = { env: { get: k => process.env[k], has: k => k in process.env, set: (k, v) => { process.env[k] = v; }, delete: k => { delete process.env[k]; }, toObject: () => ({ ...process.env }) }, get context() { return als.getStore()?.context || null; }, }; globalThis.Deno ??= { env: globalThis.Netlify.env }; const mods = {}; const load = async f => (mods[f] ??= await import(pathToFileURL(f))); const pattern = p => new URLPattern({ pathname: p }); for (const f of Object.values(m.functions)) f.patterns = f.paths.map(pattern), f.excludedP = f.excluded.map(pattern); for (const e of m.edge) { e.test = e.pattern ? (re => p => re.test(p))(new RegExp(e.pattern)) : (pt => p => pt.test({ pathname: p }))(pattern(e.path)); e.skip = [...e.excluded.map(pattern).map(x => p => x.test({ pathname: p })), ...e.excludedPattern.map(x => (re => p => re.test(p))(new RegExp(x)))]; } const pub = m.publish; // A file in the publish directory for a URL path, like Netlify: exact, .html, /index.html. const file = p => { let rel; try { rel = decodeURIComponent(p); } catch { return null; } const base = path.join(pub, path.normalize(rel)); if (!base.startsWith(pub)) return null; const cands = rel.endsWith('/') ? [path.join(base, 'index.html'), base.replace(/\/+$/, '') + '.html'] : [base, base + '.html', path.join(base, 'index.html')]; for (const c of cands) { try { if (fs.statSync(c).isFile()) return c; } catch {} } return null; }; const headerRules = m.headers.map(h => ({ ...h, re: globRe(h.for) })); const withHeaders = (p, res) => { for (const h of headerRules) if (h.re.test(p)) for (const [k, v] of Object.entries(h.values)) if (k.toLowerCase() !== 'basic-auth') res.headers.set(k, v); return res; }; const sendFile = async (req, f, status = 200) => { const st = fs.statSync(f); const etag = `"${st.size.toString(16)}-${st.mtimeMs.toString(16)}"`; const headers = new Headers({ 'content-type': types[path.extname(f).slice(1).toLowerCase()] || 'application/octet-stream', 'cache-control': 'public, max-age=0, must-revalidate', etag }); if (status === 200 && req.headers.get('if-none-match') === etag) return new Response(null, { status: 304, headers }); headers.set('content-length', String(st.size)); const body = req.method === 'HEAD' ? null : Readable.toWeb(fs.createReadStream(f)); return new Response(body, { status, headers }); }; const notFound = req => { const f = file('/404.html'); return f ? sendFile(req, f, 404) : new Response('Not Found\n', { status: 404, headers: { 'content-type': 'text/plain' } }); }; const ctxFor = (req, url, params = {}) => { const setCookies = []; const jar = Object.fromEntries((req.headers.get('cookie') || '').split(/;\s*/).filter(Boolean).map(c => [c.split('=')[0], decodeURIComponent(c.slice(c.indexOf('=') + 1))])); return { params, url, ip: (req.headers.get('x-forwarded-for') || '').split(',')[0].trim() || '127.0.0.1', geo: {}, requestId: crypto.randomUUID(), server: { region: 'exe.dev' }, account: { id: '' }, site: { id: process.env.SITE_ID, name: process.env.SITE_NAME, url: process.env.URL }, deploy: { id: deployID, context: m.context, published: instance === 'production' }, cookies: { get: n => jar[n], set: (n, v) => { const o = typeof n === 'object' ? n : { name: n, value: v }; setCookies.push(`${o.name}=${encodeURIComponent(o.value)}; Path=${o.path || '/'}${o.domain ? '; Domain=' + o.domain : ''}${o.expires ? '; Expires=' + new Date(o.expires).toUTCString() : ''}${o.maxAge != null ? '; Max-Age=' + o.maxAge : ''}${o.httpOnly ? '; HttpOnly' : ''}${o.secure ? '; Secure' : ''}${o.sameSite ? '; SameSite=' + o.sameSite : ''}`); }, delete: n => setCookies.push(`${typeof n === 'object' ? n.name : n}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT`), }, waitUntil: p => Promise.resolve(p).catch(e => console.error('waitUntil:', e)), json: o => Response.json(o), _cookies: setCookies, }; }; const finish = (ctx, res) => { if (!ctx._cookies.length) return res; res = new Response(res.body, res); for (const c of ctx._cookies) res.headers.append('set-cookie', c); return res; }; const runFunction = async (name, req, url, params = {}) => { const fn = m.functions[name]; const mod = await load(fn.file); const ctx = ctxFor(req, url, params); const call = async () => { if (fn.v2) return als.run({ context: ctx }, () => (typeof mod.default === 'function' ? mod.default(req, ctx) : mod.default.fetch(req, ctx))); const handler = mod.handler || mod.default.handler; const buf = Buffer.from(await req.arrayBuffer()); const text = /^(text\/|application\/(json|x-www-form-urlencoded|xml|javascript))/.test(req.headers.get('content-type') || '') || !buf.length; const q = {}, mq = {}; for (const [k, v] of url.searchParams) { q[k] = v; (mq[k] ??= []).push(v); } const event = { path: url.pathname, httpMethod: req.method, headers: Object.fromEntries(req.headers), multiValueHeaders: Object.fromEntries([...req.headers].map(([k, v]) => [k, [v]])), queryStringParameters: q, multiValueQueryStringParameters: mq, rawUrl: url.href, rawQuery: url.search.slice(1), body: buf.length ? buf.toString(text ? 'utf8' : 'base64') : null, isBase64Encoded: !text, }; const lctx = { functionName: name, awsRequestId: ctx.requestId, clientContext: {}, callbackWaitsForEmptyEventLoop: true }; const r = await new Promise((resolve, reject) => { const p = handler(event, lctx, (err, res) => (err ? reject(err) : resolve(res))); if (p && typeof p.then === 'function') p.then(resolve, reject); }); if (!r) return new Response(null, { status: 204 }); const headers = new Headers(r.headers || {}); for (const [k, vs] of Object.entries(r.multiValueHeaders || {})) for (const v of vs) headers.append(k, v); const body = r.body == null ? null : r.isBase64Encoded ? Buffer.from(r.body, 'base64') : String(r.body); return new Response(body, { status: r.statusCode || 200, headers }); }; if (fn.background) { call().catch(e => console.error(`function ${name} (background):`, e)); return new Response(null, { status: 202 }); } try { const res = await call(); return finish(ctx, res instanceof Response ? res : new Response(null, { status: 204 })); } catch (e) { console.error(`function ${name}:`, e); return new Response('Function error\n', { status: 500 }); } }; // Which function, if any, answers this path. Scheduled functions don't answer URLs. const routeFunction = (req, url) => { const p = url.pathname; for (const [name, fn] of Object.entries(m.functions)) { if (fn.schedule) continue; if (fn.method.length && !fn.method.includes(req.method)) continue; if (fn.excludedP.some(x => x.test({ pathname: p }))) continue; if (fn.patterns.length) { for (const pt of fn.patterns) { const r = pt.exec({ pathname: p }); if (r && !(fn.preferStatic && file(p))) return [name, Object.fromEntries(Object.entries(r.pathname.groups).filter(([k]) => isNaN(k)))]; } } else if (p === `/.netlify/functions/${name}` || p.startsWith(`/.netlify/functions/${name}/`)) return [name, {}]; } return null; }; const forms = async (req, url) => { const ct = req.headers.get('content-type') || ''; if (!/^(application\/x-www-form-urlencoded|multipart\/form-data)/.test(ct)) return null; let fd; try { fd = await req.clone().formData(); } catch { return null; } const name = fd.get('form-name'); if (name == null) return null; const form = m.forms[name]; if (!form) return new Response(`No form named ${name} in the site's HTML\n`, { status: 404, headers: { 'content-type': 'text/plain' } }); const id = crypto.randomUUID(); const data = {}, dir = path.join(DATA, 'forms', name); for (const [k, v] of fd) { if (k === 'form-name') continue; if (typeof v === 'string') { data[k] = v; continue; } if (!v.size) continue; await fsp.mkdir(path.join(dir, id), { recursive: true }); const f = path.join(dir, id, path.basename(v.name || k)); await fsp.writeFile(f, Buffer.from(await v.arrayBuffer())); data[k] = { filename: v.name, type: v.type, size: v.size, path: f }; } const spam = !!(form.honeypot && data[form.honeypot]); const sub = { id, form_name: name, created_at: new Date().toISOString(), data, ip: ctxFor(req, url).ip, user_agent: req.headers.get('user-agent') || '', referrer: req.headers.get('referer') || '' }; await fsp.mkdir(dir, { recursive: true }); await fsp.appendFile(path.join(DATA, 'forms', `${name}${spam ? '.spam' : ''}.jsonl`), JSON.stringify(sub) + '\n'); if (!spam && instance === 'production') { const to = process.env.FORMS_EMAIL; if (to) fetch('http://169.254.169.254/gateway/email/send', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ to, subject: `New submission to ${name} on ${new URL(process.env.URL).host}`, body: Object.entries(data).map(([k, v]) => `${k}: ${typeof v === 'string' ? v : v.filename}`).join('\n') }) }) .catch(e => console.error('forms email:', e.message)); if (m.functions['submission-created']) { const ev = new Request(new URL('/.netlify/functions/submission-created', url), { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ payload: { ...sub, human_fields: data } }) }); runFunction('submission-created', ev, new URL(ev.url)).catch(e => console.error('submission-created:', e)); } } if (form.action) return new Response(null, { status: 303, headers: { location: form.action } }); return new Response('Thank you

Thank you! Your form submission has been received.

Back to our site

\n', { headers: { 'content-type': 'text/html; charset=utf-8' } }); }; const images = async (req, url) => { const src = url.searchParams.get('url'); if (!src) return new Response('missing url\n', { status: 400 }); let input; if (/^https?:/.test(src)) { if (!m.images.remote.some(r => new RegExp(r).test(src))) return new Response('remote image not allowed\n', { status: 400 }); const r = await fetch(src); if (!r.ok) return new Response('image not found\n', { status: 404 }); input = Buffer.from(await r.arrayBuffer()); } else { const f = file(new URL(src, url).pathname); if (!f) return new Response('image not found\n', { status: 404 }); input = fs.readFileSync(f); } const q = Object.fromEntries(url.searchParams); let fm = q.fm || (/image\/avif/.test(req.headers.get('accept') || '') ? 'avif' : /image\/webp/.test(req.headers.get('accept') || '') ? 'webp' : ''); if (fm === 'jpg') fm = 'jpeg'; const key = crypto.createHash('sha256').update(input).update(JSON.stringify([q.w, q.h, q.fit, q.position, fm, q.q])).digest('hex'); const cached = path.join(SITE, 'cache', 'images', key); if (!fs.existsSync(cached)) { const sharp = (await import('sharp')).default; let img = sharp(input, { animated: true }); if (q.w || q.h) img = img.resize({ width: +q.w || undefined, height: +q.h || undefined, fit: { contain: 'contain', cover: 'cover', fill: 'fill' }[q.fit] || (q.w && q.h ? 'cover' : 'inside'), position: q.position || 'centre', withoutEnlargement: true }); if (fm) img = img.toFormat(fm, q.q ? { quality: +q.q } : {}); const { data, info } = await img.toBuffer({ resolveWithObject: true }); await fsp.mkdir(path.dirname(cached), { recursive: true }); await fsp.writeFile(cached + '.type', `image/${info.format === 'heif' ? 'avif' : info.format}`); await fsp.writeFile(cached, data); } return new Response(fs.readFileSync(cached), { headers: { 'content-type': fs.readFileSync(cached + '.type', 'utf8'), 'cache-control': 'public, max-age=31536000, immutable', vary: 'Accept' } }); }; // Redirect rules, in order. Static files win unless the rule is forced. const redirect = async (req, url) => { const p = url.pathname; for (const r of m.redirects) { if (r.host && r.host !== url.host) continue; const match = matchPath(r.path ?? r.from, p); if (!match) continue; let ok = true; for (const [k, v] of Object.entries(r.query || {})) { if (!url.searchParams.has(k)) { ok = false; break; } if (v.startsWith(':')) match[v.slice(1)] = url.searchParams.get(k); else if (url.searchParams.get(k) !== v) ok = false; } for (const [k, v] of Object.entries(r.conditions || {})) { const vals = [].concat(v).map(x => String(x).toLowerCase()); if (k === 'Language') { const al = (req.headers.get('accept-language') || '').toLowerCase().split(/[,;]/)[0].trim(); if (!vals.some(x => al === x || al.startsWith(x + '-'))) ok = false; } else if (k === 'Cookie') { const has = (req.headers.get('cookie') || '').split(/;\s*/).map(c => c.split('=')[0].toLowerCase()); if (!vals.some(x => has.includes(x))) ok = false; } else ok = false; } if (!ok) continue; if (!r.force && file(p)) return null; let to = r.to.replace(/:([a-zA-Z_]\w*)/g, (s, k) => (k in match ? match[k] : s)); const dest = new URL(to, url); if (!r.query || !Object.keys(r.query).length) if (!dest.search && url.search) dest.search = url.search; const status = r.status || 301; if (status >= 300 && status < 400) return new Response(null, { status, headers: { location: /^https?:/.test(to) ? dest.href : dest.pathname + dest.search + dest.hash } }); if (/^https?:/.test(to)) { // a proxy const headers = new Headers(req.headers); headers.delete('host'); for (const [k, v] of Object.entries(r.headers || {})) headers.set(k, v); const res = await fetch(dest, { method: req.method, headers, body: ['GET', 'HEAD'].includes(req.method) ? undefined : req.body, duplex: 'half', redirect: 'manual' }); const h = new Headers(res.headers); h.delete('content-encoding'); h.delete('content-length'); return new Response(res.body, { status: status === 200 ? res.status : status, headers: h }); } const inner = await origin(new Request(dest, req), dest, false); return status === 200 ? inner : new Response(inner.body, { status, headers: inner.headers }); } return null; }; // Everything after the edge functions: functions, forms, redirects, files. const origin = async (req, url, rules = true) => { if (url.pathname === '/.netlify/images') return images(req, url); const fn = routeFunction(req, url); if (fn) return runFunction(fn[0], req, url, fn[1]); if (rules && req.method === 'POST') { const r = await forms(req, url); if (r) return r; } if (rules) { const r = await redirect(req, url); if (r) return r; } const f = file(url.pathname); if (f) return withHeaders(url.pathname, await sendFile(req, f)); if (server?.routes(url.pathname)) return withHeaders(url.pathname, await server.run(req, url)); return withHeaders(url.pathname, await notFound(req)); }; // Netlify Server: the app in netlify/server, run in this process, hosted the way // Netlify hosts it. It answers what nothing else does (or its config.path). const nodePrimitives = Symbol.for('$netlify.requestNodePrimitives'); const upgraded = Symbol.for('$netlify.responseUpgraded'); let server = null; if (m.server) { const life = globalThis[Symbol.for('@netlify/server/lifecycle')] ??= { start: [], shutdown: [], health: [], started: false }; const inner = port + 100; // where a Node.js server listens, on loopback const contexts = new Map(); // A Node.js server's requests run in their Netlify context too. const emit = http.Server.prototype.emit; http.Server.prototype.emit = function (ev, nreq, ...rest) { const ctx = (ev === 'request' || ev === 'upgrade') && contexts.get(nreq.headers['x-exe-context']); if (!ctx) return emit.call(this, ev, nreq, ...rest); delete nreq.headers['x-exe-context']; return als.run({ context: ctx }, () => emit.call(this, ev, nreq, ...rest)); }; process.env.PORT = String(inner); const mod = await import(pathToFileURL(m.server.file)); for (const h of life.start) await h(); life.started = true; const d = mod.default; const c = { ...m.server.config, ...(mod.config || (typeof d?.fetch === 'function' && d.config) || {}) }; const listen = s => new Promise((ok, fail) => s.once('error', fail).listen(inner, '127.0.0.1', ok)); let kind; if (d instanceof http.Server) { kind = 'node'; if (!d.listening) await listen(d); } else if (typeof d?.fetch === 'function') kind = 'fetch'; else if (typeof d === 'function' && ((typeof d.handle === 'function' && typeof d.use === 'function') || d.length >= 3)) { kind = 'node'; await listen(http.createServer(d)); } else if (typeof d === 'function') kind = 'handler'; else { // it starts itself, on PORT kind = 'node'; const up = () => new Promise(ok => net.connect(inner, '127.0.0.1').once('connect', function () { this.destroy(); ok(true); }).once('error', () => ok(false))); let t = 0; while (!(await up())) { if ((t += 100) > 10000) throw new Error(`the server in netlify/server didn't listen on PORT (${inner}) within 10 s`); await new Promise(r => setTimeout(r, 100)); } } const paths = [].concat(c.path || []).map(pattern), excluded = [].concat(c.excludedPath || []).map(pattern); server = { kind, routes: p => !p.startsWith('/.netlify/') && (!paths.length || paths.some(x => x.test({ pathname: p }))) && !excluded.some(x => x.test({ pathname: p })), run: async (req, url) => { const ctx = ctxFor(req, url); try { if (kind === 'fetch') return finish(ctx, await als.run({ context: ctx }, () => d.fetch(req, ctx))); if (kind === 'handler') return finish(ctx, (await als.run({ context: ctx }, () => d(req, ctx))) || new Response(null, { status: 204 })); const id = crypto.randomUUID(); contexts.set(id, ctx); try { const headers = Object.fromEntries(req.headers); Object.assign(headers, { host: url.host, 'x-forwarded-host': url.host, 'x-forwarded-proto': url.protocol.slice(0, -1), 'x-exe-context': id }); const res = await new Promise((ok, fail) => { const r = http.request({ host: '127.0.0.1', port: inner, method: req.method, path: url.pathname + url.search, headers }, ok).on('error', fail); if (req.body) Readable.fromWeb(req.body).on('error', fail).pipe(r); else r.end(); }); const h = new Headers(); for (let i = 0; i < res.rawHeaders.length; i += 2) { if (!/^(connection|keep-alive|transfer-encoding)$/i.test(res.rawHeaders[i])) h.append(res.rawHeaders[i], res.rawHeaders[i + 1]); } const empty = req.method === 'HEAD' || [204, 205, 304].includes(res.statusCode); return new Response(empty ? null : Readable.toWeb(res), { status: res.statusCode, headers: h }); } finally { contexts.delete(id); } } catch (e) { console.error('server:', e); return new Response('Server error\n', { status: 500 }); } }, // WebSockets: upgradeWebSocket() from @netlify/server, or a Node.js server's own 'upgrade'. upgrade: async (nreq, socket, head, url) => { const ctx = ctxFor(toRequest(nreq, 'https'), url); if (kind === 'node') { const id = crypto.randomUUID(); contexts.set(id, ctx); const up = net.connect(inner, '127.0.0.1', () => { const lines = [`${nreq.method} ${nreq.url} HTTP/1.1`, `x-exe-context: ${id}`]; for (let i = 0; i < nreq.rawHeaders.length; i += 2) lines.push(`${nreq.rawHeaders[i]}: ${nreq.rawHeaders[i + 1]}`); up.write(lines.join('\r\n') + '\r\n\r\n'); up.write(head); socket.pipe(up).pipe(socket); setTimeout(() => contexts.delete(id), 10000); }); up.on('error', () => socket.destroy()); socket.on('error', () => up.destroy()); return; } const req = toRequest(nreq, 'https'); req[nodePrimitives] = { request: nreq, socket, head }; const res = await als.run({ context: ctx }, () => (kind === 'fetch' ? d.fetch(req, ctx) : d(req, ctx))); if (res?.[upgraded]) return; socket.end(`HTTP/1.1 ${res?.status || 400} ${http.STATUS_CODES[res?.status || 400]}\r\nconnection: close\r\ncontent-length: 0\r\n\r\n`); }, }; // onShutdown() hooks run when the service stops or restarts. process.once('SIGTERM', async () => { const timer = setTimeout(() => process.exit(0), 10000); for (const h of life.shutdown) await Promise.resolve(h({ drained: Promise.resolve() })).catch(e => console.error('onShutdown:', e)); clearTimeout(timer); process.exit(0); }); } // Basic-Auth rules in _headers protect paths, as on Netlify. const auth = (req, url) => { for (const h of headerRules) { const v = Object.entries(h.values).find(([k]) => k.toLowerCase() === 'basic-auth'); if (!v || !h.re.test(url.pathname)) continue; const given = Buffer.from((req.headers.get('authorization') || '').replace(/^Basic /i, ''), 'base64').toString(); if (!String(v[1]).split(/\s+/).includes(given)) return new Response('Unauthorized\n', { status: 401, headers: { 'www-authenticate': 'Basic realm="site"' } }); } return null; }; const handle = async req => { const url = new URL(req.url); const denied = auth(req, url); if (denied) return denied; const chain = m.edge.filter(e => e.test(url.pathname) && !e.skip.some(s => s(url.pathname))); const step = async (i, r) => { if (i === chain.length) { const res = await origin(r, new URL(r.url)); return new Response(res.body, res); } // mutable headers const mod = await load(chain[i].file); const ctx = ctxFor(r, new URL(r.url)); ctx.next = (nr = r) => step(i + 1, nr instanceof Request ? nr : r); ctx.rewrite = to => origin(new Request(new URL(to, r.url), r), new URL(to, r.url)); const res = await als.run({ context: ctx }, () => mod.default(r, ctx)); if (res instanceof URL) return ctx.rewrite(res); return res instanceof Response ? finish(ctx, res) : step(i + 1, r); }; return step(0, req); }; const toRequest = (nreq, scheme) => { const h = nreq.headers; const url = `${h['x-forwarded-proto'] || scheme}://${h['x-forwarded-host'] || h.host}${nreq.url}`; const body = ['GET', 'HEAD'].includes(nreq.method) ? undefined : Readable.toWeb(nreq); return new Request(url, { method: nreq.method, headers: Object.entries(h).flatMap(([k, v]) => [].concat(v).map(x => [k, x])), body, duplex: 'half' }); }; const write = async (res, nres) => { const headers = {}; for (const [k, v] of res.headers) if (k !== 'set-cookie') headers[k] = v; const cookies = res.headers.getSetCookie(); if (cookies.length) headers['set-cookie'] = cookies; nres.writeHead(res.status, headers); if (res.body) for await (const chunk of res.body) nres.write(chunk); nres.end(); }; const listener = (internal) => async (nreq, nres) => { const t = Date.now(); try { let req = toRequest(nreq, 'https'); let res; // The loopback port runs scheduled functions, for cron. const sched = internal && new URL(req.url).pathname.match(/^\/\.netlify\/functions\/([^/]+)$/); if (sched && m.functions[sched[1]]?.schedule) res = await runFunction(sched[1], req, new URL(req.url)); else res = await handle(req); await write(res, nres); if (process.env.LOG_REQUESTS) console.log(`${nreq.method} ${nreq.url} ${res.status} ${Date.now() - t}ms`); } catch (e) { console.error(`${nreq.method} ${nreq.url}:`, e); if (!nres.headersSent) nres.writeHead(500, { 'content-type': 'text/plain' }); nres.end('Internal error\n'); } }; http.createServer(listener(false)) .on('upgrade', (nreq, socket, head) => { const url = new URL(nreq.url, `https://${nreq.headers.host}`); if (!server?.routes(url.pathname)) return socket.destroy(); server.upgrade(nreq, socket, head, url).catch(e => { console.error('upgrade:', e); socket.destroy(); }); }) .listen(port, () => console.log(`serving ${root} (${m.context}${server ? `, ${server.kind} server` : ''}) on :${port}`)); if (instance === 'production') http.createServer(listener(true)).listen(8099, '127.0.0.1'); } // Netlify path patterns: /blog/:slug, /docs/*. A trailing slash doesn't matter. function matchPath(from, p) { const strip = s => (s.length > 1 ? s.replace(/\/+$/, '') : s); const a = strip(from).split('/'), b = strip(p).split('/'), out = {}; for (let i = 0; i < a.length; i++) { if (a[i] === '*') { out.splat = b.slice(i).join('/'); return out; } if (i >= b.length) return null; if (a[i].startsWith(':')) { out[a[i].slice(1)] = decodeURIComponent(b[i]); continue; } if (a[i] !== b[i]) return null; } return a.length === b.length ? out : null; } function globRe(pat) { // a header rule's path: * and :placeholder const s = pat.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/:[a-zA-Z_]\w*/g, '[^/]+'); return new RegExp(`^${s}/?$`); } if (cmd === 'prepare') await prepare(arg, arg2); else if (cmd === 'serve') await serve(arg || 'production'); else if (cmd === 'import-data') await importData(path.resolve(arg)); else if (cmd === 'config') { // for the deploy script: the build settings, as shell variables const c = await readConfig(path.resolve(arg), arg2); const q = s => `'${String(s).replace(/'/g, `'\\''`)}'`; console.log(`base=${q(c.base)}\ncommand=${q(c.command)}\npublish=${q(c.publish)}`); for (const [k, v] of Object.entries(c.environment)) console.log(`export ${k}=${q(v)}`); } else { console.error('usage: node site.mjs prepare DIR [CONTEXT] | serve production|preview | config DIR [CONTEXT] | import-data DIR'); process.exit(2); }