1cd0df83cSstephan/*
2cd0df83cSstephan  2022-05-23
3cd0df83cSstephan
4cd0df83cSstephan  The author disclaims copyright to this source code.  In place of a
5cd0df83cSstephan  legal notice, here is a blessing:
6cd0df83cSstephan
7cd0df83cSstephan  *   May you do good and not evil.
8cd0df83cSstephan  *   May you find forgiveness for yourself and forgive others.
9cd0df83cSstephan  *   May you share freely, never taking more than you give.
10cd0df83cSstephan
11cd0df83cSstephan  ***********************************************************************
12cd0df83cSstephan
13cd0df83cSstephan  This is a JS Worker file for the main sqlite3 api. It loads
14cd0df83cSstephan  sqlite3.js, initializes the module, and postMessage()'s a message
15cd0df83cSstephan  after the module is initialized:
16cd0df83cSstephan
17cd0df83cSstephan  {type: 'sqlite3-api', result: 'worker1-ready'}
18cd0df83cSstephan
19cd0df83cSstephan  This seemingly superfluous level of indirection is necessary when
20cd0df83cSstephan  loading sqlite3.js via a Worker. Instantiating a worker with new
21cd0df83cSstephan  Worker("sqlite.js") will not (cannot) call sqlite3InitModule() to
22cd0df83cSstephan  initialize the module due to a timing/order-of-operations conflict
23cd0df83cSstephan  (and that symbol is not exported in a way that a Worker loading it
24cd0df83cSstephan  that way can see it).  Thus JS code wanting to load the sqlite3
25cd0df83cSstephan  Worker-specific API needs to pass _this_ file (or equivalent) to the
26cd0df83cSstephan  Worker constructor and then listen for an event in the form shown
27cd0df83cSstephan  above in order to know when the module has completed initialization.
28cd0df83cSstephan
29fd31ae3bSstephan  This file accepts a URL arguments to adjust how it loads sqlite3.js:
30cd0df83cSstephan
31cd0df83cSstephan  - `sqlite3.dir`, if set, treats the given directory name as the
32cd0df83cSstephan    directory from which `sqlite3.js` will be loaded.
33cd0df83cSstephan*/
34cd0df83cSstephan"use strict";
35cd0df83cSstephan(()=>{
36cd0df83cSstephan  const urlParams = new URL(self.location.href).searchParams;
37cd0df83cSstephan  let theJs = 'sqlite3.js';
38fd31ae3bSstephan  if(urlParams.has('sqlite3.dir')){
39cd0df83cSstephan    theJs = urlParams.get('sqlite3.dir') + '/' + theJs;
40cd0df83cSstephan  }
41d89a66ecSstephan  //console.warn("worker1 theJs =",theJs);
42cd0df83cSstephan  importScripts(theJs);
43cd0df83cSstephan  sqlite3InitModule().then((sqlite3)=>{
44*8948fbeeSstephan    if(sqlite3.capi.sqlite3_wasmfs_opfs_dir){
45cd0df83cSstephan      sqlite3.capi.sqlite3_wasmfs_opfs_dir();
46*8948fbeeSstephan    }
47cd0df83cSstephan    sqlite3.initWorker1API();
48cd0df83cSstephan  });
49cd0df83cSstephan})();
50