interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / wasm / api / sqlite3-vfs-opfs.c-pp.js

180 lines · 6831 bytes Raw Blame History
1//#if not target:node and not omit-opfs
2/*
3 2022-09-18
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 holds the synchronous half of an sqlite3_vfs
15 implementation which proxies, in a synchronous fashion, the
16 asynchronous Origin-Private FileSystem (OPFS) APIs using a second
17 Worker, implemented in sqlite3-opfs-async-proxy.js. This file is
18 intended to be appended to the main sqlite3 JS deliverable somewhere
19 after sqlite3-api-oo1.js.
20*/
21'use strict';
22sqlite3ApiBootstrap.initializers.push(function(sqlite3){
23 if( !sqlite3.opfs || sqlite3.config.disable?.vfs?.opfs ){
24 return;
25 }
26 const util = sqlite3.util,
27 opfsUtil = sqlite3.opfs || sqlite3.util.toss("Missing sqlite3.opfs");
28 /**
29 installOpfsVfs() returns a Promise which, on success, installs an
30 sqlite3_vfs named "opfs", suitable for use with all sqlite3 APIs
31 which accept a VFS. It is intended to be called via
32 sqlite3ApiBootstrap.initializers or an equivalent mechanism.
33
34 The installed VFS uses the Origin-Private FileSystem API for
35 all file storage. On error it is rejected with an exception
36 explaining the problem. Reasons for rejection include, but are
37 not limited to:
38
39 - The counterpart Worker (see below) could not be loaded.
40
41 - The environment does not support OPFS. That includes when
42 this function is called from the main window thread.
43
44 Significant notes and limitations:
45
46 - The OPFS features used here are only available in dedicated Worker
47 threads. This file tries to detect that case, resulting in a
48 rejected Promise if those features do not seem to be available.
49
50 - It requires the SharedArrayBuffer and Atomics classes, and the
51 former is only available if the HTTP server emits the so-called
52 COOP and COEP response headers. These features are required for
53 proxying OPFS's synchronous API via the synchronous interface
54 required by the sqlite3_vfs API.
55
56 - This function may only be called a single time. When called, this
57 function removes itself from the sqlite3 object.
58
59 All arguments to this function are for internal/development purposes
60 only. They do not constitute a public API and may change at any
61 time.
62
63 The argument may optionally be a plain object with the following
64 configuration options:
65
66 - proxyUri: name of the async proxy JS file or a synchronous function
67 which, when called, returns such a name.
68
69 - verbose (=2): an integer 0-3. 0 disables all logging, 1 enables
70 logging of errors. 2 enables logging of warnings and errors. 3
71 additionally enables debugging info. Logging is performed
72 via the sqlite3.config.{log|warn|error}() functions.
73
74 - sanityChecks (=false): if true, some basic sanity tests are run on
75 the OPFS VFS API after it's initialized, before the returned
76 Promise resolves. This is only intended for testing and
77 development of the VFS, not client-side use.
78
79 Additionaly, the (officially undocumented) 'opfs-disable' URL
80 argument will disable OPFS, making this function a no-op.
81
82 On success, the Promise resolves to the top-most sqlite3 namespace
83 object. Success does not necessarily mean that it installs the VFS,
84 as there are legitimate non-error reasons for OPFS not to be
85 available.
86*/
87const installOpfsVfs = async function(options){
88 options = opfsUtil.initOptions('opfs',options);
89 if( !options ) return sqlite3;
90 const capi = sqlite3.capi,
91 state = opfsUtil.createVfsState(),
92 opfsVfs = state.vfs,
93 metrics = opfsVfs.metrics.counters,
94 mTimeStart = opfsVfs.mTimeStart,
95 mTimeEnd = opfsVfs.mTimeEnd,
96 opRun = opfsVfs.opRun,
97 debug = (...args)=>sqlite3.config.debug("opfs:",...args),
98 warn = (...args)=>sqlite3.config.warn("opfs:",...args),
99 __openFiles = opfsVfs.__openFiles;
100
101 //debug("options:",JSON.stringify(options));
102 /*
103 At this point, createVfsState() has populated:
104
105 - state: the configuration object we share with the async proxy.
106
107 - opfsVfs: an sqlite3_vfs instance with lots of JS state attached
108 to it.
109
110 with any code common to both the "opfs" and "opfs-wl" VFSes. Now
111 comes the VFS-dependent work...
112 */
113 return opfsVfs.bindVfs(util.nu({
114 xLock: function(pFile,lockType){
115 mTimeStart('xLock');
116 ++metrics.xLock.count;
117 const f = __openFiles[pFile];
118 let rc = 0;
119 /* All OPFS locks are exclusive locks. If xLock() has
120 previously succeeded, do nothing except record the lock
121 type. If no lock is active, have the async counterpart
122 lock the file. */
123 if( f.lockType ) {
124 f.lockType = lockType;
125 }else{
126 rc = opRun('xLock', pFile, lockType);
127 if( 0===rc ) f.lockType = lockType;
128 }
129 mTimeEnd();
130 return rc;
131 },
132 xUnlock: function(pFile,lockType){
133 mTimeStart('xUnlock');
134 ++metrics.xUnlock.count;
135 const f = __openFiles[pFile];
136 let rc = 0;
137 if( capi.SQLITE_LOCK_NONE === lockType
138 && f.lockType ){
139 rc = opRun('xUnlock', pFile, lockType);
140 }
141 if( 0===rc ) f.lockType = lockType;
142 mTimeEnd();
143 return rc;
144 }
145 }), function(sqlite3, vfs){
146 /* Post-VFS-registration initialization... */
147 if(sqlite3.oo1){
148 const OpfsDb = function(...args){
149 const opt = sqlite3.oo1.DB.dbCtorHelper.normalizeArgs(...args);
150 opt.vfs = vfs.$zName;
151 sqlite3.oo1.DB.dbCtorHelper.call(this, opt);
152 };
153 OpfsDb.prototype = Object.create(sqlite3.oo1.DB.prototype);
154 sqlite3.oo1.OpfsDb = OpfsDb;
155 OpfsDb.importDb = opfsUtil.importDb;
156 if( true ){
157 /* 2026-03-06: this was a design mis-decision and is
158 inconsistent with sqlite3_open() and friends, but is
159 retained against the risk of introducing regressions if
160 it's removed. */
161 sqlite3.oo1.DB.dbCtorHelper.setVfsPostOpenCallback(
162 opfsVfs.pointer,
163 function(oo1Db, sqlite3){
164 /* Set a relatively high default busy-timeout handler to
165 help OPFS dbs deal with multi-tab/multi-worker
166 contention. */
167 sqlite3.capi.sqlite3_busy_timeout(oo1Db, 10000);
168 }
169 );
170 }
171 }/*extend sqlite3.oo1*/
172 })/*bindVfs()*/;
173}/*installOpfsVfs()*/;
174sqlite3ApiBootstrap.initializersAsync.push(async (sqlite3)=>{
175 return installOpfsVfs().catch((e)=>{
176 sqlite3.config.warn("Ignoring inability to install 'opfs' sqlite3_vfs:",e);
177 })
178});
179}/*sqlite3ApiBootstrap.initializers.push()*/);
180//#/if global snip