@peerbit/indexer-sqlite3 0.0.1-cccc078

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.
Files changed (63) hide show
  1. package/README.md +111 -0
  2. package/dist/benchmark/index.d.ts +2 -0
  3. package/dist/benchmark/index.d.ts.map +1 -0
  4. package/dist/benchmark/index.js +6 -0
  5. package/dist/benchmark/index.js.map +1 -0
  6. package/dist/peerbit/sqlite3-bundler-friendly.mjs +14481 -0
  7. package/dist/peerbit/sqlite3-node.mjs +12561 -0
  8. package/dist/peerbit/sqlite3-opfs-async-proxy.js +826 -0
  9. package/dist/peerbit/sqlite3-worker1-bundler-friendly.mjs +35 -0
  10. package/dist/peerbit/sqlite3-worker1-promiser.js +193 -0
  11. package/dist/peerbit/sqlite3-worker1-promiser.mjs +187 -0
  12. package/dist/peerbit/sqlite3-worker1.js +46 -0
  13. package/dist/peerbit/sqlite3.js +14520 -0
  14. package/dist/peerbit/sqlite3.min.js +21695 -0
  15. package/dist/peerbit/sqlite3.mjs +14483 -0
  16. package/dist/peerbit/sqlite3.wasm +0 -0
  17. package/dist/peerbit/sqlite3.worker.min.js +17995 -0
  18. package/dist/src/engine.d.ts +90 -0
  19. package/dist/src/engine.d.ts.map +1 -0
  20. package/dist/src/engine.js +414 -0
  21. package/dist/src/engine.js.map +1 -0
  22. package/dist/src/index.d.ts +5 -0
  23. package/dist/src/index.d.ts.map +1 -0
  24. package/dist/src/index.js +15 -0
  25. package/dist/src/index.js.map +1 -0
  26. package/dist/src/schema.d.ts +73 -0
  27. package/dist/src/schema.d.ts.map +1 -0
  28. package/dist/src/schema.js +1075 -0
  29. package/dist/src/schema.js.map +1 -0
  30. package/dist/src/sqlite3-messages.worker.d.ts +86 -0
  31. package/dist/src/sqlite3-messages.worker.d.ts.map +1 -0
  32. package/dist/src/sqlite3-messages.worker.js +9 -0
  33. package/dist/src/sqlite3-messages.worker.js.map +1 -0
  34. package/dist/src/sqlite3.browser.d.ts +4 -0
  35. package/dist/src/sqlite3.browser.d.ts.map +1 -0
  36. package/dist/src/sqlite3.browser.js +181 -0
  37. package/dist/src/sqlite3.browser.js.map +1 -0
  38. package/dist/src/sqlite3.d.ts +4 -0
  39. package/dist/src/sqlite3.d.ts.map +1 -0
  40. package/dist/src/sqlite3.js +51 -0
  41. package/dist/src/sqlite3.js.map +1 -0
  42. package/dist/src/sqlite3.wasm.d.ts +30 -0
  43. package/dist/src/sqlite3.wasm.d.ts.map +1 -0
  44. package/dist/src/sqlite3.wasm.js +180 -0
  45. package/dist/src/sqlite3.wasm.js.map +1 -0
  46. package/dist/src/sqlite3.worker.d.ts +2 -0
  47. package/dist/src/sqlite3.worker.d.ts.map +1 -0
  48. package/dist/src/sqlite3.worker.js +105 -0
  49. package/dist/src/sqlite3.worker.js.map +1 -0
  50. package/dist/src/types.d.ts +23 -0
  51. package/dist/src/types.d.ts.map +1 -0
  52. package/dist/src/types.js +2 -0
  53. package/dist/src/types.js.map +1 -0
  54. package/package.json +80 -0
  55. package/src/engine.ts +639 -0
  56. package/src/index.ts +16 -0
  57. package/src/schema.ts +1607 -0
  58. package/src/sqlite3-messages.worker.ts +123 -0
  59. package/src/sqlite3.browser.ts +245 -0
  60. package/src/sqlite3.ts +56 -0
  61. package/src/sqlite3.wasm.ts +211 -0
  62. package/src/sqlite3.worker.ts +109 -0
  63. package/src/types.ts +39 -0
