Skip to content

Commit 76bfc7f

Browse files
committed
1 binaries uploaded
1 parent e58212e commit 76bfc7f

10 files changed

+54697
-0
lines changed

sqlite-wasm/jswasm/sqlite3-bundler-friendly.mjs

Lines changed: 13302 additions & 0 deletions
Large diffs are not rendered by default.

sqlite-wasm/jswasm/sqlite3-node.mjs

Lines changed: 13347 additions & 0 deletions
Large diffs are not rendered by default.

sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js

Lines changed: 1016 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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());
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
globalThis.sqlite3Worker1Promiser = function callee(
27+
config = callee.defaultConfig,
28+
) {
29+
if (1 === arguments.length && 'function' === typeof arguments[0]) {
30+
const f = config;
31+
config = Object.assign(Object.create(null), callee.defaultConfig);
32+
config.onready = f;
33+
} else {
34+
config = Object.assign(Object.create(null), callee.defaultConfig, config);
35+
}
36+
const handlerMap = Object.create(null);
37+
const noop = function () {};
38+
const err = config.onerror || noop;
39+
const debug = config.debug || noop;
40+
const idTypeMap = config.generateMessageId ? undefined : Object.create(null);
41+
const genMsgId =
42+
config.generateMessageId ||
43+
function (msg) {
44+
return (
45+
msg.type + '#' + (idTypeMap[msg.type] = (idTypeMap[msg.type] || 0) + 1)
46+
);
47+
};
48+
const toss = (...args) => {
49+
throw new Error(args.join(' '));
50+
};
51+
if (!config.worker) config.worker = callee.defaultConfig.worker;
52+
if ('function' === typeof config.worker) config.worker = config.worker();
53+
let dbId;
54+
config.worker.onmessage = function (ev) {
55+
ev = ev.data;
56+
debug('worker1.onmessage', ev);
57+
let msgHandler = handlerMap[ev.messageId];
58+
if (!msgHandler) {
59+
if (ev && 'sqlite3-api' === ev.type && 'worker1-ready' === ev.result) {
60+
if (config.onready) config.onready();
61+
return;
62+
}
63+
msgHandler = handlerMap[ev.type];
64+
if (msgHandler && msgHandler.onrow) {
65+
msgHandler.onrow(ev);
66+
return;
67+
}
68+
if (config.onunhandled) config.onunhandled(arguments[0]);
69+
else err('sqlite3Worker1Promiser() unhandled worker message:', ev);
70+
return;
71+
}
72+
delete handlerMap[ev.messageId];
73+
switch (ev.type) {
74+
case 'error':
75+
msgHandler.reject(ev);
76+
return;
77+
case 'open':
78+
if (!dbId) dbId = ev.dbId;
79+
break;
80+
case 'close':
81+
if (ev.dbId === dbId) dbId = undefined;
82+
break;
83+
default:
84+
break;
85+
}
86+
try {
87+
msgHandler.resolve(ev);
88+
} catch (e) {
89+
msgHandler.reject(e);
90+
}
91+
};
92+
return function () {
93+
let msg;
94+
if (1 === arguments.length) {
95+
msg = arguments[0];
96+
} else if (2 === arguments.length) {
97+
msg = {
98+
type: arguments[0],
99+
args: arguments[1],
100+
};
101+
} else {
102+
toss('Invalid arugments for sqlite3Worker1Promiser()-created factory.');
103+
}
104+
if (!msg.dbId) msg.dbId = dbId;
105+
msg.messageId = genMsgId(msg);
106+
msg.departureTime = performance.now();
107+
const proxy = Object.create(null);
108+
proxy.message = msg;
109+
let rowCallbackId;
110+
if ('exec' === msg.type && msg.args) {
111+
if ('function' === typeof msg.args.callback) {
112+
rowCallbackId = msg.messageId + ':row';
113+
proxy.onrow = msg.args.callback;
114+
msg.args.callback = rowCallbackId;
115+
handlerMap[rowCallbackId] = proxy;
116+
} else if ('string' === typeof msg.args.callback) {
117+
toss(
118+
'exec callback may not be a string when using the Promise interface.',
119+
);
120+
}
121+
}
122+
123+
let p = new Promise(function (resolve, reject) {
124+
proxy.resolve = resolve;
125+
proxy.reject = reject;
126+
handlerMap[msg.messageId] = proxy;
127+
debug(
128+
'Posting',
129+
msg.type,
130+
'message to Worker dbId=' + (dbId || 'default') + ':',
131+
msg,
132+
);
133+
config.worker.postMessage(msg);
134+
});
135+
if (rowCallbackId) p = p.finally(() => delete handlerMap[rowCallbackId]);
136+
return p;
137+
};
138+
};
139+
globalThis.sqlite3Worker1Promiser.defaultConfig = {
140+
worker: function () {
141+
return new Worker('sqlite3-worker1-bundler-friendly.mjs', {
142+
type: 'module',
143+
});
144+
}.bind({
145+
currentScript: globalThis?.document?.currentScript,
146+
}),
147+
onerror: (...args) => console.error('worker1 promiser error', ...args),
148+
};
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
globalThis.sqlite3Worker1Promiser = function callee(
27+
config = callee.defaultConfig,
28+
) {
29+
if (1 === arguments.length && 'function' === typeof arguments[0]) {
30+
const f = config;
31+
config = Object.assign(Object.create(null), callee.defaultConfig);
32+
config.onready = f;
33+
} else {
34+
config = Object.assign(Object.create(null), callee.defaultConfig, config);
35+
}
36+
const handlerMap = Object.create(null);
37+
const noop = function () {};
38+
const err = config.onerror || noop;
39+
const debug = config.debug || noop;
40+
const idTypeMap = config.generateMessageId ? undefined : Object.create(null);
41+
const genMsgId =
42+
config.generateMessageId ||
43+
function (msg) {
44+
return (
45+
msg.type + '#' + (idTypeMap[msg.type] = (idTypeMap[msg.type] || 0) + 1)
46+
);
47+
};
48+
const toss = (...args) => {
49+
throw new Error(args.join(' '));
50+
};
51+
if (!config.worker) config.worker = callee.defaultConfig.worker;
52+
if ('function' === typeof config.worker) config.worker = config.worker();
53+
let dbId;
54+
config.worker.onmessage = function (ev) {
55+
ev = ev.data;
56+
debug('worker1.onmessage', ev);
57+
let msgHandler = handlerMap[ev.messageId];
58+
if (!msgHandler) {
59+
if (ev && 'sqlite3-api' === ev.type && 'worker1-ready' === ev.result) {
60+
if (config.onready) config.onready();
61+
return;
62+
}
63+
msgHandler = handlerMap[ev.type];
64+
if (msgHandler && msgHandler.onrow) {
65+
msgHandler.onrow(ev);
66+
return;
67+
}
68+
if (config.onunhandled) config.onunhandled(arguments[0]);
69+
else err('sqlite3Worker1Promiser() unhandled worker message:', ev);
70+
return;
71+
}
72+
delete handlerMap[ev.messageId];
73+
switch (ev.type) {
74+
case 'error':
75+
msgHandler.reject(ev);
76+
return;
77+
case 'open':
78+
if (!dbId) dbId = ev.dbId;
79+
break;
80+
case 'close':
81+
if (ev.dbId === dbId) dbId = undefined;
82+
break;
83+
default:
84+
break;
85+
}
86+
try {
87+
msgHandler.resolve(ev);
88+
} catch (e) {
89+
msgHandler.reject(e);
90+
}
91+
};
92+
return function () {
93+
let msg;
94+
if (1 === arguments.length) {
95+
msg = arguments[0];
96+
} else if (2 === arguments.length) {
97+
msg = {
98+
type: arguments[0],
99+
args: arguments[1],
100+
};
101+
} else {
102+
toss('Invalid arugments for sqlite3Worker1Promiser()-created factory.');
103+
}
104+
if (!msg.dbId) msg.dbId = dbId;
105+
msg.messageId = genMsgId(msg);
106+
msg.departureTime = performance.now();
107+
const proxy = Object.create(null);
108+
proxy.message = msg;
109+
let rowCallbackId;
110+
if ('exec' === msg.type && msg.args) {
111+
if ('function' === typeof msg.args.callback) {
112+
rowCallbackId = msg.messageId + ':row';
113+
proxy.onrow = msg.args.callback;
114+
msg.args.callback = rowCallbackId;
115+
handlerMap[rowCallbackId] = proxy;
116+
} else if ('string' === typeof msg.args.callback) {
117+
toss(
118+
'exec callback may not be a string when using the Promise interface.',
119+
);
120+
}
121+
}
122+
123+
let p = new Promise(function (resolve, reject) {
124+
proxy.resolve = resolve;
125+
proxy.reject = reject;
126+
handlerMap[msg.messageId] = proxy;
127+
debug(
128+
'Posting',
129+
msg.type,
130+
'message to Worker dbId=' + (dbId || 'default') + ':',
131+
msg,
132+
);
133+
config.worker.postMessage(msg);
134+
});
135+
if (rowCallbackId) p = p.finally(() => delete handlerMap[rowCallbackId]);
136+
return p;
137+
};
138+
};
139+
globalThis.sqlite3Worker1Promiser.defaultConfig = {
140+
worker: function () {
141+
let theJs = 'sqlite3-worker1.js';
142+
if (this.currentScript) {
143+
const src = this.currentScript.src.split('/');
144+
src.pop();
145+
theJs = src.join('/') + '/' + theJs;
146+
} else if (globalThis.location) {
147+
const urlParams = new URL(globalThis.location.href).searchParams;
148+
if (urlParams.has('sqlite3.dir')) {
149+
theJs = urlParams.get('sqlite3.dir') + '/' + theJs;
150+
}
151+
}
152+
return new Worker(theJs + globalThis.location.search);
153+
}.bind({
154+
currentScript: globalThis?.document?.currentScript,
155+
}),
156+
onerror: (...args) => console.error('worker1 promiser error', ...args),
157+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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(self.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+
44+
importScripts(theJs);
45+
}
46+
sqlite3InitModule().then((sqlite3) => sqlite3.initWorker1API());

0 commit comments

Comments
 (0)