sqlite3-vfs-opfs-sahpool.c-pp.js 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. //#ifnot target=node
  2. /*
  3. 2023-07-14
  4. The author disclaims copyright to this source code. In place of a
  5. legal notice, here is a blessing:
  6. * May you do good and not evil.
  7. * May you find forgiveness for yourself and forgive others.
  8. * May you share freely, never taking more than you give.
  9. ***********************************************************************
  10. This file holds a sqlite3_vfs backed by OPFS storage which uses a
  11. different implementation strategy than the "opfs" VFS. This one is a
  12. port of Roy Hashimoto's OPFS SyncAccessHandle pool:
  13. https://github.com/rhashimoto/wa-sqlite/blob/master/src/examples/AccessHandlePoolVFS.js
  14. As described at:
  15. https://github.com/rhashimoto/wa-sqlite/discussions/67
  16. with Roy's explicit permission to permit us to port his to our
  17. infrastructure rather than having to clean-room reverse-engineer it:
  18. https://sqlite.org/forum/forumpost/e140d84e71
  19. Primary differences from the "opfs" VFS include:
  20. - This one avoids the need for a sub-worker to synchronize
  21. communication between the synchronous C API and the
  22. only-partly-synchronous OPFS API.
  23. - It does so by opening a fixed number of OPFS files at
  24. library-level initialization time, obtaining SyncAccessHandles to
  25. each, and manipulating those handles via the synchronous sqlite3_vfs
  26. interface. If it cannot open them (e.g. they are already opened by
  27. another tab) then the VFS will not be installed.
  28. - Because of that, this one lacks all library-level concurrency
  29. support.
  30. - Also because of that, it does not require the SharedArrayBuffer,
  31. so can function without the COOP/COEP HTTP response headers.
  32. - It can hypothetically support Safari 16.4+, whereas the "opfs" VFS
  33. requires v17 due to a subworker/storage bug in 16.x which makes it
  34. incompatible with that VFS.
  35. - This VFS requires the "semi-fully-sync" FileSystemSyncAccessHandle
  36. (hereafter "SAH") APIs released with Chrome v108 (and all other
  37. major browsers released since March 2023). If that API is not
  38. detected, the VFS is not registered.
  39. */
  40. globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
  41. 'use strict';
  42. const toss = sqlite3.util.toss;
  43. const toss3 = sqlite3.util.toss3;
  44. const initPromises = Object.create(null) /* cache of (name:result) of VFS init results */;
  45. const capi = sqlite3.capi;
  46. const util = sqlite3.util;
  47. const wasm = sqlite3.wasm;
  48. // Config opts for the VFS...
  49. const SECTOR_SIZE = 4096;
  50. const HEADER_MAX_PATH_SIZE = 512;
  51. const HEADER_FLAGS_SIZE = 4;
  52. const HEADER_DIGEST_SIZE = 8;
  53. const HEADER_CORPUS_SIZE = HEADER_MAX_PATH_SIZE + HEADER_FLAGS_SIZE;
  54. const HEADER_OFFSET_FLAGS = HEADER_MAX_PATH_SIZE;
  55. const HEADER_OFFSET_DIGEST = HEADER_CORPUS_SIZE;
  56. const HEADER_OFFSET_DATA = SECTOR_SIZE;
  57. /* Bitmask of file types which may persist across sessions.
  58. SQLITE_OPEN_xyz types not listed here may be inadvertently
  59. left in OPFS but are treated as transient by this VFS and
  60. they will be cleaned up during VFS init. */
  61. const PERSISTENT_FILE_TYPES =
  62. capi.SQLITE_OPEN_MAIN_DB |
  63. capi.SQLITE_OPEN_MAIN_JOURNAL |
  64. capi.SQLITE_OPEN_SUPER_JOURNAL |
  65. capi.SQLITE_OPEN_WAL;
  66. const FLAG_COMPUTE_DIGEST_V2 = capi.SQLITE_OPEN_MEMORY
  67. /* Part of the fix for
  68. https://github.com/sqlite/sqlite-wasm/issues/97
  69. Summary: prior to version 3.50.0 computeDigest() always computes
  70. a value of [0,0] due to overflows, so it does not do anything
  71. useful. Fixing it invalidates old persistent files, so we
  72. instead only fix it for files created or updated since the bug
  73. was discovered and fixed.
  74. This flag determines whether we use the broken legacy
  75. computeDigest() or the v2 variant. We only use this flag for
  76. newly-created/overwritten files. Pre-existing files have the
  77. broken digest stored in them so need to continue to use that.
  78. What this means, in terms of db file compatibility between
  79. versions:
  80. - DBs created with versions older than this fix (<3.50.0)
  81. can be read by post-fix versions. Such DBs which are written
  82. to in-place (not replaced) by newer versions can still be read
  83. by older versions, as the affected digest is only modified
  84. when the SAH slot is assigned to a given filename.
  85. - DBs created with post-fix versions will, when read by a pre-fix
  86. version, be seen as having a "bad digest" and will be
  87. unceremoniously replaced by that pre-fix version. When swapping
  88. back to a post-fix version, that version will see that the file
  89. entry is missing the FLAG_COMPUTE_DIGEST_V2 bit so will treat it
  90. as a legacy file.
  91. This flag is stored in the same memory as the various
  92. SQLITE_OPEN_... flags and we must be careful here to not use a
  93. flag bit which is otherwise relevant for the VFS.
  94. SQLITE_OPEN_MEMORY is handled by sqlite3_open_v2() and friends,
  95. not the VFS, so we'll repurpose that one. If we take a
  96. currently-unused bit and it ends up, at some later point, being
  97. used, we would have to invalidate existing VFS files in order to
  98. move to another bit. Similarly, if the SQLITE_OPEN_MEMORY bit
  99. were ever reassigned (which it won't be!), we'd invalidate all
  100. VFS-side files.
  101. */;
  102. /** Subdirectory of the VFS's space where "opaque" (randomly-named)
  103. files are stored. Changing this effectively invalidates the data
  104. stored under older names (orphaning it), so don't do that. */
  105. const OPAQUE_DIR_NAME = ".opaque";
  106. /**
  107. Returns short a string of random alphanumeric characters
  108. suitable for use as a random filename.
  109. */
  110. const getRandomName = ()=>Math.random().toString(36).slice(2);
  111. const textDecoder = new TextDecoder();
  112. const textEncoder = new TextEncoder();
  113. const optionDefaults = Object.assign(Object.create(null),{
  114. name: 'opfs-sahpool',
  115. directory: undefined /* derived from .name */,
  116. initialCapacity: 6,
  117. clearOnInit: false,
  118. /* Logging verbosity 3+ == everything, 2 == warnings+errors, 1 ==
  119. errors only. */
  120. verbosity: 2,
  121. forceReinitIfPreviouslyFailed: false
  122. });
  123. /** Logging routines, from most to least serious. */
  124. const loggers = [
  125. sqlite3.config.error,
  126. sqlite3.config.warn,
  127. sqlite3.config.log
  128. ];
  129. const log = sqlite3.config.log;
  130. const warn = sqlite3.config.warn;
  131. const error = sqlite3.config.error;
  132. /* Maps (sqlite3_vfs*) to OpfsSAHPool instances */
  133. const __mapVfsToPool = new Map();
  134. const getPoolForVfs = (pVfs)=>__mapVfsToPool.get(pVfs);
  135. const setPoolForVfs = (pVfs,pool)=>{
  136. if(pool) __mapVfsToPool.set(pVfs, pool);
  137. else __mapVfsToPool.delete(pVfs);
  138. };
  139. /* Maps (sqlite3_file*) to OpfsSAHPool instances */
  140. const __mapSqlite3File = new Map();
  141. const getPoolForPFile = (pFile)=>__mapSqlite3File.get(pFile);
  142. const setPoolForPFile = (pFile,pool)=>{
  143. if(pool) __mapSqlite3File.set(pFile, pool);
  144. else __mapSqlite3File.delete(pFile);
  145. };
  146. /**
  147. Impls for the sqlite3_io_methods methods. Maintenance reminder:
  148. members are in alphabetical order to simplify finding them.
  149. */
  150. const ioMethods = {
  151. xCheckReservedLock: function(pFile,pOut){
  152. const pool = getPoolForPFile(pFile);
  153. pool.log('xCheckReservedLock');
  154. pool.storeErr();
  155. wasm.poke32(pOut, 1);
  156. return 0;
  157. },
  158. xClose: function(pFile){
  159. const pool = getPoolForPFile(pFile);
  160. pool.storeErr();
  161. const file = pool.getOFileForS3File(pFile);
  162. if(file) {
  163. try{
  164. pool.log(`xClose ${file.path}`);
  165. pool.mapS3FileToOFile(pFile, false);
  166. file.sah.flush();
  167. if(file.flags & capi.SQLITE_OPEN_DELETEONCLOSE){
  168. pool.deletePath(file.path);
  169. }
  170. }catch(e){
  171. return pool.storeErr(e, capi.SQLITE_IOERR);
  172. }
  173. }
  174. return 0;
  175. },
  176. xDeviceCharacteristics: function(pFile){
  177. return capi.SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
  178. },
  179. xFileControl: function(pFile, opId, pArg){
  180. return capi.SQLITE_NOTFOUND;
  181. },
  182. xFileSize: function(pFile,pSz64){
  183. const pool = getPoolForPFile(pFile);
  184. pool.log(`xFileSize`);
  185. const file = pool.getOFileForS3File(pFile);
  186. const size = file.sah.getSize() - HEADER_OFFSET_DATA;
  187. //log(`xFileSize ${file.path} ${size}`);
  188. wasm.poke64(pSz64, BigInt(size));
  189. return 0;
  190. },
  191. xLock: function(pFile,lockType){
  192. const pool = getPoolForPFile(pFile);
  193. pool.log(`xLock ${lockType}`);
  194. pool.storeErr();
  195. const file = pool.getOFileForS3File(pFile);
  196. file.lockType = lockType;
  197. return 0;
  198. },
  199. xRead: function(pFile,pDest,n,offset64){
  200. const pool = getPoolForPFile(pFile);
  201. pool.storeErr();
  202. const file = pool.getOFileForS3File(pFile);
  203. pool.log(`xRead ${file.path} ${n} @ ${offset64}`);
  204. try {
  205. const nRead = file.sah.read(
  206. wasm.heap8u().subarray(pDest, pDest+n),
  207. {at: HEADER_OFFSET_DATA + Number(offset64)}
  208. );
  209. if(nRead < n){
  210. wasm.heap8u().fill(0, pDest + nRead, pDest + n);
  211. return capi.SQLITE_IOERR_SHORT_READ;
  212. }
  213. return 0;
  214. }catch(e){
  215. return pool.storeErr(e, capi.SQLITE_IOERR);
  216. }
  217. },
  218. xSectorSize: function(pFile){
  219. return SECTOR_SIZE;
  220. },
  221. xSync: function(pFile,flags){
  222. const pool = getPoolForPFile(pFile);
  223. pool.log(`xSync ${flags}`);
  224. pool.storeErr();
  225. const file = pool.getOFileForS3File(pFile);
  226. //log(`xSync ${file.path} ${flags}`);
  227. try{
  228. file.sah.flush();
  229. return 0;
  230. }catch(e){
  231. return pool.storeErr(e, capi.SQLITE_IOERR);
  232. }
  233. },
  234. xTruncate: function(pFile,sz64){
  235. const pool = getPoolForPFile(pFile);
  236. pool.log(`xTruncate ${sz64}`);
  237. pool.storeErr();
  238. const file = pool.getOFileForS3File(pFile);
  239. //log(`xTruncate ${file.path} ${iSize}`);
  240. try{
  241. file.sah.truncate(HEADER_OFFSET_DATA + Number(sz64));
  242. return 0;
  243. }catch(e){
  244. return pool.storeErr(e, capi.SQLITE_IOERR);
  245. }
  246. },
  247. xUnlock: function(pFile,lockType){
  248. const pool = getPoolForPFile(pFile);
  249. pool.log('xUnlock');
  250. const file = pool.getOFileForS3File(pFile);
  251. file.lockType = lockType;
  252. return 0;
  253. },
  254. xWrite: function(pFile,pSrc,n,offset64){
  255. const pool = getPoolForPFile(pFile);
  256. pool.storeErr();
  257. const file = pool.getOFileForS3File(pFile);
  258. pool.log(`xWrite ${file.path} ${n} ${offset64}`);
  259. try{
  260. const nBytes = file.sah.write(
  261. wasm.heap8u().subarray(pSrc, pSrc+n),
  262. { at: HEADER_OFFSET_DATA + Number(offset64) }
  263. );
  264. return n===nBytes ? 0 : toss("Unknown write() failure.");
  265. }catch(e){
  266. return pool.storeErr(e, capi.SQLITE_IOERR);
  267. }
  268. }
  269. }/*ioMethods*/;
  270. const opfsIoMethods = new capi.sqlite3_io_methods();
  271. opfsIoMethods.$iVersion = 1;
  272. sqlite3.vfs.installVfs({
  273. io: {struct: opfsIoMethods, methods: ioMethods}
  274. });
  275. /**
  276. Impls for the sqlite3_vfs methods. Maintenance reminder: members
  277. are in alphabetical order to simplify finding them.
  278. */
  279. const vfsMethods = {
  280. xAccess: function(pVfs,zName,flags,pOut){
  281. //log(`xAccess ${wasm.cstrToJs(zName)}`);
  282. const pool = getPoolForVfs(pVfs);
  283. pool.storeErr();
  284. try{
  285. const name = pool.getPath(zName);
  286. wasm.poke32(pOut, pool.hasFilename(name) ? 1 : 0);
  287. }catch(e){
  288. /*ignored*/
  289. wasm.poke32(pOut, 0);
  290. }
  291. return 0;
  292. },
  293. xCurrentTime: function(pVfs,pOut){
  294. wasm.poke(pOut, 2440587.5 + (new Date().getTime()/86400000),
  295. 'double');
  296. return 0;
  297. },
  298. xCurrentTimeInt64: function(pVfs,pOut){
  299. wasm.poke(pOut, (2440587.5 * 86400000) + new Date().getTime(),
  300. 'i64');
  301. return 0;
  302. },
  303. xDelete: function(pVfs, zName, doSyncDir){
  304. const pool = getPoolForVfs(pVfs);
  305. pool.log(`xDelete ${wasm.cstrToJs(zName)}`);
  306. pool.storeErr();
  307. try{
  308. pool.deletePath(pool.getPath(zName));
  309. return 0;
  310. }catch(e){
  311. pool.storeErr(e);
  312. return capi.SQLITE_IOERR_DELETE;
  313. }
  314. },
  315. xFullPathname: function(pVfs,zName,nOut,pOut){
  316. //const pool = getPoolForVfs(pVfs);
  317. //pool.log(`xFullPathname ${wasm.cstrToJs(zName)}`);
  318. const i = wasm.cstrncpy(pOut, zName, nOut);
  319. return i<nOut ? 0 : capi.SQLITE_CANTOPEN;
  320. },
  321. xGetLastError: function(pVfs,nOut,pOut){
  322. const pool = getPoolForVfs(pVfs);
  323. const e = pool.popErr();
  324. pool.log(`xGetLastError ${nOut} e =`,e);
  325. if(e){
  326. const scope = wasm.scopedAllocPush();
  327. try{
  328. const [cMsg, n] = wasm.scopedAllocCString(e.message, true);
  329. wasm.cstrncpy(pOut, cMsg, nOut);
  330. if(n > nOut) wasm.poke8(pOut + nOut - 1, 0);
  331. }catch(e){
  332. return capi.SQLITE_NOMEM;
  333. }finally{
  334. wasm.scopedAllocPop(scope);
  335. }
  336. }
  337. return e ? (e.sqlite3Rc || capi.SQLITE_IOERR) : 0;
  338. },
  339. //xSleep is optionally defined below
  340. xOpen: function f(pVfs, zName, pFile, flags, pOutFlags){
  341. const pool = getPoolForVfs(pVfs);
  342. try{
  343. flags &= ~FLAG_COMPUTE_DIGEST_V2;
  344. pool.log(`xOpen ${wasm.cstrToJs(zName)} ${flags}`);
  345. // First try to open a path that already exists in the file system.
  346. const path = (zName && wasm.peek8(zName))
  347. ? pool.getPath(zName)
  348. : getRandomName();
  349. let sah = pool.getSAHForPath(path);
  350. if(!sah && (flags & capi.SQLITE_OPEN_CREATE)) {
  351. // File not found so try to create it.
  352. if(pool.getFileCount() < pool.getCapacity()) {
  353. // Choose an unassociated OPFS file from the pool.
  354. sah = pool.nextAvailableSAH();
  355. pool.setAssociatedPath(sah, path, flags);
  356. }else{
  357. // File pool is full.
  358. toss('SAH pool is full. Cannot create file',path);
  359. }
  360. }
  361. if(!sah){
  362. toss('file not found:',path);
  363. }
  364. // Subsequent I/O methods are only passed the sqlite3_file
  365. // pointer, so map the relevant info we need to that pointer.
  366. const file = {path, flags, sah};
  367. pool.mapS3FileToOFile(pFile, file);
  368. file.lockType = capi.SQLITE_LOCK_NONE;
  369. const sq3File = new capi.sqlite3_file(pFile);
  370. sq3File.$pMethods = opfsIoMethods.pointer;
  371. sq3File.dispose();
  372. wasm.poke32(pOutFlags, flags);
  373. return 0;
  374. }catch(e){
  375. pool.storeErr(e);
  376. return capi.SQLITE_CANTOPEN;
  377. }
  378. }/*xOpen()*/
  379. }/*vfsMethods*/;
  380. /**
  381. Creates and initializes an sqlite3_vfs instance for an
  382. OpfsSAHPool. The argument is the VFS's name (JS string).
  383. Throws if the VFS name is already registered or if something
  384. goes terribly wrong via sqlite3.vfs.installVfs().
  385. Maintenance reminder: the only detail about the returned object
  386. which is specific to any given OpfsSAHPool instance is the $zName
  387. member. All other state is identical.
  388. */
  389. const createOpfsVfs = function(vfsName){
  390. if( sqlite3.capi.sqlite3_vfs_find(vfsName)){
  391. toss3("VFS name is already registered:", vfsName);
  392. }
  393. const opfsVfs = new capi.sqlite3_vfs();
  394. /* We fetch the default VFS so that we can inherit some
  395. methods from it. */
  396. const pDVfs = capi.sqlite3_vfs_find(null);
  397. const dVfs = pDVfs
  398. ? new capi.sqlite3_vfs(pDVfs)
  399. : null /* dVfs will be null when sqlite3 is built with
  400. SQLITE_OS_OTHER. */;
  401. opfsVfs.$iVersion = 2/*yes, two*/;
  402. opfsVfs.$szOsFile = capi.sqlite3_file.structInfo.sizeof;
  403. opfsVfs.$mxPathname = HEADER_MAX_PATH_SIZE;
  404. opfsVfs.addOnDispose(
  405. opfsVfs.$zName = wasm.allocCString(vfsName),
  406. ()=>setPoolForVfs(opfsVfs.pointer, 0)
  407. );
  408. if(dVfs){
  409. /* Inherit certain VFS members from the default VFS,
  410. if available. */
  411. opfsVfs.$xRandomness = dVfs.$xRandomness;
  412. opfsVfs.$xSleep = dVfs.$xSleep;
  413. dVfs.dispose();
  414. }
  415. if(!opfsVfs.$xRandomness && !vfsMethods.xRandomness){
  416. /* If the default VFS has no xRandomness(), add a basic JS impl... */
  417. vfsMethods.xRandomness = function(pVfs, nOut, pOut){
  418. const heap = wasm.heap8u();
  419. let i = 0;
  420. for(; i < nOut; ++i) heap[pOut + i] = (Math.random()*255000) & 0xFF;
  421. return i;
  422. };
  423. }
  424. if(!opfsVfs.$xSleep && !vfsMethods.xSleep){
  425. vfsMethods.xSleep = (pVfs,ms)=>0;
  426. }
  427. sqlite3.vfs.installVfs({
  428. vfs: {struct: opfsVfs, methods: vfsMethods}
  429. });
  430. return opfsVfs;
  431. };
  432. /**
  433. Class for managing OPFS-related state for the
  434. OPFS SharedAccessHandle Pool sqlite3_vfs.
  435. */
  436. class OpfsSAHPool {
  437. /* OPFS dir in which VFS metadata is stored. */
  438. vfsDir;
  439. /* Directory handle to this.vfsDir. */
  440. #dhVfsRoot;
  441. /* Directory handle to the subdir of this.#dhVfsRoot which holds
  442. the randomly-named "opaque" files. This subdir exists in the
  443. hope that we can eventually support client-created files in
  444. this.#dhVfsRoot. */
  445. #dhOpaque;
  446. /* Directory handle to this.dhVfsRoot's parent dir. Needed
  447. for a VFS-wipe op. */
  448. #dhVfsParent;
  449. /* Maps SAHs to their opaque file names. */
  450. #mapSAHToName = new Map();
  451. /* Maps client-side file names to SAHs. */
  452. #mapFilenameToSAH = new Map();
  453. /* Set of currently-unused SAHs. */
  454. #availableSAH = new Set();
  455. /* Maps (sqlite3_file*) to xOpen's file objects. */
  456. #mapS3FileToOFile_ = new Map();
  457. /* Maps SAH to an abstract File Object which contains
  458. various metadata about that handle. */
  459. //#mapSAHToMeta = new Map();
  460. /** Buffer used by [sg]etAssociatedPath(). */
  461. #apBody = new Uint8Array(HEADER_CORPUS_SIZE);
  462. // DataView for this.#apBody
  463. #dvBody;
  464. // associated sqlite3_vfs instance
  465. #cVfs;
  466. // Logging verbosity. See optionDefaults.verbosity.
  467. #verbosity;
  468. constructor(options = Object.create(null)){
  469. this.#verbosity = options.verbosity ?? optionDefaults.verbosity;
  470. this.vfsName = options.name || optionDefaults.name;
  471. this.#cVfs = createOpfsVfs(this.vfsName);
  472. setPoolForVfs(this.#cVfs.pointer, this);
  473. this.vfsDir = options.directory || ("."+this.vfsName);
  474. this.#dvBody =
  475. new DataView(this.#apBody.buffer, this.#apBody.byteOffset);
  476. this.isReady = this
  477. .reset(!!(options.clearOnInit ?? optionDefaults.clearOnInit))
  478. .then(()=>{
  479. if(this.$error) throw this.$error;
  480. return this.getCapacity()
  481. ? Promise.resolve(undefined)
  482. : this.addCapacity(options.initialCapacity
  483. || optionDefaults.initialCapacity);
  484. });
  485. }
  486. #logImpl(level,...args){
  487. if(this.#verbosity>level) loggers[level](this.vfsName+":",...args);
  488. };
  489. log(...args){this.#logImpl(2, ...args)};
  490. warn(...args){this.#logImpl(1, ...args)};
  491. error(...args){this.#logImpl(0, ...args)};
  492. getVfs(){return this.#cVfs}
  493. /* Current pool capacity. */
  494. getCapacity(){return this.#mapSAHToName.size}
  495. /* Current number of in-use files from pool. */
  496. getFileCount(){return this.#mapFilenameToSAH.size}
  497. /* Returns an array of the names of all
  498. currently-opened client-specified filenames. */
  499. getFileNames(){
  500. const rc = [];
  501. for(const n of this.#mapFilenameToSAH.keys()) rc.push(n);
  502. return rc;
  503. }
  504. /**
  505. Adds n files to the pool's capacity. This change is
  506. persistent across settings. Returns a Promise which resolves
  507. to the new capacity.
  508. */
  509. async addCapacity(n){
  510. for(let i = 0; i < n; ++i){
  511. const name = getRandomName();
  512. const h = await this.#dhOpaque.getFileHandle(name, {create:true});
  513. const ah = await h.createSyncAccessHandle();
  514. this.#mapSAHToName.set(ah,name);
  515. this.setAssociatedPath(ah, '', 0);
  516. //this.#createFileObject(ah,undefined,name);
  517. }
  518. return this.getCapacity();
  519. }
  520. /**
  521. Reduce capacity by n, but can only reduce up to the limit
  522. of currently-available SAHs. Returns a Promise which resolves
  523. to the number of slots really removed.
  524. */
  525. async reduceCapacity(n){
  526. let nRm = 0;
  527. for(const ah of Array.from(this.#availableSAH)){
  528. if(nRm === n || this.getFileCount() === this.getCapacity()){
  529. break;
  530. }
  531. const name = this.#mapSAHToName.get(ah);
  532. //this.#unmapFileObject(ah);
  533. ah.close();
  534. await this.#dhOpaque.removeEntry(name);
  535. this.#mapSAHToName.delete(ah);
  536. this.#availableSAH.delete(ah);
  537. ++nRm;
  538. }
  539. return nRm;
  540. }
  541. /**
  542. Releases all currently-opened SAHs. The only legal operation
  543. after this is acquireAccessHandles() or (if this is called from
  544. pauseVfs()) either of isPaused() or unpauseVfs().
  545. */
  546. releaseAccessHandles(){
  547. for(const ah of this.#mapSAHToName.keys()) ah.close();
  548. this.#mapSAHToName.clear();
  549. this.#mapFilenameToSAH.clear();
  550. this.#availableSAH.clear();
  551. }
  552. /**
  553. Opens all files under this.vfsDir/this.#dhOpaque and acquires a
  554. SAH for each. Returns a Promise which resolves to no value but
  555. completes once all SAHs are acquired. If acquiring an SAH
  556. throws, this.$error will contain the corresponding Error
  557. object.
  558. If it throws, it releases any SAHs which it may have
  559. acquired before the exception was thrown, leaving the VFS in a
  560. well-defined but unusable state.
  561. If clearFiles is true, the client-stored state of each file is
  562. cleared when its handle is acquired, including its name, flags,
  563. and any data stored after the metadata block.
  564. */
  565. async acquireAccessHandles(clearFiles=false){
  566. const files = [];
  567. for await (const [name,h] of this.#dhOpaque){
  568. if('file'===h.kind){
  569. files.push([name,h]);
  570. }
  571. }
  572. return Promise.all(files.map(async([name,h])=>{
  573. try{
  574. const ah = await h.createSyncAccessHandle()
  575. this.#mapSAHToName.set(ah, name);
  576. if(clearFiles){
  577. ah.truncate(HEADER_OFFSET_DATA);
  578. this.setAssociatedPath(ah, '', 0);
  579. }else{
  580. const path = this.getAssociatedPath(ah);
  581. if(path){
  582. this.#mapFilenameToSAH.set(path, ah);
  583. }else{
  584. this.#availableSAH.add(ah);
  585. }
  586. }
  587. }catch(e){
  588. this.storeErr(e);
  589. this.releaseAccessHandles();
  590. throw e;
  591. }
  592. }));
  593. }
  594. /**
  595. Given an SAH, returns the client-specified name of
  596. that file by extracting it from the SAH's header.
  597. On error, it disassociates SAH from the pool and
  598. returns an empty string.
  599. */
  600. getAssociatedPath(sah){
  601. sah.read(this.#apBody, {at: 0});
  602. // Delete any unexpected files left over by previous
  603. // untimely errors...
  604. const flags = this.#dvBody.getUint32(HEADER_OFFSET_FLAGS);
  605. if(this.#apBody[0] &&
  606. ((flags & capi.SQLITE_OPEN_DELETEONCLOSE) ||
  607. (flags & PERSISTENT_FILE_TYPES)===0)){
  608. warn(`Removing file with unexpected flags ${flags.toString(16)}`,
  609. this.#apBody);
  610. this.setAssociatedPath(sah, '', 0);
  611. return '';
  612. }
  613. const fileDigest = new Uint32Array(HEADER_DIGEST_SIZE / 4);
  614. sah.read(fileDigest, {at: HEADER_OFFSET_DIGEST});
  615. const compDigest = this.computeDigest(this.#apBody, flags);
  616. //warn("getAssociatedPath() flags",'0x'+flags.toString(16), "compDigest", compDigest);
  617. if(fileDigest.every((v,i) => v===compDigest[i])){
  618. // Valid digest
  619. const pathBytes = this.#apBody.findIndex((v)=>0===v);
  620. if(0===pathBytes){
  621. // This file is unassociated, so truncate it to avoid
  622. // leaving stale db data laying around.
  623. sah.truncate(HEADER_OFFSET_DATA);
  624. }
  625. //warn("getAssociatedPath() flags",'0x'+flags.toString(16), "compDigest", compDigest,"pathBytes",pathBytes);
  626. return pathBytes
  627. ? textDecoder.decode(this.#apBody.subarray(0,pathBytes))
  628. : '';
  629. }else{
  630. // Invalid digest
  631. warn('Disassociating file with bad digest.');
  632. this.setAssociatedPath(sah, '', 0);
  633. return '';
  634. }
  635. }
  636. /**
  637. Stores the given client-defined path and SQLITE_OPEN_xyz flags
  638. into the given SAH. If path is an empty string then the file is
  639. disassociated from the pool but its previous name is preserved
  640. in the metadata.
  641. */
  642. setAssociatedPath(sah, path, flags){
  643. const enc = textEncoder.encodeInto(path, this.#apBody);
  644. if(HEADER_MAX_PATH_SIZE <= enc.written + 1/*NUL byte*/){
  645. toss("Path too long:",path);
  646. }
  647. if(path && flags){
  648. /* When creating or re-writing files, update their digest, if
  649. needed, to v2. We continue to use v1 for the (!path) case
  650. (empty files) because there's little reason not to use a
  651. digest of 0 for empty entries. */
  652. flags |= FLAG_COMPUTE_DIGEST_V2;
  653. }
  654. this.#apBody.fill(0, enc.written, HEADER_MAX_PATH_SIZE);
  655. this.#dvBody.setUint32(HEADER_OFFSET_FLAGS, flags);
  656. const digest = this.computeDigest(this.#apBody, flags);
  657. //console.warn("setAssociatedPath(",path,") digest",digest);
  658. sah.write(this.#apBody, {at: 0});
  659. sah.write(digest, {at: HEADER_OFFSET_DIGEST});
  660. sah.flush();
  661. if(path){
  662. this.#mapFilenameToSAH.set(path, sah);
  663. this.#availableSAH.delete(sah);
  664. }else{
  665. // This is not a persistent file, so eliminate the contents.
  666. sah.truncate(HEADER_OFFSET_DATA);
  667. this.#availableSAH.add(sah);
  668. }
  669. }
  670. /**
  671. Computes a digest for the given byte array and returns it as a
  672. two-element Uint32Array. This digest gets stored in the
  673. metadata for each file as a validation check. Changing this
  674. algorithm invalidates all existing databases for this VFS, so
  675. don't do that.
  676. See the docs for FLAG_COMPUTE_DIGEST_V2 for more details.
  677. */
  678. computeDigest(byteArray, fileFlags){
  679. if( fileFlags & FLAG_COMPUTE_DIGEST_V2 ){
  680. let h1 = 0xdeadbeef;
  681. let h2 = 0x41c6ce57;
  682. for(const v of byteArray){
  683. h1 = Math.imul(h1 ^ v, 2654435761);
  684. h2 = Math.imul(h2 ^ v, 104729);
  685. }
  686. return new Uint32Array([h1>>>0, h2>>>0]);
  687. }else{
  688. /* this is what the buggy legacy computation worked out to */
  689. return new Uint32Array([0,0]);
  690. }
  691. }
  692. /**
  693. Re-initializes the state of the SAH pool, releasing and
  694. re-acquiring all handles.
  695. See acquireAccessHandles() for the specifics of the clearFiles
  696. argument.
  697. */
  698. async reset(clearFiles){
  699. await this.isReady;
  700. let h = await navigator.storage.getDirectory();
  701. let prev, prevName;
  702. for(const d of this.vfsDir.split('/')){
  703. if(d){
  704. prev = h;
  705. h = await h.getDirectoryHandle(d,{create:true});
  706. }
  707. }
  708. this.#dhVfsRoot = h;
  709. this.#dhVfsParent = prev;
  710. this.#dhOpaque = await this.#dhVfsRoot.getDirectoryHandle(
  711. OPAQUE_DIR_NAME,{create:true}
  712. );
  713. this.releaseAccessHandles();
  714. return this.acquireAccessHandles(clearFiles);
  715. }
  716. /**
  717. Returns the pathname part of the given argument,
  718. which may be any of:
  719. - a URL object
  720. - A JS string representing a file name
  721. - Wasm C-string representing a file name
  722. All "../" parts and duplicate slashes are resolve/removed from
  723. the returned result.
  724. */
  725. getPath(arg) {
  726. if(wasm.isPtr(arg)) arg = wasm.cstrToJs(arg);
  727. return ((arg instanceof URL)
  728. ? arg
  729. : new URL(arg, 'file://localhost/')).pathname;
  730. }
  731. /**
  732. Removes the association of the given client-specified file
  733. name (JS string) from the pool. Returns true if a mapping
  734. is found, else false.
  735. */
  736. deletePath(path) {
  737. const sah = this.#mapFilenameToSAH.get(path);
  738. if(sah) {
  739. // Un-associate the name from the SAH.
  740. this.#mapFilenameToSAH.delete(path);
  741. this.setAssociatedPath(sah, '', 0);
  742. }
  743. return !!sah;
  744. }
  745. /**
  746. Sets e (an Error object) as this object's current error. Pass a
  747. falsy (or no) value to clear it. If code is truthy it is
  748. assumed to be an SQLITE_xxx result code, defaulting to
  749. SQLITE_IOERR if code is falsy.
  750. Returns the 2nd argument.
  751. */
  752. storeErr(e,code){
  753. if(e){
  754. e.sqlite3Rc = code || capi.SQLITE_IOERR;
  755. this.error(e);
  756. }
  757. this.$error = e;
  758. return code;
  759. }
  760. /**
  761. Pops this object's Error object and returns
  762. it (a falsy value if no error is set).
  763. */
  764. popErr(){
  765. const rc = this.$error;
  766. this.$error = undefined;
  767. return rc;
  768. }
  769. /**
  770. Returns the next available SAH without removing
  771. it from the set.
  772. */
  773. nextAvailableSAH(){
  774. const [rc] = this.#availableSAH.keys();
  775. return rc;
  776. }
  777. /**
  778. Given an (sqlite3_file*), returns the mapped
  779. xOpen file object.
  780. */
  781. getOFileForS3File(pFile){
  782. return this.#mapS3FileToOFile_.get(pFile);
  783. }
  784. /**
  785. Maps or unmaps (if file is falsy) the given (sqlite3_file*)
  786. to an xOpen file object and to this pool object.
  787. */
  788. mapS3FileToOFile(pFile,file){
  789. if(file){
  790. this.#mapS3FileToOFile_.set(pFile, file);
  791. setPoolForPFile(pFile, this);
  792. }else{
  793. this.#mapS3FileToOFile_.delete(pFile);
  794. setPoolForPFile(pFile, false);
  795. }
  796. }
  797. /**
  798. Returns true if the given client-defined file name is in this
  799. object's name-to-SAH map.
  800. */
  801. hasFilename(name){
  802. return this.#mapFilenameToSAH.has(name)
  803. }
  804. /**
  805. Returns the SAH associated with the given
  806. client-defined file name.
  807. */
  808. getSAHForPath(path){
  809. return this.#mapFilenameToSAH.get(path);
  810. }
  811. /**
  812. Removes this object's sqlite3_vfs registration and shuts down
  813. this object, releasing all handles, mappings, and whatnot,
  814. including deleting its data directory. There is currently no
  815. way to "revive" the object and reaquire its
  816. resources. Similarly, there is no recovery strategy if removal
  817. of any given SAH fails, so such errors are ignored by this
  818. function.
  819. This function is intended primarily for testing.
  820. Resolves to true if it did its job, false if the
  821. VFS has already been shut down.
  822. @see pauseVfs()
  823. @see unpauseVfs()
  824. */
  825. async removeVfs(){
  826. if(!this.#cVfs.pointer || !this.#dhOpaque) return false;
  827. capi.sqlite3_vfs_unregister(this.#cVfs.pointer);
  828. this.#cVfs.dispose();
  829. delete initPromises[this.vfsName];
  830. try{
  831. this.releaseAccessHandles();
  832. await this.#dhVfsRoot.removeEntry(OPAQUE_DIR_NAME, {recursive: true});
  833. this.#dhOpaque = undefined;
  834. await this.#dhVfsParent.removeEntry(
  835. this.#dhVfsRoot.name, {recursive: true}
  836. );
  837. this.#dhVfsRoot = this.#dhVfsParent = undefined;
  838. }catch(e){
  839. sqlite3.config.error(this.vfsName,"removeVfs() failed with no recovery strategy:",e);
  840. /*otherwise ignored - there is no recovery strategy*/
  841. }
  842. return true;
  843. }
  844. /**
  845. "Pauses" this VFS by unregistering it from SQLite and
  846. relinquishing all open SAHs, leaving the associated files
  847. intact. If this object is already paused, this is a
  848. no-op. Returns this object.
  849. This function throws if SQLite has any opened file handles
  850. hosted by this VFS, as the alternative would be to invoke
  851. Undefined Behavior by closing file handles out from under the
  852. library. Similarly, automatically closing any database handles
  853. opened by this VFS would invoke Undefined Behavior in
  854. downstream code which is holding those pointers.
  855. If this function throws due to open file handles then it has
  856. no side effects. If the OPFS API throws while closing handles
  857. then the VFS is left in an undefined state.
  858. @see isPaused()
  859. @see unpauseVfs()
  860. */
  861. pauseVfs(){
  862. if(this.#mapS3FileToOFile_.size>0){
  863. sqlite3.SQLite3Error.toss(
  864. capi.SQLITE_MISUSE, "Cannot pause VFS",
  865. this.vfsName,"because it has opened files."
  866. );
  867. }
  868. if(this.#mapSAHToName.size>0){
  869. capi.sqlite3_vfs_unregister(this.vfsName);
  870. this.releaseAccessHandles();
  871. }
  872. return this;
  873. }
  874. /**
  875. Returns true if this pool is currently paused else false.
  876. @see pauseVfs()
  877. @see unpauseVfs()
  878. */
  879. isPaused(){
  880. return 0===this.#mapSAHToName.size;
  881. }
  882. /**
  883. "Unpauses" this VFS, reacquiring all SAH's and (if successful)
  884. re-registering it with SQLite. This is a no-op if the VFS is
  885. not currently paused.
  886. The returned Promise resolves to this object. See
  887. acquireAccessHandles() for how it behaves if it throws due to
  888. SAH acquisition failure.
  889. @see isPaused()
  890. @see pauseVfs()
  891. */
  892. async unpauseVfs(){
  893. if(0===this.#mapSAHToName.size){
  894. return this.acquireAccessHandles(false).
  895. then(()=>capi.sqlite3_vfs_register(this.#cVfs, 0),this);
  896. }
  897. return this;
  898. }
  899. //! Documented elsewhere in this file.
  900. exportFile(name){
  901. const sah = this.#mapFilenameToSAH.get(name) || toss("File not found:",name);
  902. const n = sah.getSize() - HEADER_OFFSET_DATA;
  903. const b = new Uint8Array(n>0 ? n : 0);
  904. if(n>0){
  905. const nRead = sah.read(b, {at: HEADER_OFFSET_DATA});
  906. if(nRead != n){
  907. toss("Expected to read "+n+" bytes but read "+nRead+".");
  908. }
  909. }
  910. return b;
  911. }
  912. //! Impl for importDb() when its 2nd arg is a function.
  913. async importDbChunked(name, callback){
  914. const sah = this.#mapFilenameToSAH.get(name)
  915. || this.nextAvailableSAH()
  916. || toss("No available handles to import to.");
  917. sah.truncate(0);
  918. let nWrote = 0, chunk, checkedHeader = false, err = false;
  919. try{
  920. while( undefined !== (chunk = await callback()) ){
  921. if(chunk instanceof ArrayBuffer) chunk = new Uint8Array(chunk);
  922. if( 0===nWrote && chunk.byteLength>=15 ){
  923. util.affirmDbHeader(chunk);
  924. checkedHeader = true;
  925. }
  926. sah.write(chunk, {at: HEADER_OFFSET_DATA + nWrote});
  927. nWrote += chunk.byteLength;
  928. }
  929. if( nWrote < 512 || 0!==nWrote % 512 ){
  930. toss("Input size",nWrote,"is not correct for an SQLite database.");
  931. }
  932. if( !checkedHeader ){
  933. const header = new Uint8Array(20);
  934. sah.read( header, {at: 0} );
  935. util.affirmDbHeader( header );
  936. }
  937. sah.write(new Uint8Array([1,1]), {
  938. at: HEADER_OFFSET_DATA + 18
  939. }/*force db out of WAL mode*/);
  940. }catch(e){
  941. this.setAssociatedPath(sah, '', 0);
  942. throw e;
  943. }
  944. this.setAssociatedPath(sah, name, capi.SQLITE_OPEN_MAIN_DB);
  945. return nWrote;
  946. }
  947. //! Documented elsewhere in this file.
  948. importDb(name, bytes){
  949. if( bytes instanceof ArrayBuffer ) bytes = new Uint8Array(bytes);
  950. else if( bytes instanceof Function ) return this.importDbChunked(name, bytes);
  951. const sah = this.#mapFilenameToSAH.get(name)
  952. || this.nextAvailableSAH()
  953. || toss("No available handles to import to.");
  954. const n = bytes.byteLength;
  955. if(n<512 || n%512!=0){
  956. toss("Byte array size is invalid for an SQLite db.");
  957. }
  958. const header = "SQLite format 3";
  959. for(let i = 0; i < header.length; ++i){
  960. if( header.charCodeAt(i) !== bytes[i] ){
  961. toss("Input does not contain an SQLite database header.");
  962. }
  963. }
  964. const nWrote = sah.write(bytes, {at: HEADER_OFFSET_DATA});
  965. if(nWrote != n){
  966. this.setAssociatedPath(sah, '', 0);
  967. toss("Expected to write "+n+" bytes but wrote "+nWrote+".");
  968. }else{
  969. sah.write(new Uint8Array([1,1]), {at: HEADER_OFFSET_DATA+18}
  970. /* force db out of WAL mode */);
  971. this.setAssociatedPath(sah, name, capi.SQLITE_OPEN_MAIN_DB);
  972. }
  973. return nWrote;
  974. }
  975. }/*class OpfsSAHPool*/;
  976. /**
  977. A OpfsSAHPoolUtil instance is exposed to clients in order to
  978. manipulate an OpfsSAHPool object without directly exposing that
  979. object and allowing for some semantic changes compared to that
  980. class.
  981. Class docs are in the client-level docs for
  982. installOpfsSAHPoolVfs().
  983. */
  984. class OpfsSAHPoolUtil {
  985. /* This object's associated OpfsSAHPool. */
  986. #p;
  987. constructor(sahPool){
  988. this.#p = sahPool;
  989. this.vfsName = sahPool.vfsName;
  990. }
  991. async addCapacity(n){ return this.#p.addCapacity(n) }
  992. async reduceCapacity(n){ return this.#p.reduceCapacity(n) }
  993. getCapacity(){ return this.#p.getCapacity(this.#p) }
  994. getFileCount(){ return this.#p.getFileCount() }
  995. getFileNames(){ return this.#p.getFileNames() }
  996. async reserveMinimumCapacity(min){
  997. const c = this.#p.getCapacity();
  998. return (c < min) ? this.#p.addCapacity(min - c) : c;
  999. }
  1000. exportFile(name){ return this.#p.exportFile(name) }
  1001. importDb(name, bytes){ return this.#p.importDb(name,bytes) }
  1002. async wipeFiles(){ return this.#p.reset(true) }
  1003. unlink(filename){ return this.#p.deletePath(filename) }
  1004. async removeVfs(){ return this.#p.removeVfs() }
  1005. pauseVfs(){ this.#p.pauseVfs(); return this; }
  1006. async unpauseVfs(){ return this.#p.unpauseVfs().then(()=>this); }
  1007. isPaused(){ return this.#p.isPaused() }
  1008. }/* class OpfsSAHPoolUtil */;
  1009. /**
  1010. Returns a resolved Promise if the current environment
  1011. has a "fully-sync" SAH impl, else a rejected Promise.
  1012. */
  1013. const apiVersionCheck = async ()=>{
  1014. const dh = await navigator.storage.getDirectory();
  1015. const fn = '.opfs-sahpool-sync-check-'+getRandomName();
  1016. const fh = await dh.getFileHandle(fn, { create: true });
  1017. const ah = await fh.createSyncAccessHandle();
  1018. const close = ah.close();
  1019. await close;
  1020. await dh.removeEntry(fn);
  1021. if(close?.then){
  1022. toss("The local OPFS API is too old for opfs-sahpool:",
  1023. "it has an async FileSystemSyncAccessHandle.close() method.");
  1024. }
  1025. return true;
  1026. };
  1027. /**
  1028. installOpfsSAHPoolVfs() asynchronously initializes the OPFS
  1029. SyncAccessHandle (a.k.a. SAH) Pool VFS. It returns a Promise which
  1030. either resolves to a utility object described below or rejects with
  1031. an Error value.
  1032. Initialization of this VFS is not automatic because its
  1033. registration requires that it lock all resources it
  1034. will potentially use, even if client code does not want
  1035. to use them. That, in turn, can lead to locking errors
  1036. when, for example, one page in a given origin has loaded
  1037. this VFS but does not use it, then another page in that
  1038. origin tries to use the VFS. If the VFS were automatically
  1039. registered, the second page would fail to load the VFS
  1040. due to OPFS locking errors.
  1041. If this function is called more than once with a given "name"
  1042. option (see below), it will return the same Promise. Calls for
  1043. different names will return different Promises which resolve to
  1044. independent objects and refer to different VFS registrations.
  1045. On success, the resulting Promise resolves to a utility object
  1046. which can be used to query and manipulate the pool. Its API is
  1047. described at the end of these docs.
  1048. This function accepts an options object to configure certain
  1049. parts but it is only acknowledged for the very first call and
  1050. ignored for all subsequent calls.
  1051. The options, in alphabetical order:
  1052. - `clearOnInit`: (default=false) if truthy, contents and filename
  1053. mapping are removed from each SAH it is acquired during
  1054. initialization of the VFS, leaving the VFS's storage in a pristine
  1055. state. Use this only for databases which need not survive a page
  1056. reload.
  1057. - `initialCapacity`: (default=6) Specifies the default capacity of
  1058. the VFS. This should not be set unduly high because the VFS has
  1059. to open (and keep open) a file for each entry in the pool. This
  1060. setting only has an effect when the pool is initially empty. It
  1061. does not have any effect if a pool already exists.
  1062. - `directory`: (default="."+`name`) Specifies the OPFS directory
  1063. name in which to store metadata for the `"opfs-sahpool"`
  1064. sqlite3_vfs. Only one instance of this VFS can be installed per
  1065. JavaScript engine, and any two engines with the same storage
  1066. directory name will collide with each other, leading to locking
  1067. errors and the inability to register the VFS in the second and
  1068. subsequent engine. Using a different directory name for each
  1069. application enables different engines in the same HTTP origin to
  1070. co-exist, but their data are invisible to each other. Changing
  1071. this name will effectively orphan any databases stored under
  1072. previous names. The default is unspecified but descriptive. This
  1073. option may contain multiple path elements, e.g. "foo/bar/baz",
  1074. and they are created automatically. In practice there should be
  1075. no driving need to change this. ACHTUNG: all files in this
  1076. directory are assumed to be managed by the VFS. Do not place
  1077. other files in that directory, as they may be deleted or
  1078. otherwise modified by the VFS.
  1079. - `name`: (default="opfs-sahpool") sets the name to register this
  1080. VFS under. Normally this should not be changed, but it is
  1081. possible to register this VFS under multiple names so long as
  1082. each has its own separate directory to work from. The storage for
  1083. each is invisible to all others. The name must be a string
  1084. compatible with `sqlite3_vfs_register()` and friends and suitable
  1085. for use in URI-style database file names.
  1086. Achtung: if a custom `name` is provided, a custom `directory`
  1087. must also be provided if any other instance is registered with
  1088. the default directory. If no directory is explicitly provided
  1089. then a directory name is synthesized from the `name` option.
  1090. - `forceReinitIfPreviouslyFailed`: (default=`false`) Is a fallback option
  1091. to assist in working around certain flaky environments which may
  1092. mysteriously fail to permit access to OPFS sync access handles on
  1093. an initial attempt but permit it on a second attemp. This option
  1094. should never be used but is provided for those who choose to
  1095. throw caution to the wind and trust such environments. If this
  1096. option is truthy _and_ the previous attempt to initialize this
  1097. VFS with the same `name` failed, the VFS will attempt to
  1098. initialize a second time instead of returning the cached
  1099. failure. See discussion at:
  1100. <https://github.com/sqlite/sqlite-wasm/issues/79>
  1101. Peculiarities of this VFS vis a vis other SQLite VFSes:
  1102. - Paths given to it _must_ be absolute. Relative paths will not
  1103. be properly recognized. This is arguably a bug but correcting it
  1104. requires some hoop-jumping in routines which have no business
  1105. doing such tricks.
  1106. - It is possible to install multiple instances under different
  1107. names, each sandboxed from one another inside their own private
  1108. directory. This feature exists primarily as a way for disparate
  1109. applications within a given HTTP origin to use this VFS without
  1110. introducing locking issues between them.
  1111. The API for the utility object passed on by this function's
  1112. Promise, in alphabetical order...
  1113. - [async] number addCapacity(n)
  1114. Adds `n` entries to the current pool. This change is persistent
  1115. across sessions so should not be called automatically at each app
  1116. startup (but see `reserveMinimumCapacity()`). Its returned Promise
  1117. resolves to the new capacity. Because this operation is necessarily
  1118. asynchronous, the C-level VFS API cannot call this on its own as
  1119. needed.
  1120. - byteArray exportFile(name)
  1121. Synchronously reads the contents of the given file into a Uint8Array
  1122. and returns it. This will throw if the given name is not currently
  1123. in active use or on I/O error. Note that the given name is _not_
  1124. visible directly in OPFS (or, if it is, it's not from this VFS).
  1125. - number getCapacity()
  1126. Returns the number of files currently contained
  1127. in the SAH pool. The default capacity is only large enough for one
  1128. or two databases and their associated temp files.
  1129. - number getFileCount()
  1130. Returns the number of files from the pool currently allocated to
  1131. slots. This is not the same as the files being "opened".
  1132. - array getFileNames()
  1133. Returns an array of the names of the files currently allocated to
  1134. slots. This list is the same length as getFileCount().
  1135. - void importDb(name, bytes)
  1136. Imports the contents of an SQLite database, provided as a byte
  1137. array or ArrayBuffer, under the given name, overwriting any
  1138. existing content. Throws if the pool has no available file slots,
  1139. on I/O error, or if the input does not appear to be a
  1140. database. In the latter case, only a cursory examination is made.
  1141. Results are undefined if the given db name refers to an opened
  1142. db. Note that this routine is _only_ for importing database
  1143. files, not arbitrary files, the reason being that this VFS will
  1144. automatically clean up any non-database files so importing them
  1145. is pointless.
  1146. If passed a function for its second argument, its behavior
  1147. changes to asynchronous and it imports its data in chunks fed to
  1148. it by the given callback function. It calls the callback (which
  1149. may be async) repeatedly, expecting either a Uint8Array or
  1150. ArrayBuffer (to denote new input) or undefined (to denote
  1151. EOF). For so long as the callback continues to return
  1152. non-undefined, it will append incoming data to the given
  1153. VFS-hosted database file. The result of the resolved Promise when
  1154. called this way is the size of the resulting database.
  1155. On success this routine rewrites the database header bytes in the
  1156. output file (not the input array) to force disabling of WAL mode.
  1157. On a write error, the handle is removed from the pool and made
  1158. available for re-use.
  1159. - [async] number reduceCapacity(n)
  1160. Removes up to `n` entries from the pool, with the caveat that it can
  1161. only remove currently-unused entries. It returns a Promise which
  1162. resolves to the number of entries actually removed.
  1163. - [async] boolean removeVfs()
  1164. Unregisters the opfs-sahpool VFS and removes its directory from OPFS
  1165. (which means that _all client content_ is removed). After calling
  1166. this, the VFS may no longer be used and there is no way to re-add it
  1167. aside from reloading the current JavaScript context.
  1168. Results are undefined if a database is currently in use with this
  1169. VFS.
  1170. The returned Promise resolves to true if it performed the removal
  1171. and false if the VFS was not installed.
  1172. If the VFS has a multi-level directory, e.g. "/foo/bar/baz", _only_
  1173. the bottom-most directory is removed because this VFS cannot know for
  1174. certain whether the higher-level directories contain data which
  1175. should be removed.
  1176. - [async] number reserveMinimumCapacity(min)
  1177. If the current capacity is less than `min`, the capacity is
  1178. increased to `min`, else this returns with no side effects. The
  1179. resulting Promise resolves to the new capacity.
  1180. - boolean unlink(filename)
  1181. If a virtual file exists with the given name, disassociates it from
  1182. the pool and returns true, else returns false without side
  1183. effects. Results are undefined if the file is currently in active
  1184. use.
  1185. - string vfsName
  1186. The SQLite VFS name under which this pool's VFS is registered.
  1187. - [async] void wipeFiles()
  1188. Clears all client-defined state of all SAHs and makes all of them
  1189. available for re-use by the pool. Results are undefined if any such
  1190. handles are currently in use, e.g. by an sqlite3 db.
  1191. APIs specific to the "pause" capability (added in version 3.49):
  1192. Summary: "pausing" the VFS disassociates it from SQLite and
  1193. relinquishes its SAHs so that they may be opened by another
  1194. instance of this VFS (running in a separate tab/page or Worker).
  1195. "Unpausing" it takes back control, if able.
  1196. - pauseVfs()
  1197. "Pauses" this VFS by unregistering it from SQLite and
  1198. relinquishing all open SAHs, leaving the associated files intact.
  1199. This enables pages/tabs to coordinate semi-concurrent usage of
  1200. this VFS. If this object is already paused, this is a
  1201. no-op. Returns this object. Throws if SQLite has any opened file
  1202. handles hosted by this VFS. If this function throws due to open
  1203. file handles then it has no side effects. If the OPFS API throws
  1204. while closing handles then the VFS is left in an undefined state.
  1205. - isPaused()
  1206. Returns true if this VFS is paused, else false.
  1207. - [async] unpauseVfs()
  1208. Restores the VFS to an active state after having called
  1209. pauseVfs() on it. This is a no-op if the VFS is not paused. The
  1210. returned Promise resolves to this object on success. A rejected
  1211. Promise means there was a problem reacquiring the SAH handles
  1212. (possibly because they're in use by another instance or have
  1213. since been removed). Generically speaking, there is no recovery
  1214. strategy for that type of error, but if the problem is simply
  1215. that the OPFS files are locked, then a later attempt to unpause
  1216. it, made after the concurrent instance releases the SAHs, may
  1217. recover from the situation.
  1218. */
  1219. sqlite3.installOpfsSAHPoolVfs = async function(options=Object.create(null)){
  1220. options = Object.assign(Object.create(null), optionDefaults, (options||{}));
  1221. const vfsName = options.name;
  1222. if(options.$testThrowPhase1){
  1223. throw options.$testThrowPhase1;
  1224. }
  1225. if(initPromises[vfsName]){
  1226. try {
  1227. const p = await initPromises[vfsName];
  1228. //log("installOpfsSAHPoolVfs() returning cached result",options,vfsName,p);
  1229. return p;
  1230. }catch(e){
  1231. //log("installOpfsSAHPoolVfs() got cached failure",options,vfsName,e);
  1232. if( options.forceReinitIfPreviouslyFailed ){
  1233. delete initPromises[vfsName];
  1234. /* Fall through and try again. */
  1235. }else{
  1236. throw e;
  1237. }
  1238. }
  1239. }
  1240. if(!globalThis.FileSystemHandle ||
  1241. !globalThis.FileSystemDirectoryHandle ||
  1242. !globalThis.FileSystemFileHandle ||
  1243. !globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle ||
  1244. !navigator?.storage?.getDirectory){
  1245. return (initPromises[vfsName] = Promise.reject(new Error("Missing required OPFS APIs.")));
  1246. }
  1247. /**
  1248. Maintenance reminder: the order of ASYNC ops in this function
  1249. is significant. We need to have them all chained at the very
  1250. end in order to be able to catch a race condition where
  1251. installOpfsSAHPoolVfs() is called twice in rapid succession,
  1252. e.g.:
  1253. installOpfsSAHPoolVfs().then(console.warn.bind(console));
  1254. installOpfsSAHPoolVfs().then(console.warn.bind(console));
  1255. If the timing of the async calls is not "just right" then that
  1256. second call can end up triggering the init a second time and chaos
  1257. ensues.
  1258. */
  1259. return initPromises[vfsName] = apiVersionCheck().then(async function(){
  1260. if(options.$testThrowPhase2){
  1261. throw options.$testThrowPhase2;
  1262. }
  1263. const thePool = new OpfsSAHPool(options);
  1264. return thePool.isReady.then(async()=>{
  1265. /** The poolUtil object will be the result of the
  1266. resolved Promise. */
  1267. const poolUtil = new OpfsSAHPoolUtil(thePool);
  1268. if(sqlite3.oo1){
  1269. const oo1 = sqlite3.oo1;
  1270. const theVfs = thePool.getVfs();
  1271. const OpfsSAHPoolDb = function(...args){
  1272. const opt = oo1.DB.dbCtorHelper.normalizeArgs(...args);
  1273. opt.vfs = theVfs.$zName;
  1274. oo1.DB.dbCtorHelper.call(this, opt);
  1275. };
  1276. OpfsSAHPoolDb.prototype = Object.create(oo1.DB.prototype);
  1277. poolUtil.OpfsSAHPoolDb = OpfsSAHPoolDb;
  1278. }/*extend sqlite3.oo1*/
  1279. thePool.log("VFS initialized.");
  1280. return poolUtil;
  1281. }).catch(async (e)=>{
  1282. await thePool.removeVfs().catch(()=>{});
  1283. throw e;
  1284. });
  1285. }).catch((err)=>{
  1286. //error("rejecting promise:",err);
  1287. return initPromises[vfsName] = Promise.reject(err);
  1288. });
  1289. }/*installOpfsSAHPoolVfs()*/;
  1290. }/*sqlite3ApiBootstrap.initializers*/);
  1291. //#else
  1292. /*
  1293. The OPFS SAH Pool VFS parts are elided from builds targeting
  1294. node.js.
  1295. */
  1296. //#endif target=node