@@ -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,193 @@
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
+ let promiserFunc;
55
+ config.worker.onmessage = function (ev) {
56
+ ev = ev.data;
57
+ debug('worker1.onmessage', ev);
58
+ let msgHandler = handlerMap[ev.messageId];
59
+ if (!msgHandler) {
60
+ if (ev && 'sqlite3-api' === ev.type && 'worker1-ready' === ev.result) {
61
+ if (config.onready) config.onready(promiserFunc);
62
+ return;
63
+ }
64
+ msgHandler = handlerMap[ev.type];
65
+ if (msgHandler && msgHandler.onrow) {
66
+ msgHandler.onrow(ev);
67
+ return;
68
+ }
69
+ if (config.onunhandled) config.onunhandled(arguments[0]);
70
+ else err('sqlite3Worker1Promiser() unhandled worker message:', ev);
71
+ return;
72
+ }
73
+ delete handlerMap[ev.messageId];
74
+ switch (ev.type) {
75
+ case 'error':
76
+ msgHandler.reject(ev);
77
+ return;
78
+ case 'open':
79
+ if (!dbId) dbId = ev.dbId;
80
+ break;
81
+ case 'close':
82
+ if (ev.dbId === dbId) dbId = undefined;
83
+ break;
84
+ default:
85
+ break;
86
+ }
87
+ try {
88
+ msgHandler.resolve(ev);
89
+ } catch (e) {
90
+ msgHandler.reject(e);
91
+ }
92
+ };
93
+ return (promiserFunc = function () {
94
+ let msg;
95
+ if (1 === arguments.length) {
96
+ msg = arguments[0];
97
+ } else if (2 === arguments.length) {
98
+ msg = Object.create(null);
99
+ msg.type = arguments[0];
100
+ msg.args = arguments[1];
101
+ msg.dbId = msg.args.dbId;
102
+ } else {
103
+ toss('Invalid arguments for sqlite3Worker1Promiser()-created factory.');
104
+ }
105
+ if (!msg.dbId && msg.type !== 'open') msg.dbId = dbId;
106
+ msg.messageId = genMsgId(msg);
107
+ msg.departureTime = performance.now();
108
+ const proxy = Object.create(null);
109
+ proxy.message = msg;
110
+ let rowCallbackId;
111
+ if ('exec' === msg.type && msg.args) {
112
+ if ('function' === typeof msg.args.callback) {
113
+ rowCallbackId = msg.messageId + ':row';
114
+ proxy.onrow = msg.args.callback;
115
+ msg.args.callback = rowCallbackId;
116
+ handlerMap[rowCallbackId] = proxy;
117
+ } else if ('string' === typeof msg.args.callback) {
118
+ toss(
119
+ 'exec callback may not be a string when using the Promise interface.',
120
+ );
121
+ }
122
+ }
123
+
124
+ let p = new Promise(function (resolve, reject) {
125
+ proxy.resolve = resolve;
126
+ proxy.reject = reject;
127
+ handlerMap[msg.messageId] = proxy;
128
+ debug(
129
+ 'Posting',
130
+ msg.type,
131
+ 'message to Worker dbId=' + (dbId || 'default') + ':',
132
+ msg,
133
+ );
134
+ config.worker.postMessage(msg);
135
+ });
136
+ if (rowCallbackId) p = p.finally(() => delete handlerMap[rowCallbackId]);
137
+ return p;
138
+ });
139
+ };
140
+
141
+ globalThis.sqlite3Worker1Promiser.defaultConfig = {
142
+ worker: function () {
143
+ let theJs = 'sqlite3-worker1.js';
144
+ if (this.currentScript) {
145
+ const src = this.currentScript.src.split('/');
146
+ src.pop();
147
+ theJs = src.join('/') + '/' + theJs;
148
+ } else if (globalThis.location) {
149
+ const urlParams = new URL(globalThis.location.href).searchParams;
150
+ if (urlParams.has('sqlite3.dir')) {
151
+ theJs = urlParams.get('sqlite3.dir') + '/' + theJs;
152
+ }
153
+ }
154
+ return new Worker(theJs + globalThis.location.search);
155
+ }.bind({
156
+ currentScript: globalThis?.document?.currentScript,
157
+ }),
158
+ onerror: (...args) => console.error('worker1 promiser error', ...args),
159
+ };
160
+
161
+ sqlite3Worker1Promiser.v2 = function (config) {
162
+ let oldFunc;
163
+ if ('function' == typeof config) {
164
+ oldFunc = config;
165
+ config = {};
166
+ } else if ('function' === typeof config?.onready) {
167
+ oldFunc = config.onready;
168
+ delete config.onready;
169
+ }
170
+ const promiseProxy = Object.create(null);
171
+ config = Object.assign(config || Object.create(null), {
172
+ onready: async function (func) {
173
+ try {
174
+ if (oldFunc) await oldFunc(func);
175
+ promiseProxy.resolve(func);
176
+ } catch (e) {
177
+ promiseProxy.reject(e);
178
+ }
179
+ },
180
+ });
181
+ const p = new Promise(function (resolve, reject) {
182
+ promiseProxy.resolve = resolve;
183
+ promiseProxy.reject = reject;
184
+ });
185
+ try {
186
+ this.original(config);
187
+ } catch (e) {
188
+ promiseProxy.reject(e);
189
+ }
190
+ return p;
191
+ }.bind({
192
+ original: sqlite3Worker1Promiser,
193
+ });
@@ -0,0 +1,187 @@
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
+ let promiserFunc;
55
+ config.worker.onmessage = function (ev) {
56
+ ev = ev.data;
57
+ debug('worker1.onmessage', ev);
58
+ let msgHandler = handlerMap[ev.messageId];
59
+ if (!msgHandler) {
60
+ if (ev && 'sqlite3-api' === ev.type && 'worker1-ready' === ev.result) {
61
+ if (config.onready) config.onready(promiserFunc);
62
+ return;
63
+ }
64
+ msgHandler = handlerMap[ev.type];
65
+ if (msgHandler && msgHandler.onrow) {
66
+ msgHandler.onrow(ev);
67
+ return;
68
+ }
69
+ if (config.onunhandled) config.onunhandled(arguments[0]);
70
+ else err('sqlite3Worker1Promiser() unhandled worker message:', ev);
71
+ return;
72
+ }
73
+ delete handlerMap[ev.messageId];
74
+ switch (ev.type) {
75
+ case 'error':
76
+ msgHandler.reject(ev);
77
+ return;
78
+ case 'open':
79
+ if (!dbId) dbId = ev.dbId;
80
+ break;
81
+ case 'close':
82
+ if (ev.dbId === dbId) dbId = undefined;
83
+ break;
84
+ default:
85
+ break;
86
+ }
87
+ try {
88
+ msgHandler.resolve(ev);
89
+ } catch (e) {
90
+ msgHandler.reject(e);
91
+ }
92
+ };
93
+ return (promiserFunc = function () {
94
+ let msg;
95
+ if (1 === arguments.length) {
96
+ msg = arguments[0];
97
+ } else if (2 === arguments.length) {
98
+ msg = Object.create(null);
99
+ msg.type = arguments[0];
100
+ msg.args = arguments[1];
101
+ msg.dbId = msg.args.dbId;
102
+ } else {
103
+ toss('Invalid arguments for sqlite3Worker1Promiser()-created factory.');
104
+ }
105
+ if (!msg.dbId && msg.type !== 'open') msg.dbId = dbId;
106
+ msg.messageId = genMsgId(msg);
107
+ msg.departureTime = performance.now();
108
+ const proxy = Object.create(null);
109
+ proxy.message = msg;
110
+ let rowCallbackId;
111
+ if ('exec' === msg.type && msg.args) {
112
+ if ('function' === typeof msg.args.callback) {
113
+ rowCallbackId = msg.messageId + ':row';
114
+ proxy.onrow = msg.args.callback;
115
+ msg.args.callback = rowCallbackId;
116
+ handlerMap[rowCallbackId] = proxy;
117
+ } else if ('string' === typeof msg.args.callback) {
118
+ toss(
119
+ 'exec callback may not be a string when using the Promise interface.',
120
+ );
121
+ }
122
+ }
123
+
124
+ let p = new Promise(function (resolve, reject) {
125
+ proxy.resolve = resolve;
126
+ proxy.reject = reject;
127
+ handlerMap[msg.messageId] = proxy;
128
+ debug(
129
+ 'Posting',
130
+ msg.type,
131
+ 'message to Worker dbId=' + (dbId || 'default') + ':',
132
+ msg,
133
+ );
134
+ config.worker.postMessage(msg);
135
+ });
136
+ if (rowCallbackId) p = p.finally(() => delete handlerMap[rowCallbackId]);
137
+ return p;
138
+ });
139
+ };
140
+
141
+ globalThis.sqlite3Worker1Promiser.defaultConfig = {
142
+ worker: function () {
143
+ return new Worker(
144
+ new URL('sqlite3-worker1-bundler-friendly.mjs', import.meta.url),
145
+ {
146
+ type: 'module',
147
+ },
148
+ );
149
+ },
150
+ onerror: (...args) => console.error('worker1 promiser error', ...args),
151
+ };
152
+
153
+ sqlite3Worker1Promiser.v2 = function (config) {
154
+ let oldFunc;
155
+ if ('function' == typeof config) {
156
+ oldFunc = config;
157
+ config = {};
158
+ } else if ('function' === typeof config?.onready) {
159
+ oldFunc = config.onready;
160
+ delete config.onready;
161
+ }
162
+ const promiseProxy = Object.create(null);
163
+ config = Object.assign(config || Object.create(null), {
164
+ onready: async function (func) {
165
+ try {
166
+ if (oldFunc) await oldFunc(func);
167
+ promiseProxy.resolve(func);
168
+ } catch (e) {
169
+ promiseProxy.reject(e);
170
+ }
171
+ },
172
+ });
173
+ const p = new Promise(function (resolve, reject) {
174
+ promiseProxy.resolve = resolve;
175
+ promiseProxy.reject = reject;
176
+ });
177
+ try {
178
+ this.original(config);
179
+ } catch (e) {
180
+ promiseProxy.reject(e);
181
+ }
182
+ return p;
183
+ }.bind({
184
+ original: sqlite3Worker1Promiser,
185
+ });
186
+
187
+ export default sqlite3Worker1Promiser.v2;
@@ -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
+
44
+ importScripts(theJs);
45
+ }
46
+ sqlite3InitModule().then((sqlite3) => sqlite3.initWorker1API());