1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*
2022-05-20
The author disclaims copyright to this source code. In place of a
legal notice, here is a blessing:
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
***********************************************************************
This is the JS Worker file for the sqlite3 fiddle app. It loads the
sqlite3 wasm module and offers access to the db via the Worker
message-passing interface.
Forewarning: this API is still very much Under Construction and
subject to any number of changes as experience reveals what those
need to be.
Because we can have only a single message handler, as opposed to an
arbitrary number of discrete event listeners like with DOM elements,
we have to define a lower-level message API. Messages abstractly
look like:
{ type: string, data: type-specific value }
Where 'type' is used for dispatching and 'data' is a
'type'-dependent value.
The 'type' values expected by each side of the main/worker
connection vary. The types are described below but subject to
change at any time as this experiment evolves.
Workers-to-Main types
- stdout, stderr: indicate stdout/stderr output from the wasm
layer. The data property is the string of the output, noting
that the emscripten binding emits these one line at a time. Thus,
if a C-side puts() emits multiple lines in a single call, the JS
side will see that as multiple calls. Example:
{type:'stdout', data: 'Hi, world.'}
- module: Status text. This is intended to alert the main thread
about module loading status so that, e.g., the main thread can
update a progress widget and DTRT when the module is finished
loading and available for work. Status messages come in the form
{type:'module', data:{
type:'status',
data: {text:string|null, step:1-based-integer}
}
with an incrementing step value for each subsequent message. When
the module loading is complete, a message with a text value of
null is posted.
- working: data='start'|'end'. Indicates that work is about to be
sent to the module or has just completed. This can be used, e.g.,
to disable UI elements which should not be activated while work
is pending. Example:
{type:'working', data:'start'}
Main-to-Worker types:
- shellExec: data=text to execute as if it had been entered in the
sqlite3 CLI shell app (as opposed to sqlite3_exec()). This event
causes the worker to emit a 'working' event (data='start') before
it starts and a 'working' event (data='end') when it finished. If
called while work is currently being executed it emits stderr
message instead of doing actual work, as the underlying db cannot
handle concurrent tasks. Example:
{type:'shellExec', data: 'select * from sqlite_master'}
- More TBD as the higher-level db layer develops.
*/
/*
Apparent browser(s) bug: console messages emitted may be duplicated
in the console, even though they're provably only run once. See:
https://stackoverflow.com/questions/49659464
Noting that it happens in Firefox as well as Chrome. Harmless but
annoying.
*/
"use strict";
(function(){
/**
Posts a message in the form {type,data}. If passed more than 2
args, the 3rd must be an array of "transferable" values to pass
as the 2nd argument to postMessage(). */
const wMsg =
(type,data,transferables)=>{
postMessage({type, data}, transferables || []);
};
const stdout = (...args)=>wMsg('stdout', args);
const stderr = (...args)=>wMsg('stderr', args);
const toss = (...args)=>{
throw new Error(args.join(' '));
};
const fixmeOPFS = "(FIXME: won't work with OPFS-over-sqlite3_vfs.)";
let sqlite3 /* gets assigned when the wasm module is loaded */;
self.onerror = function(/*message, source, lineno, colno, error*/) {
const err = arguments[4];
if(err && 'ExitStatus'==err.name){
/* This is relevant for the sqlite3 shell binding but not the
lower-level binding. */
fiddleModule.isDead = true;
stderr("FATAL ERROR:", err.message);
stderr("Restarting the app requires reloading the page.");
wMsg('error', err);
}
console.error(err);
fiddleModule.setStatus('Exception thrown, see JavaScript console: '+err);
};
const Sqlite3Shell = {
/** Returns the name of the currently-opened db. */
dbFilename: function f(){
if(!f._) f._ = sqlite3.wasm.xWrap('fiddle_db_filename', "string", ['string']);
return f._(0);
},
dbHandle: function f(){
if(!f._) f._ = sqlite3.wasm.xWrap("fiddle_db_handle", "sqlite3*");
return f._();
},
dbIsOpfs: function f(){
return sqlite3.opfs && sqlite3.capi.sqlite3_js_db_uses_vfs(
this.dbHandle(), "opfs"
);
},
runMain: function f(){
if(f.argv) return 0===f.argv.rc;
const dbName = "/fiddle.sqlite3";
f.argv = [
'sqlite3-fiddle.wasm',
'-bail', '-safe',
dbName
/* Reminder: because of how we run fiddle, we have to ensure
that any argv strings passed to its main() are valid until
the wasm environment shuts down. */
];
const capi = sqlite3.capi, wasm = sqlite3.wasm;
/* We need to call sqlite3_shutdown() in order to avoid numerous
legitimate warnings from the shell about it being initialized
after sqlite3_initialize() has been called. This means,
however, that any initialization done by the JS code may need
to be re-done (e.g. re-registration of dynamically-loaded
VFSes). We need a more generic approach to running such
init-level code. */
capi.sqlite3_shutdown();
f.argv.pArgv = wasm.allocMainArgv(f.argv);
f.argv.rc = wasm.exports.fiddle_main(
f.argv.length, f.argv.pArgv
);
if(f.argv.rc){
stderr("Fatal error initializing sqlite3 shell.");
fiddleModule.isDead = true;
return false;
}
stdout("SQLite version", capi.sqlite3_libversion(),
capi.sqlite3_sourceid().substr(0,19));
stdout('Welcome to the "fiddle" shell.');
if(sqlite3.opfs){
stdout("\nOPFS is available. To open a persistent db, use:\n\n",
" .open file:name?vfs=opfs\n\nbut note that some",
"features (e.g. upload) do not yet work with OPFS.");
sqlite3.opfs.registerVfs();
}
stdout('\nEnter ".help" for usage hints.');
this.exec([ // initialization commands...
'.nullvalue NULL',
'.headers on'
].join('\n'));
return true;
},
/**
Runs the given text through the shell as if it had been typed
in by a user. Fires a working/start event before it starts and
working/end event when it finishes.
*/
exec: function f(sql){
if(!f._){
if(!this.runMain()) return;
f._ = sqlite3.wasm.xWrap('fiddle_exec', null, ['string']);
}
if(fiddleModule.isDead){
stderr("shell module has exit()ed. Cannot run SQL.");
return;
}
wMsg('working','start');
try {
if(f._running){
stderr('Cannot run multiple commands concurrently.');
}else if(sql){
if(Array.isArray(sql)) sql = sql.join('');
f._running = true;
f._(sql);
}
}finally{
delete f._running;
wMsg('working','end');
}
},
resetDb: function f(){
if(!f._) f._ = sqlite3.wasm.xWrap('fiddle_reset_db', null);
stdout("Resetting database.");
f._();
stdout("Reset",this.dbFilename());
},
/* Interrupt can't work: this Worker is tied up working, so won't get the
interrupt event which would be needed to perform the interrupt. */
interrupt: function f(){
if(!f._) f._ = sqlite3.wasm.xWrap('fiddle_interrupt', null);
stdout("Requesting interrupt.");
f._();
}
};
self.onmessage = function f(ev){
ev = ev.data;
if(!f.cache){
f.cache = {
prevFilename: null
};
}
//console.debug("worker: onmessage.data",ev);
switch(ev.type){
case 'shellExec': Sqlite3Shell.exec(ev.data); return;
case 'db-reset': Sqlite3Shell.resetDb(); return;
case 'interrupt': Sqlite3Shell.interrupt(); return;
/** Triggers the export of the current db. Fires an
event in the form:
{type:'db-export',
data:{
filename: name of db,
buffer: contents of the db file (Uint8Array),
error: on error, a message string and no buffer property.
}
}
*/
case 'db-export': {
const fn = Sqlite3Shell.dbFilename();
stdout("Exporting",fn+".");
const fn2 = fn ? fn.split(/[/\\]/).pop() : null;
try{
if(!fn2) toss("DB appears to be closed.");
const buffer = sqlite3.capi.sqlite3_js_db_export(
Sqlite3Shell.dbHandle()
);
wMsg('db-export',{filename: fn2, buffer: buffer.buffer}, [buffer.buffer]);
}catch(e){
console.error("Export failed:",e);
/* Post a failure message so that UI elements disabled
during the export can be re-enabled. */
wMsg('db-export',{
filename: fn,
error: e.message
});
}
return;
}
case 'open': {
/* Expects: {
buffer: ArrayBuffer | Uint8Array,
filename: the filename for the db. Any dir part is
stripped.
}
*/
const opt = ev.data;
let buffer = opt.buffer;
stderr('open():',fixmeOPFS);
if(buffer instanceof ArrayBuffer){
buffer = new Uint8Array(buffer);
}else if(!(buffer instanceof Uint8Array)){
stderr("'open' expects {buffer:Uint8Array} containing an uploaded db.");
return;
}
const fn = (
opt.filename
? opt.filename.split(/[/\\]/).pop().replace('"','_')
: ("db-"+((Math.random() * 10000000) | 0)+
"-"+((Math.random() * 10000000) | 0)+".sqlite3")
);
try {
/* We cannot delete the existing db file until the new one
is installed, which means that we risk overflowing our
quota (if any) by having both the previous and current
db briefly installed in the virtual filesystem. */
const fnAbs = '/'+fn;
const oldName = Sqlite3Shell.dbFilename();
if(oldName && oldName===fnAbs){
/* We cannot create the replacement file while the current file
is opened, nor does the shell have a .close command, so we
must temporarily switch to another db... */
Sqlite3Shell.exec('.open :memory:');
fiddleModule.FS.unlink(fnAbs);
}
fiddleModule.FS.createDataFile("/", fn, buffer, true, true);
Sqlite3Shell.exec('.open "'+fnAbs+'"');
if(oldName && oldName!==fnAbs){
try{fiddleModule.fsUnlink(oldName)}
catch(e){/*ignored*/}
}
stdout("Replaced DB with",fn+".");
}catch(e){
stderr("Error installing db",fn+":",e.message);
}
return;
}
};
console.warn("Unknown fiddle-worker message type:",ev);
};
/**
emscripten module for use with build mode -sMODULARIZE.
*/
const fiddleModule = {
print: stdout,
printErr: stderr,
/**
Intercepts status updates from the emscripting module init
and fires worker events with a type of 'status' and a
payload of:
{
text: string | null, // null at end of load process
step: integer // starts at 1, increments 1 per call
}
We have no way of knowing in advance how many steps will
be processed/posted, so creating a "percentage done" view is
not really practical. One can be approximated by giving it a
current value of message.step and max value of message.step+1,
though.
When work is finished, a message with a text value of null is
submitted.
After a message with text==null is posted, the module may later
post messages about fatal problems, e.g. an exit() being
triggered, so it is recommended that UI elements for posting
status messages not be outright removed from the DOM when
text==null, and that they instead be hidden until/unless
text!=null.
*/
setStatus: function f(text){
if(!f.last) f.last = { step: 0, text: '' };
else if(text === f.last.text) return;
f.last.text = text;
wMsg('module',{
type:'status',
data:{step: ++f.last.step, text: text||null}
});
}
};
importScripts('fiddle-module.js'+self.location.search);
/**
initFiddleModule() is installed via fiddle-module.js due to
building with:
emcc ... -sMODULARIZE=1 -sEXPORT_NAME=initFiddleModule
*/
sqlite3InitModule(fiddleModule).then((_sqlite3)=>{
sqlite3 = _sqlite3;
const dbVfs = sqlite3.wasm.xWrap('fiddle_db_vfs', "*", ['string']);
fiddleModule.fsUnlink = (fn)=>{
return sqlite3.wasm.sqlite3_wasm_vfs_unlink(dbVfs(0), fn);
};
wMsg('fiddle-ready');
})/*then()*/;
})();
|