fix(electron-shell): 1.10.10 修复打包原生模块 ABI 不匹配导致启动崩溃
1.10.9 把系统 Node ABI(137) 的 better_sqlite3.node 打进 Electron 30(123) 安装包,require 即抛 NODE_MODULE_VERSION 不匹配,全量装机启动崩。 - 打包改走 scripts/pack.js:打包前切 Electron ABI 二进制、打包后恢复 Node ABI(本地 npm start 不受影响),缺失时按需下载(gh-proxy 镜像兜底)并缓存 native/(不入库) - scripts/after-pack.js 增加包内原生模块哈希校验,不一致直接打包失败,杜绝再产出装不上的包 - electron/db.js 原生模块加载失败降级容错:记 ERROR,壳子照常启动,仅方案配置接口报错 - 版本 1.10.10、package-lock 同步、CHANGELOG 记录事故、发版 SOP 补充禁止直跑 electron-builder
This commit is contained in:
@@ -1,14 +1,58 @@
|
||||
// electron-builder afterPack 钩子:把 package.json 版本号写入 exe 同级 shell.version。
|
||||
// shell-updater 靠这个文件判定"打包态 + 本机版本",缺失会被误判为源码开发模式、
|
||||
// 不参与壳子自更新(win-unpacked 根目录 = 安装后 exe 同级目录)。
|
||||
// electron-builder afterPack 钩子:
|
||||
// 1) 把 package.json 版本号写入 exe 同级 shell.version(shell-updater 靠它判定"打包态 + 本机版本",
|
||||
// 缺失会被误判为源码开发模式、不参与壳子自更新;win-unpacked 根目录 = 安装后 exe 同级目录)
|
||||
// 2) 校验打进包里的 better-sqlite3 原生模块是 **Electron ABI**(1.10.9 全员启动崩事故的守门人:
|
||||
// 当时打进的是系统 Node ABI(137) 二进制,Electron 30 只认 123,装机后 require 即抛错)。
|
||||
// 不一致直接让打包失败,绝不产出装不上的安装包。
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const crypto = require("crypto");
|
||||
|
||||
const ROOT = path.join(__dirname, "..");
|
||||
const NATIVE_BIN = path.join(ROOT, "native", "electron", "build", "Release", "better_sqlite3.node");
|
||||
|
||||
function sha256(file) {
|
||||
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
||||
}
|
||||
|
||||
function verifyNativeAbi(context) {
|
||||
const packed = path.join(
|
||||
context.appOutDir,
|
||||
"resources",
|
||||
"app.asar.unpacked",
|
||||
"node_modules",
|
||||
"better-sqlite3",
|
||||
"build",
|
||||
"Release",
|
||||
"better_sqlite3.node"
|
||||
);
|
||||
if (!fs.existsSync(packed)) {
|
||||
throw new Error(`[after-pack] 包内未找到 better-sqlite3 原生模块:${packed}`);
|
||||
}
|
||||
if (!fs.existsSync(NATIVE_BIN)) {
|
||||
throw new Error(
|
||||
`[after-pack] 缺少 Electron ABI 参考二进制:${NATIVE_BIN}\n` +
|
||||
"请用 npm run pack 打包(会自动准备并切换 ABI),不要直接跑 electron-builder"
|
||||
);
|
||||
}
|
||||
const got = sha256(packed);
|
||||
const want = sha256(NATIVE_BIN);
|
||||
if (got !== want) {
|
||||
throw new Error(
|
||||
"[after-pack] 包内 better-sqlite3 不是 Electron ABI,装机会启动即崩!\n" +
|
||||
` 包内 sha256=${got}\n 期望 sha256=${want}\n` +
|
||||
"请用 npm run pack 打包(它会先切换 ABI),不要直接跑 electron-builder"
|
||||
);
|
||||
}
|
||||
console.log(`[after-pack] 原生模块 ABI 校验通过(sha256=${got.slice(0, 12)})`);
|
||||
}
|
||||
|
||||
exports.default = async function afterPack(context) {
|
||||
const pkg = require(path.join(__dirname, "..", "package.json"));
|
||||
const pkg = require(path.join(ROOT, "package.json"));
|
||||
const target = path.join(context.appOutDir, "shell.version");
|
||||
fs.writeFileSync(target, `${pkg.version}\n`, "utf-8");
|
||||
console.log(`[after-pack] shell.version -> ${target} (${pkg.version})`);
|
||||
verifyNativeAbi(context);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user