// Conv3d's steps around scripts/carry-static.mjs (copied from hatchkit // cli/src/templates/client-host/scripts/, c70206f) for the conv3d-docs Worker. // See scripts/WORKER-RELEASES.md. // // stage this release's /_release// files // carry-static.mjs --site … --out --dir _next/static --dir _release // finish releases.json, _headers, limit checks // seed-image first version, from the last nginx image import { createHash } from "node:crypto"; import { cp, lstat, mkdir, readFile, readdir, writeFile } from "node:fs/promises"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { carry } from "./carry-static.mjs"; import { DOCS_BOOTSTRAP } from "./docs-bootstrap.mjs"; export const ORIGIN = "https://conv3d.trebeljahr.com"; // Workers Free: 20,000 files per version, 25 MiB per file, 100 _headers rules. export const LIMITS = Object.freeze({ files: 20000, fileBytes: 25 * 1024 * 1024, headerRules: 100, }); export const CARRY_DIRS = ["_next/static", "_release"]; const isSha = (value) => /^[a-f0-9]{40}$/.test(value ?? ""); const SEGMENT = /^[A-Za-z0-9_.~-]+$/; // Runtime-fetched public files, addressed by release SHA (docs/lib/release-asset.ts). const RELEASE_DIRS = ["models", "draco", "media"]; const RELEASE_PATH = /^\/_release\/([a-f0-9]{40})\/(?:(?:models|draco|media)\/.+|api\/search)$/; // Root names written here or by carry-static, or used only by the nginx image. const RESERVED = [ "_headers", "_redirects", ".assetsignore", "_release", "releases.json", "release-assets.json", "__releases", "__legacy-assets", ]; const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex"); async function walk(root, prefix = "") { const found = []; for (const entry of await readdir(join(root, prefix), { withFileTypes: true })) { const relative = prefix ? `${prefix}/${entry.name}` : entry.name; if (entry.isSymbolicLink()) throw new Error(`Symlinks are not allowed: ${relative}`); if (entry.isDirectory()) found.push(...(await walk(root, relative))); else if (entry.isFile()) found.push(relative); else throw new Error(`Unsupported file: ${relative}`); } return found.sort(); } async function exists(path) { try { await lstat(path); return true; } catch (error) { if (error.code === "ENOENT") return false; throw error; } } async function exportCommit(directory) { return JSON.parse(await readFile(join(directory, "version.json"), "utf8")).commit; } // URL path → source file of the runtime-fetched files one release export holds. async function releaseFiles(exportDir, id) { const sources = new Map(); for (const dir of RELEASE_DIRS) if (await exists(join(exportDir, dir))) for (const file of await walk(exportDir, dir)) { if (!file.split("/").every((segment) => SEGMENT.test(segment))) throw new Error(`Unsupported public file name: ${file}`); sources.set(`/_release/${id}/${file}`, join(exportDir, file)); } if (await exists(join(exportDir, "api", "search"))) sources.set(`/_release/${id}/api/search`, join(exportDir, "api", "search")); return sources; } /** Copies this release's runtime-fetched files under /_release//. */ export async function stage(exportDir, sha) { if (!isSha(sha) || (await exportCommit(exportDir)) !== sha) throw new Error("Export revision differs from the release SHA."); for (const name of RESERVED) if (await exists(join(exportDir, name))) throw new Error(`The export must not contain ${name}.`); const files = await releaseFiles(exportDir, sha); for (const [path, source] of files) { const target = join(exportDir, path); await mkdir(dirname(target), { recursive: true }); await writeFile(target, await readFile(source)); } return { head: sha, staged: files.size }; } export function headersFile(sha, releases = [sha]) { if (!isSha(sha) || !releases.every(isSha)) throw new Error("A full commit SHA is required."); return [ `# Generated by scripts/worker-release.mjs for ${sha}.`, "# Exported Flight data has no build ID, so Next compares this header with", "# its deploymentId. A different release makes the client load the page.", "/*", ` x-nextjs-deployment-id: ${sha}`, "/version.json", " Cache-Control: no-store", "/releases.json", " Cache-Control: no-store", "/release-assets.json", " Cache-Control: no-store", "# Exported route handlers have no file extension.", "/api/search", " Content-Type: application/json", ...releases.flatMap((id) => [`/_release/${id}/api/search`, " Content-Type: application/json"]), "", ].join("\n"); } export async function checkLimits(outDir) { const files = (await walk(outDir)).filter((file) => file !== "_headers"); if (files.length > LIMITS.files) throw new Error(`${files.length} files exceed the version limit.`); let bytes = 0; let largest = { file: null, size: 0 }; for (const file of files) { const { size } = await lstat(join(outDir, file)); if (size > LIMITS.fileBytes) throw new Error(`${file} exceeds the per-file limit.`); if (size > largest.size) largest = { file, size }; bytes += size; } const rules = (await readFile(join(outDir, "_headers"), "utf8")) .split("\n") .filter((line) => line.startsWith("/") || line.startsWith("https://")).length; if (rules > LIMITS.headerRules) throw new Error(`${rules} _headers rules exceed the limit.`); return { files: files.length, bytes, largest, headerRules: rules }; } /** After carry-static: the release list ReleaseLifetime polls, _headers, limits. */ export async function finish(exportDir, sha) { if (!isSha(sha) || (await exportCommit(exportDir)) !== sha) throw new Error("Export revision differs from the release SHA."); const manifest = JSON.parse(await readFile(join(exportDir, "release-assets.json"), "utf8")); // A re-run of the live commit carries the live copy under the same SHA. const releases = [...new Set(manifest?.releases?.map((release) => release.sha) ?? [])]; // ReleaseLifetime accepts one to six full SHAs. if (manifest.schema !== 1 || releases[0] !== sha || releases.length > 6 || !releases.every(isSha)) throw new Error("Invalid release asset manifest."); for (const release of manifest.releases) for (const { path } of release.files) if (path.startsWith("/_release/") && RELEASE_PATH.exec(path)?.[1] !== release.sha) throw new Error(`${path} is listed for release ${release.sha}.`); if (await exists(join(exportDir, "_release"))) for (const entry of await readdir(join(exportDir, "_release"))) if (!releases.includes(entry)) throw new Error(`/_release/${entry} belongs to no listed release.`); // ReleaseLifetime reloads a tab whose release is no longer listed here. await writeFile( join(exportDir, "releases.json"), `${JSON.stringify({ schema: 2, head: sha, releases })}\n`, ); await writeFile(join(exportDir, "_headers"), headersFile(sha, releases)); return { head: sha, releases, ...(await checkLimits(exportDir)) }; } // First adoption: the nginx image keeps each retained export under // __releases/ and the legacy baseline under __legacy-assets. They are // served to carry-static as if they were a live Worker, so the first version // holds exactly the files nginx serves. export async function seedFromImage(imageRoot, outDir) { const metadata = JSON.parse(await readFile(join(imageRoot, "releases.json"), "utf8")); const [head, ...earlier] = metadata.releases ?? []; if (metadata.schema !== 1 || head !== metadata.current || ![head, ...earlier].every(isSha)) throw new Error("Invalid nginx release metadata."); const sources = new Map(); const listed = []; const add = async (sha, files) => { const entries = []; for (const [path, source] of files) { const bytes = await readFile(source); entries.push({ path, sha256: sha256(bytes), size: bytes.length }); if (!sources.has(path)) sources.set(path, source); } listed.push({ sha, files: entries }); return entries; }; for (const id of earlier) { const exportDir = join(imageRoot, "__releases", id); if ((await exportCommit(exportDir)) !== id) throw new Error(`Export ${id} has another identity.`); const files = await releaseFiles(exportDir, id); for (const file of await walk(exportDir, "_next/static")) files.set(`/${file}`, join(exportDir, file)); await add(id, files); } const legacy = new Map(); for (const file of await walk(join(imageRoot, "__legacy-assets"))) legacy.set(`/_next/static/${file}`, join(imageRoot, "__legacy-assets", file)); const legacyEntries = await add(DOCS_BOOTSTRAP.sha, legacy); const inventory = legacyEntries .map(({ path, size, sha256: hash }) => ({ path: path.slice("/_next/static/".length), bytes: size, sha256: hash, })) .sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0)); if (sha256(JSON.stringify(inventory)) !== DOCS_BOOTSTRAP.inventorySha256) throw new Error("Legacy assets differ from the fixed baseline inventory."); await mkdir(dirname(resolve(outDir)), { recursive: true }); await mkdir(outDir); await cp(join(imageRoot, "__releases", head), outDir, { recursive: true }); await stage(outDir, head); const image = async (url) => { const { pathname } = new URL(url); if (pathname === "/release-assets.json") return new Response(JSON.stringify({ schema: 1, releases: listed }), { status: 200 }); const source = sources.get(pathname); return source ? new Response(await readFile(source), { status: 200 }) : new Response("", { status: 404 }); }; const carried = await carry({ site: "https://nginx-image.invalid", out: outDir, release: head, dirs: CARRY_DIRS, keep: listed.length, requirePrevious: true, fetch: image, }); return { carried: carried.carried, ...(await finish(outDir, head)) }; } if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const [command, ...rest] = process.argv.slice(2); try { let result; if (command === "stage") result = await stage(rest[0], rest[1]); else if (command === "finish") result = await finish(rest[0], rest[1]); else if (command === "seed-image") result = await seedFromImage(rest[0], rest[1]); else throw new Error("usage: worker-release.mjs stage|finish|seed-image ..."); console.log(JSON.stringify(result)); } catch (error) { console.error(error instanceof Error ? error.message : "Worker release step failed."); process.exitCode = 1; } }