desktop-native-package-policy.mjs 5.4 KB

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