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 | } |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 37 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 38 | /** |
| 39 | Will hold state copied to this object from the syncronous side of |
| 40 | this API. |
| 41 | */ |
| 42 | const state = Object.create(null); |
| 43 | /** |
| 44 | verbose: |
| 45 | |
| 46 | 0 = no logging output |
| 47 | 1 = only errors |
| 48 | 2 = warnings and errors |
| 49 | 3 = debug, warnings, and errors |
| 50 | */ |
| 51 | state.verbose = 2; |
| 52 | |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 53 | const loggers = { |
| 54 | 0:console.error.bind(console), |
| 55 | 1:console.warn.bind(console), |
| 56 | 2:console.log.bind(console) |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 57 | }; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 58 | const logImpl = (level,...args)=>{ |
| 59 | if(state.verbose>level) loggers[level]("OPFS asyncer:",...args); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 60 | }; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 61 | const log = (...args)=>logImpl(2, ...args); |
| 62 | const warn = (...args)=>logImpl(1, ...args); |
| 63 | const error = (...args)=>logImpl(0, ...args); |
stephan | f815011 | 2022-09-19 17:09:09 +0000 | [diff] [blame] | 64 | const metrics = Object.create(null); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 65 | metrics.reset = ()=>{ |
| 66 | let k; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 67 | const r = (m)=>(m.count = m.time = m.wait = 0); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 68 | for(k in state.opIds){ |
| 69 | r(metrics[k] = Object.create(null)); |
| 70 | } |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 71 | let s = metrics.s11n = Object.create(null); |
| 72 | s = s.serialize = Object.create(null); |
| 73 | s.count = s.time = 0; |
| 74 | s = metrics.s11n.deserialize = Object.create(null); |
| 75 | s.count = s.time = 0; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 76 | }; |
| 77 | metrics.dump = ()=>{ |
| 78 | let k, n = 0, t = 0, w = 0; |
| 79 | for(k in state.opIds){ |
| 80 | const m = metrics[k]; |
| 81 | n += m.count; |
| 82 | t += m.time; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 83 | w += m.wait; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 84 | m.avgTime = (m.count && m.time) ? (m.time / m.count) : 0; |
| 85 | } |
| 86 | console.log(self.location.href, |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 87 | "metrics for",self.location.href,":\n", |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 88 | metrics, |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 89 | "\nTotal of",n,"op(s) for",t,"ms", |
| 90 | "approx",w,"ms spent waiting on OPFS APIs."); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 91 | console.log("Serialization metrics:",metrics.s11n); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 92 | }; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 93 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 94 | /** |
| 95 | Map of sqlite3_file pointers (integers) to metadata related to a |
| 96 | given OPFS file handles. The pointers are, in this side of the |
| 97 | interface, opaque file handle IDs provided by the synchronous |
| 98 | part of this constellation. Each value is an object with a structure |
| 99 | demonstrated in the xOpen() impl. |
| 100 | */ |
| 101 | const __openFiles = Object.create(null); |
| 102 | |
| 103 | /** |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 104 | Expects an OPFS file path. It gets resolved, such that ".." |
| 105 | components are properly expanded, and returned. If the 2nd |
| 106 | are is true, it's returned as an array of path elements, |
| 107 | else it's returned as an absolute path string. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 108 | */ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 109 | const getResolvedPath = function(filename,splitIt){ |
| 110 | const p = new URL( |
| 111 | filename, 'file://irrelevant' |
| 112 | ).pathname; |
| 113 | return splitIt ? p.split('/').filter((v)=>!!v) : p; |
stephan | 509f405 | 2022-09-19 09:58:01 +0000 | [diff] [blame] | 114 | }; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | Takes the absolute path to a filesystem element. Returns an array |
| 118 | of [handleOfContainingDir, filename]. If the 2nd argument is |
| 119 | truthy then each directory element leading to the file is created |
| 120 | along the way. Throws if any creation or resolution fails. |
| 121 | */ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 122 | const getDirForFilename = async function f(absFilename, createDirs = false){ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 123 | const path = getResolvedPath(absFilename, true); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 124 | const filename = path.pop(); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 125 | let dh = state.rootDir; |
| 126 | for(const dirName of path){ |
| 127 | if(dirName){ |
| 128 | dh = await dh.getDirectoryHandle(dirName, {create: !!createDirs}); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 129 | } |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 130 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 131 | return [dh, filename]; |
| 132 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 133 | |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 134 | /** |
| 135 | Returns the sync access handle associated with the given file |
| 136 | handle object (which must be a valid handle object), lazily opening |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 137 | it if needed. |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 138 | |
| 139 | In order to help alleviate cross-tab contention for a dabase, |
| 140 | if an exception is thrown while acquiring the handle, this routine |
| 141 | will wait briefly and try again, up to 3 times. If acquisition |
| 142 | still fails at that point it will give up and propagate the |
| 143 | exception. |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 144 | */ |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 145 | const getSyncHandle = async (fh)=>{ |
| 146 | if(!fh.syncHandle){ |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 147 | const t = performance.now(); |
| 148 | log("Acquiring sync handle for",fh.filenameAbs); |
| 149 | const maxTries = 3; |
| 150 | let i = 1, ms = 300; |
| 151 | for(; true; ms *= ++i){ |
| 152 | try { |
| 153 | //if(1===i) toss("Just testing."); |
| 154 | //TODO? A config option which tells it to throw here |
stephan | 3e771c0 | 2022-10-14 13:26:18 +0000 | [diff] [blame^] | 155 | //randomly every now and then, for testing purposes. |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 156 | fh.syncHandle = await fh.fileHandle.createSyncAccessHandle(); |
| 157 | break; |
| 158 | }catch(e){ |
| 159 | if(i === maxTries){ |
| 160 | toss("Error getting sync handle.",maxTries, |
| 161 | "attempts failed. ",fh.filenameAbs, ":", e.message); |
| 162 | throw e; |
| 163 | } |
stephan | 3e771c0 | 2022-10-14 13:26:18 +0000 | [diff] [blame^] | 164 | warn("Error getting sync handle. Waiting",ms, |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 165 | "ms and trying again.",fh.filenameAbs,e); |
| 166 | Atomics.wait(state.sabOPView, state.opIds.xSleep, 0, ms); |
| 167 | } |
| 168 | } |
| 169 | log("Got sync handle for",fh.filenameAbs,'in',performance.now() - t,'ms'); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 170 | } |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 171 | return fh.syncHandle; |
| 172 | }; |
| 173 | |
| 174 | const closeSyncHandle = async (fh)=>{ |
| 175 | if(fh.syncHandle){ |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 176 | log("Closing sync handle for",fh.filenameAbs); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 177 | const h = fh.syncHandle; |
| 178 | delete fh.syncHandle; |
| 179 | return h.close(); |
| 180 | } |
| 181 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 182 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 183 | /** |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 184 | Stores the given value at state.sabOPView[state.opIds.rc] and then |
| 185 | Atomics.notify()'s it. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 186 | */ |
| 187 | const storeAndNotify = (opName, value)=>{ |
stephan | c9e2602 | 2022-09-20 10:11:52 +0000 | [diff] [blame] | 188 | log(opName+"() => notify(",state.opIds.rc,",",value,")"); |
| 189 | Atomics.store(state.sabOPView, state.opIds.rc, value); |
| 190 | Atomics.notify(state.sabOPView, state.opIds.rc); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 191 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 192 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 193 | /** |
| 194 | Throws if fh is a file-holding object which is flagged as read-only. |
| 195 | */ |
| 196 | const affirmNotRO = function(opName,fh){ |
| 197 | if(fh.readOnly) toss(opName+"(): File is read-only: "+fh.filenameAbs); |
| 198 | }; |
| 199 | |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 200 | |
| 201 | const opTimer = Object.create(null); |
| 202 | opTimer.op = undefined; |
| 203 | opTimer.start = undefined; |
| 204 | const mTimeStart = (op)=>{ |
| 205 | opTimer.start = performance.now(); |
| 206 | opTimer.op = op; |
| 207 | //metrics[op] || toss("Maintenance required: missing metrics for",op); |
| 208 | ++metrics[op].count; |
| 209 | }; |
| 210 | const mTimeEnd = ()=>( |
| 211 | metrics[opTimer.op].time += performance.now() - opTimer.start |
| 212 | ); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 213 | const waitTimer = Object.create(null); |
| 214 | waitTimer.op = undefined; |
| 215 | waitTimer.start = undefined; |
| 216 | const wTimeStart = (op)=>{ |
| 217 | waitTimer.start = performance.now(); |
| 218 | waitTimer.op = op; |
| 219 | //metrics[op] || toss("Maintenance required: missing metrics for",op); |
| 220 | }; |
| 221 | const wTimeEnd = ()=>( |
| 222 | metrics[waitTimer.op].wait += performance.now() - waitTimer.start |
| 223 | ); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 224 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 225 | /** |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 226 | Set to true by the 'opfs-async-shutdown' command to quite the wait loop. |
| 227 | This is only intended for debugging purposes: we cannot inspect this |
| 228 | file's state while the tight waitLoop() is running. |
| 229 | */ |
| 230 | let flagAsyncShutdown = false; |
| 231 | |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 232 | |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 233 | /** |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 234 | Asynchronous wrappers for sqlite3_vfs and sqlite3_io_methods |
| 235 | methods. Maintenance reminder: members are in alphabetical order |
| 236 | to simplify finding them. |
| 237 | */ |
| 238 | const vfsAsyncImpls = { |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 239 | 'opfs-async-metrics': async ()=>{ |
| 240 | mTimeStart('opfs-async-metrics'); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 241 | metrics.dump(); |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 242 | storeAndNotify('opfs-async-metrics', 0); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 243 | mTimeEnd(); |
| 244 | }, |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 245 | 'opfs-async-shutdown': async ()=>{ |
| 246 | flagAsyncShutdown = true; |
| 247 | storeAndNotify('opfs-async-shutdown', 0); |
| 248 | }, |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 249 | mkdir: async (dirname)=>{ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 250 | mTimeStart('mkdir'); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 251 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 252 | wTimeStart('mkdir'); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 253 | try { |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 254 | await getDirForFilename(dirname+"/filepart", true); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 255 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 256 | state.s11n.storeException(2,e); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 257 | rc = state.sq3Codes.SQLITE_IOERR; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 258 | }finally{ |
| 259 | wTimeEnd(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 260 | } |
| 261 | storeAndNotify('mkdir', rc); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 262 | mTimeEnd(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 263 | }, |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 264 | xAccess: async (filename)=>{ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 265 | mTimeStart('xAccess'); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 266 | /* OPFS cannot support the full range of xAccess() queries sqlite3 |
| 267 | calls for. We can essentially just tell if the file is |
| 268 | accessible, but if it is it's automatically writable (unless |
| 269 | it's locked, which we cannot(?) know without trying to open |
| 270 | it). OPFS does not have the notion of read-only. |
| 271 | |
| 272 | The return semantics of this function differ from sqlite3's |
| 273 | xAccess semantics because we are limited in what we can |
| 274 | communicate back to our synchronous communication partner: 0 = |
| 275 | accessible, non-0 means not accessible. |
| 276 | */ |
| 277 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 278 | wTimeStart('xAccess'); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 279 | try{ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 280 | const [dh, fn] = await getDirForFilename(filename); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 281 | await dh.getFileHandle(fn); |
| 282 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 283 | state.s11n.storeException(2,e); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 284 | rc = state.sq3Codes.SQLITE_IOERR; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 285 | }finally{ |
| 286 | wTimeEnd(); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 287 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 288 | storeAndNotify('xAccess', rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 289 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 290 | }, |
| 291 | xClose: async function(fid){ |
| 292 | const opName = 'xClose'; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 293 | mTimeStart(opName); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 294 | const fh = __openFiles[fid]; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 295 | let rc = 0; |
| 296 | wTimeStart('xClose'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 297 | if(fh){ |
| 298 | delete __openFiles[fid]; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 299 | await closeSyncHandle(fh); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 300 | if(fh.deleteOnClose){ |
| 301 | try{ await fh.dirHandle.removeEntry(fh.filenamePart) } |
| 302 | catch(e){ warn("Ignoring dirHandle.removeEntry() failure of",fh,e) } |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 303 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 304 | }else{ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 305 | state.s11n.serialize(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 306 | rc = state.sq3Codes.SQLITE_NOTFOUND; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 307 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 308 | wTimeEnd(); |
| 309 | storeAndNotify(opName, rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 310 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 311 | }, |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 312 | xDelete: async function(...args){ |
| 313 | mTimeStart('xDelete'); |
| 314 | const rc = await vfsAsyncImpls.xDeleteNoWait(...args); |
| 315 | storeAndNotify('xDelete', rc); |
| 316 | mTimeEnd(); |
| 317 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 318 | xDeleteNoWait: async function(filename, syncDir = 0, recursive = false){ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 319 | /* The syncDir flag is, for purposes of the VFS API's semantics, |
| 320 | ignored here. However, if it has the value 0x1234 then: after |
| 321 | deleting the given file, recursively try to delete any empty |
| 322 | directories left behind in its wake (ignoring any errors and |
| 323 | stopping at the first failure). |
| 324 | |
| 325 | That said: we don't know for sure that removeEntry() fails if |
| 326 | the dir is not empty because the API is not documented. It has, |
| 327 | however, a "recursive" flag which defaults to false, so |
| 328 | presumably it will fail if the dir is not empty and that flag |
| 329 | is false. |
| 330 | */ |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 331 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 332 | wTimeStart('xDelete'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 333 | try { |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 334 | while(filename){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 335 | const [hDir, filenamePart] = await getDirForFilename(filename, false); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 336 | if(!filenamePart) break; |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 337 | await hDir.removeEntry(filenamePart, {recursive}); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 338 | if(0x1234 !== syncDir) break; |
| 339 | filename = getResolvedPath(filename, true); |
| 340 | filename.pop(); |
| 341 | filename = filename.join('/'); |
| 342 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 343 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 344 | state.s11n.storeException(2,e); |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 345 | rc = state.sq3Codes.SQLITE_IOERR_DELETE; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 346 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 347 | wTimeEnd(); |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 348 | return rc; |
| 349 | }, |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 350 | xFileSize: async function(fid){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 351 | mTimeStart('xFileSize'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 352 | const fh = __openFiles[fid]; |
| 353 | let sz; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 354 | wTimeStart('xFileSize'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 355 | try{ |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 356 | sz = await (await getSyncHandle(fh)).getSize(); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 357 | state.s11n.serialize(Number(sz)); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 358 | sz = 0; |
| 359 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 360 | state.s11n.storeException(2,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 361 | sz = state.sq3Codes.SQLITE_IOERR; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 362 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 363 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 364 | storeAndNotify('xFileSize', sz); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 365 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 366 | }, |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 367 | xLock: async function(fid,lockType){ |
| 368 | mTimeStart('xLock'); |
| 369 | const fh = __openFiles[fid]; |
| 370 | let rc = 0; |
| 371 | if( !fh.syncHandle ){ |
| 372 | try { await getSyncHandle(fh) } |
| 373 | catch(e){ |
| 374 | state.s11n.storeException(1,e); |
| 375 | rc = state.sq3Codes.SQLITE_IOERR; |
| 376 | } |
| 377 | } |
| 378 | storeAndNotify('xLock',rc); |
| 379 | mTimeEnd(); |
| 380 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 381 | xOpen: async function(fid/*sqlite3_file pointer*/, filename, flags){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 382 | const opName = 'xOpen'; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 383 | mTimeStart(opName); |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 384 | const deleteOnClose = (state.sq3Codes.SQLITE_OPEN_DELETEONCLOSE & flags); |
| 385 | const create = (state.sq3Codes.SQLITE_OPEN_CREATE & flags); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 386 | wTimeStart('xOpen'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 387 | try{ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 388 | let hDir, filenamePart; |
| 389 | try { |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 390 | [hDir, filenamePart] = await getDirForFilename(filename, !!create); |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 391 | }catch(e){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 392 | storeAndNotify(opName, state.sql3Codes.SQLITE_NOTFOUND); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 393 | mTimeEnd(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 394 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 395 | return; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 396 | } |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 397 | const hFile = await hDir.getFileHandle(filenamePart, {create}); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 398 | /** |
| 399 | wa-sqlite, at this point, grabs a SyncAccessHandle and |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 400 | assigns it to the syncHandle prop of the file state |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 401 | object, but only for certain cases and it's unclear why it |
| 402 | places that limitation on it. |
| 403 | */ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 404 | wTimeEnd(); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 405 | __openFiles[fid] = Object.assign(Object.create(null),{ |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 406 | filenameAbs: filename, |
| 407 | filenamePart: filenamePart, |
| 408 | dirHandle: hDir, |
| 409 | fileHandle: hFile, |
| 410 | sabView: state.sabFileBufView, |
| 411 | readOnly: create |
| 412 | ? false : (state.sq3Codes.SQLITE_OPEN_READONLY & flags), |
| 413 | deleteOnClose: deleteOnClose |
| 414 | }); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 415 | storeAndNotify(opName, 0); |
| 416 | }catch(e){ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 417 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 418 | error(opName,e); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 419 | state.s11n.storeException(1,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 420 | storeAndNotify(opName, state.sq3Codes.SQLITE_IOERR); |
| 421 | } |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 422 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 423 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 424 | xRead: async function(fid,n,offset){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 425 | mTimeStart('xRead'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 426 | let rc = 0; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 427 | const fh = __openFiles[fid]; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 428 | try{ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 429 | wTimeStart('xRead'); |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 430 | const nRead = (await getSyncHandle(fh)).read( |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 431 | fh.sabView.subarray(0, n), |
| 432 | {at: Number(offset)} |
| 433 | ); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 434 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 435 | if(nRead < n){/* Zero-fill remaining bytes */ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 436 | fh.sabView.fill(0, nRead, n); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 437 | rc = state.sq3Codes.SQLITE_IOERR_SHORT_READ; |
| 438 | } |
| 439 | }catch(e){ |
| 440 | error("xRead() failed",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 441 | state.s11n.storeException(1,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 442 | rc = state.sq3Codes.SQLITE_IOERR_READ; |
| 443 | } |
| 444 | storeAndNotify('xRead',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 445 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 446 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 447 | xSync: async function(fid,flags/*ignored*/){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 448 | mTimeStart('xSync'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 449 | const fh = __openFiles[fid]; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 450 | let rc = 0; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 451 | if(!fh.readOnly && fh.syncHandle){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 452 | try { |
| 453 | wTimeStart('xSync'); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 454 | await fh.syncHandle.flush(); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 455 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 456 | state.s11n.storeException(2,e); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 457 | }finally{ |
| 458 | wTimeEnd(); |
| 459 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 460 | } |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 461 | storeAndNotify('xSync',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 462 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 463 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 464 | xTruncate: async function(fid,size){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 465 | mTimeStart('xTruncate'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 466 | let rc = 0; |
| 467 | const fh = __openFiles[fid]; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 468 | wTimeStart('xTruncate'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 469 | try{ |
| 470 | affirmNotRO('xTruncate', fh); |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 471 | await (await getSyncHandle(fh)).truncate(size); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 472 | }catch(e){ |
| 473 | error("xTruncate():",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 474 | state.s11n.storeException(2,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 475 | rc = state.sq3Codes.SQLITE_IOERR_TRUNCATE; |
| 476 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 477 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 478 | storeAndNotify('xTruncate',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 479 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 480 | }, |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 481 | xUnlock: async function(fid,lockType){ |
| 482 | mTimeStart('xUnlock'); |
| 483 | let rc = 0; |
| 484 | const fh = __openFiles[fid]; |
| 485 | if( state.sq3Codes.SQLITE_LOCK_NONE===lockType |
| 486 | && fh.syncHandle ){ |
| 487 | try { await closeSyncHandle(fh) } |
| 488 | catch(e){ |
| 489 | state.s11n.storeException(1,e); |
| 490 | rc = state.sq3Codes.SQLITE_IOERR; |
| 491 | /* Maybe we want to not report this? "Destructors do not |
| 492 | throw." */ |
| 493 | } |
| 494 | } |
| 495 | storeAndNotify('xUnlock',rc); |
| 496 | mTimeEnd(); |
| 497 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 498 | xWrite: async function(fid,n,offset){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 499 | mTimeStart('xWrite'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 500 | let rc; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 501 | wTimeStart('xWrite'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 502 | try{ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 503 | const fh = __openFiles[fid]; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 504 | affirmNotRO('xWrite', fh); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 505 | rc = ( |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 506 | n === (await getSyncHandle(fh)) |
| 507 | .write(fh.sabView.subarray(0, n), |
| 508 | {at: Number(offset)}) |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 509 | ) ? 0 : state.sq3Codes.SQLITE_IOERR_WRITE; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 510 | }catch(e){ |
| 511 | error("xWrite():",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 512 | state.s11n.storeException(1,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 513 | rc = state.sq3Codes.SQLITE_IOERR_WRITE; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 514 | }finally{ |
| 515 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 516 | } |
| 517 | storeAndNotify('xWrite',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 518 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 519 | } |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 520 | }/*vfsAsyncImpls*/; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 521 | |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 522 | const initS11n = ()=>{ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 523 | /** |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 524 | ACHTUNG: this code is 100% duplicated in the other half of this |
| 525 | proxy! The documentation is maintained in the "synchronous half". |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 526 | */ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 527 | if(state.s11n) return state.s11n; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 528 | const textDecoder = new TextDecoder(), |
| 529 | textEncoder = new TextEncoder('utf-8'), |
| 530 | viewU8 = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize), |
| 531 | viewDV = new DataView(state.sabIO, state.sabS11nOffset, state.sabS11nSize); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 532 | state.s11n = Object.create(null); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 533 | const TypeIds = Object.create(null); |
| 534 | TypeIds.number = { id: 1, size: 8, getter: 'getFloat64', setter: 'setFloat64' }; |
| 535 | TypeIds.bigint = { id: 2, size: 8, getter: 'getBigInt64', setter: 'setBigInt64' }; |
| 536 | TypeIds.boolean = { id: 3, size: 4, getter: 'getInt32', setter: 'setInt32' }; |
| 537 | TypeIds.string = { id: 4 }; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 538 | const getTypeId = (v)=>( |
| 539 | TypeIds[typeof v] |
| 540 | || toss("Maintenance required: this value type cannot be serialized.",v) |
| 541 | ); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 542 | const getTypeIdById = (tid)=>{ |
| 543 | switch(tid){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 544 | case TypeIds.number.id: return TypeIds.number; |
| 545 | case TypeIds.bigint.id: return TypeIds.bigint; |
| 546 | case TypeIds.boolean.id: return TypeIds.boolean; |
| 547 | case TypeIds.string.id: return TypeIds.string; |
| 548 | default: toss("Invalid type ID:",tid); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 549 | } |
| 550 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 551 | state.s11n.deserialize = function(){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 552 | ++metrics.s11n.deserialize.count; |
| 553 | const t = performance.now(); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 554 | const argc = viewU8[0]; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 555 | const rc = argc ? [] : null; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 556 | if(argc){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 557 | const typeIds = []; |
| 558 | let offset = 1, i, n, v; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 559 | for(i = 0; i < argc; ++i, ++offset){ |
| 560 | typeIds.push(getTypeIdById(viewU8[offset])); |
| 561 | } |
| 562 | for(i = 0; i < argc; ++i){ |
| 563 | const t = typeIds[i]; |
| 564 | if(t.getter){ |
| 565 | v = viewDV[t.getter](offset, state.littleEndian); |
| 566 | offset += t.size; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 567 | }else{/*String*/ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 568 | n = viewDV.getInt32(offset, state.littleEndian); |
| 569 | offset += 4; |
| 570 | v = textDecoder.decode(viewU8.slice(offset, offset+n)); |
| 571 | offset += n; |
| 572 | } |
| 573 | rc.push(v); |
| 574 | } |
| 575 | } |
| 576 | //log("deserialize:",argc, rc); |
| 577 | metrics.s11n.deserialize.time += performance.now() - t; |
| 578 | return rc; |
| 579 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 580 | state.s11n.serialize = function(...args){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 581 | const t = performance.now(); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 582 | ++metrics.s11n.serialize.count; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 583 | if(args.length){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 584 | //log("serialize():",args); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 585 | const typeIds = []; |
| 586 | let i = 0, offset = 1; |
| 587 | viewU8[0] = args.length & 0xff /* header = # of args */; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 588 | for(; i < args.length; ++i, ++offset){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 589 | /* Write the TypeIds.id value into the next args.length |
| 590 | bytes. */ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 591 | typeIds.push(getTypeId(args[i])); |
| 592 | viewU8[offset] = typeIds[i].id; |
| 593 | } |
| 594 | for(i = 0; i < args.length; ++i) { |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 595 | /* Deserialize the following bytes based on their |
| 596 | corresponding TypeIds.id from the header. */ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 597 | const t = typeIds[i]; |
| 598 | if(t.setter){ |
| 599 | viewDV[t.setter](offset, args[i], state.littleEndian); |
| 600 | offset += t.size; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 601 | }else{/*String*/ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 602 | const s = textEncoder.encode(args[i]); |
| 603 | viewDV.setInt32(offset, s.byteLength, state.littleEndian); |
| 604 | offset += 4; |
| 605 | viewU8.set(s, offset); |
| 606 | offset += s.byteLength; |
| 607 | } |
| 608 | } |
| 609 | //log("serialize() result:",viewU8.slice(0,offset)); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 610 | }else{ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 611 | viewU8[0] = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 612 | } |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 613 | metrics.s11n.serialize.time += performance.now() - t; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 614 | }; |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 615 | |
| 616 | state.s11n.storeException = state.asyncS11nExceptions |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 617 | ? ((priority,e)=>{ |
| 618 | if(priority<=state.asyncS11nExceptions){ |
| 619 | state.s11n.serialize(e.message); |
| 620 | } |
| 621 | }) |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 622 | : ()=>{}; |
| 623 | |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 624 | return state.s11n; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 625 | }/*initS11n()*/; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 626 | |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 627 | const waitLoop = async function f(){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 628 | const opHandlers = Object.create(null); |
stephan | c9e2602 | 2022-09-20 10:11:52 +0000 | [diff] [blame] | 629 | for(let k of Object.keys(state.opIds)){ |
| 630 | const vi = vfsAsyncImpls[k]; |
| 631 | if(!vi) continue; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 632 | const o = Object.create(null); |
| 633 | opHandlers[state.opIds[k]] = o; |
| 634 | o.key = k; |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 635 | o.f = vi || toss("No vfsAsyncImpls[",k,"]"); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 636 | } |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 637 | /** |
| 638 | waitTime is how long (ms) to wait for each Atomics.wait(). |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 639 | We need to wake up periodically to give the thread a chance |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 640 | to do other things. |
| 641 | */ |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 642 | const waitTime = 1000; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 643 | let lastOpTime = performance.now(); |
| 644 | let now; |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 645 | while(!flagAsyncShutdown){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 646 | try { |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 647 | if('timed-out'===Atomics.wait( |
| 648 | state.sabOPView, state.opIds.whichOp, 0, waitTime |
| 649 | )){ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 650 | continue; |
| 651 | } |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 652 | lastOpTime = performance.now(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 653 | const opId = Atomics.load(state.sabOPView, state.opIds.whichOp); |
| 654 | Atomics.store(state.sabOPView, state.opIds.whichOp, 0); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 655 | const hnd = opHandlers[opId] ?? toss("No waitLoop handler for whichOp #",opId); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 656 | const args = state.s11n.deserialize() || []; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 657 | state.s11n.serialize(/* clear s11n to keep the caller from |
| 658 | confusing this with an exception string |
| 659 | written by the upcoming operation */); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 660 | //warn("waitLoop() whichOp =",opId, hnd, args); |
| 661 | if(hnd.f) await hnd.f(...args); |
| 662 | else error("Missing callback for opId",opId); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 663 | }catch(e){ |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 664 | error('in waitLoop():',e); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 665 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 666 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 667 | }; |
| 668 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 669 | navigator.storage.getDirectory().then(function(d){ |
| 670 | const wMsg = (type)=>postMessage({type}); |
| 671 | state.rootDir = d; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 672 | self.onmessage = function({data}){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 673 | switch(data.type){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 674 | case 'opfs-async-init':{ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 675 | /* Receive shared state from synchronous partner */ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 676 | const opt = data.args; |
| 677 | state.littleEndian = opt.littleEndian; |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 678 | state.asyncS11nExceptions = opt.asyncS11nExceptions; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 679 | state.verbose = opt.verbose ?? 2; |
| 680 | state.fileBufferSize = opt.fileBufferSize; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 681 | state.sabS11nOffset = opt.sabS11nOffset; |
| 682 | state.sabS11nSize = opt.sabS11nSize; |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 683 | state.sabOP = opt.sabOP; |
| 684 | state.sabOPView = new Int32Array(state.sabOP); |
| 685 | state.sabIO = opt.sabIO; |
| 686 | state.sabFileBufView = new Uint8Array(state.sabIO, 0, state.fileBufferSize); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 687 | state.sabS11nView = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 688 | state.opIds = opt.opIds; |
| 689 | state.sq3Codes = opt.sq3Codes; |
| 690 | Object.keys(vfsAsyncImpls).forEach((k)=>{ |
| 691 | if(!Number.isFinite(state.opIds[k])){ |
| 692 | toss("Maintenance required: missing state.opIds[",k,"]"); |
| 693 | } |
| 694 | }); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 695 | initS11n(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 696 | metrics.reset(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 697 | log("init state",state); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 698 | wMsg('opfs-async-inited'); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 699 | waitLoop(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 700 | break; |
| 701 | } |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 702 | case 'opfs-async-restart': |
| 703 | if(flagAsyncShutdown){ |
| 704 | warn("Restarting after opfs-async-shutdown. Might or might not work."); |
| 705 | flagAsyncShutdown = false; |
| 706 | waitLoop(); |
| 707 | } |
| 708 | break; |
| 709 | case 'opfs-async-metrics': |
| 710 | metrics.dump(); |
| 711 | break; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 712 | } |
| 713 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 714 | wMsg('opfs-async-loaded'); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 715 | }).catch((e)=>error("error initializing OPFS asyncer:",e)); |