88 * app needs Electron at runtime; vitest needs Node. To keep both working from a
99 * single `pnpm install`, we download both prebuilds and stash them at:
1010 *
11- * build/Release/better_sqlite3.node-node.node (Node ABI, used by vitest)
12- * build/Release/better_sqlite3.node-electron.node (Electron ABI, used by app)
11+ * build/Release/better_sqlite3.node-node.node (Node ABI, used by vitest)
12+ * build/Release/better_sqlite3.node-electron-x64.node (Electron ABI, x64 app)
13+ * build/Release/better_sqlite3.node-electron-arm64.node(Electron ABI, arm64 app)
14+ * build/Release/better_sqlite3.node-electron.node (legacy alias for host arch)
1315 *
1416 * snapshots-db.ts then opts into the right file via better-sqlite3's
1517 * `nativeBinding` constructor option, depending on whether process.versions.electron
1921 * recorded versions in install-sqlite-bindings.lock.json. Safe to re-run on
2022 * every install.
2123 *
22- * SchemaVersion 1 : marker for the on-disk lock format so we can migrate later
24+ * SchemaVersion 2 : marker for the on-disk lock format so we can migrate later
2325 * without breaking older checkouts.
2426 */
2527const { execFileSync } = require ( 'node:child_process' ) ;
2628const fs = require ( 'node:fs' ) ;
2729const path = require ( 'node:path' ) ;
2830
29- const LOCK_SCHEMA_VERSION = 1 ;
31+ const LOCK_SCHEMA_VERSION = 2 ;
3032
3133function log ( msg ) {
3234 process . stdout . write ( `[sqlite-bindings] ${ msg } \n` ) ;
@@ -51,6 +53,13 @@ function resolveElectronVersion() {
5153 }
5254}
5355
56+ function electronTargetArches ( platform , hostArch ) {
57+ if ( platform === 'darwin' || platform === 'win32' ) {
58+ return [ 'x64' , 'arm64' ] ;
59+ }
60+ return [ hostArch ] ;
61+ }
62+
5463function resolvePrebuildInstallEntrypoint ( pkgDir ) {
5564 try {
5665 return require . resolve ( 'prebuild-install/bin.js' , { paths : [ pkgDir ] } ) ;
@@ -135,7 +144,14 @@ function main() {
135144 const electronVersion = resolveElectronVersion ( ) ;
136145
137146 const nodeBinary = path . join ( releaseDir , 'better_sqlite3.node-node.node' ) ;
138- const electronBinary = path . join ( releaseDir , 'better_sqlite3.node-electron.node' ) ;
147+ const electronArches = electronTargetArches ( platform , arch ) ;
148+ const electronBinaries = Object . fromEntries (
149+ electronArches . map ( ( targetArch ) => [
150+ targetArch ,
151+ path . join ( releaseDir , `better_sqlite3.node-electron-${ targetArch } .node` ) ,
152+ ] ) ,
153+ ) ;
154+ const legacyElectronBinary = path . join ( releaseDir , 'better_sqlite3.node-electron.node' ) ;
139155 const lockPath = path . join ( releaseDir , 'install-sqlite-bindings.lock.json' ) ;
140156
141157 const lock = ( ( ) => {
@@ -154,7 +170,13 @@ function main() {
154170 nodeVersion,
155171 electronVersion,
156172 hasNodeBinary : fs . existsSync ( nodeBinary ) ,
157- hasElectronBinary : fs . existsSync ( electronBinary ) ,
173+ electronArches,
174+ hasElectronBinaries : Object . fromEntries (
175+ electronArches . map ( ( targetArch ) => [
176+ targetArch ,
177+ fs . existsSync ( electronBinaries [ targetArch ] ) ,
178+ ] ) ,
179+ ) ,
158180 } ;
159181
160182 const upToDate =
@@ -165,9 +187,13 @@ function main() {
165187 lock . nodeVersion === nodeVersion &&
166188 lock . electronVersion === electronVersion &&
167189 lock . hasNodeBinary === targetLock . hasNodeBinary &&
168- lock . hasElectronBinary === targetLock . hasElectronBinary &&
190+ JSON . stringify ( lock . electronArches ) === JSON . stringify ( targetLock . electronArches ) &&
191+ JSON . stringify ( lock . hasElectronBinaries ) === JSON . stringify ( targetLock . hasElectronBinaries ) &&
169192 ( ! targetLock . hasNodeBinary || fs . existsSync ( nodeBinary ) ) &&
170- ( ! targetLock . hasElectronBinary || fs . existsSync ( electronBinary ) ) ;
193+ electronArches . every (
194+ ( targetArch ) =>
195+ targetLock . hasElectronBinaries [ targetArch ] !== true || fs . existsSync ( electronBinaries [ targetArch ] ) ,
196+ ) ;
171197
172198 if ( upToDate ) {
173199 log (
@@ -187,23 +213,25 @@ function main() {
187213 optional : true ,
188214 } ) ;
189215
190- let hasElectronBinary = false ;
216+ const hasElectronBinaries = { } ;
191217 if ( electronVersion === null ) {
192218 log ( 'electron not installed; skipping Electron native binding (fine for prod-only installs)' ) ;
193219 } else {
194- log ( `downloading Electron prebuild (electron=${ electronVersion } , ${ platform } -${ arch } )` ) ;
195- hasElectronBinary = downloadPrebuild ( {
196- pkgDir,
197- runtime : 'electron' ,
198- target : electronVersion ,
199- arch,
200- platform,
201- dest : electronBinary ,
202- optional : false ,
203- } ) ;
220+ for ( const targetArch of electronArches ) {
221+ log ( `downloading Electron prebuild (electron=${ electronVersion } , ${ platform } -${ targetArch } )` ) ;
222+ hasElectronBinaries [ targetArch ] = downloadPrebuild ( {
223+ pkgDir,
224+ runtime : 'electron' ,
225+ target : electronVersion ,
226+ arch : targetArch ,
227+ platform,
228+ dest : electronBinaries [ targetArch ] ,
229+ optional : false ,
230+ } ) ;
231+ }
204232 }
205233
206- if ( ! hasNodeBinary && ! hasElectronBinary ) {
234+ if ( ! hasNodeBinary && ! electronArches . some ( ( targetArch ) => hasElectronBinaries [ targetArch ] ) ) {
207235 throw new Error ( 'Failed to stage any better-sqlite3 native bindings' ) ;
208236 }
209237
@@ -215,13 +243,21 @@ function main() {
215243 const defaultBinary = path . join ( releaseDir , 'better_sqlite3.node' ) ;
216244 if ( hasNodeBinary ) {
217245 fs . copyFileSync ( nodeBinary , defaultBinary ) ;
218- } else if ( hasElectronBinary ) {
219- fs . copyFileSync ( electronBinary , defaultBinary ) ;
246+ } else {
247+ const firstElectronArch = electronArches . find ( ( targetArch ) => hasElectronBinaries [ targetArch ] ) ;
248+ if ( firstElectronArch ) fs . copyFileSync ( electronBinaries [ firstElectronArch ] , defaultBinary ) ;
249+ }
250+
251+ const hostElectronBinary = electronBinaries [ arch ] ;
252+ if ( hostElectronBinary && fs . existsSync ( hostElectronBinary ) ) {
253+ fs . copyFileSync ( hostElectronBinary , legacyElectronBinary ) ;
254+ } else if ( fs . existsSync ( legacyElectronBinary ) ) {
255+ fs . rmSync ( legacyElectronBinary ) ;
220256 }
221257
222258 fs . writeFileSync (
223259 lockPath ,
224- `${ JSON . stringify ( { ...targetLock , hasNodeBinary, hasElectronBinary } , null , 2 ) } \n` ,
260+ `${ JSON . stringify ( { ...targetLock , hasNodeBinary, hasElectronBinaries } , null , 2 ) } \n` ,
225261 ) ;
226262 log ( 'done' ) ;
227263}
0 commit comments