| 1 | import { createHash } from "node:crypto"; |
| 2 | import { createReadStream, readFileSync } from "node:fs"; |
| 3 | import path from "node:path"; |
| 4 | import { pathToFileURL } from "node:url"; |
| 5 | |
| 6 | export async function verifyArchive(artifact, archivePath) { |
| 7 | if (!Number.isSafeInteger(artifact.id) || artifact.id <= 0 || artifact.expired !== false || |
| 8 | !/^sha256:[0-9a-f]{64}$/.test(artifact.digest ?? "")) { |
| 9 | throw new Error("artifact has no trustworthy active archive digest"); |
| 10 | } |
| 11 | const hash = createHash("sha256"); |
| 12 | for await (const chunk of createReadStream(archivePath)) hash.update(chunk); |
| 13 | if (`sha256:${hash.digest("hex")}` !== artifact.digest) { |
| 14 | throw new Error(`artifact ${artifact.id} archive digest mismatch`); |
| 15 | } |
| 16 | return artifact.digest; |
| 17 | } |
| 18 | |
| 19 | if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) { |
| 20 | const [metadataPath, archivePath] = process.argv.slice(2); |
| 21 | if (!metadataPath || !archivePath) throw new Error("usage: verify-release-artifact-archive.mjs METADATA_JSON ARCHIVE_ZIP"); |
| 22 | console.log(await verifyArchive(JSON.parse(readFileSync(metadataPath, "utf8")), archivePath)); |
| 23 | } |
| 24 |