@livestore/sqlite-wasm 3.46.0-build0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ /*
2
+ 2022-05-23
3
+
4
+ The author disclaims copyright to this source code. In place of a
5
+ legal notice, here is a blessing:
6
+
7
+ * May you do good and not evil.
8
+ * May you find forgiveness for yourself and forgive others.
9
+ * May you share freely, never taking more than you give.
10
+
11
+ ***********************************************************************
12
+
13
+ This is a JS Worker file for the main sqlite3 api. It loads
14
+ sqlite3.js, initializes the module, and postMessage()'s a message
15
+ after the module is initialized:
16
+
17
+ {type: 'sqlite3-api', result: 'worker1-ready'}
18
+
19
+ This seemingly superfluous level of indirection is necessary when
20
+ loading sqlite3.js via a Worker. Instantiating a worker with new
21
+ Worker("sqlite.js") will not (cannot) call sqlite3InitModule() to
22
+ initialize the module due to a timing/order-of-operations conflict
23
+ (and that symbol is not exported in a way that a Worker loading it
24
+ that way can see it). Thus JS code wanting to load the sqlite3
25
+ Worker-specific API needs to pass _this_ file (or equivalent) to the
26
+ Worker constructor and then listen for an event in the form shown
27
+ above in order to know when the module has completed initialization.
28
+
29
+ This file accepts a URL arguments to adjust how it loads sqlite3.js:
30
+
31
+ - `sqlite3.dir`, if set, treats the given directory name as the
32
+ directory from which `sqlite3.js` will be loaded.
33
+ */
34
+ import { default as sqlite3InitModule } from './sqlite3-bundler-friendly.mjs';
35
+ sqlite3InitModule().then((sqlite3) => sqlite3.initWorker1API());
@@ -0,0 +1,263 @@
1
+ /*
2
+ 2022-08-24
3
+
4
+ The author disclaims copyright to this source code. In place of a
5
+ legal notice, here is a blessing:
6
+
7
+ * May you do good and not evil.
8
+ * May you find forgiveness for yourself and forgive others.
9
+ * May you share freely, never taking more than you give.
10
+
11
+ ***********************************************************************
12
+
13
+ This file implements a Promise-based proxy for the sqlite3 Worker
14
+ API #1. It is intended to be included either from the main thread or
15
+ a Worker, but only if (A) the environment supports nested Workers
16
+ and (B) it's _not_ a Worker which loads the sqlite3 WASM/JS
17
+ module. This file's features will load that module and provide a
18
+ slightly simpler client-side interface than the slightly-lower-level
19
+ Worker API does.
20
+
21
+ This script necessarily exposes one global symbol, but clients may
22
+ freely `delete` that symbol after calling it.
23
+ */
24
+ 'use strict';
25
+ /**
26
+ * Configures an sqlite3 Worker API #1 Worker such that it can be manipulated
27
+ * via a Promise-based interface and returns a factory function which returns
28
+ * Promises for communicating with the worker. This proxy has an _almost_
29
+ * identical interface to the normal worker API, with any exceptions documented
30
+ * below.
31
+ *
32
+ * It requires a configuration object with the following properties:
33
+ *
34
+ * - `worker` (required): a Worker instance which loads `sqlite3-worker1.js` or a
35
+ * functional equivalent. Note that the promiser factory replaces the
36
+ * worker.onmessage property. This config option may alternately be a
37
+ * function, in which case this function re-assigns this property with the
38
+ * result of calling that function, enabling delayed instantiation of a
39
+ * Worker.
40
+ * - `onready` (optional, but...): this callback is called with no arguments when
41
+ * the worker fires its initial 'sqlite3-api'/'worker1-ready' message, which
42
+ * it does when sqlite3.initWorker1API() completes its initialization. This is
43
+ * the simplest way to tell the worker to kick off work at the earliest
44
+ * opportunity.
45
+ * - `onunhandled` (optional): a callback which gets passed the message event
46
+ * object for any worker.onmessage() events which are not handled by this
47
+ * proxy. Ideally that "should" never happen, as this proxy aims to handle all
48
+ * known message types.
49
+ * - `generateMessageId` (optional): a function which, when passed an
50
+ * about-to-be-posted message object, generates a _unique_ message ID for the
51
+ * message, which this API then assigns as the messageId property of the
52
+ * message. It _must_ generate unique IDs on each call so that dispatching can
53
+ * work. If not defined, a default generator is used (which should be
54
+ * sufficient for most or all cases).
55
+ * - `debug` (optional): a console.debug()-style function for logging information
56
+ * about messages.
57
+ *
58
+ * This function returns a stateful factory function with the following
59
+ * interfaces:
60
+ *
61
+ * - Promise function(messageType, messageArgs)
62
+ * - Promise function({message object})
63
+ *
64
+ * The first form expects the "type" and "args" values for a Worker message. The
65
+ * second expects an object in the form {type:..., args:...} plus any other
66
+ * properties the client cares to set. This function will always set the
67
+ * `messageId` property on the object, even if it's already set, and will set
68
+ * the `dbId` property to the current database ID if it is _not_ set in the
69
+ * message object.
70
+ *
71
+ * The function throws on error.
72
+ *
73
+ * The function installs a temporary message listener, posts a message to the
74
+ * configured Worker, and handles the message's response via the temporary
75
+ * message listener. The then() callback of the returned Promise is passed the
76
+ * `message.data` property from the resulting message, i.e. the payload from the
77
+ * worker, stripped of the lower-level event state which the onmessage() handler
78
+ * receives.
79
+ *
80
+ * Example usage:
81
+ *
82
+ * const config = {...};
83
+ * const sq3Promiser = sqlite3Worker1Promiser(config);
84
+ * sq3Promiser('open', {filename:"/foo.db"}).then(function(msg){
85
+ * console.log("open response",msg); // => {type:'open', result: {filename:'/foo.db'}, ...}
86
+ * });
87
+ * sq3Promiser({type:'close'}).then((msg)=>{
88
+ * console.log("close response",msg); // => {type:'close', result: {filename:'/foo.db'}, ...}
89
+ * });
90
+ *
91
+ * Differences from Worker API #1:
92
+ *
93
+ * - Exec's {callback: STRING} option does not work via this interface (it
94
+ * triggers an exception), but {callback: function} does and works exactly
95
+ * like the STRING form does in the Worker: the callback is called one time
96
+ * for each row of the result set, passed the same worker message format as
97
+ * the worker API emits:
98
+ *
99
+ * {type:typeString, row:VALUE, rowNumber:1-based-#, columnNames: array}
100
+ *
101
+ * Where `typeString` is an internally-synthesized message type string used
102
+ * temporarily for worker message dispatching. It can be ignored by all client
103
+ * code except that which tests this API. The `row` property contains the row
104
+ * result in the form implied by the `rowMode` option (defaulting to `'array'`).
105
+ * The `rowNumber` is a 1-based integer value incremented by 1 on each call into
106
+ * the callback.
107
+ *
108
+ * At the end of the result set, the same event is fired with (row=undefined,
109
+ * rowNumber=null) to indicate that the end of the result set has been reached.
110
+ * Note that the rows arrive via worker-posted messages, with all the
111
+ * implications of that.
112
+ *
113
+ * Notable shortcomings:
114
+ *
115
+ * - This API was not designed with ES6 modules in mind. Neither Firefox nor
116
+ * Safari support, as of March 2023, the {type:"module"} flag to the Worker
117
+ * constructor, so that particular usage is not something we're going to
118
+ * target for the time being:
119
+ *
120
+ * https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker
121
+ */
122
+ globalThis.sqlite3Worker1Promiser = function callee(
123
+ config = callee.defaultConfig,
124
+ ) {
125
+ // Inspired by: https://stackoverflow.com/a/52439530
126
+ if (1 === arguments.length && 'function' === typeof arguments[0]) {
127
+ const f = config;
128
+ config = Object.assign(Object.create(null), callee.defaultConfig);
129
+ config.onready = f;
130
+ } else {
131
+ config = Object.assign(Object.create(null), callee.defaultConfig, config);
132
+ }
133
+ const handlerMap = Object.create(null);
134
+ const noop = function () {};
135
+ const err =
136
+ config.onerror || noop; /* config.onerror is intentionally undocumented
137
+ pending finding a less ambiguous name */
138
+ const debug = config.debug || noop;
139
+ const idTypeMap = config.generateMessageId ? undefined : Object.create(null);
140
+ const genMsgId =
141
+ config.generateMessageId ||
142
+ function (msg) {
143
+ return (
144
+ msg.type + '#' + (idTypeMap[msg.type] = (idTypeMap[msg.type] || 0) + 1)
145
+ );
146
+ };
147
+ const toss = (...args) => {
148
+ throw new Error(args.join(' '));
149
+ };
150
+ if (!config.worker) config.worker = callee.defaultConfig.worker;
151
+ if ('function' === typeof config.worker) config.worker = config.worker();
152
+ let dbId;
153
+ let promiserFunc;
154
+ config.worker.onmessage = function (ev) {
155
+ ev = ev.data;
156
+ debug('worker1.onmessage', ev);
157
+ let msgHandler = handlerMap[ev.messageId];
158
+ if (!msgHandler) {
159
+ if (ev && 'sqlite3-api' === ev.type && 'worker1-ready' === ev.result) {
160
+ /*fired one time when the Worker1 API initializes*/
161
+ if (config.onready) config.onready(promiserFunc);
162
+ return;
163
+ }
164
+ msgHandler = handlerMap[ev.type] /* check for exec per-row callback */;
165
+ if (msgHandler && msgHandler.onrow) {
166
+ msgHandler.onrow(ev);
167
+ return;
168
+ }
169
+ if (config.onunhandled) config.onunhandled(arguments[0]);
170
+ else err('sqlite3Worker1Promiser() unhandled worker message:', ev);
171
+ return;
172
+ }
173
+ delete handlerMap[ev.messageId];
174
+ switch (ev.type) {
175
+ case 'error':
176
+ msgHandler.reject(ev);
177
+ return;
178
+ case 'open':
179
+ if (!dbId) dbId = ev.dbId;
180
+ break;
181
+ case 'close':
182
+ if (ev.dbId === dbId) dbId = undefined;
183
+ break;
184
+ default:
185
+ break;
186
+ }
187
+ try {
188
+ msgHandler.resolve(ev);
189
+ } catch (e) {
190
+ msgHandler.reject(e);
191
+ }
192
+ } /*worker.onmessage()*/;
193
+ return (promiserFunc = function (/*(msgType, msgArgs) || (msgEnvelope)*/) {
194
+ let msg;
195
+ if (1 === arguments.length) {
196
+ msg = arguments[0];
197
+ } else if (2 === arguments.length) {
198
+ msg = Object.create(null);
199
+ msg.type = arguments[0];
200
+ msg.args = arguments[1];
201
+ msg.dbId = msg.args.dbId;
202
+ } else {
203
+ toss('Invalid arugments for sqlite3Worker1Promiser()-created factory.');
204
+ }
205
+ if (!msg.dbId && msg.type !== 'open') msg.dbId = dbId;
206
+ msg.messageId = genMsgId(msg);
207
+ msg.departureTime = performance.now();
208
+ const proxy = Object.create(null);
209
+ proxy.message = msg;
210
+ let rowCallbackId /* message handler ID for exec on-row callback proxy */;
211
+ if ('exec' === msg.type && msg.args) {
212
+ if ('function' === typeof msg.args.callback) {
213
+ rowCallbackId = msg.messageId + ':row';
214
+ proxy.onrow = msg.args.callback;
215
+ msg.args.callback = rowCallbackId;
216
+ handlerMap[rowCallbackId] = proxy;
217
+ } else if ('string' === typeof msg.args.callback) {
218
+ toss(
219
+ 'exec callback may not be a string when using the Promise interface.',
220
+ );
221
+ /**
222
+ * Design note: the reason for this limitation is that this API takes
223
+ * over worker.onmessage() and the client has no way of adding their own
224
+ * message-type handlers to it. Per-row callbacks are implemented as
225
+ * short-lived message.type mappings for worker.onmessage().
226
+ *
227
+ * We "could" work around this by providing a new
228
+ * config.fallbackMessageHandler (or some such) which contains
229
+ * a map of event type names to callbacks. Seems like overkill
230
+ * for now, seeing as the client can pass callback functions
231
+ * to this interface (whereas the string-form "callback" is
232
+ * needed for the over-the-Worker interface).
233
+ */
234
+ }
235
+ }
236
+ //debug("requestWork", msg);
237
+ let p = new Promise(function (resolve, reject) {
238
+ proxy.resolve = resolve;
239
+ proxy.reject = reject;
240
+ handlerMap[msg.messageId] = proxy;
241
+ debug(
242
+ 'Posting',
243
+ msg.type,
244
+ 'message to Worker dbId=' + (dbId || 'default') + ':',
245
+ msg,
246
+ );
247
+ config.worker.postMessage(msg);
248
+ });
249
+ if (rowCallbackId) p = p.finally(() => delete handlerMap[rowCallbackId]);
250
+ return p;
251
+ });
252
+ } /*sqlite3Worker1Promiser()*/;
253
+ globalThis.sqlite3Worker1Promiser.defaultConfig = {
254
+ worker: function () {
255
+ return new Worker(
256
+ new URL('sqlite3-worker1-bundler-friendly.mjs', import.meta.url),
257
+ {
258
+ type: 'module',
259
+ },
260
+ );
261
+ },
262
+ onerror: (...args) => console.error('worker1 promiser error', ...args),
263
+ };
@@ -0,0 +1,273 @@
1
+ /*
2
+ 2022-08-24
3
+
4
+ The author disclaims copyright to this source code. In place of a
5
+ legal notice, here is a blessing:
6
+
7
+ * May you do good and not evil.
8
+ * May you find forgiveness for yourself and forgive others.
9
+ * May you share freely, never taking more than you give.
10
+
11
+ ***********************************************************************
12
+
13
+ This file implements a Promise-based proxy for the sqlite3 Worker
14
+ API #1. It is intended to be included either from the main thread or
15
+ a Worker, but only if (A) the environment supports nested Workers
16
+ and (B) it's _not_ a Worker which loads the sqlite3 WASM/JS
17
+ module. This file's features will load that module and provide a
18
+ slightly simpler client-side interface than the slightly-lower-level
19
+ Worker API does.
20
+
21
+ This script necessarily exposes one global symbol, but clients may
22
+ freely `delete` that symbol after calling it.
23
+ */
24
+ 'use strict';
25
+ /**
26
+ * Configures an sqlite3 Worker API #1 Worker such that it can be manipulated
27
+ * via a Promise-based interface and returns a factory function which returns
28
+ * Promises for communicating with the worker. This proxy has an _almost_
29
+ * identical interface to the normal worker API, with any exceptions documented
30
+ * below.
31
+ *
32
+ * It requires a configuration object with the following properties:
33
+ *
34
+ * - `worker` (required): a Worker instance which loads `sqlite3-worker1.js` or a
35
+ * functional equivalent. Note that the promiser factory replaces the
36
+ * worker.onmessage property. This config option may alternately be a
37
+ * function, in which case this function re-assigns this property with the
38
+ * result of calling that function, enabling delayed instantiation of a
39
+ * Worker.
40
+ * - `onready` (optional, but...): this callback is called with no arguments when
41
+ * the worker fires its initial 'sqlite3-api'/'worker1-ready' message, which
42
+ * it does when sqlite3.initWorker1API() completes its initialization. This is
43
+ * the simplest way to tell the worker to kick off work at the earliest
44
+ * opportunity.
45
+ * - `onunhandled` (optional): a callback which gets passed the message event
46
+ * object for any worker.onmessage() events which are not handled by this
47
+ * proxy. Ideally that "should" never happen, as this proxy aims to handle all
48
+ * known message types.
49
+ * - `generateMessageId` (optional): a function which, when passed an
50
+ * about-to-be-posted message object, generates a _unique_ message ID for the
51
+ * message, which this API then assigns as the messageId property of the
52
+ * message. It _must_ generate unique IDs on each call so that dispatching can
53
+ * work. If not defined, a default generator is used (which should be
54
+ * sufficient for most or all cases).
55
+ * - `debug` (optional): a console.debug()-style function for logging information
56
+ * about messages.
57
+ *
58
+ * This function returns a stateful factory function with the following
59
+ * interfaces:
60
+ *
61
+ * - Promise function(messageType, messageArgs)
62
+ * - Promise function({message object})
63
+ *
64
+ * The first form expects the "type" and "args" values for a Worker message. The
65
+ * second expects an object in the form {type:..., args:...} plus any other
66
+ * properties the client cares to set. This function will always set the
67
+ * `messageId` property on the object, even if it's already set, and will set
68
+ * the `dbId` property to the current database ID if it is _not_ set in the
69
+ * message object.
70
+ *
71
+ * The function throws on error.
72
+ *
73
+ * The function installs a temporary message listener, posts a message to the
74
+ * configured Worker, and handles the message's response via the temporary
75
+ * message listener. The then() callback of the returned Promise is passed the
76
+ * `message.data` property from the resulting message, i.e. the payload from the
77
+ * worker, stripped of the lower-level event state which the onmessage() handler
78
+ * receives.
79
+ *
80
+ * Example usage:
81
+ *
82
+ * const config = {...};
83
+ * const sq3Promiser = sqlite3Worker1Promiser(config);
84
+ * sq3Promiser('open', {filename:"/foo.db"}).then(function(msg){
85
+ * console.log("open response",msg); // => {type:'open', result: {filename:'/foo.db'}, ...}
86
+ * });
87
+ * sq3Promiser({type:'close'}).then((msg)=>{
88
+ * console.log("close response",msg); // => {type:'close', result: {filename:'/foo.db'}, ...}
89
+ * });
90
+ *
91
+ * Differences from Worker API #1:
92
+ *
93
+ * - Exec's {callback: STRING} option does not work via this interface (it
94
+ * triggers an exception), but {callback: function} does and works exactly
95
+ * like the STRING form does in the Worker: the callback is called one time
96
+ * for each row of the result set, passed the same worker message format as
97
+ * the worker API emits:
98
+ *
99
+ * {type:typeString, row:VALUE, rowNumber:1-based-#, columnNames: array}
100
+ *
101
+ * Where `typeString` is an internally-synthesized message type string used
102
+ * temporarily for worker message dispatching. It can be ignored by all client
103
+ * code except that which tests this API. The `row` property contains the row
104
+ * result in the form implied by the `rowMode` option (defaulting to `'array'`).
105
+ * The `rowNumber` is a 1-based integer value incremented by 1 on each call into
106
+ * the callback.
107
+ *
108
+ * At the end of the result set, the same event is fired with (row=undefined,
109
+ * rowNumber=null) to indicate that the end of the result set has been reached.
110
+ * Note that the rows arrive via worker-posted messages, with all the
111
+ * implications of that.
112
+ *
113
+ * Notable shortcomings:
114
+ *
115
+ * - This API was not designed with ES6 modules in mind. Neither Firefox nor
116
+ * Safari support, as of March 2023, the {type:"module"} flag to the Worker
117
+ * constructor, so that particular usage is not something we're going to
118
+ * target for the time being:
119
+ *
120
+ * https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker
121
+ */
122
+ globalThis.sqlite3Worker1Promiser = function callee(
123
+ config = callee.defaultConfig,
124
+ ) {
125
+ // Inspired by: https://stackoverflow.com/a/52439530
126
+ if (1 === arguments.length && 'function' === typeof arguments[0]) {
127
+ const f = config;
128
+ config = Object.assign(Object.create(null), callee.defaultConfig);
129
+ config.onready = f;
130
+ } else {
131
+ config = Object.assign(Object.create(null), callee.defaultConfig, config);
132
+ }
133
+ const handlerMap = Object.create(null);
134
+ const noop = function () {};
135
+ const err =
136
+ config.onerror || noop; /* config.onerror is intentionally undocumented
137
+ pending finding a less ambiguous name */
138
+ const debug = config.debug || noop;
139
+ const idTypeMap = config.generateMessageId ? undefined : Object.create(null);
140
+ const genMsgId =
141
+ config.generateMessageId ||
142
+ function (msg) {
143
+ return (
144
+ msg.type + '#' + (idTypeMap[msg.type] = (idTypeMap[msg.type] || 0) + 1)
145
+ );
146
+ };
147
+ const toss = (...args) => {
148
+ throw new Error(args.join(' '));
149
+ };
150
+ if (!config.worker) config.worker = callee.defaultConfig.worker;
151
+ if ('function' === typeof config.worker) config.worker = config.worker();
152
+ let dbId;
153
+ let promiserFunc;
154
+ config.worker.onmessage = function (ev) {
155
+ ev = ev.data;
156
+ debug('worker1.onmessage', ev);
157
+ let msgHandler = handlerMap[ev.messageId];
158
+ if (!msgHandler) {
159
+ if (ev && 'sqlite3-api' === ev.type && 'worker1-ready' === ev.result) {
160
+ /*fired one time when the Worker1 API initializes*/
161
+ if (config.onready) config.onready(promiserFunc);
162
+ return;
163
+ }
164
+ msgHandler = handlerMap[ev.type] /* check for exec per-row callback */;
165
+ if (msgHandler && msgHandler.onrow) {
166
+ msgHandler.onrow(ev);
167
+ return;
168
+ }
169
+ if (config.onunhandled) config.onunhandled(arguments[0]);
170
+ else err('sqlite3Worker1Promiser() unhandled worker message:', ev);
171
+ return;
172
+ }
173
+ delete handlerMap[ev.messageId];
174
+ switch (ev.type) {
175
+ case 'error':
176
+ msgHandler.reject(ev);
177
+ return;
178
+ case 'open':
179
+ if (!dbId) dbId = ev.dbId;
180
+ break;
181
+ case 'close':
182
+ if (ev.dbId === dbId) dbId = undefined;
183
+ break;
184
+ default:
185
+ break;
186
+ }
187
+ try {
188
+ msgHandler.resolve(ev);
189
+ } catch (e) {
190
+ msgHandler.reject(e);
191
+ }
192
+ } /*worker.onmessage()*/;
193
+ return (promiserFunc = function (/*(msgType, msgArgs) || (msgEnvelope)*/) {
194
+ let msg;
195
+ if (1 === arguments.length) {
196
+ msg = arguments[0];
197
+ } else if (2 === arguments.length) {
198
+ msg = Object.create(null);
199
+ msg.type = arguments[0];
200
+ msg.args = arguments[1];
201
+ msg.dbId = msg.args.dbId;
202
+ } else {
203
+ toss('Invalid arugments for sqlite3Worker1Promiser()-created factory.');
204
+ }
205
+ if (!msg.dbId && msg.type !== 'open') msg.dbId = dbId;
206
+ msg.messageId = genMsgId(msg);
207
+ msg.departureTime = performance.now();
208
+ const proxy = Object.create(null);
209
+ proxy.message = msg;
210
+ let rowCallbackId /* message handler ID for exec on-row callback proxy */;
211
+ if ('exec' === msg.type && msg.args) {
212
+ if ('function' === typeof msg.args.callback) {
213
+ rowCallbackId = msg.messageId + ':row';
214
+ proxy.onrow = msg.args.callback;
215
+ msg.args.callback = rowCallbackId;
216
+ handlerMap[rowCallbackId] = proxy;
217
+ } else if ('string' === typeof msg.args.callback) {
218
+ toss(
219
+ 'exec callback may not be a string when using the Promise interface.',
220
+ );
221
+ /**
222
+ * Design note: the reason for this limitation is that this API takes
223
+ * over worker.onmessage() and the client has no way of adding their own
224
+ * message-type handlers to it. Per-row callbacks are implemented as
225
+ * short-lived message.type mappings for worker.onmessage().
226
+ *
227
+ * We "could" work around this by providing a new
228
+ * config.fallbackMessageHandler (or some such) which contains
229
+ * a map of event type names to callbacks. Seems like overkill
230
+ * for now, seeing as the client can pass callback functions
231
+ * to this interface (whereas the string-form "callback" is
232
+ * needed for the over-the-Worker interface).
233
+ */
234
+ }
235
+ }
236
+ //debug("requestWork", msg);
237
+ let p = new Promise(function (resolve, reject) {
238
+ proxy.resolve = resolve;
239
+ proxy.reject = reject;
240
+ handlerMap[msg.messageId] = proxy;
241
+ debug(
242
+ 'Posting',
243
+ msg.type,
244
+ 'message to Worker dbId=' + (dbId || 'default') + ':',
245
+ msg,
246
+ );
247
+ config.worker.postMessage(msg);
248
+ });
249
+ if (rowCallbackId) p = p.finally(() => delete handlerMap[rowCallbackId]);
250
+ return p;
251
+ });
252
+ } /*sqlite3Worker1Promiser()*/;
253
+ globalThis.sqlite3Worker1Promiser.defaultConfig = {
254
+ worker: function () {
255
+ let theJs = 'sqlite3-worker1.js';
256
+ if (this.currentScript) {
257
+ const src = this.currentScript.src.split('/');
258
+ src.pop();
259
+ theJs = src.join('/') + '/' + theJs;
260
+ //sqlite3.config.warn("promiser currentScript, theJs =",this.currentScript,theJs);
261
+ } else if (globalThis.location) {
262
+ //sqlite3.config.warn("promiser globalThis.location =",globalThis.location);
263
+ const urlParams = new URL(globalThis.location.href).searchParams;
264
+ if (urlParams.has('sqlite3.dir')) {
265
+ theJs = urlParams.get('sqlite3.dir') + '/' + theJs;
266
+ }
267
+ }
268
+ return new Worker(theJs + globalThis.location.search);
269
+ }.bind({
270
+ currentScript: globalThis?.document?.currentScript,
271
+ }),
272
+ onerror: (...args) => console.error('worker1 promiser error', ...args),
273
+ };
@@ -0,0 +1,46 @@
1
+ /*
2
+ 2022-05-23
3
+
4
+ The author disclaims copyright to this source code. In place of a
5
+ legal notice, here is a blessing:
6
+
7
+ * May you do good and not evil.
8
+ * May you find forgiveness for yourself and forgive others.
9
+ * May you share freely, never taking more than you give.
10
+
11
+ ***********************************************************************
12
+
13
+ This is a JS Worker file for the main sqlite3 api. It loads
14
+ sqlite3.js, initializes the module, and postMessage()'s a message
15
+ after the module is initialized:
16
+
17
+ {type: 'sqlite3-api', result: 'worker1-ready'}
18
+
19
+ This seemingly superfluous level of indirection is necessary when
20
+ loading sqlite3.js via a Worker. Instantiating a worker with new
21
+ Worker("sqlite.js") will not (cannot) call sqlite3InitModule() to
22
+ initialize the module due to a timing/order-of-operations conflict
23
+ (and that symbol is not exported in a way that a Worker loading it
24
+ that way can see it). Thus JS code wanting to load the sqlite3
25
+ Worker-specific API needs to pass _this_ file (or equivalent) to the
26
+ Worker constructor and then listen for an event in the form shown
27
+ above in order to know when the module has completed initialization.
28
+
29
+ This file accepts a URL arguments to adjust how it loads sqlite3.js:
30
+
31
+ - `sqlite3.dir`, if set, treats the given directory name as the
32
+ directory from which `sqlite3.js` will be loaded.
33
+ */
34
+ 'use strict';
35
+ {
36
+ const urlParams = globalThis.location
37
+ ? new URL(globalThis.location.href).searchParams
38
+ : new URLSearchParams();
39
+ let theJs = 'sqlite3.js';
40
+ if (urlParams.has('sqlite3.dir')) {
41
+ theJs = urlParams.get('sqlite3.dir') + '/' + theJs;
42
+ }
43
+ //console.warn("worker1 theJs =",theJs);
44
+ importScripts(theJs);
45
+ }
46
+ sqlite3InitModule().then((sqlite3) => sqlite3.initWorker1API());