@aztec/sqlite3mc-wasm 5.0.0-rc.1 → 5.0.0-rc.2

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,176 @@
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
+ In non-ESM builds this file necessarily exposes one global symbol,
22
+ but clients may freely `delete` that symbol after calling it.
23
+ */
24
+
25
+ globalThis.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){
26
+
27
+ if(1===arguments.length && 'function'===typeof arguments[0]){
28
+ const f = config;
29
+ config = Object.assign(Object.create(null), callee.defaultConfig);
30
+ config.onready = f;
31
+ }else{
32
+ config = Object.assign(Object.create(null), callee.defaultConfig, config);
33
+ }
34
+ const handlerMap = Object.create(null);
35
+ const noop = function(){};
36
+ const err = config.onerror
37
+ || noop ;
38
+ const debug = config.debug || noop;
39
+ const idTypeMap = config.generateMessageId ? undefined : Object.create(null);
40
+ const genMsgId = config.generateMessageId || function(msg){
41
+ return msg.type+'#'+(idTypeMap[msg.type] = (idTypeMap[msg.type]||0) + 1);
42
+ };
43
+ const toss = (...args)=>{throw new Error(args.join(' '))};
44
+ if(!config.worker) config.worker = callee.defaultConfig.worker;
45
+ if('function'===typeof config.worker) config.worker = config.worker();
46
+ let dbId;
47
+ let promiserFunc;
48
+ config.worker.onmessage = function(ev){
49
+ ev = ev.data;
50
+ debug('worker1.onmessage',ev);
51
+ let msgHandler = handlerMap[ev.messageId];
52
+ if(!msgHandler){
53
+ if(ev && 'sqlite3-api'===ev.type && 'worker1-ready'===ev.result) {
54
+
55
+ if(config.onready) config.onready(promiserFunc);
56
+ return;
57
+ }
58
+ msgHandler = handlerMap[ev.type] ;
59
+ if(msgHandler && msgHandler.onrow){
60
+ msgHandler.onrow(ev);
61
+ return;
62
+ }
63
+ if(config.onunhandled) config.onunhandled(arguments[0]);
64
+ else err("sqlite3Worker1Promiser() unhandled worker message:",ev);
65
+ return;
66
+ }
67
+ delete handlerMap[ev.messageId];
68
+ switch(ev.type){
69
+ case 'error':
70
+ msgHandler.reject(ev);
71
+ return;
72
+ case 'open':
73
+ if(!dbId) dbId = ev.dbId;
74
+ break;
75
+ case 'close':
76
+ if(ev.dbId===dbId) dbId = undefined;
77
+ break;
78
+ default:
79
+ break;
80
+ }
81
+ try {msgHandler.resolve(ev)}
82
+ catch(e){msgHandler.reject(e)}
83
+ };
84
+ return promiserFunc = function(){
85
+ let msg;
86
+ if(1===arguments.length){
87
+ msg = arguments[0];
88
+ }else if(2===arguments.length){
89
+ msg = Object.create(null);
90
+ msg.type = arguments[0];
91
+ msg.args = arguments[1];
92
+ msg.dbId = msg.args.dbId;
93
+ }else{
94
+ toss("Invalid arguments for sqlite3Worker1Promiser()-created factory.");
95
+ }
96
+ if(!msg.dbId && msg.type!=='open') msg.dbId = dbId;
97
+ msg.messageId = genMsgId(msg);
98
+ msg.departureTime = performance.now();
99
+ const proxy = Object.create(null);
100
+ proxy.message = msg;
101
+ let rowCallbackId ;
102
+ if('exec'===msg.type && msg.args){
103
+ if('function'===typeof msg.args.callback){
104
+ rowCallbackId = msg.messageId+':row';
105
+ proxy.onrow = msg.args.callback;
106
+ msg.args.callback = rowCallbackId;
107
+ handlerMap[rowCallbackId] = proxy;
108
+ }else if('string' === typeof msg.args.callback){
109
+ toss("exec callback may not be a string when using the Promise interface.");
110
+
111
+ }
112
+ }
113
+
114
+ let p = new Promise(function(resolve, reject){
115
+ proxy.resolve = resolve;
116
+ proxy.reject = reject;
117
+ handlerMap[msg.messageId] = proxy;
118
+ debug("Posting",msg.type,"message to Worker dbId="+(dbId||'default')+':',msg);
119
+ config.worker.postMessage(msg);
120
+ });
121
+ if(rowCallbackId) p = p.finally(()=>delete handlerMap[rowCallbackId]);
122
+ return p;
123
+ };
124
+ };
125
+
126
+ globalThis.sqlite3Worker1Promiser.defaultConfig = {
127
+ worker: function(){
128
+ return new Worker(new URL("sqlite3-worker1-bundler-friendly.mjs", import.meta.url),{
129
+ type: 'module'
130
+ });
131
+ }
132
+ ,
133
+ onerror: (...args)=>console.error('sqlite3Worker1Promiser():',...args)
134
+ };
135
+
136
+
137
+ globalThis.sqlite3Worker1Promiser.v2 = function callee(config = callee.defaultConfig){
138
+ let oldFunc;
139
+ if( 'function' == typeof config ){
140
+ oldFunc = config;
141
+ config = {};
142
+ }else if('function'===typeof config?.onready){
143
+ oldFunc = config.onready;
144
+ delete config.onready;
145
+ }
146
+ const promiseProxy = Object.create(null);
147
+ config = Object.assign((config || Object.create(null)),{
148
+ onready: async function(func){
149
+ try {
150
+ if( oldFunc ) await oldFunc(func);
151
+ promiseProxy.resolve(func);
152
+ }
153
+ catch(e){promiseProxy.reject(e)}
154
+ }
155
+ });
156
+ const p = new Promise(function(resolve,reject){
157
+ promiseProxy.resolve = resolve;
158
+ promiseProxy.reject = reject;
159
+ });
160
+ try{
161
+ this.original(config);
162
+ }catch(e){
163
+ promiseProxy.reject(e);
164
+ }
165
+ return p;
166
+ }.bind({
167
+
168
+ original: sqlite3Worker1Promiser
169
+ });
170
+
171
+ globalThis.sqlite3Worker1Promiser.v2.defaultConfig =
172
+ globalThis.sqlite3Worker1Promiser.defaultConfig;
173
+
174
+
175
+ export default sqlite3Worker1Promiser.v2;
176
+ delete globalThis.sqlite3Worker1Promiser;
@@ -18,8 +18,8 @@
18
18
  slightly simpler client-side interface than the slightly-lower-level
