Skip to content

Build your personal knowledge base with Trilium Notes

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

Elian Doran4ab84171686c
diff --git a/apps/desktop/electron-forge/forge.config.ts b/apps/desktop/electron-forge/forge.config.ts
index 8d279d59b9..5e3a39869b 100644
--- a/apps/desktop/electron-forge/forge.config.ts
+++ b/apps/desktop/electron-forge/forge.config.ts
@@ -1,8 +1,9 @@
-import path from "path";
+import path, { join } from "path";
import fs from "fs-extra";
import { LOCALES } from "@triliumnext/commons";
import { PRODUCT_NAME } from "../src/app-info.js";
import type { ForgeConfig } from "@electron-forge/shared-types";
+import { existsSync } from "fs";
const ELECTRON_FORGE_DIR = __dirname;
@@ -228,8 +229,22 @@ const config: ForgeConfig = {
// Ensure all locales that should be kept are actually present.
for (const locale of localesToKeep) {
if (!keptLocales.has(locale)) {
- console.error(`Locale ${locale} was not found in the packaged app.`);
- process.exit(1);
+ throw new Error(`Locale ${locale} was not found in the packaged app.`);
+ }
+ }
+
+ // 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;
}
+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;