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 | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 13 | An Worker which manages asynchronous OPFS handles on behalf of a |
| 14 | synchronous API which controls it via a combination of Worker |
| 15 | messages, SharedArrayBuffer, and Atomics. It is the asynchronous |
| 16 | counterpart of the API defined in sqlite3-api-opfs.js. |
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 ".." |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 105 | components are properly expanded, and returned. If the 2nd arg is |
| 106 | true, the result is returned as an array of path elements, else an |
| 107 | absolute path string is returned. |
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 |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 118 | of [handleOfContainingDir, filename]. If the 2nd argument is truthy |
| 119 | then each directory element leading to the file is created along |
| 120 | the way. Throws if any creation or resolution fails. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 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 | |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 174 | /** |
| 175 | If the given file-holding object has a sync handle attached to it, |
| 176 | that handle is remove and asynchronously closed. Though it may |
| 177 | sound sensible to continue work as soon as the close() returns |
| 178 | (noting that it's asynchronous), doing so can cause operations |
| 179 | performed soon afterwards, e.g. a call to getSyncHandle() to fail |
| 180 | because they may happen out of order from the close(). OPFS does |
| 181 | not guaranty that the actual order of operations is retained in |
| 182 | such cases. i.e. always "await" on the result of this function. |
| 183 | */ |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 184 | const closeSyncHandle = async (fh)=>{ |
| 185 | if(fh.syncHandle){ |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 186 | log("Closing sync handle for",fh.filenameAbs); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 187 | const h = fh.syncHandle; |
| 188 | delete fh.syncHandle; |
| 189 | return h.close(); |
| 190 | } |
| 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 | /** |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 194 | Stores the given value at state.sabOPView[state.opIds.rc] and then |
| 195 | Atomics.notify()'s it. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 196 | */ |
| 197 | const storeAndNotify = (opName, value)=>{ |
stephan | c9e2602 | 2022-09-20 10:11:52 +0000 | [diff] [blame] | 198 | log(opName+"() => notify(",state.opIds.rc,",",value,")"); |
| 199 | Atomics.store(state.sabOPView, state.opIds.rc, value); |
| 200 | Atomics.notify(state.sabOPView, state.opIds.rc); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 201 | }; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 202 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 203 | /** |
| 204 | Throws if fh is a file-holding object which is flagged as read-only. |
| 205 | */ |
| 206 | const affirmNotRO = function(opName,fh){ |
| 207 | if(fh.readOnly) toss(opName+"(): File is read-only: "+fh.filenameAbs); |
| 208 | }; |
| 209 | |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 210 | /** |
| 211 | We track 2 different timers: the "metrics" timer records how much |
| 212 | time we spend performing work. The "wait" timer records how much |
| 213 | time we spend waiting on the underlying OPFS timer. See the calls |
| 214 | to mTimeStart(), mTimeEnd(), wTimeStart(), and wTimeEnd() |
| 215 | throughout this file to see how they're used. |
| 216 | */ |
| 217 | const __mTimer = Object.create(null); |
| 218 | __mTimer.op = undefined; |
| 219 | __mTimer.start = undefined; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 220 | const mTimeStart = (op)=>{ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 221 | __mTimer.start = performance.now(); |
| 222 | __mTimer.op = op; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 223 | //metrics[op] || toss("Maintenance required: missing metrics for",op); |
| 224 | ++metrics[op].count; |
| 225 | }; |
| 226 | const mTimeEnd = ()=>( |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 227 | metrics[__mTimer.op].time += performance.now() - __mTimer.start |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 228 | ); |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 229 | const __wTimer = Object.create(null); |
| 230 | __wTimer.op = undefined; |
| 231 | __wTimer.start = undefined; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 232 | const wTimeStart = (op)=>{ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 233 | __wTimer.start = performance.now(); |
| 234 | __wTimer.op = op; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 235 | //metrics[op] || toss("Maintenance required: missing metrics for",op); |
| 236 | }; |
| 237 | const wTimeEnd = ()=>( |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 238 | metrics[__wTimer.op].wait += performance.now() - __wTimer.start |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 239 | ); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 240 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 241 | /** |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 242 | Gets set to true by the 'opfs-async-shutdown' command to quit the |
| 243 | wait loop. This is only intended for debugging purposes: we cannot |
| 244 | inspect this file's state while the tight waitLoop() is running and |
| 245 | need a way to stop that loop for introspection purposes. |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 246 | */ |
| 247 | let flagAsyncShutdown = false; |
| 248 | |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 249 | |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 250 | /** |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 251 | Asynchronous wrappers for sqlite3_vfs and sqlite3_io_methods |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 252 | methods, as well as helpers like mkdir(). Maintenance reminder: |
| 253 | members are in alphabetical order to simplify finding them. |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 254 | */ |
| 255 | const vfsAsyncImpls = { |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 256 | 'opfs-async-metrics': async ()=>{ |
| 257 | mTimeStart('opfs-async-metrics'); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 258 | metrics.dump(); |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 259 | storeAndNotify('opfs-async-metrics', 0); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 260 | mTimeEnd(); |
| 261 | }, |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 262 | 'opfs-async-shutdown': async ()=>{ |
| 263 | flagAsyncShutdown = true; |
| 264 | storeAndNotify('opfs-async-shutdown', 0); |
| 265 | }, |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 266 | mkdir: async (dirname)=>{ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 267 | mTimeStart('mkdir'); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 268 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 269 | wTimeStart('mkdir'); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 270 | try { |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 271 | await getDirForFilename(dirname+"/filepart", true); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 272 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 273 | state.s11n.storeException(2,e); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 274 | rc = state.sq3Codes.SQLITE_IOERR; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 275 | }finally{ |
| 276 | wTimeEnd(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 277 | } |
| 278 | storeAndNotify('mkdir', rc); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 279 | mTimeEnd(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 280 | }, |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 281 | xAccess: async (filename)=>{ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 282 | mTimeStart('xAccess'); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 283 | /* OPFS cannot support the full range of xAccess() queries sqlite3 |
| 284 | calls for. We can essentially just tell if the file is |
| 285 | accessible, but if it is it's automatically writable (unless |
| 286 | it's locked, which we cannot(?) know without trying to open |
| 287 | it). OPFS does not have the notion of read-only. |
| 288 | |
| 289 | The return semantics of this function differ from sqlite3's |
| 290 | xAccess semantics because we are limited in what we can |
| 291 | communicate back to our synchronous communication partner: 0 = |
| 292 | accessible, non-0 means not accessible. |
| 293 | */ |
| 294 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 295 | wTimeStart('xAccess'); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 296 | try{ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 297 | const [dh, fn] = await getDirForFilename(filename); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 298 | await dh.getFileHandle(fn); |
| 299 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 300 | state.s11n.storeException(2,e); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 301 | rc = state.sq3Codes.SQLITE_IOERR; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 302 | }finally{ |
| 303 | wTimeEnd(); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 304 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 305 | storeAndNotify('xAccess', rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 306 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 307 | }, |
| 308 | xClose: async function(fid){ |
| 309 | const opName = 'xClose'; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 310 | mTimeStart(opName); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 311 | const fh = __openFiles[fid]; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 312 | let rc = 0; |
| 313 | wTimeStart('xClose'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 314 | if(fh){ |
| 315 | delete __openFiles[fid]; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 316 | await closeSyncHandle(fh); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 317 | if(fh.deleteOnClose){ |
| 318 | try{ await fh.dirHandle.removeEntry(fh.filenamePart) } |
| 319 | catch(e){ warn("Ignoring dirHandle.removeEntry() failure of",fh,e) } |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 320 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 321 | }else{ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 322 | state.s11n.serialize(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 323 | rc = state.sq3Codes.SQLITE_NOTFOUND; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 324 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 325 | wTimeEnd(); |
| 326 | storeAndNotify(opName, rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 327 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 328 | }, |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 329 | xDelete: async function(...args){ |
| 330 | mTimeStart('xDelete'); |
| 331 | const rc = await vfsAsyncImpls.xDeleteNoWait(...args); |
| 332 | storeAndNotify('xDelete', rc); |
| 333 | mTimeEnd(); |
| 334 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 335 | xDeleteNoWait: async function(filename, syncDir = 0, recursive = false){ |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 336 | /* The syncDir flag is, for purposes of the VFS API's semantics, |
| 337 | ignored here. However, if it has the value 0x1234 then: after |
| 338 | deleting the given file, recursively try to delete any empty |
| 339 | directories left behind in its wake (ignoring any errors and |
| 340 | stopping at the first failure). |
| 341 | |
| 342 | That said: we don't know for sure that removeEntry() fails if |
| 343 | the dir is not empty because the API is not documented. It has, |
| 344 | however, a "recursive" flag which defaults to false, so |
| 345 | presumably it will fail if the dir is not empty and that flag |
| 346 | is false. |
| 347 | */ |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 348 | let rc = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 349 | wTimeStart('xDelete'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 350 | try { |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 351 | while(filename){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 352 | const [hDir, filenamePart] = await getDirForFilename(filename, false); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 353 | if(!filenamePart) break; |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 354 | await hDir.removeEntry(filenamePart, {recursive}); |
stephan | 8200a6d | 2022-09-17 23:29:27 +0000 | [diff] [blame] | 355 | if(0x1234 !== syncDir) break; |
| 356 | filename = getResolvedPath(filename, true); |
| 357 | filename.pop(); |
| 358 | filename = filename.join('/'); |
| 359 | } |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 360 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 361 | state.s11n.storeException(2,e); |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 362 | rc = state.sq3Codes.SQLITE_IOERR_DELETE; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 363 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 364 | wTimeEnd(); |
stephan | f386012 | 2022-09-18 17:32:35 +0000 | [diff] [blame] | 365 | return rc; |
| 366 | }, |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 367 | xFileSize: async function(fid){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 368 | mTimeStart('xFileSize'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 369 | const fh = __openFiles[fid]; |
| 370 | let sz; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 371 | wTimeStart('xFileSize'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 372 | try{ |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 373 | sz = await (await getSyncHandle(fh)).getSize(); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 374 | state.s11n.serialize(Number(sz)); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 375 | sz = 0; |
| 376 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 377 | state.s11n.storeException(2,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 378 | sz = state.sq3Codes.SQLITE_IOERR; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 379 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 380 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 381 | storeAndNotify('xFileSize', sz); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 382 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 383 | }, |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 384 | xLock: async function(fid,lockType){ |
| 385 | mTimeStart('xLock'); |
| 386 | const fh = __openFiles[fid]; |
| 387 | let rc = 0; |
| 388 | if( !fh.syncHandle ){ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 389 | wTimeStart('xLock'); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 390 | try { await getSyncHandle(fh) } |
| 391 | catch(e){ |
| 392 | state.s11n.storeException(1,e); |
| 393 | rc = state.sq3Codes.SQLITE_IOERR; |
| 394 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 395 | wTimeEnd(); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 396 | } |
| 397 | storeAndNotify('xLock',rc); |
| 398 | mTimeEnd(); |
| 399 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 400 | xOpen: async function(fid/*sqlite3_file pointer*/, filename, flags){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 401 | const opName = 'xOpen'; |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 402 | mTimeStart(opName); |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 403 | const deleteOnClose = (state.sq3Codes.SQLITE_OPEN_DELETEONCLOSE & flags); |
| 404 | const create = (state.sq3Codes.SQLITE_OPEN_CREATE & flags); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 405 | wTimeStart('xOpen'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 406 | try{ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 407 | let hDir, filenamePart; |
| 408 | try { |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 409 | [hDir, filenamePart] = await getDirForFilename(filename, !!create); |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 410 | }catch(e){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 411 | storeAndNotify(opName, state.sql3Codes.SQLITE_NOTFOUND); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 412 | mTimeEnd(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 413 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 414 | return; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 415 | } |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 416 | const hFile = await hDir.getFileHandle(filenamePart, {create}); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 417 | /** |
| 418 | wa-sqlite, at this point, grabs a SyncAccessHandle and |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 419 | assigns it to the syncHandle prop of the file state |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 420 | object, but only for certain cases and it's unclear why it |
| 421 | places that limitation on it. |
| 422 | */ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 423 | wTimeEnd(); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 424 | __openFiles[fid] = Object.assign(Object.create(null),{ |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 425 | filenameAbs: filename, |
| 426 | filenamePart: filenamePart, |
| 427 | dirHandle: hDir, |
| 428 | fileHandle: hFile, |
| 429 | sabView: state.sabFileBufView, |
| 430 | readOnly: create |
| 431 | ? false : (state.sq3Codes.SQLITE_OPEN_READONLY & flags), |
| 432 | deleteOnClose: deleteOnClose |
| 433 | }); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 434 | storeAndNotify(opName, 0); |
| 435 | }catch(e){ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 436 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 437 | error(opName,e); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 438 | state.s11n.storeException(1,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 439 | storeAndNotify(opName, state.sq3Codes.SQLITE_IOERR); |
| 440 | } |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 441 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 442 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 443 | xRead: async function(fid,n,offset){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 444 | mTimeStart('xRead'); |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 445 | let rc = 0, nRead; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 446 | const fh = __openFiles[fid]; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 447 | try{ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 448 | wTimeStart('xRead'); |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 449 | nRead = (await getSyncHandle(fh)).read( |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 450 | fh.sabView.subarray(0, n), |
| 451 | {at: Number(offset)} |
| 452 | ); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 453 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 454 | if(nRead < n){/* Zero-fill remaining bytes */ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 455 | fh.sabView.fill(0, nRead, n); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 456 | rc = state.sq3Codes.SQLITE_IOERR_SHORT_READ; |
| 457 | } |
| 458 | }catch(e){ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 459 | if(undefined===nRead) wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 460 | error("xRead() failed",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 461 | state.s11n.storeException(1,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 462 | rc = state.sq3Codes.SQLITE_IOERR_READ; |
| 463 | } |
| 464 | storeAndNotify('xRead',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 465 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 466 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 467 | xSync: async function(fid,flags/*ignored*/){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 468 | mTimeStart('xSync'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 469 | const fh = __openFiles[fid]; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 470 | let rc = 0; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 471 | if(!fh.readOnly && fh.syncHandle){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 472 | try { |
| 473 | wTimeStart('xSync'); |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 474 | await fh.syncHandle.flush(); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 475 | }catch(e){ |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 476 | state.s11n.storeException(2,e); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 477 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 478 | wTimeEnd(); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 479 | } |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 480 | storeAndNotify('xSync',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 481 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 482 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 483 | xTruncate: async function(fid,size){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 484 | mTimeStart('xTruncate'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 485 | let rc = 0; |
| 486 | const fh = __openFiles[fid]; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 487 | wTimeStart('xTruncate'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 488 | try{ |
| 489 | affirmNotRO('xTruncate', fh); |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 490 | await (await getSyncHandle(fh)).truncate(size); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 491 | }catch(e){ |
| 492 | error("xTruncate():",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 493 | state.s11n.storeException(2,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 494 | rc = state.sq3Codes.SQLITE_IOERR_TRUNCATE; |
| 495 | } |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 496 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 497 | storeAndNotify('xTruncate',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 498 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 499 | }, |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 500 | xUnlock: async function(fid,lockType){ |
| 501 | mTimeStart('xUnlock'); |
| 502 | let rc = 0; |
| 503 | const fh = __openFiles[fid]; |
| 504 | if( state.sq3Codes.SQLITE_LOCK_NONE===lockType |
| 505 | && fh.syncHandle ){ |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 506 | wTimeStart('xUnlock'); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 507 | try { await closeSyncHandle(fh) } |
| 508 | catch(e){ |
| 509 | state.s11n.storeException(1,e); |
| 510 | rc = state.sq3Codes.SQLITE_IOERR; |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 511 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 512 | wTimeEnd(); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 513 | } |
| 514 | storeAndNotify('xUnlock',rc); |
| 515 | mTimeEnd(); |
| 516 | }, |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 517 | xWrite: async function(fid,n,offset){ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 518 | mTimeStart('xWrite'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 519 | let rc; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 520 | wTimeStart('xWrite'); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 521 | try{ |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 522 | const fh = __openFiles[fid]; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 523 | affirmNotRO('xWrite', fh); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 524 | rc = ( |
stephan | 7ff8da8 | 2022-10-03 09:21:37 +0000 | [diff] [blame] | 525 | n === (await getSyncHandle(fh)) |
| 526 | .write(fh.sabView.subarray(0, n), |
| 527 | {at: Number(offset)}) |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 528 | ) ? 0 : state.sq3Codes.SQLITE_IOERR_WRITE; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 529 | }catch(e){ |
| 530 | error("xWrite():",e,fh); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 531 | state.s11n.storeException(1,e); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 532 | rc = state.sq3Codes.SQLITE_IOERR_WRITE; |
| 533 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 534 | wTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 535 | storeAndNotify('xWrite',rc); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 536 | mTimeEnd(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 537 | } |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 538 | }/*vfsAsyncImpls*/; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 539 | |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 540 | const initS11n = ()=>{ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 541 | /** |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 542 | ACHTUNG: this code is 100% duplicated in the other half of this |
| 543 | proxy! The documentation is maintained in the "synchronous half". |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 544 | */ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 545 | if(state.s11n) return state.s11n; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 546 | const textDecoder = new TextDecoder(), |
| 547 | textEncoder = new TextEncoder('utf-8'), |
| 548 | viewU8 = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize), |
| 549 | viewDV = new DataView(state.sabIO, state.sabS11nOffset, state.sabS11nSize); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 550 | state.s11n = Object.create(null); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 551 | const TypeIds = Object.create(null); |
| 552 | TypeIds.number = { id: 1, size: 8, getter: 'getFloat64', setter: 'setFloat64' }; |
| 553 | TypeIds.bigint = { id: 2, size: 8, getter: 'getBigInt64', setter: 'setBigInt64' }; |
| 554 | TypeIds.boolean = { id: 3, size: 4, getter: 'getInt32', setter: 'setInt32' }; |
| 555 | TypeIds.string = { id: 4 }; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 556 | const getTypeId = (v)=>( |
| 557 | TypeIds[typeof v] |
| 558 | || toss("Maintenance required: this value type cannot be serialized.",v) |
| 559 | ); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 560 | const getTypeIdById = (tid)=>{ |
| 561 | switch(tid){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 562 | case TypeIds.number.id: return TypeIds.number; |
| 563 | case TypeIds.bigint.id: return TypeIds.bigint; |
| 564 | case TypeIds.boolean.id: return TypeIds.boolean; |
| 565 | case TypeIds.string.id: return TypeIds.string; |
| 566 | default: toss("Invalid type ID:",tid); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 567 | } |
| 568 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 569 | state.s11n.deserialize = function(){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 570 | ++metrics.s11n.deserialize.count; |
| 571 | const t = performance.now(); |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 572 | const argc = viewU8[0]; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 573 | const rc = argc ? [] : null; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 574 | if(argc){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 575 | const typeIds = []; |
| 576 | let offset = 1, i, n, v; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 577 | for(i = 0; i < argc; ++i, ++offset){ |
| 578 | typeIds.push(getTypeIdById(viewU8[offset])); |
| 579 | } |
| 580 | for(i = 0; i < argc; ++i){ |
| 581 | const t = typeIds[i]; |
| 582 | if(t.getter){ |
| 583 | v = viewDV[t.getter](offset, state.littleEndian); |
| 584 | offset += t.size; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 585 | }else{/*String*/ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 586 | n = viewDV.getInt32(offset, state.littleEndian); |
| 587 | offset += 4; |
| 588 | v = textDecoder.decode(viewU8.slice(offset, offset+n)); |
| 589 | offset += n; |
| 590 | } |
| 591 | rc.push(v); |
| 592 | } |
| 593 | } |
| 594 | //log("deserialize:",argc, rc); |
| 595 | metrics.s11n.deserialize.time += performance.now() - t; |
| 596 | return rc; |
| 597 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 598 | state.s11n.serialize = function(...args){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 599 | const t = performance.now(); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 600 | ++metrics.s11n.serialize.count; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 601 | if(args.length){ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 602 | //log("serialize():",args); |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 603 | const typeIds = []; |
| 604 | let i = 0, offset = 1; |
| 605 | viewU8[0] = args.length & 0xff /* header = # of args */; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 606 | for(; i < args.length; ++i, ++offset){ |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 607 | /* Write the TypeIds.id value into the next args.length |
| 608 | bytes. */ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 609 | typeIds.push(getTypeId(args[i])); |
| 610 | viewU8[offset] = typeIds[i].id; |
| 611 | } |
| 612 | for(i = 0; i < args.length; ++i) { |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 613 | /* Deserialize the following bytes based on their |
| 614 | corresponding TypeIds.id from the header. */ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 615 | const t = typeIds[i]; |
| 616 | if(t.setter){ |
| 617 | viewDV[t.setter](offset, args[i], state.littleEndian); |
| 618 | offset += t.size; |
stephan | 72ab400 | 2022-09-21 12:27:35 +0000 | [diff] [blame] | 619 | }else{/*String*/ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 620 | const s = textEncoder.encode(args[i]); |
| 621 | viewDV.setInt32(offset, s.byteLength, state.littleEndian); |
| 622 | offset += 4; |
| 623 | viewU8.set(s, offset); |
| 624 | offset += s.byteLength; |
| 625 | } |
| 626 | } |
| 627 | //log("serialize() result:",viewU8.slice(0,offset)); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 628 | }else{ |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 629 | viewU8[0] = 0; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 630 | } |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 631 | metrics.s11n.serialize.time += performance.now() - t; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 632 | }; |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 633 | |
| 634 | state.s11n.storeException = state.asyncS11nExceptions |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 635 | ? ((priority,e)=>{ |
| 636 | if(priority<=state.asyncS11nExceptions){ |
| 637 | state.s11n.serialize(e.message); |
| 638 | } |
| 639 | }) |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 640 | : ()=>{}; |
| 641 | |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 642 | return state.s11n; |
stephan | b8c8d4e | 2022-09-20 13:25:39 +0000 | [diff] [blame] | 643 | }/*initS11n()*/; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 644 | |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 645 | const waitLoop = async function f(){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 646 | const opHandlers = Object.create(null); |
stephan | c9e2602 | 2022-09-20 10:11:52 +0000 | [diff] [blame] | 647 | for(let k of Object.keys(state.opIds)){ |
| 648 | const vi = vfsAsyncImpls[k]; |
| 649 | if(!vi) continue; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 650 | const o = Object.create(null); |
| 651 | opHandlers[state.opIds[k]] = o; |
| 652 | o.key = k; |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 653 | o.f = vi; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 654 | } |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 655 | /** |
| 656 | waitTime is how long (ms) to wait for each Atomics.wait(). |
stephan | c7fb48d | 2022-10-04 09:12:05 +0000 | [diff] [blame] | 657 | We need to wake up periodically to give the thread a chance |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 658 | to do other things. |
| 659 | */ |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 660 | const waitTime = 1000; |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 661 | while(!flagAsyncShutdown){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 662 | try { |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 663 | if('timed-out'===Atomics.wait( |
| 664 | state.sabOPView, state.opIds.whichOp, 0, waitTime |
| 665 | )){ |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 666 | continue; |
| 667 | } |
| 668 | const opId = Atomics.load(state.sabOPView, state.opIds.whichOp); |
| 669 | Atomics.store(state.sabOPView, state.opIds.whichOp, 0); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 670 | const hnd = opHandlers[opId] ?? toss("No waitLoop handler for whichOp #",opId); |
stephan | 56fae74 | 2022-09-24 10:12:19 +0000 | [diff] [blame] | 671 | const args = state.s11n.deserialize() || []; |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 672 | state.s11n.serialize(/* clear s11n to keep the caller from |
| 673 | confusing this with an exception string |
| 674 | written by the upcoming operation */); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 675 | //warn("waitLoop() whichOp =",opId, hnd, args); |
| 676 | if(hnd.f) await hnd.f(...args); |
| 677 | else error("Missing callback for opId",opId); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 678 | }catch(e){ |
stephan | 5f0b67c | 2022-10-03 11:33:35 +0000 | [diff] [blame] | 679 | error('in waitLoop():',e); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 680 | } |
stephan | e72ddfd | 2022-10-14 15:52:29 +0000 | [diff] [blame^] | 681 | } |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 682 | }; |
| 683 | |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 684 | navigator.storage.getDirectory().then(function(d){ |
| 685 | const wMsg = (type)=>postMessage({type}); |
| 686 | state.rootDir = d; |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 687 | self.onmessage = function({data}){ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 688 | switch(data.type){ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 689 | case 'opfs-async-init':{ |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 690 | /* Receive shared state from synchronous partner */ |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 691 | const opt = data.args; |
| 692 | state.littleEndian = opt.littleEndian; |
stephan | e8afca3 | 2022-09-21 14:02:47 +0000 | [diff] [blame] | 693 | state.asyncS11nExceptions = opt.asyncS11nExceptions; |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 694 | state.verbose = opt.verbose ?? 2; |
| 695 | state.fileBufferSize = opt.fileBufferSize; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 696 | state.sabS11nOffset = opt.sabS11nOffset; |
| 697 | state.sabS11nSize = opt.sabS11nSize; |
stephan | c4b87be | 2022-09-20 01:28:47 +0000 | [diff] [blame] | 698 | state.sabOP = opt.sabOP; |
| 699 | state.sabOPView = new Int32Array(state.sabOP); |
| 700 | state.sabIO = opt.sabIO; |
| 701 | state.sabFileBufView = new Uint8Array(state.sabIO, 0, state.fileBufferSize); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 702 | state.sabS11nView = new Uint8Array(state.sabIO, state.sabS11nOffset, state.sabS11nSize); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 703 | state.opIds = opt.opIds; |
| 704 | state.sq3Codes = opt.sq3Codes; |
| 705 | Object.keys(vfsAsyncImpls).forEach((k)=>{ |
| 706 | if(!Number.isFinite(state.opIds[k])){ |
| 707 | toss("Maintenance required: missing state.opIds[",k,"]"); |
| 708 | } |
| 709 | }); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 710 | initS11n(); |
stephan | aec046a | 2022-09-19 18:22:29 +0000 | [diff] [blame] | 711 | metrics.reset(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 712 | log("init state",state); |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 713 | wMsg('opfs-async-inited'); |
stephan | 5e8bb0a | 2022-09-20 08:27:57 +0000 | [diff] [blame] | 714 | waitLoop(); |
stephan | 0731554 | 2022-09-17 20:50:12 +0000 | [diff] [blame] | 715 | break; |
| 716 | } |
stephan | 3c272ba | 2022-10-04 00:54:00 +0000 | [diff] [blame] | 717 | case 'opfs-async-restart': |
| 718 | if(flagAsyncShutdown){ |
| 719 | warn("Restarting after opfs-async-shutdown. Might or might not work."); |
| 720 | flagAsyncShutdown = false; |
| 721 | waitLoop(); |
| 722 | } |
| 723 | break; |
| 724 | case 'opfs-async-metrics': |
| 725 | metrics.dump(); |
| 726 | break; |
stephan | 132a87b | 2022-09-17 15:08:22 +0000 | [diff] [blame] | 727 | } |
| 728 | }; |
stephan | 138647a | 2022-09-20 03:31:02 +0000 | [diff] [blame] | 729 | wMsg('opfs-async-loaded'); |
stephan | 9a55773 | 2022-10-04 17:06:51 +0000 | [diff] [blame] | 730 | }).catch((e)=>error("error initializing OPFS asyncer:",e)); |