source-candidate.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { createHash } from "node:crypto";
  2. import { mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
  3. import path from "node:path";
  4. import { parseArgs } from "node:util";
  5. import { extractSourceArchive } from "./lib/source-archive.mjs";
  6. const { values, positionals } = parseArgs({
  7. allowPositionals: true,
  8. options: {
  9. archive: { type: "string" },
  10. revision: { type: "string" },
  11. destination: { type: "string" },
  12. reports: { type: "string" },
  13. },
  14. });
  15. const [command] = positionals;
  16. if (
  17. positionals.length !== 1 ||
  18. !["unpack", "finalize"].includes(command) ||
  19. !values.archive ||
  20. !/^[a-f0-9]{40}$/u.test(values.revision ?? "")
  21. )
  22. throw new Error("Expected unpack/finalize, --archive and a full --revision");
  23. const archive = path.resolve(values.archive);
  24. const receipt = JSON.parse(readFileSync(`${archive}.json`, "utf8"));
  25. const digest = createHash("sha256").update(readFileSync(archive)).digest("hex");
  26. if (
  27. receipt.schemaVersion !== 1 ||
  28. receipt.revision !== values.revision ||
  29. receipt.sha256 !== digest ||
  30. receipt.format !== "source-only-no-git-history" ||
  31. receipt.publicationPerformed !== false
  32. )
  33. throw new Error("Candidate archive or revision does not match its receipt");
  34. if (command === "unpack") {
  35. if (!values.destination) throw new Error("Missing --destination");
  36. // Refuse an existing directory, including a directory containing stale builds.
  37. mkdirSync(values.destination);
  38. await extractSourceArchive(archive, path.resolve(values.destination));
  39. console.log(
  40. `Authenticated and extracted source revision ${receipt.revision}`,
  41. );
  42. } else {
  43. if (!values.reports) throw new Error("Missing --reports");
  44. const reports = readdirSync(values.reports, { withFileTypes: true })
  45. .filter((e) => e.isDirectory())
  46. .map((e) =>
  47. JSON.parse(
  48. readFileSync(
  49. path.join(values.reports, e.name, "verification.json"),
  50. "utf8",
  51. ),
  52. ),
  53. );
  54. if (
  55. reports.length !== 3 ||
  56. new Set(reports.map((r) => r.platform)).size !== 3 ||
  57. reports.some(
  58. (r) =>
  59. !["linux", "darwin", "win32"].includes(r.platform) ||
  60. r.status !== "PASS" ||
  61. r.profile !== "archive" ||
  62. r.revision !== receipt.revision ||
  63. !r.gates?.length ||
  64. r.gates.some((g) => !["PASS", "SKIP"].includes(g.status)),
  65. )
  66. )
  67. throw new Error(
  68. "Candidate requires successful same-revision archive reports from all three platforms",
  69. );
  70. const manifest = {
  71. schemaVersion: 1,
  72. revision: receipt.revision,
  73. sha256: digest,
  74. publicationPerformed: false,
  75. validation: reports.map((r) => ({
  76. platform: r.platform,
  77. arch: r.arch,
  78. node: r.node,
  79. profile: r.profile,
  80. status: r.status,
  81. })),
  82. };
  83. writeFileSync(
  84. path.join(path.dirname(archive), "candidate.json"),
  85. `${JSON.stringify(manifest, null, 2)}\n`,
  86. );
  87. writeFileSync(`${archive}.sha256`, `${digest} ${path.basename(archive)}\n`);
  88. console.log(
  89. `Source candidate verified on all three platforms: ${receipt.revision}`,
  90. );
  91. }