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 = new URL(self.location.href).searchParams; 37 let theJs = 'sqlite3.js'; 38 if(urlParams.has('sqlite3.dir')){ 39 theJs = urlParams.get('sqlite3.dir') + '/' + theJs; 40 } 41 //console.warn("worker1 theJs =",theJs); 42 importScripts(theJs); 43 sqlite3InitModule().then((sqlite3)=>{ 44 if(sqlite3.capi.sqlite3_wasmfs_opfs_dir){ 45 sqlite3.capi.sqlite3_wasmfs_opfs_dir(); 46 } 47 sqlite3.initWorker1API(); 48 }); 49})(); 50