@@ -82,10 +82,16 @@ class BlockingAsyncDatabase implements AsyncDatabase {
8282}
8383
8484class BetterSqliteWorker implements AsyncDatabaseOpener {
85+ options : PowerSyncWorkerOptions ;
86+
87+ constructor ( options : PowerSyncWorkerOptions ) {
88+ this . options = options ;
89+ }
90+
8591 async open ( path : string , isWriter : boolean ) : Promise < AsyncDatabase > {
8692 const baseDB = new BetterSQLite3Database ( path ) ;
8793 baseDB . pragma ( 'journal_mode = WAL' ) ;
88- loadExtension ( baseDB ) ;
94+ baseDB . loadExtension ( this . options . extensionPath ( ) , 'sqlite3_powersync_init' ) ;
8995 if ( ! isWriter ) {
9096 baseDB . pragma ( 'query_only = true' ) ;
9197 }
@@ -97,29 +103,43 @@ class BetterSqliteWorker implements AsyncDatabaseOpener {
97103 }
98104}
99105
100- const loadExtension = ( db : Database ) => {
101- const isCommonJsModule = import . meta. isBundlingToCommonJs ?? false ;
102-
103- const platform = OS . platform ( ) ;
104- let extensionPath : string ;
105- if ( platform === 'win32' ) {
106- extensionPath = 'powersync.dll' ;
107- } else if ( platform === 'linux' ) {
108- extensionPath = 'libpowersync.so' ;
109- } else if ( platform === 'darwin' ) {
110- extensionPath = 'libpowersync.dylib' ;
111- } else {
112- throw 'Unknown platform, PowerSync for Node.js currently supports Windows, Linux and macOS.' ;
113- }
106+ export interface PowerSyncWorkerOptions {
107+ /**
108+ * A function responsible for finding the powersync DLL/so/dylib file.
109+ *
110+ * @returns The absolute path of the PowerSync SQLite core extensions library.
111+ */
112+ extensionPath : ( ) => string ;
113+ }
114114
115- let resolved : string ;
116- if ( isCommonJsModule ) {
117- resolved = path . resolve ( __dirname , '../lib/' , extensionPath ) ;
118- } else {
119- resolved = url . fileURLToPath ( new URL ( `../${ extensionPath } ` , import . meta. url ) ) ;
120- }
115+ export function startPowerSyncWorker ( options ?: Partial < PowerSyncWorkerOptions > ) {
116+ const resolvedOptions : PowerSyncWorkerOptions = {
117+ ...options ,
118+ extensionPath ( ) {
119+ const isCommonJsModule = import . meta. isBundlingToCommonJs ?? false ;
120+
121+ const platform = OS . platform ( ) ;
122+ let extensionPath : string ;
123+ if ( platform === 'win32' ) {
124+ extensionPath = 'powersync.dll' ;
125+ } else if ( platform === 'linux' ) {
126+ extensionPath = 'libpowersync.so' ;
127+ } else if ( platform === 'darwin' ) {
128+ extensionPath = 'libpowersync.dylib' ;
129+ } else {
130+ throw 'Unknown platform, PowerSync for Node.js currently supports Windows, Linux and macOS.' ;
131+ }
121132
122- db . loadExtension ( resolved , 'sqlite3_powersync_init' ) ;
123- } ;
133+ let resolved : string ;
134+ if ( isCommonJsModule ) {
135+ resolved = path . resolve ( __dirname , '../lib/' , extensionPath ) ;
136+ } else {
137+ resolved = url . fileURLToPath ( new URL ( `../${ extensionPath } ` , import . meta. url ) ) ;
138+ }
124139
125- Comlink . expose ( new BetterSqliteWorker ( ) , parentPort ! as Comlink . Endpoint ) ;
140+ return resolved ;
141+ }
142+ } ;
143+
144+ Comlink . expose ( new BetterSqliteWorker ( resolvedOptions ) , parentPort ! as Comlink . Endpoint ) ;
145+ }
0 commit comments