source-candidate.mjs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // Windows validation is temporarily paused in the candidate workflow.
  55. const requiredPlatforms = ["linux", "darwin"];
  56. if (
  57. reports.length !== requiredPlatforms.length ||
  58. new Set(reports.map((r) => r.platform)).size !== requiredPlatforms.length ||
  59. reports.some(
  60. (r) =>
  61. !requiredPlatforms.includes(r.platform) ||
  62. r.status !== "PASS" ||
  63. r.profile !== "archive" ||
  64. r.revision !== receipt.revision ||
  65. !r.gates?.length ||
  66. r.gates.some((g) => !["PASS", "SKIP"].includes(g.status)),
  67. )
  68. )
  69. throw new Error(
  70. "Candidate requires successful same-revision archive reports from Linux and macOS",
  71. );
  72. const manifest = {
  73. schemaVersion: 1,
  74. revision: receipt.revision,
  75. sha256: digest,
  76. publicationPerformed: false,
  77. validation: reports.map((r) => ({
  78. platform: r.platform,
  79. arch: r.arch,
  80. node: r.node,
  81. profile: r.profile,
  82. status: r.status,
  83. })),
  84. };
  85. writeFileSync(
  86. path.join(path.dirname(archive), "candidate.json"),
  87. `${JSON.stringify(manifest, null, 2)}\n`,
  88. );
  89. writeFileSync(`${archive}.sha256`, `${digest} ${path.basename(archive)}\n`);
  90. console.log(
  91. `Source candidate verified on Linux and macOS: ${receipt.revision}`,
  92. );
  93. }