// 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(/