| #!/usr/bin/env node |
|
|
| |
|
|
| import fs from "node:fs"; |
| import path from "node:path"; |
| import { createHash } from "node:crypto"; |
| import { spawnSync } from "node:child_process"; |
| import { fileURLToPath } from "node:url"; |
|
|
| const VERSION = "0.4.0"; |
| const here = path.dirname(fileURLToPath(import.meta.url)); |
| const root = path.resolve(here, ".."); |
| const packageRoot = `agency-transfer-election-cases-v${VERSION}`; |
| const workbookName = `agency-transfer-election-evidence-index-v${VERSION}.xlsx`; |
| const cliArgs = process.argv.slice(2); |
| const checkoutParity = cliArgs.includes("--checkout-parity"); |
| const unknownOptions = cliArgs.filter((arg) => arg.startsWith("--") && arg !== "--checkout-parity"); |
| const positional = cliArgs.filter((arg) => !arg.startsWith("--")); |
| if (unknownOptions.length) throw new Error(`Unknown option: ${unknownOptions.join(", ")}`); |
| if (positional.length > 1) throw new Error("Expected at most one ZIP path"); |
| const zipPath = path.resolve(positional[0] || path.join(root, "release", `${packageRoot}.zip`)); |
|
|
| function sha256(buffer) { |
| return createHash("sha256").update(buffer).digest("hex"); |
| } |
|
|
| function unzip(args, binary = false) { |
| const result = spawnSync("unzip", args, { |
| encoding: binary ? null : "utf8", |
| maxBuffer: 128 * 1024 * 1024 |
| }); |
| if (result.error) throw result.error; |
| if (result.status !== 0) throw new Error(`unzip ${args.join(" ")} failed (${result.status}): ${String(result.stderr || "").trim()}`); |
| return result.stdout; |
| } |
|
|
| function safeMember(member) { |
| return ( |
| typeof member === "string" && member.length > 0 && !member.startsWith("/") && |
| !member.includes("\\") && !member.split("/").includes("..") && |
| member.startsWith(`${packageRoot}/`) |
| ); |
| } |
|
|
| if (!fs.existsSync(zipPath)) throw new Error(`Package ZIP is missing: ${zipPath}`); |
| unzip(["-tqq", zipPath]); |
| const members = String(unzip(["-Z1", zipPath])).trim().split(/\r?\n/).filter(Boolean); |
| if (!members.length || members.some((member) => !safeMember(member))) throw new Error("ZIP contains an empty, unsafe or out-of-root member path"); |
| if (new Set(members).size !== members.length) throw new Error("ZIP contains duplicate member paths"); |
| const fileMembers = members.filter((member) => !member.endsWith("/")); |
| const manifestMember = `${packageRoot}/PACKAGE-MANIFEST.json`; |
| const sumsMember = `${packageRoot}/PACKAGE-SHA256SUMS`; |
| if (!fileMembers.includes(manifestMember) || !fileMembers.includes(sumsMember)) throw new Error("ZIP package manifest/checksums are missing"); |
|
|
| const manifestBuffer = Buffer.from(unzip(["-p", zipPath, manifestMember], true)); |
| const manifest = JSON.parse(manifestBuffer.toString("utf8")); |
| if (manifest.manifest_version !== 1 || manifest.dataset_version !== VERSION || manifest.package_root !== packageRoot) { |
| throw new Error("ZIP package manifest identity is invalid"); |
| } |
| if (!Array.isArray(manifest.files) || manifest.file_count !== manifest.files.length) throw new Error("ZIP package manifest file count is invalid"); |
| const declaredPaths = manifest.files.map((entry) => entry?.path); |
| if (declaredPaths.some((member) => !safeMember(`${packageRoot}/${member}`)) || new Set(declaredPaths).size !== declaredPaths.length) { |
| throw new Error("ZIP package manifest contains an unsafe or duplicate path"); |
| } |
| const actualPayloadPaths = fileMembers |
| .filter((member) => ![manifestMember, sumsMember].includes(member)) |
| .map((member) => member.slice(packageRoot.length + 1)) |
| .sort(); |
| if (JSON.stringify([...declaredPaths].sort()) !== JSON.stringify(actualPayloadPaths)) throw new Error("ZIP members differ from PACKAGE-MANIFEST.json"); |
|
|
| for (const entry of manifest.files) { |
| const member = `${packageRoot}/${entry.path}`; |
| const buffer = Buffer.from(unzip(["-p", zipPath, member], true)); |
| if (buffer.byteLength !== entry.bytes || sha256(buffer) !== entry.sha256) throw new Error(`ZIP member differs from manifest: ${entry.path}`); |
| if (checkoutParity) { |
| const checkoutPath = entry.path.startsWith("workbook/") |
| ? path.join(root, "release", workbookName) |
| : path.join(root, entry.path); |
| if (!fs.existsSync(checkoutPath) || sha256(fs.readFileSync(checkoutPath)) !== entry.sha256) { |
| throw new Error(`ZIP member differs from current checkout/release artifact: ${entry.path}`); |
| } |
| } |
| } |
| const expectedSums = `${manifest.files.map((entry) => `${entry.sha256} ${entry.path}`).join("\n")}\n`; |
| const actualSums = Buffer.from(unzip(["-p", zipPath, sumsMember], true)).toString("utf8"); |
| if (actualSums !== expectedSums) throw new Error("PACKAGE-SHA256SUMS differs from PACKAGE-MANIFEST.json"); |
|
|
| const zipBuffer = fs.readFileSync(zipPath); |
| process.stdout.write(`${JSON.stringify({ |
| status: "pass", |
| version: VERSION, |
| file: path.relative(root, zipPath), |
| bytes: zipBuffer.byteLength, |
| sha256: sha256(zipBuffer), |
| members: fileMembers.length, |
| payload_files: manifest.files.length, |
| checkout_parity: checkoutParity ? "pass" : "not_checked" |
| })}\n`); |
|
|