Skip to content

Commit abe8ec6

Browse files
committed
Allow compiling custom workers
1 parent 8c14e99 commit abe8ec6

File tree

7 files changed

+71
-31
lines changed

7 files changed

+71
-31
lines changed

packages/node/package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@
2121
},
2222
"type": "module",
2323
"exports": {
24-
"import": "./lib/index.js",
25-
"require": "./dist/bundle.cjs",
26-
"types": "./lib/index.d.ts"
24+
".": {
25+
"import": "./lib/index.js",
26+
"require": "./dist/bundle.cjs",
27+
"types": "./lib/index.d.ts"
28+
},
29+
"./worker.js": {
30+
"import": "./lib/worker.js",
31+
"require": "./dist/worker.cjs",
32+
"types": "./lib/worker.d.ts"
33+
}
2734
},
2835
"repository": {
2936
"type": "git",

packages/node/rollup.config.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ export default [
2222
}
2323
},
2424
{
25-
input: 'lib/db/SqliteWorker.js',
25+
input: 'lib/db/DefaultWorker.js',
26+
plugins: [plugin()],
27+
output: {
28+
file: 'dist/DefaultWorker.cjs',
29+
format: 'cjs',
30+
sourcemap: true
31+
}
32+
},
33+
{
34+
input: 'lib/worker.js',
2635
plugins: [plugin()],
2736
output: {
2837
file: 'dist/worker.cjs',

packages/node/src/db/BetterSQLite3DBAdapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
6363

6464
const workerFactory = this.options.openWorker ?? ((...args) => new Worker(...args));
6565
if (isCommonJsModule) {
66-
worker = workerFactory(path.resolve(__dirname, 'worker.cjs'), { name: workerName });
66+
worker = workerFactory(path.resolve(__dirname, 'DefaultWorker.cjs'), { name: workerName });
6767
} else {
68-
worker = workerFactory(new URL('./SqliteWorker.js', import.meta.url), { name: workerName });
68+
worker = workerFactory(new URL('./DefaultWorker.js', import.meta.url), { name: workerName });
6969
}
7070

7171
const listeners = new WeakMap<EventListenerOrEventListenerObject, (e: any) => void>();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { startPowerSyncWorker } from './SqliteWorker.js';
2+
3+
startPowerSyncWorker();

packages/node/src/db/SqliteWorker.ts

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,16 @@ class BlockingAsyncDatabase implements AsyncDatabase {
8282
}
8383

8484
class 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+
}

packages/node/src/node.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
interface ImportMeta {
2-
isBundlingToCommonJs?: boolean // This property is set by our rollup configuration
2+
isBundlingToCommonJs?: boolean; // This property is set by our rollup configuration
33
}

packages/node/src/worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './db/SqliteWorker.js';

0 commit comments

Comments
 (0)