apol's picture
v0.4.0: harden deterministic build and validation
bdd8da5 verified
Raw
History Blame Contribute Delete
1.55 kB
#!/usr/bin/env node
/** Materialize the deterministic, passing audit report in both compatibility locations. */
import fs from "node:fs/promises";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const here = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(here, "..");
const dataDir = path.join(root, "data");
const result = spawnSync(process.execPath, [path.join(here, "audit-v03.mjs"), dataDir], {
cwd: root,
encoding: "utf8",
maxBuffer: 32 * 1024 * 1024
});
if (result.error) throw result.error;
if (result.status !== 0) {
process.stderr.write(result.stdout || result.stderr || "Audit failed without output\n");
process.exitCode = result.status ?? 1;
} else {
const report = JSON.parse(result.stdout);
if (report.auditor !== "agency-transfer-election-cases-v0.4.0/audit-v04" || report.status !== "pass") {
throw new Error("Refusing to store an audit with an unexpected auditor ID or non-pass status");
}
const stable = `${JSON.stringify(report, null, 2)}\n`;
const temporary = [];
for (const name of ["audit.json", "audit-v03.json"]) {
const temp = path.join(dataDir, `.${name}.${process.pid}.tmp`);
await fs.writeFile(temp, stable);
temporary.push([temp, path.join(dataDir, name)]);
}
for (const [temp, target] of temporary) await fs.rename(temp, target);
process.stdout.write(`${JSON.stringify({ auditor: report.auditor, files: temporary.map(([, target]) => path.relative(root, target)) })}\n`);
}