e2e-source-evidence.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { execFile } from "node:child_process";
  2. import { createHash } from "node:crypto";
  3. import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { promisify } from "node:util";
  6. const execute = promisify(execFile);
  7. type Sources = Record<string, string>;
  8. const hash = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex");
  9. export function changedSourceFiles(before: Sources, after: Sources) {
  10. return [...new Set([...Object.keys(before), ...Object.keys(after)])]
  11. .filter((file) => before[file] !== after[file])
  12. .sort();
  13. }
  14. export async function captureWorkspaceSources(repoRoot: string): Promise<Sources> {
  15. const { stdout } = await execute(
  16. "git",
  17. ["ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", "packages"],
  18. { cwd: repoRoot, maxBuffer: 16 * 1024 * 1024 },
  19. );
  20. const files = [
  21. ...new Set(
  22. stdout.split("\0").filter((file) => /^packages\/[^/]+\/src\/.*\.[cm]?[jt]sx?$/u.test(file)),
  23. ),
  24. ].sort();
  25. const result: Sources = {};
  26. // 串行小文件读取控制内存;只保存指纹,不将源码或凭据复制到报告。
  27. for (const file of files) result[file] = hash(await readFile(join(repoRoot, file)));
  28. return result;
  29. }
  30. async function outputHashes(root: string): Promise<Sources> {
  31. const result: Sources = {};
  32. async function visit(relative: string) {
  33. for (const entry of await readdir(join(root, relative), { withFileTypes: true })) {
  34. const file = relative ? `${relative}/${entry.name}` : entry.name;
  35. if (entry.isDirectory()) await visit(file);
  36. else if (entry.isFile() && /\.(js|mjs|cjs|map|html|css)$/u.test(file))
  37. result[file] = hash(await readFile(join(root, file)));
  38. }
  39. }
  40. try {
  41. await visit("");
  42. } catch (error) {
  43. if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
  44. }
  45. return result;
  46. }
  47. export async function withE2ESourceEvidence(options: {
  48. freshBuild: boolean;
  49. captureSources: () => Promise<Sources>;
  50. build: () => Promise<void>;
  51. appOutputDir: string;
  52. artifactDir: string;
  53. }) {
  54. const before = options.freshBuild ? await options.captureSources() : {};
  55. await options.build();
  56. const after = options.freshBuild ? await options.captureSources() : {};
  57. const outputs = await outputHashes(options.appOutputDir);
  58. const changedFiles = changedSourceFiles(before, after);
  59. const evidence = {
  60. schemaVersion: 1,
  61. capturedAt: new Date().toISOString(),
  62. status: !options.freshBuild
  63. ? "unverified-reused-build"
  64. : changedFiles.length
  65. ? "source-changed-during-build"
  66. : Object.keys(outputs).length
  67. ? "fresh-stable"
  68. : "missing-build-output",
  69. domains: ["renderer", "host", "main"],
  70. sources: after,
  71. outputs,
  72. changedFiles,
  73. };
  74. await mkdir(options.artifactDir, { recursive: true });
  75. await writeFile(
  76. join(options.artifactDir, "build-source-evidence.json"),
  77. JSON.stringify(evidence, null, 2),
  78. );
  79. return evidence;
  80. }