sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / wasm / tests / opfs / sahpool / sahpool-worker.js
| 1 | /* |
| 2 | 2025-01-31 |
| 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 file is part of sahpool-pausing.js's demonstration of the |
| 14 | pause/unpause feature of the opfs-sahpool VFS. |
| 15 | */ |
| 16 | const searchParams = new URL(self.location.href).searchParams; |
| 17 | const workerId = searchParams.get('workerId'); |
| 18 | const wPost = (type,...args)=>postMessage({type, workerId, payload:args}); |
| 19 | const log = (...args)=>wPost('log',...args); |
| 20 | let capi, wasm, S, poolUtil; |
| 21 | |
| 22 | const sahPoolConfig = { |
| 23 | name: 'opfs-sahpool-pausable', |
| 24 | clearOnInit: false, |
| 25 | initialCapacity: 3 |
| 26 | }; |
| 27 | |
| 28 | importScripts(searchParams.get('sqlite3.dir') + '/sqlite3.js'); |
| 29 | |
| 30 | const sqlExec = function(sql){ |
| 31 | const db = new poolUtil.OpfsSAHPoolDb('/my.db'); |
| 32 | try{ |
| 33 | return db.exec(sql); |
| 34 | }finally{ |
| 35 | db.close(); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | const clog = console.log.bind(console); |
| 40 | globalThis.onmessage = function({data}){ |
| 41 | clog(workerId+": onmessage:",data); |
| 42 | switch(data.type){ |
| 43 | case 'vfs-acquire': |
| 44 | if( poolUtil ){ |
| 45 | poolUtil.unpauseVfs().then(()=>wPost('vfs-unpaused')); |
| 46 | }else{ |
| 47 | S.installOpfsSAHPoolVfs(sahPoolConfig).then(pu=>{ |
| 48 | poolUtil = pu; |
| 49 | wPost('vfs-acquired'); |
| 50 | }); |
| 51 | } |
| 52 | break; |
| 53 | case 'db-init': |
| 54 | try{ |
| 55 | sqlExec([ |
| 56 | "DROP TABLE IF EXISTS mytable;", |
| 57 | "CREATE TABLE mytable(a);", |
| 58 | "INSERT INTO mytable(a) VALUES(11),(22),(33)" |
| 59 | ]); |
| 60 | wPost('db-inited'); |
| 61 | }catch(e){ |
| 62 | wPost('error',e.message); |
| 63 | } |
| 64 | break; |
| 65 | case 'db-query': { |
| 66 | const rc = sqlExec({ |
| 67 | sql: 'select * from mytable order by a', |
| 68 | rowMode: 'array', |
| 69 | returnValue: 'resultRows' |
| 70 | }); |
| 71 | wPost('query-result',rc); |
| 72 | break; |
| 73 | } |
| 74 | case 'vfs-remove': |
| 75 | poolUtil.removeVfs().then(()=>wPost('vfs-removed')); |
| 76 | break; |
| 77 | case 'vfs-pause': |
| 78 | poolUtil.pauseVfs(); |
| 79 | wPost('vfs-paused'); |
| 80 | break; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | const hasOpfs = ()=>{ |
| 85 | return globalThis.FileSystemHandle |
| 86 | && globalThis.FileSystemDirectoryHandle |
| 87 | && globalThis.FileSystemFileHandle |
| 88 | && globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle |
| 89 | && navigator?.storage?.getDirectory; |
| 90 | }; |
| 91 | if( !hasOpfs() ){ |
| 92 | wPost('error',"OPFS not detected"); |
| 93 | }else{ |
| 94 | globalThis.sqlite3InitModule().then(async function(sqlite3){ |
| 95 | S = sqlite3; |
| 96 | capi = S.capi; |
| 97 | wasm = S.wasm; |
| 98 | log("sqlite3 version:",capi.sqlite3_libversion(), |
| 99 | capi.sqlite3_sourceid()); |
| 100 | //return sqlite3.installOpfsSAHPoolVfs(sahPoolConfig).then(pu=>poolUtil=pu); |
| 101 | }).then(()=>{ |
| 102 | wPost('initialized'); |
| 103 | }); |
| 104 | } |