3 let speedtestJs = 'speedtest1.js';
4 const urlParams = new URL(self.location.href).searchParams;
5 if(urlParams.has('sqlite3.dir')){
6 speedtestJs = urlParams.get('sqlite3.dir') + '/' + speedtestJs;
8 importScripts(speedtestJs);
10 If this build includes WASMFS, this function initializes it and
11 returns the name of the dir on which OPFS is mounted, else it
12 returns an empty string.
14 const wasmfsDir = function f(wasmUtil){
15 if(undefined !== f._) return f._;
17 if( !self.FileSystemHandle
18 || !self.FileSystemDirectoryHandle
19 || !self.FileSystemFileHandle){
23 if(0===wasmUtil.xCallWrapped(
24 'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir
31 // sqlite3_wasm_init_wasmfs() is not available
35 wasmfsDir._ = undefined;
37 const mPost = function(msgType,payload){
38 postMessage({type: msgType, data: payload});
41 const App = Object.create(null);
43 const logMsg = (type,msgArgs)=>{
44 const msg = msgArgs.join(' ');
45 App.logBuffer.push(msg);
48 const log = (...args)=>logMsg('stdout',args);
49 const logErr = (...args)=>logMsg('stderr',args);
50 const realSahName = 'opfs-sahpool-speedtest1';
52 const runSpeedtest = async function(cliFlagsArray){
53 const scope = App.wasm.scopedAllocPush();
54 const dbFile = App.pDir+"/speedtest1.sqlite3";
57 "speedtest1.wasm", ...cliFlagsArray, dbFile
59 App.logBuffer.length = 0;
60 const ndxSahPool = argv.indexOf('opfs-sahpool');
62 argv[ndxSahPool] = realSahName;
63 log("Updated argv for opfs-sahpool: --vfs",realSahName);
65 mPost('run-start', [...argv]);
66 if(App.sqlite3.installOpfsSAHPoolVfs
67 && !App.sqlite3.$SAHPoolUtil
69 log("Installing opfs-sahpool as",realSahName,"...");
70 await App.sqlite3.installOpfsSAHPoolVfs({
76 log("opfs-sahpool successfully installed as",PoolUtil.vfsName);
77 App.sqlite3.$SAHPoolUtil = PoolUtil;
78 //console.log("sqlite3.oo1.OpfsSAHPoolDb =", App.sqlite3.oo1.OpfsSAHPoolDb);
81 App.wasm.xCall('wasm_main', argv.length,
82 App.wasm.scopedAllocMainArgv(argv));
84 mPost('error',e.message);
86 App.wasm.scopedAllocPop(scope);
87 mPost('run-end', App.logBuffer.join('\n'));
88 App.logBuffer.length = 0;
92 self.onmessage = function(msg){
96 runSpeedtest(msg.data || [])
97 .catch(e=>mPost('error',e));
100 logErr("Unhandled worker message type:",msg.type);
105 const EmscriptenModule = {
108 setStatus: (text)=>mPost('load-status',text)
110 log("Initializing speedtest1 module...");
111 self.sqlite3InitModule(EmscriptenModule).then(async (sqlite3)=>{
112 const S = globalThis.S = App.sqlite3 = sqlite3;
113 log("Loaded speedtest1 module. Setting up...");
114 App.pDir = wasmfsDir(S.wasm);
116 //if(App.pDir) log("Persistent storage:",pDir);
117 //else log("Using transient storage.");
119 log("Registered VFSes:", ...S.capi.sqlite3_js_vfs_list());