source-server-update-fixture.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { execFile } from "node:child_process";
  2. import { createHash } from "node:crypto";
  3. import { createReadStream } from "node:fs";
  4. import { copyFile, mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
  5. import { join, resolve } from "node:path";
  6. import { promisify } from "node:util";
  7. import {
  8. SOURCE_SERVER_RELEASE_DESCRIPTOR_FILE_NAME,
  9. sourceServerReleaseDescriptorSchema,
  10. } from "@zcode/shared";
  11. const run = promisify(execFile);
  12. /** 只更改本 run 的 staged archive,制造同版本异 SHA;公共发布产物保持不可变。 */
  13. export async function prepareSourceServerUpdateArchive(
  14. remoteSha: string,
  15. marker: string,
  16. options: { desktopRoot?: string } = {},
  17. ) {
  18. const desktopRoot = options.desktopRoot ?? resolve(import.meta.dirname, "../../..");
  19. const runId = process.env.ZCODE_E2E_RUN_ID?.trim();
  20. if (!runId || !/^[a-zA-Z0-9-]+$/u.test(runId)) throw new Error("更新 fixture 缺少安全 runId");
  21. const { version } = JSON.parse(
  22. await readFile(resolve(desktopRoot, "../../package.json"), "utf8"),
  23. );
  24. const releaseDir = join(desktopRoot, ".e2e-cache", runId, "mock-cdn/releases", version);
  25. const descriptorPath = join(releaseDir, SOURCE_SERVER_RELEASE_DESCRIPTOR_FILE_NAME);
  26. const descriptor = sourceServerReleaseDescriptorSchema.parse(
  27. JSON.parse(await readFile(descriptorPath, "utf8")),
  28. );
  29. const entry = descriptor.archives.find((item) => item.sha256 === remoteSha);
  30. if (!entry) throw new Error("更新 fixture 的远端 SHA 不在本轮发布清单中");
  31. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  32. if (!artifactDir) throw new Error("更新 fixture 缺少隔离产物目录");
  33. await mkdir(artifactDir, { recursive: true });
  34. const work = await mkdtemp(join(artifactDir, "release-repack-"));
  35. const archivePath = join(releaseDir, entry.fileName);
  36. try {
  37. await run("tar", ["-xzf", archivePath, "-C", work], { timeout: 300_000 });
  38. const listing = await run("tar", ["-tzf", archivePath], {
  39. maxBuffer: 1 << 24,
  40. timeout: 60_000,
  41. });
  42. const top = listing.stdout
  43. .split("\n")
  44. .find((line) => line.trim())
  45. ?.split("/")[0];
  46. if (!top || top === ".." || top === ".")
  47. throw new Error("更新 fixture 的 archive 顶层目录无效");
  48. await writeFile(join(work, top, "runtime/E2E_R3_UPGRADE_MARKER.txt"), `${marker} ${runId}\n`);
  49. const repacked = join(work, entry.fileName);
  50. await run("tar", ["-czf", repacked, "-C", work, top], { timeout: 300_000 });
  51. const hash = createHash("sha256");
  52. for await (const bytes of createReadStream(repacked)) hash.update(bytes);
  53. const newSha = hash.digest("hex");
  54. // staged 文件是硬链接,必须先解除链接,不能原地写坏其他 case 的源包。
  55. await rm(archivePath, { force: true });
  56. await copyFile(repacked, archivePath);
  57. entry.sha256 = newSha;
  58. entry.sizeBytes = (await stat(archivePath)).size;
  59. await rm(descriptorPath, { force: true });
  60. await writeFile(descriptorPath, JSON.stringify(descriptor, null, 2));
  61. return { oldSha: remoteSha, newSha };
  62. } finally {
  63. await rm(work, { recursive: true, force: true });
  64. }
  65. }