feat(forge): add safeguard for ARM64 better-sqlite3 binary

This commit is contained in:
Elian Doran 2025-11-30 17:32:00 +02:00
parent a77e76d5c6
commit 4ab8417168
No known key found for this signature in database

View File

@ -1,8 +1,9 @@
import path from "path"; import path, { join } from "path";
import fs from "fs-extra"; import fs from "fs-extra";
import { LOCALES } from "@triliumnext/commons"; import { LOCALES } from "@triliumnext/commons";
import { PRODUCT_NAME } from "../src/app-info.js"; import { PRODUCT_NAME } from "../src/app-info.js";
import type { ForgeConfig } from "@electron-forge/shared-types"; import type { ForgeConfig } from "@electron-forge/shared-types";
import { existsSync } from "fs";
const ELECTRON_FORGE_DIR = __dirname; const ELECTRON_FORGE_DIR = __dirname;
@ -228,8 +229,22 @@ const config: ForgeConfig = {
// Ensure all locales that should be kept are actually present. // Ensure all locales that should be kept are actually present.
for (const locale of localesToKeep) { for (const locale of localesToKeep) {
if (!keptLocales.has(locale)) { if (!keptLocales.has(locale)) {
console.error(`Locale ${locale} was not found in the packaged app.`); throw new Error(`Locale ${locale} was not found in the packaged app.`);
process.exit(1); }
}
// Check that the bettersqlite3 binary has the right architecture.
if (packageResult.platform === "linux" && packageResult.arch === "arm64") {
for (const outputPath of packageResult.outputPaths) {
const binaryPath = join(outputPath, "resources/app.asar.unpacked/node_modules/better-sqlite3/build/Release/better_sqlite3.node");
if (!existsSync(binaryPath)) {
throw new Error(`[better-sqlite3] Unable to find .node file at ${binaryPath}`);
}
const actualArch = getELFArch(binaryPath);
if (actualArch !== "ARM64") {
throw new Error(`[better-sqlite3] Expected ARM64 architecture but got ${actualArch} at: ${binaryPath}`);
}
} }
} }
}, },
@ -284,4 +299,20 @@ function getExtraResourcesForPlatform() {
return resources; return resources;
} }
function getELFArch(file: string) {
const buf = fs.readFileSync(file);
if (buf[0] !== 0x7f || buf[1] !== 0x45 || buf[2] !== 0x4c || buf[3] !== 0x46) {
throw new Error("Not an ELF file");
}
const eiClass = buf[4]; // 1=32-bit, 2=64-bit
const eiMachine = buf[18]; // architecture code
if (eiMachine === 0x3E) return 'x86-64';
if (eiMachine === 0xB7) return 'ARM64';
return 'other';
}
export default config; export default config;