stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | 2022-09-16 |
| 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 | |
stephan | e6f8a09 | 2022-09-17 21:13:26 +0000 | [diff] [blame] | 13 | An INCOMPLETE and UNDER CONSTRUCTION experiment for OPFS: a Worker |
| 14 | which manages asynchronous OPFS handles on behalf of a synchronous |
| 15 | API which controls it via a combination of Worker messages, |
| 16 | SharedArrayBuffer, and Atomics. |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 17 | |
| 18 | Highly indebted to: |
| 19 | |
| 20 | https://github.com/rhashimoto/wa-sqlite/blob/master/src/examples/OriginPrivateFileSystemVFS.js |
| 21 | |
| 22 | for demonstrating how to use the OPFS APIs. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 23 | |
| 24 | This file is to be loaded as a Worker. It does not have any direct |
| 25 | access to the sqlite3 JS/WASM bits, so any bits which it needs (most |
| 26 | notably SQLITE_xxx integer codes) have to be imported into it via an |
| 27 | initialization process. |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 28 | */ |
| 29 | 'use strict'; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 30 | const toss = function(...args){throw new Error(args.join(' '))}; |
| 31 | if(self.window === self){ |
| 32 | toss("This code cannot run from the main thread.", |
| 33 | "Load it as a Worker from a separate Worker."); |
| 34 | }else if(!navigator.storage.getDirectory){ |
| 35 | toss("This API requires navigator.storage.getDirectory."); |
| 36 | } |
| 37 | /** |
| 38 | Will hold state copied to this object from the syncronous side of |
| 39 | this API. |
| 40 | */ |
| 41 | const state = Object.create(null); |
| 42 | /** |
| 43 | verbose: |
| 44 | |
| 45 | 0 = no logging output |
| 46 | 1 = only errors |
| 47 | 2 = warnings and errors |
| 48 | 3 = debug, warnings, and errors |
| 49 | */ |
| 50 | state.verbose = 2; |
| 51 | |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 52 | const loggers = { |
| 53 | 0:console.error.bind(console), |
| 54 | 1:console.warn.bind(console), |
| 55 | 2:console.log.bind(console) |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 56 | }; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 57 | const logImpl = (level,...args)=>{ |
| 58 | if(state.verbose>level) loggers[level]("OPFS asyncer:",...args); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 59 | }; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 60 | const log = (...args)=>logImpl(2, ...args); |
| 61 | const warn = (...args)=>logImpl(1, ...args); |
| 62 | const error = (...args)=>logImpl(0, ...args); |
stephan | f815011 | 2022-09-19 17:09:09 +0000 | [diff] [blame] | 63 | const metrics = Object.create(null); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 64 | metrics.reset = ()=>{ |
| 65 | let k; |
| 66 | const r = (m)=>(m.count = m.time = 0); |
| 67 | for(k in state.opIds){ |
| 68 | r(metrics[k] = Object.create(null)); |
| 69 | } |
| 70 | }; |
| 71 | metrics.dump = ()=>{ |
| 72 | let k, n = 0, t = 0, w = 0; |
| 73 | for(k in state.opIds){ |
| 74 | const m = metrics[k]; |
| 75 | n += m.count; |
| 76 | t += m.time; |
| 77 | m.avgTime = (m.count && m.time) ? (m.time / m.count) : 0; |
| 78 | } |
| 79 | console.log(self.location.href, |
| 80 | "metrics for",self.location.href,":",metrics, |
| 81 | "\nTotal of",n,"op(s) for",t,"ms"); |
| 82 | }; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 83 | |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 84 | warn("This file is very much experimental and under construction.", |
| 85 | self.location.pathname); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | Map of sqlite3_file pointers (integers) to metadata related to a |
| 89 | given OPFS file handles. The pointers are, in this side of the |
| 90 | interface, opaque file handle IDs provided by the synchronous |
| 91 | part of this constellation. Each value is an object with a structure |
| 92 | demonstrated in the xOpen() impl. |
| 93 | */ |
| 94 | const __openFiles = Object.create(null); |
| 95 | |
| 96 | /** |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 97 | Expects an OPFS file path. It gets resolved, such that ".." |
| 98 | components are properly expanded, and returned. If the 2nd |
| 99 | are is true, it's returned as an array of path elements, |
| 100 | else it's returned as an absolute path string. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 101 | */ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 102 | const getResolvedPath = function(filename,splitIt){ |
| 103 | const p = new URL( |
| 104 | filename, 'file://irrelevant' |
| 105 | ).pathname; |
| 106 | return splitIt ? p.split('/').filter((v)=>!!v) : p; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 107 | }; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | Takes the absolute path to a filesystem element. Returns an array |
| 111 | of [handleOfContainingDir, filename]. If the 2nd argument is |
| 112 | truthy then each directory element leading to the file is created |
| 113 | along the way. Throws if any creation or resolution fails. |
| 114 | */ |
| 115 | const getDirForPath = async function f(absFilename, createDirs = false){ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 116 | const path = getResolvedPath(absFilename, true); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 117 | const filename = path.pop(); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 118 | let dh = state.rootDir; |
| 119 | for(const dirName of path){ |
| 120 | if(dirName){ |
| 121 | dh = await dh.getDirectoryHandle(dirName, {create: !!createDirs}); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 122 | } |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 123 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 124 | return [dh, filename]; |
| 125 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 126 | |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 127 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 128 | /** |
| 129 | Stores the given value at the array index reserved for the given op |
| 130 | and then Atomics.notify()'s it. |
| 131 | */ |
| 132 | const storeAndNotify = (opName, value)=>{ |
| 133 | log(opName+"() is notify()ing w/ value:",value); |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame^] | 134 | Atomics.store(state.sabOPView, state.opIds[opName], value); |
| 135 | Atomics.notify(state.sabOPView, state.opIds[opName]); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 136 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 137 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 138 | /** |
| 139 | Throws if fh is a file-holding object which is flagged as read-only. |
| 140 | */ |
| 141 | const affirmNotRO = function(opName,fh){ |
| 142 | if(fh.readOnly) toss(opName+"(): File is read-only: "+fh.filenameAbs); |
| 143 | }; |
| 144 | |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 145 | |
| 146 | const opTimer = Object.create(null); |
| 147 | opTimer.op = undefined; |
| 148 | opTimer.start = undefined; |
| 149 | const mTimeStart = (op)=>{ |
| 150 | opTimer.start = performance.now(); |
| 151 | opTimer.op = op; |
| 152 | //metrics[op] || toss("Maintenance required: missing metrics for",op); |
| 153 | ++metrics[op].count; |
| 154 | }; |
| 155 | const mTimeEnd = ()=>( |
| 156 | metrics[opTimer.op].time += performance.now() - opTimer.start |
| 157 | ); |
| 158 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 159 | /** |
| 160 | Asynchronous wrappers for sqlite3_vfs and sqlite3_io_methods |
| 161 | methods. Maintenance reminder: members are in alphabetical order |
| 162 | to simplify finding them. |
| 163 | */ |
| 164 | const vfsAsyncImpls = { |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 165 | mkdir: async function(dirname){ |
| 166 | let rc = 0; |
| 167 | try { |
| 168 | await getDirForPath(dirname+"/filepart", true); |
| 169 | }catch(e){ |
| 170 | //error("mkdir failed",filename, e.message); |
| 171 | rc = state.sq3Codes.SQLITE_IOERR; |
| 172 | } |
| 173 | storeAndNotify('mkdir', rc); |
| 174 | }, |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 175 | xAccess: async function(filename){ |
| 176 | log("xAccess(",arguments[0],")"); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 177 | mTimeStart('xAccess'); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 178 | /* OPFS cannot support the full range of xAccess() queries sqlite3 |
| 179 | calls for. We can essentially just tell if the file is |
| 180 | accessible, but if it is it's automatically writable (unless |
| 181 | it's locked, which we cannot(?) know without trying to open |
| 182 | it). OPFS does not have the notion of read-only. |
| 183 | |
| 184 | The return semantics of this function differ from sqlite3's |
| 185 | xAccess semantics because we are limited in what we can |
| 186 | communicate back to our synchronous communication partner: 0 = |
| 187 | accessible, non-0 means not accessible. |
| 188 | */ |
| 189 | let rc = 0; |
| 190 | try{ |
| 191 | const [dh, fn] = await getDirForPath(filename); |
| 192 | await dh.getFileHandle(fn); |
| 193 | }catch(e){ |
| 194 | rc = state.sq3Codes.SQLITE_IOERR; |
| 195 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 196 | storeAndNotify('xAccess', rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 197 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 198 | }, |
| 199 | xClose: async function(fid){ |
| 200 | const opName = 'xClose'; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 201 | mTimeStart(opName); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 202 | log(opName+"(",arguments[0],")"); |
| 203 | const fh = __openFiles[fid]; |
| 204 | if(fh){ |
| 205 | delete __openFiles[fid]; |
| 206 | if(fh.accessHandle) await fh.accessHandle.close(); |
| 207 | if(fh.deleteOnClose){ |
| 208 | try{ await fh.dirHandle.removeEntry(fh.filenamePart) } |
| 209 | catch(e){ warn("Ignoring dirHandle.removeEntry() failure of",fh,e) } |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 210 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 211 | storeAndNotify(opName, 0); |
| 212 | }else{ |
| 213 | storeAndNotify(opName, state.sq3Codes.SQLITE_NOFOUND); |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 214 | } |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 215 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 216 | }, |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame^] | 217 | xDelete: async function(...args){ |
| 218 | mTimeStart('xDelete'); |
| 219 | const rc = await vfsAsyncImpls.xDeleteNoWait(...args); |
| 220 | storeAndNotify('xDelete', rc); |
| 221 | mTimeEnd(); |
| 222 | }, |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 223 | xDeleteNoWait: async function({filename, syncDir, recursive = false}){ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 224 | /* The syncDir flag is, for purposes of the VFS API's semantics, |
| 225 | ignored here. However, if it has the value 0x1234 then: after |
| 226 | deleting the given file, recursively try to delete any empty |
| 227 | directories left behind in its wake (ignoring any errors and |
| 228 | stopping at the first failure). |
| 229 | |
| 230 | That said: we don't know for sure that removeEntry() fails if |
| 231 | the dir is not empty because the API is not documented. It has, |
| 232 | however, a "recursive" flag which defaults to false, so |
| 233 | presumably it will fail if the dir is not empty and that flag |
| 234 | is false. |
| 235 | */ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 236 | log("xDelete(",arguments[0],")"); |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 237 | let rc = 0; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 238 | try { |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 239 | while(filename){ |
| 240 | const [hDir, filenamePart] = await getDirForPath(filename, false); |
| 241 | //log("Removing:",hDir, filenamePart); |
| 242 | if(!filenamePart) break; |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 243 | await hDir.removeEntry(filenamePart, {recursive}); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 244 | if(0x1234 !== syncDir) break; |
| 245 | filename = getResolvedPath(filename, true); |
| 246 | filename.pop(); |
| 247 | filename = filename.join('/'); |
| 248 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 249 | }catch(e){ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 250 | /* Ignoring: _presumably_ the file can't be found or a dir is |
| 251 | not empty. */ |
| 252 | //error("Delete failed",filename, e.message); |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 253 | rc = state.sq3Codes.SQLITE_IOERR_DELETE; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 254 | } |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 255 | return rc; |
| 256 | }, |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 257 | xFileSize: async function(fid){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 258 | mTimeStart('xFileSize'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 259 | log("xFileSize(",arguments,")"); |
| 260 | const fh = __openFiles[fid]; |
| 261 | let sz; |
| 262 | try{ |
| 263 | sz = await fh.accessHandle.getSize(); |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame^] | 264 | if(!fh.sabViewFileSize){ |
| 265 | fh.sabViewFileSize = new DataView(state.sabIO,state.fbInt64Offset,8); |
| 266 | } |
stephan | f815011 | 2022-09-19 17:09:09 +0000 | [diff] [blame] | 267 | fh.sabViewFileSize.setBigInt64(0, BigInt(sz), true); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 268 | sz = 0; |
| 269 | }catch(e){ |
| 270 | error("xFileSize():",e, fh); |
| 271 | sz = state.sq3Codes.SQLITE_IOERR; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 272 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 273 | storeAndNotify('xFileSize', sz); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 274 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 275 | }, |
| 276 | xOpen: async function({ |
| 277 | fid/*sqlite3_file pointer*/, |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 278 | filename, |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame^] | 279 | flags |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 280 | }){ |
| 281 | const opName = 'xOpen'; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 282 | mTimeStart(opName); |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame^] | 283 | log(opName+"(",arguments[0],")"); |
| 284 | const deleteOnClose = (state.sq3Codes.SQLITE_OPEN_DELETEONCLOSE & flags); |
| 285 | const create = (state.sq3Codes.SQLITE_OPEN_CREATE & flags); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 286 | try{ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 287 | let hDir, filenamePart; |
| 288 | try { |
| 289 | [hDir, filenamePart] = await getDirForPath(filename, !!create); |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 290 | }catch(e){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 291 | storeAndNotify(opName, state.sql3Codes.SQLITE_NOTFOUND); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 292 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 293 | return; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 294 | } |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame^] | 295 | const hFile = await hDir.getFileHandle(filenamePart, {create}); |
| 296 | const fobj = Object.create(null); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 297 | /** |
| 298 | wa-sqlite, at this point, grabs a SyncAccessHandle and |
| 299 | assigns it to the accessHandle prop of the file state |
| 300 | object, but only for certain cases and it's unclear why it |
| 301 | places that limitation on it. |
| 302 | */ |
| 303 | fobj.accessHandle = await hFile.createSyncAccessHandle(); |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame^] | 304 | __openFiles[fid] = fobj; |
| 305 | fobj.filenameAbs = filename; |
| 306 | fobj.filenamePart = filenamePart; |
| 307 | fobj.dirHandle = hDir; |
| 308 | fobj.fileHandle = hFile; |
| 309 | fobj.sabView = state.sabFileBufView; |
| 310 | fobj.readOnly = create ? false : (state.sq3Codes.SQLITE_OPEN_READONLY & flags); |
| 311 | fobj.deleteOnClose = deleteOnClose; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 312 | storeAndNotify(opName, 0); |
| 313 | }catch(e){ |
| 314 | error(opName,e); |
| 315 | storeAndNotify(opName, state.sq3Codes.SQLITE_IOERR); |
| 316 | } |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 317 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 318 | }, |
| 319 | xRead: async function({fid,n,offset}){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 320 | mTimeStart('xRead'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 321 | log("xRead(",arguments[0],")"); |
| 322 | let rc = 0; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 323 | try{ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 324 | const fh = __openFiles[fid]; |
| 325 | const nRead = fh.accessHandle.read( |
| 326 | fh.sabView.subarray(0, n), |
| 327 | {at: Number(offset)} |
| 328 | ); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 329 | if(nRead < n){/* Zero-fill remaining bytes */ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 330 | fh.sabView.fill(0, nRead, n); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 331 | rc = state.sq3Codes.SQLITE_IOERR_SHORT_READ; |
| 332 | } |
| 333 | }catch(e){ |
| 334 | error("xRead() failed",e,fh); |
| 335 | rc = state.sq3Codes.SQLITE_IOERR_READ; |
| 336 | } |
| 337 | storeAndNotify('xRead',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 338 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 339 | }, |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 340 | xSync: async function({fid,flags/*ignored*/}){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 341 | mTimeStart('xSync'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 342 | log("xSync(",arguments[0],")"); |
| 343 | const fh = __openFiles[fid]; |
| 344 | if(!fh.readOnly && fh.accessHandle) await fh.accessHandle.flush(); |
| 345 | storeAndNotify('xSync',0); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 346 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 347 | }, |
| 348 | xTruncate: async function({fid,size}){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 349 | mTimeStart('xTruncate'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 350 | log("xTruncate(",arguments[0],")"); |
| 351 | let rc = 0; |
| 352 | const fh = __openFiles[fid]; |
| 353 | try{ |
| 354 | affirmNotRO('xTruncate', fh); |
stephan | 61418d5 | 2022-09-19 14:56:13 +0000 | [diff] [blame] | 355 | await fh.accessHandle.truncate(Number(size)); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 356 | }catch(e){ |
| 357 | error("xTruncate():",e,fh); |
| 358 | rc = state.sq3Codes.SQLITE_IOERR_TRUNCATE; |
| 359 | } |
| 360 | storeAndNotify('xTruncate',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 361 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 362 | }, |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 363 | xWrite: async function({fid,n,offset}){ |
| 364 | mTimeStart('xWrite'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 365 | log("xWrite(",arguments[0],")"); |
| 366 | let rc; |
| 367 | try{ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 368 | const fh = __openFiles[fid]; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 369 | affirmNotRO('xWrite', fh); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 370 | rc = ( |
| 371 | n === fh.accessHandle.write(fh.sabView.subarray(0, n), |
| 372 | {at: Number(offset)}) |
| 373 | ) ? 0 : state.sq3Codes.SQLITE_IOERR_WRITE; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 374 | }catch(e){ |
| 375 | error("xWrite():",e,fh); |
| 376 | rc = state.sq3Codes.SQLITE_IOERR_WRITE; |
| 377 | } |
| 378 | storeAndNotify('xWrite',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 379 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 380 | } |
| 381 | }; |
| 382 | |
| 383 | navigator.storage.getDirectory().then(function(d){ |
| 384 | const wMsg = (type)=>postMessage({type}); |
| 385 | state.rootDir = d; |
| 386 | log("state.rootDir =",state.rootDir); |
| 387 | self.onmessage = async function({data}){ |
| 388 | log("self.onmessage()",data); |
| 389 | switch(data.type){ |
| 390 | case 'init':{ |
| 391 | /* Receive shared state from synchronous partner */ |
| 392 | const opt = data.payload; |
| 393 | state.verbose = opt.verbose ?? 2; |
| 394 | state.fileBufferSize = opt.fileBufferSize; |
| 395 | state.fbInt64Offset = opt.fbInt64Offset; |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame^] | 396 | state.sabOP = opt.sabOP; |
| 397 | state.sabOPView = new Int32Array(state.sabOP); |
| 398 | state.sabIO = opt.sabIO; |
| 399 | state.sabFileBufView = new Uint8Array(state.sabIO, 0, state.fileBufferSize); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 400 | state.opIds = opt.opIds; |
| 401 | state.sq3Codes = opt.sq3Codes; |
| 402 | Object.keys(vfsAsyncImpls).forEach((k)=>{ |
| 403 | if(!Number.isFinite(state.opIds[k])){ |
| 404 | toss("Maintenance required: missing state.opIds[",k,"]"); |
| 405 | } |
| 406 | }); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 407 | metrics.reset(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 408 | log("init state",state); |
| 409 | wMsg('inited'); |
| 410 | break; |
| 411 | } |
| 412 | default:{ |
| 413 | let err; |
| 414 | const m = vfsAsyncImpls[data.type] || toss("Unknown message type:",data.type); |
| 415 | try { |
| 416 | await m(data.payload).catch((e)=>err=e); |
| 417 | }catch(e){ |
| 418 | err = e; |
| 419 | } |
| 420 | if(err){ |
| 421 | error("Error handling",data.type+"():",e); |
| 422 | storeAndNotify(data.type, state.sq3Codes.SQLITE_ERROR); |
| 423 | } |
| 424 | break; |
| 425 | } |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 426 | } |
| 427 | }; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 428 | wMsg('loaded'); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 429 | }).catch((e)=>error(e)); |