/** * The source stamp of the Worker's rules module: sha256 over every file the * WASM is built from (crates/cotton-rules and crates/cotton-rules-wasm, their * sources and manifests, and the build script), each as its path and bytes. * * `pnpm rules:wasm` writes it beside the module (apps/business/src/rules/wasm/ * stamp.json); scripts/__tests__/rules-wasm-stamp.test.ts fails when the * sources move on and the checked-in module was not rebuilt. * * tsx scripts/rules/stamp.ts prints the stamp * tsx scripts/rules/stamp.ts --write writes stamp.json */ import { createHash } from "node:crypto"; import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; import { dirname, join, relative, resolve } from "node:path"; import { fileURLToPath } from "node:url"; export const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); export const STAMP_FILE = "apps/business/src/rules/wasm/stamp.json"; const INPUTS = ["crates/cotton-rules/Cargo.toml", "crates/cotton-rules/src", "crates/cotton-rules-wasm/Cargo.toml", "crates/cotton-rules-wasm/src", "scripts/rules/build-wasm.sh"]; function files(path: string): string[] { if (!statSync(path).isDirectory()) return [path]; return readdirSync(path).sort().flatMap((name) => files(join(path, name))); } /** The stamp of the sources as they stand under `root`. */ export function rulesStamp(root = ROOT): string { const hash = createHash("sha256"); for (const input of INPUTS) for (const file of files(join(root, input))) { hash.update(relative(root, file)); hash.update("\0"); hash.update(readFileSync(file)); hash.update("\0"); } return hash.digest("hex"); } if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { const stamp = rulesStamp(); if (process.argv.includes("--write")) writeFileSync(join(ROOT, STAMP_FILE), `${JSON.stringify({ sources: stamp }, null, 2)}\n`); console.log(stamp); }