aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/sqlite3-worker1.js
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2022-10-19 04:44:58 +0000
committerstephan <stephan@noemail.net>2022-10-19 04:44:58 +0000
commitcd0df83c156d12c89bcbe41420af51d83444855d (patch)
treef6e70858e5441d36916c58d053a44643a138bc86 /ext/wasm/api/sqlite3-worker1.js
parent71de8e02416aa9a4ad90ac6958ff3fa025a33d2d (diff)
downloadsqlite-cd0df83c156d12c89bcbe41420af51d83444855d.tar.gz
sqlite-cd0df83c156d12c89bcbe41420af51d83444855d.zip
Apply considerable acrobatics to get the JS/WASM deliverables building to and loadable from a directory other than the one which contains the app-level code. Requires an only-slightly-leaky abstraction of passing a URL argument when loading sqlite3.js but provides much greater flexibility in where the JS/WASM files are located.
FossilOrigin-Name: 6d468dab9eb84d4548f68014959f02fe4f66455472ff24fe729382bb2972e3d1
Diffstat (limited to 'ext/wasm/api/sqlite3-worker1.js')
-rw-r--r--ext/wasm/api/sqlite3-worker1.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/ext/wasm/api/sqlite3-worker1.js b/ext/wasm/api/sqlite3-worker1.js
new file mode 100644
index 000000000..bc860300b
--- /dev/null
+++ b/ext/wasm/api/sqlite3-worker1.js
@@ -0,0 +1,53 @@
+/*
+ 2022-05-23
+
+ 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 a JS Worker file for the main sqlite3 api. It loads
+ sqlite3.js, initializes the module, and postMessage()'s a message
+ after the module is initialized:
+
+ {type: 'sqlite3-api', result: 'worker1-ready'}
+
+ This seemingly superfluous level of indirection is necessary when
+ loading sqlite3.js via a Worker. Instantiating a worker with new
+ Worker("sqlite.js") will not (cannot) call sqlite3InitModule() to
+ initialize the module due to a timing/order-of-operations conflict
+ (and that symbol is not exported in a way that a Worker loading it
+ that way can see it). Thus JS code wanting to load the sqlite3
+ Worker-specific API needs to pass _this_ file (or equivalent) to the
+ Worker constructor and then listen for an event in the form shown
+ above in order to know when the module has completed initialization.
+
+ This file accepts a couple of URL arguments to adjust how it loads
+ sqlite3.js:
+
+ - `sqlite3.js`, if set, is used as the URI to `sqlite3.js` and it
+ may contain path elements, e.g. `sqlite3.js=foo/bar/my-sqlite3.js`.
+ - `sqlite3.dir`, if set, treats the given directory name as the
+ directory from which `sqlite3.js` will be loaded.
+
+ By default is loads 'sqlite3.js'.
+*/
+"use strict";
+(()=>{
+ const urlParams = new URL(self.location.href).searchParams;
+ let theJs = 'sqlite3.js';
+ if(urlParams.has('sqlite3.js')){
+ theJs = urlParams.get('sqlite3.js');
+ }else if(urlParams.has('sqlite3.dir')){
+ theJs = urlParams.get('sqlite3.dir') + '/' + theJs;
+ }
+ importScripts(theJs);
+ sqlite3InitModule().then((sqlite3)=>{
+ sqlite3.capi.sqlite3_wasmfs_opfs_dir();
+ sqlite3.initWorker1API();
+ });
+})();