sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / wasm / api / sqlite3-vfs-opfs-wl.c-pp.js
| 1 | //#if not target:node and not omit-opfs-wl |
| 2 | /* |
| 3 | 2026-02-20 |
| 4 | |
| 5 | The author disclaims copyright to this source code. In place of a |
| 6 | legal notice, here is a blessing: |
| 7 | |
| 8 | * May you do good and not evil. |
| 9 | * May you find forgiveness for yourself and forgive others. |
| 10 | * May you share freely, never taking more than you give. |
| 11 | |
| 12 | *********************************************************************** |
| 13 | |
| 14 | This file is a reimplementation of the "opfs" VFS (as distinct from |
| 15 | "opfs-sahpool") which uses WebLocks for locking instead of a bespoke |
| 16 | Atomics.wait()/notify() protocol. This file holds the "synchronous |
| 17 | half" of the VFS, whereas it shares the "asynchronous half" with the |
| 18 | "opfs" VFS. |
| 19 | |
| 20 | Testing has failed to show any genuine functional difference between |
| 21 | these VFSes other than "opfs-wl" being able to dole out xLock() |
| 22 | requests in a strictly FIFO manner by virtue of WebLocks being |
| 23 | globally managed by the browser. This tends to lead to, but does not |
| 24 | guaranty, fairer distribution of locks. Differences are unlikely to |
| 25 | be noticed except, perhaps, under very high contention. |
| 26 | |
| 27 | This file is intended to be appended to the main sqlite3 JS |
| 28 | deliverable somewhere after opfs-common-shared.c-pp.js. |
| 29 | */ |
| 30 | 'use strict'; |
| 31 | sqlite3ApiBootstrap.initializers.push(function(sqlite3){ |
| 32 | if( !sqlite3.opfs || sqlite3.config.disable?.vfs?.['opfs-wl'] ){ |
| 33 | return; |
| 34 | } |
| 35 | const util = sqlite3.util, |
| 36 | toss = sqlite3.util.toss; |
| 37 | const opfsUtil = sqlite3.opfs; |
| 38 | const vfsName = 'opfs-wl'; |
| 39 | /** |
| 40 | installOpfsWlVfs() returns a Promise which, on success, installs an |
| 41 | sqlite3_vfs named "opfs-wl", suitable for use with all sqlite3 APIs |
| 42 | which accept a VFS. It is intended to be called via |
| 43 | sqlite3ApiBootstrap.initializers or an equivalent mechanism. |
| 44 | |
| 45 | This VFS is essentially identical to the "opfs" VFS but uses |
| 46 | WebLocks for its xLock() and xUnlock() implementations. |
| 47 | |
| 48 | Quirks specific to this VFS: |
| 49 | |
| 50 | - The (officially undocumented) 'opfs-wl-disable' URL |
| 51 | argument will disable OPFS, making this function a no-op. |
| 52 | |
| 53 | Aside from locking differences in the VFSes, this function |
| 54 | otherwise behaves the same as |
| 55 | sqlite3-vfs-opfs.c-pp.js:installOpfsVfs(). |
| 56 | */ |
| 57 | const installOpfsWlVfs = async function(options){ |
| 58 | options = opfsUtil.initOptions(vfsName,options); |
| 59 | if( !options ) return sqlite3; |
| 60 | const capi = sqlite3.capi, |
| 61 | state = opfsUtil.createVfsState(), |
| 62 | opfsVfs = state.vfs, |
| 63 | metrics = opfsVfs.metrics.counters, |
| 64 | mTimeStart = opfsVfs.mTimeStart, |
| 65 | mTimeEnd = opfsVfs.mTimeEnd, |
| 66 | opRun = opfsVfs.opRun, |
| 67 | debug = (...args)=>sqlite3.config.debug(vfsName+":",...args), |
| 68 | warn = (...args)=>sqlite3.config.warn(vfsName+":",...args), |
| 69 | __openFiles = opfsVfs.__openFiles; |
| 70 | |
| 71 | //debug("state",JSON.stringify(options)); |
| 72 | /* |
| 73 | At this point, createVfsState() has populated: |
| 74 | |
| 75 | - state: the configuration object we share with the async proxy. |
| 76 | |
| 77 | - opfsVfs: an sqlite3_vfs instance with lots of JS state attached |
| 78 | to it. |
| 79 | |
| 80 | with any code common to both the "opfs" and "opfs-wl" VFSes. Now |
| 81 | comes the VFS-dependent work... |
| 82 | */ |
| 83 | return opfsVfs.bindVfs(util.nu({ |
| 84 | xLock: function(pFile,lockType){ |
| 85 | mTimeStart('xLock'); |
| 86 | //debug("xLock()..."); |
| 87 | const f = __openFiles[pFile]; |
| 88 | const rc = opRun('xLock', pFile, lockType); |
| 89 | if( !rc ) f.lockType = lockType; |
| 90 | mTimeEnd(); |
| 91 | return rc; |
| 92 | }, |
| 93 | xUnlock: function(pFile,lockType){ |
| 94 | mTimeStart('xUnlock'); |
| 95 | const f = __openFiles[pFile]; |
| 96 | const rc = opRun('xUnlock', pFile, lockType); |
| 97 | if( !rc ) f.lockType = lockType; |
| 98 | mTimeEnd(); |
| 99 | return rc; |
| 100 | } |
| 101 | }), function(sqlite3, vfs){ |
| 102 | /* Post-VFS-registration initialization... */ |
| 103 | if(sqlite3.oo1){ |
| 104 | const OpfsWlDb = function(...args){ |
| 105 | const opt = sqlite3.oo1.DB.dbCtorHelper.normalizeArgs(...args); |
| 106 | opt.vfs = vfs.$zName; |
| 107 | sqlite3.oo1.DB.dbCtorHelper.call(this, opt); |
| 108 | }; |
| 109 | OpfsWlDb.prototype = Object.create(sqlite3.oo1.DB.prototype); |
| 110 | sqlite3.oo1.OpfsWlDb = OpfsWlDb; |
| 111 | OpfsWlDb.importDb = opfsUtil.importDb; |
| 112 | /* The "opfs" VFS variant adds a |
| 113 | oo1.DB.dbCtorHelper.setVfsPostOpenCallback() callback to set |
| 114 | a high busy_timeout. That was a design mis-decision and is |
| 115 | inconsistent with sqlite3_open() and friends, but is retained |
| 116 | against the risk of introducing regressions if it's removed. |
| 117 | This variant does not repeat that mistake. |
| 118 | */ |
| 119 | } |
| 120 | })/*bindVfs()*/; |
| 121 | }/*installOpfsWlVfs()*/; |
| 122 | sqlite3ApiBootstrap.initializersAsync.push(async (sqlite3)=>{ |
| 123 | return installOpfsWlVfs().catch((e)=>{ |
| 124 | sqlite3.config.warn("Ignoring inability to install the",vfsName,"sqlite3_vfs:",e); |
| 125 | }); |
| 126 | }); |
| 127 | }/*sqlite3ApiBootstrap.initializers.push()*/); |
| 128 | //#/if global snip |