desktop-native-package-policy.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { keyringPlatformPackage } from "../../../scripts/stage-credential-keyring.mjs";
  2. const SUPPORTED_DESKTOP_PLATFORM_KEYS = [
  3. "darwin-arm64",
  4. "darwin-x64",
  5. "linux-arm64",
  6. "linux-x64",
  7. "win32-arm64",
  8. "win32-x64",
  9. ];
  10. // sharp 通过 optionalDependencies 分发全平台二进制变体。electron-builder 26.17 的
  11. // pnpm 收集器按 lockfile 全平台图收包,会把 darwin/linux/异架构 win32 变体(Mach-O/ELF,
  12. // 约 110MB 未压缩)全部带进 app.asar:既撑大安装包,又会被 signApp 送进 Windows 签名
  13. // 触发 0x8010006a 连锁失败(2026-10-26 CI 实况,签名钩子已按 PE 魔数跳过)。
  14. // 每个目标平台只保留运行时实际解析的一组变体。
  15. const SHARP_VARIANT_PACKAGES = [
  16. "sharp-win32-x64",
  17. "sharp-win32-arm64",
  18. "sharp-darwin-x64",
  19. "sharp-darwin-arm64",
  20. "sharp-libvips-darwin-x64",
  21. "sharp-libvips-darwin-arm64",
  22. "sharp-linux-x64",
  23. "sharp-linux-arm64",
  24. "sharp-libvips-linux-x64",
  25. "sharp-libvips-linux-arm64",
  26. "sharp-linuxmusl-x64",
  27. "sharp-linuxmusl-arm64",
  28. "sharp-libvips-linuxmusl-x64",
  29. "sharp-libvips-linuxmusl-arm64",
  30. ];
  31. const SHARP_VARIANTS_BY_PLATFORM = {
  32. "win32-x64": ["sharp-win32-x64"],
  33. "win32-arm64": ["sharp-win32-arm64"],
  34. "darwin-x64": ["sharp-darwin-x64", "sharp-libvips-darwin-x64"],
  35. "darwin-arm64": ["sharp-darwin-arm64", "sharp-libvips-darwin-arm64"],
  36. "linux-x64": ["sharp-linux-x64", "sharp-libvips-linux-x64"],
  37. "linux-arm64": ["sharp-linux-arm64", "sharp-libvips-linux-arm64"],
  38. };
  39. function assertSupportedTargetPlatformKey(targetPlatformKey) {
  40. if (!SUPPORTED_DESKTOP_PLATFORM_KEYS.includes(targetPlatformKey)) {
  41. throw new Error(`不支持的桌面目标平台: ${targetPlatformKey}`);
  42. }
  43. }
  44. export function createDesktopNativePackagePrunePatterns(targetPlatformKey) {
  45. assertSupportedTargetPlatformKey(targetPlatformKey);
  46. const keyringVariants = [
  47. "darwin-arm64",
  48. "darwin-x64",
  49. "win32-arm64",
  50. "win32-x64",
  51. "linux-arm64",
  52. "linux-x64",
  53. ].map(keyringPlatformPackage);
  54. keyringVariants.push(
  55. "@napi-rs/keyring-linux-arm64-musl",
  56. "@napi-rs/keyring-linux-x64-musl",
  57. "@napi-rs/keyring-win32-ia32-msvc",
  58. "@napi-rs/keyring-freebsd-x64",
  59. "@napi-rs/keyring-linux-riscv64-gnu",
  60. "@napi-rs/keyring-linux-arm-gnueabihf",
  61. );
  62. return [
  63. ...keyringVariants
  64. .filter((name) => name !== keyringPlatformPackage(targetPlatformKey))
  65. .map((name) => `!node_modules/${name}/**`),
  66. // PDF 预览已经由 Vite 打进 renderer,pdfjs-dist 的 Canvas optional dependency
  67. // 只服务 Node 渲染;pnpm 跨平台安装的 8 套 Canvas native 不应带进桌面安装包。
  68. "!node_modules/@napi-rs/canvas/**",
  69. "!node_modules/@napi-rs/canvas-*/**",
  70. // Linux prebuild 会在 beforePack 复制进 node-pty;源平台包本身不属于桌面运行时。
  71. "!node_modules/@lydell/node-pty-*/**",
  72. // 桌面运行时统一使用目标 prebuild,禁止把安装机现场编译物或 ABI bin 缓存带进跨平台包。
  73. "!node_modules/node-pty/build/**",
  74. "!node_modules/node-pty/bin/**",
  75. ...SUPPORTED_DESKTOP_PLATFORM_KEYS.filter((key) => key !== targetPlatformKey).map(
  76. (key) => `!node_modules/node-pty/prebuilds/${key}/**`,
  77. ),
  78. // 裁剪非目标平台的 @img/sharp 变体;目标平台变体由下方违规检查兜底确认。
  79. ...SHARP_VARIANT_PACKAGES.filter(
  80. (name) => !SHARP_VARIANTS_BY_PLATFORM[targetPlatformKey].includes(name),
  81. ).map((name) => `!node_modules/@img/${name}/**`),
  82. ];
  83. }
  84. function normalizeAsarPath(path) {
  85. const normalized = path.trim().replaceAll("\\", "/");
  86. return normalized.startsWith("/") ? normalized : `/${normalized}`;
  87. }
  88. export function parseAsarListWithPackState(output) {
  89. return output
  90. .split("\n")
  91. .map((line) => line.trim())
  92. .filter(Boolean)
  93. .map((line) => {
  94. const match = /^(pack|unpack)\s*:\s*(.+)$/.exec(line);
  95. if (!match) {
  96. throw new Error(`无法解析 asar pack state: ${line}`);
  97. }
  98. return { packState: match[1], path: normalizeAsarPath(match[2]) };
  99. });
  100. }
  101. function isNativeRuntimeFile(path, targetPlatformKey) {
  102. if (/\.(?:node|dll|dylib|exe)$/i.test(path)) return true;
  103. return path === `/node_modules/node-pty/prebuilds/${targetPlatformKey}/spawn-helper`;
  104. }
  105. export function findDesktopNativePackageViolations(entries, targetPlatformKey) {
  106. assertSupportedTargetPlatformKey(targetPlatformKey);
  107. const violations = [];
  108. for (const entry of entries) {
  109. const { packState, path } = entry;
  110. if (
  111. path === "/node_modules/@napi-rs/canvas" ||
  112. path.startsWith("/node_modules/@napi-rs/canvas/") ||
  113. path.startsWith("/node_modules/@napi-rs/canvas-")
  114. ) {
  115. violations.push(`不应打包 renderer 无需的 Canvas native: ${path}`);
  116. continue;
  117. }
  118. const keyringMatch = /^\/node_modules\/(@napi-rs\/keyring-[a-z0-9-]+)(?:\/|$)/.exec(path);
  119. if (keyringMatch && keyringMatch[1] !== keyringPlatformPackage(targetPlatformKey)) {
  120. violations.push(`keyring 包含非目标平台变体: ${keyringMatch[1]}`);
  121. continue;
  122. }
  123. // 裁剪规则漏网或收集器行为回退时,非目标平台的 @img/sharp 变体必须在这里 fail-closed。
  124. const sharpVariantMatch = /^\/node_modules\/@img\/(sharp-[a-z0-9-]+)(?:\/|$)/.exec(path);
  125. if (
  126. sharpVariantMatch &&
  127. !SHARP_VARIANTS_BY_PLATFORM[targetPlatformKey].includes(sharpVariantMatch[1])
  128. ) {
  129. violations.push(`sharp 包含非目标平台变体: ${sharpVariantMatch[1]} ${path}`);
  130. continue;
  131. }
  132. if (path.startsWith("/node_modules/@lydell/node-pty-")) {
  133. violations.push(`不应打包仅用于准备 prebuild 的平台源包: ${path}`);
  134. continue;
  135. }
  136. if (
  137. path.startsWith("/node_modules/node-pty/build/") ||
  138. path.startsWith("/node_modules/node-pty/bin/")
  139. ) {
  140. violations.push(`不应打包安装机生成的 node-pty 产物: ${path}`);
  141. continue;
  142. }
  143. const nodePtyPrebuildMatch = /^\/node_modules\/node-pty\/prebuilds\/([^/]+)/.exec(path);
  144. if (nodePtyPrebuildMatch && nodePtyPrebuildMatch[1] !== targetPlatformKey) {
  145. violations.push(`node-pty 包含非目标平台 prebuild: ${path}`);
  146. continue;
  147. }
  148. if (isNativeRuntimeFile(path, targetPlatformKey) && packState !== "unpack") {
  149. violations.push(`native 文件仍作为 packed payload 留在 app.asar: ${path}`);
  150. }
  151. }
  152. return violations;
  153. }