1- import * as NodeSqlite from "node:sqlite " ;
1+ import * as NodeWorkerThreads from "node:worker_threads " ;
22
33import * as Effect from "effect/Effect" ;
44import * as FileSystem from "effect/FileSystem" ;
@@ -24,6 +24,20 @@ const isProcessAlive = (pid: number): boolean => {
2424 }
2525} ;
2626
27+ // V1 files predate auto_vacuum, and a page backup would keep that. VACUUM INTO compacts the copy and
28+ // enables incremental mode, so the server can return freed pages; the worker keeps it off the event loop.
29+ const snapshotWorkerSource = `
30+ const { workerData } = require("node:worker_threads");
31+ const { DatabaseSync } = require("node:sqlite");
32+ const database = new DatabaseSync(workerData.sourcePath, { readOnly: true });
33+ try {
34+ database.exec("PRAGMA auto_vacuum = INCREMENTAL");
35+ database.prepare("VACUUM INTO ?").run(workerData.snapshotPath);
36+ } finally {
37+ database.close();
38+ }
39+ ` ;
40+
2741// Legacy names have no PID; newer copies stay while their process is alive.
2842const removeAbandonedCopies = Effect . fn ( "removeAbandonedCopies" ) ( function * ( directory : string ) {
2943 const fs = yield * FileSystem . FileSystem ;
@@ -58,14 +72,20 @@ export const initializeV2Database = Effect.fn("initializeV2Database")(function*
5872 prefix : `.v2-import-${ process . pid } -` ,
5973 } ) ;
6074 const snapshotPath = path . join ( temporaryDirectory , "snapshot.sqlite" ) ;
61- yield * Effect . tryPromise ( async ( ) => {
62- const database = new NodeSqlite . DatabaseSync ( sourcePath , { readOnly : true } ) ;
63- try {
64- await NodeSqlite . backup ( database , snapshotPath ) ;
65- } finally {
66- database . close ( ) ;
67- }
68- } ) ;
75+ yield * Effect . tryPromise (
76+ ( signal ) =>
77+ new Promise < void > ( ( resolve , reject ) => {
78+ const worker = new NodeWorkerThreads . Worker ( snapshotWorkerSource , {
79+ eval : true ,
80+ workerData : { sourcePath, snapshotPath } ,
81+ } ) ;
82+ signal . addEventListener ( "abort" , ( ) => void worker . terminate ( ) , { once : true } ) ;
83+ worker . once ( "error" , reject ) ;
84+ worker . once ( "exit" , ( code ) =>
85+ code === 0 ? resolve ( ) : reject ( new Error ( `Snapshot worker exited with ${ code } ` ) ) ,
86+ ) ;
87+ } ) ,
88+ ) ;
6989 // Publish only a complete snapshot, without replacing an existing V2 database.
7090 yield * fs . link ( snapshotPath , destinationPath ) . pipe (
7191 Effect . catchIf (
0 commit comments