packagedPlatformDependencyPrune.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { execFileSync } from "node:child_process";
  2. import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
  3. import { createRequire } from "node:module";
  4. import { tmpdir } from "node:os";
  5. import { dirname, resolve } from "node:path";
  6. import { getRawHeader } from "@electron/asar";
  7. import { describe, expect, it } from "vitest";
  8. import { pruneIncompatiblePlatformPackages } from "../scripts/packaged-platform-dependency-prune.mjs";
  9. import { ASAR_UNPACK_NATIVE_GLOB, replaceAppAsarFromStaging } from "../scripts/app-asar-repack.mjs";
  10. const require = createRequire(import.meta.url);
  11. const asarCliPath = resolve(
  12. dirname(require.resolve("@electron/asar/package.json")),
  13. "bin",
  14. "asar.js",
  15. );
  16. function runAsar(args: string[]) {
  17. execFileSync(process.execPath, [asarCliPath, ...args]);
  18. }
  19. function nativeFilesInAsar(asarPath: string) {
  20. const header = getRawHeader(asarPath).header as { files: Record<string, unknown> };
  21. const output: string[] = [];
  22. const visit = (files: Record<string, unknown>, parent = "") => {
  23. for (const [name, value] of Object.entries(files)) {
  24. const path = parent ? `${parent}/${name}` : name;
  25. const entry = value as { files?: Record<string, unknown>; unpacked?: boolean };
  26. if (entry.files) visit(entry.files, path);
  27. else if (entry.unpacked) output.push(path);
  28. }
  29. };
  30. visit(header.files);
  31. return output;
  32. }
  33. describe("packaged platform dependency prune", () => {
  34. it("removes incompatible packages before repack and drops stale unpacked native files", async () => {
  35. const root = mkdtempSync(resolve(tmpdir(), "zcode-platform-prune-"));
  36. try {
  37. const source = resolve(root, "source");
  38. const nodeModules = resolve(source, "node_modules/@img");
  39. const target = resolve(nodeModules, "sharp-win32-x64");
  40. const otherArch = resolve(nodeModules, "sharp-win32-ia32");
  41. const otherOs = resolve(nodeModules, "sharp-linux-arm");
  42. const unrelated = resolve(nodeModules, "colour");
  43. for (const directory of [target, otherArch, otherOs, unrelated]) {
  44. mkdirSync(directory, { recursive: true });
  45. }
  46. writeFileSync(
  47. resolve(target, "package.json"),
  48. JSON.stringify({ name: "@img/sharp-win32-x64", os: ["win32"], cpu: ["x64"] }),
  49. );
  50. writeFileSync(resolve(target, "sharp.node"), "target-native");
  51. writeFileSync(
  52. resolve(otherArch, "package.json"),
  53. JSON.stringify({ name: "@img/sharp-win32-ia32", os: ["win32"], cpu: ["ia32"] }),
  54. );
  55. writeFileSync(resolve(otherArch, "sharp.node"), "wrong-arch-native");
  56. writeFileSync(
  57. resolve(otherOs, "package.json"),
  58. JSON.stringify({ name: "@img/sharp-linux-arm", os: ["linux"], cpu: ["arm"] }),
  59. );
  60. writeFileSync(resolve(otherOs, "libvips.dll"), "wrong-os-native");
  61. writeFileSync(resolve(unrelated, "package.json"), JSON.stringify({ name: "@img/colour" }));
  62. writeFileSync(resolve(unrelated, "index.js"), "export {};\n");
  63. const appAsar = resolve(root, "app.asar");
  64. runAsar(["pack", source, appAsar, "--unpack", ASAR_UNPACK_NATIVE_GLOB]);
  65. expect(nativeFilesInAsar(appAsar)).toHaveLength(3);
  66. runAsar(["extract", appAsar, resolve(root, "staging")]);
  67. const staging = resolve(root, "staging");
  68. const summary = await pruneIncompatiblePlatformPackages(staging, {
  69. os: "win32",
  70. arch: "x64",
  71. });
  72. expect(summary.packagesRemoved).toBe(2);
  73. await replaceAppAsarFromStaging({
  74. sourceDir: staging,
  75. appAsarPath: appAsar,
  76. targetPlatformKey: "win32-x64",
  77. runAsarCommand: runAsar,
  78. });
  79. expect(nativeFilesInAsar(appAsar)).toEqual(["node_modules/@img/sharp-win32-x64/sharp.node"]);
  80. expect(existsSync(resolve(`${appAsar}.unpacked`, "node_modules/@img/sharp-win32-ia32"))).toBe(
  81. false,
  82. );
  83. expect(existsSync(resolve(`${appAsar}.unpacked`, "node_modules/@img/sharp-linux-arm"))).toBe(
  84. false,
  85. );
  86. expect(
  87. execFileSync(process.execPath, [asarCliPath, "list", appAsar], {
  88. encoding: "utf8",
  89. }).replaceAll("\\", "/"),
  90. ).toContain("node_modules/@img/colour/index.js");
  91. expect(
  92. readFileSync(
  93. resolve(`${appAsar}.unpacked`, "node_modules/@img/sharp-win32-x64/sharp.node"),
  94. "utf8",
  95. ),
  96. ).toBe("target-native");
  97. } finally {
  98. rmSync(root, { force: true, recursive: true });
  99. }
  100. });
  101. });