19
19
  Worker API does.
20
20
 
21
- This script necessarily exposes one global symbol, but clients may
22
- freely `delete` that symbol after calling it.
21
+ In non-ESM builds this file necessarily exposes one global symbol,
22
+ but clients may freely `delete` that symbol after calling it.
23
23
  */
24
24
  'use strict';
25
25
 
@@ -145,11 +145,11 @@ globalThis.sqlite3Worker1Promiser.defaultConfig = {
145
145
  currentScript: globalThis?.document?.currentScript
146
146
  })
147
147
  ,
148
- onerror: (...args)=>console.error('worker1 promiser error',...args)
148
+ onerror: (...args)=>console.error('sqlite3Worker1Promiser():',...args)
149
149
  };
150
150
 
151
151
 
152
- sqlite3Worker1Promiser.v2 = function(config){
152
+ globalThis.sqlite3Worker1Promiser.v2 = function callee(config = callee.defaultConfig){
153
153
  let oldFunc;
154
154
  if( 'function' == typeof config ){
155
155
  oldFunc = config;
@@ -183,3 +183,6 @@ sqlite3Worker1Promiser.v2 = function(config){
183
183
  original: sqlite3Worker1Promiser
184
184
  });
185
185
 
186
+ globalThis.sqlite3Worker1Promiser.v2.defaultConfig =
187
+ globalThis.sqlite3Worker1Promiser.defaultConfig;
188
+
@@ -18,10 +18,9 @@
18
18
  slightly simpler client-side interface than the slightly-lower-level
19
19
  Worker API does.
20
20
 
21
- This script necessarily exposes one global symbol, but clients may
22
- freely `delete` that symbol after calling it.
21
+ In non-ESM builds this file necessarily exposes one global symbol,
22
+ but clients may freely `delete` that symbol after calling it.
23
23
  */
24
- 'use strict';
25
24
 
26
25
  globalThis.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){
27
26
 
@@ -126,16 +125,16 @@ globalThis.sqlite3Worker1Promiser = function callee(config = callee.defaultConfi
126
125
 
127
126
  globalThis.sqlite3Worker1Promiser.defaultConfig = {
128
127
  worker: function(){
129
- return new Worker(new URL("sqlite3-worker1-bundler-friendly.mjs", import.meta.url),{
128
+ return new Worker(new URL("sqlite3-worker1.mjs", import.meta.url),{
130
129
  type: 'module'
131
130
  });
132
131
  }
133
132
  ,
134
- onerror: (...args)=>console.error('worker1 promiser error',...args)
133
+ onerror: (...args)=>console.error('sqlite3Worker1Promiser():',...args)
135
134
  };
136
135
 
137
136
 
138
- sqlite3Worker1Promiser.v2 = function(config){
137
+ globalThis.sqlite3Worker1Promiser.v2 = function callee(config = callee.defaultConfig){
139
138
  let oldFunc;
140
139
  if( 'function' == typeof config ){
141
140
  oldFunc = config;
@@ -169,5 +168,9 @@ sqlite3Worker1Promiser.v2 = function(config){
169
168
  original: sqlite3Worker1Promiser
170
169
  });
171
170
 
171
+ globalThis.sqlite3Worker1Promiser.v2.defaultConfig =
172
+ globalThis.sqlite3Worker1Promiser.defaultConfig;
173
+
172
174
 
173
175
  export default sqlite3Worker1Promiser.v2;
176
+ delete globalThis.sqlite3Worker1Promiser;
@@ -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 sqlite3InitModule from './sqlite3.mjs';
35
+ sqlite3InitModule().then(sqlite3 => sqlite3.initWorker1API());
@@ -1,4 +1,4 @@
1
- // Type declaration for the sqlite3mc bundler-friendly ES module.
1
+ // Type declaration for the sqlite3mc ES module.
2
2
  // sqlite3mc is API-compatible with @sqlite.org/sqlite-wasm, so we reuse its
3
3
  // Sqlite3Static type directly.
4
4
  import type { Sqlite3Static } from '@sqlite.org/sqlite-wasm';