1'use strict'; 2(function(){ 3 importScripts('common/whwasmutil.js','speedtest1.js'); 4 /** 5 If this environment contains OPFS, this function initializes it and 6 returns the name of the dir on which OPFS is mounted, else it returns 7 an empty string. 8 */ 9 const wasmfsDir = function f(wasmUtil){ 10 if(undefined !== f._) return f._; 11 const pdir = '/opfs'; 12 if( !self.FileSystemHandle 13 || !self.FileSystemDirectoryHandle 14 || !self.FileSystemFileHandle){ 15 return f._ = ""; 16 } 17 try{ 18 if(0===wasmUtil.xCallWrapped( 19 'sqlite3_wasm_init_wasmfs', 'i32', ['string'], pdir 20 )){ 21 return f._ = pdir; 22 }else{ 23 return f._ = ""; 24 } 25 }catch(e){ 26 // sqlite3_wasm_init_wasmfs() is not available 27 return f._ = ""; 28 } 29 }; 30 wasmfsDir._ = undefined; 31 32 const mPost = function(msgType,payload){ 33 postMessage({type: msgType, data: payload}); 34 }; 35 36 const App = Object.create(null); 37 App.logBuffer = []; 38 const logMsg = (type,msgArgs)=>{ 39 const msg = msgArgs.join(' '); 40 App.logBuffer.push(msg); 41 mPost(type,msg); 42 }; 43 const log = (...args)=>logMsg('stdout',args); 44 const logErr = (...args)=>logMsg('stderr',args); 45 46 const runSpeedtest = function(cliFlagsArray){ 47 const scope = App.wasm.scopedAllocPush(); 48 const dbFile = App.pDir+"/speedtest1.sqlite3"; 49 App.unlink(dbFile); 50 try{ 51 const argv = [ 52 "speedtest1.wasm", ...cliFlagsArray, dbFile 53 ]; 54 App.logBuffer.length = 0; 55 mPost('run-start', [...argv]); 56 App.wasm.xCall('wasm_main', argv.length, 57 App.wasm.scopedAllocMainArgv(argv)); 58 }catch(e){ 59 mPost('error',e.message); 60 }finally{ 61 App.wasm.scopedAllocPop(scope); 62 App.unlink(dbFile); 63 mPost('run-end', App.logBuffer.join('\n')); 64 App.logBuffer.length = 0; 65 } 66 }; 67 68 self.onmessage = function(msg){ 69 msg = msg.data; 70 switch(msg.type){ 71 case 'run': runSpeedtest(msg.data || []); break; 72 default: 73 logErr("Unhandled worker message type:",msg.type); 74 break; 75 } 76 }; 77 78 const EmscriptenModule = { 79 print: log, 80 printErr: logErr, 81 setStatus: (text)=>mPost('load-status',text) 82 }; 83 self.sqlite3InitModule(EmscriptenModule).then((sqlite3)=>{ 84 const S = sqlite3; 85 const vfsUnlink = S.capi.wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]); 86 App.unlink = function(fname){ 87 vfsUnlink(fname); 88 if(S.opfs) S.opfs.deleteEntry(fname); 89 }; 90 App.pDir = wasmfsDir(S.wasm); 91 App.wasm = S.capi.wasm; 92 //if(App.pDir) log("Persistent storage:",pDir); 93 //else log("Using transient storage."); 94 mPost('ready',true); 95 log("Registered VFSes:", ...S.capi.sqlite3_web_vfs_list()); 96 }); 97})(); 98