@powersync/web 1.31.0 → 1.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js +1878 -0
  2. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js.map +1 -0
  3. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530150.index.umd.js +555 -0
  4. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530150.index.umd.js.map +1 -0
  5. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530151.index.umd.js +555 -0
  6. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530151.index.umd.js.map +1 -0
  7. package/dist/index.umd.js +4700 -43409
  8. package/dist/index.umd.js.map +1 -1
  9. package/dist/worker/SharedSyncImplementation.umd.js +4187 -4919
  10. package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
  11. package/dist/worker/WASQLiteDB.umd.js +4206 -4953
  12. package/dist/worker/WASQLiteDB.umd.js.map +1 -1
  13. package/dist/worker/{node_modules_bson_lib_bson_mjs.umd.js → node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js} +6 -6
  14. package/dist/worker/node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js.map +1 -0
  15. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-3a94cf.umd.js +44 -0
  16. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-3a94cf.umd.js.map +1 -0
  17. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-868779.umd.js +44 -0
  18. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-868779.umd.js.map +1 -0
  19. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-f60d0d.umd.js +44 -0
  20. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-f60d0d.umd.js.map +1 -0
  21. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +44 -0
  22. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +1 -0
  23. package/dist/worker/{node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-0d2437.umd.js} +24 -24
  24. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-0d2437.umd.js.map +1 -0
  25. package/dist/worker/{node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-1d4e74.umd.js} +18 -18
  26. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-1d4e74.umd.js.map +1 -0
  27. package/dist/worker/{node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-3622cf.umd.js} +18 -18
  28. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-3622cf.umd.js.map +1 -0
  29. package/lib/package.json +26 -22
  30. package/lib/src/attachments/IndexDBFileSystemAdapter.d.ts +25 -0
  31. package/lib/src/attachments/IndexDBFileSystemAdapter.js +104 -0
  32. package/lib/src/db/NavigatorTriggerClaimManager.d.ts +6 -0
  33. package/lib/src/db/NavigatorTriggerClaimManager.js +20 -0
  34. package/lib/src/db/PowerSyncDatabase.d.ts +5 -2
  35. package/lib/src/db/PowerSyncDatabase.js +46 -8
  36. package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.d.ts +1 -1
  37. package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js +1 -1
  38. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.d.ts +2 -2
  39. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +2 -2
  40. package/lib/src/db/adapters/AsyncDatabaseConnection.d.ts +1 -1
  41. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +4 -4
  42. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +29 -14
  43. package/lib/src/db/adapters/WebDBAdapter.d.ts +5 -2
  44. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.d.ts +2 -2
  45. package/lib/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.d.ts +12 -0
  46. package/lib/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.js +19 -0
  47. package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.d.ts +2 -2
  48. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +4 -4
  49. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +6 -6
  50. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +4 -4
  51. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +7 -7
  52. package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.d.ts +1 -1
  53. package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js +3 -3
  54. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +5 -5
  55. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +5 -5
  56. package/lib/src/db/sync/WebRemote.js +1 -1
  57. package/lib/src/db/sync/WebStreamingSyncImplementation.d.ts +1 -1
  58. package/lib/src/db/sync/WebStreamingSyncImplementation.js +1 -1
  59. package/lib/src/index.d.ts +13 -12
  60. package/lib/src/index.js +13 -12
  61. package/lib/src/worker/db/SharedWASQLiteConnection.d.ts +2 -2
  62. package/lib/src/worker/db/WASQLiteDB.worker.js +3 -3
  63. package/lib/src/worker/db/WorkerWASQLiteConnection.d.ts +2 -2
  64. package/lib/src/worker/db/WorkerWASQLiteConnection.js +1 -1
  65. package/lib/src/worker/db/open-worker-database.d.ts +2 -2
  66. package/lib/src/worker/db/open-worker-database.js +1 -1
  67. package/lib/src/worker/sync/BroadcastLogger.d.ts +1 -1
  68. package/lib/src/worker/sync/SharedSyncImplementation.d.ts +4 -4
  69. package/lib/src/worker/sync/SharedSyncImplementation.js +5 -5
  70. package/lib/src/worker/sync/SharedSyncImplementation.worker.js +2 -2
  71. package/lib/src/worker/sync/WorkerClient.d.ts +1 -1
  72. package/lib/src/worker/sync/WorkerClient.js +2 -2
  73. package/lib/tsconfig.tsbuildinfo +1 -1
  74. package/package.json +21 -17
  75. package/src/attachments/IndexDBFileSystemAdapter.ts +117 -0
  76. package/src/db/NavigatorTriggerClaimManager.ts +23 -0
  77. package/src/db/PowerSyncDatabase.ts +53 -9
  78. package/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.ts +1 -1
  79. package/src/db/adapters/AbstractWebSQLOpenFactory.ts +3 -3
  80. package/src/db/adapters/AsyncDatabaseConnection.ts +1 -1
  81. package/src/db/adapters/LockedAsyncDatabaseAdapter.ts +32 -20
  82. package/src/db/adapters/WebDBAdapter.ts +6 -2
  83. package/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.ts +2 -2
  84. package/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.ts +23 -0
  85. package/src/db/adapters/wa-sqlite/WASQLiteConnection.ts +2 -2
  86. package/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.ts +8 -8
  87. package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +8 -8
  88. package/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.ts +3 -3
  89. package/src/db/sync/SharedWebStreamingSyncImplementation.ts +7 -7
  90. package/src/db/sync/WebRemote.ts +1 -1
  91. package/src/db/sync/WebStreamingSyncImplementation.ts +2 -2
  92. package/src/index.ts +13 -12
  93. package/src/worker/db/SharedWASQLiteConnection.ts +2 -2
  94. package/src/worker/db/WASQLiteDB.worker.ts +5 -5
  95. package/src/worker/db/WorkerWASQLiteConnection.ts +2 -2
  96. package/src/worker/db/open-worker-database.ts +2 -2
  97. package/src/worker/sync/BroadcastLogger.ts +1 -1
  98. package/src/worker/sync/SharedSyncImplementation.ts +9 -9
  99. package/src/worker/sync/SharedSyncImplementation.worker.ts +2 -2
  100. package/src/worker/sync/WorkerClient.ts +2 -2
  101. package/dist/worker/node_modules_bson_lib_bson_mjs.umd.js.map +0 -1
  102. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js +0 -44
  103. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js.map +0 -1
  104. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js +0 -44
  105. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js.map +0 -1
  106. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js +0 -44
  107. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js.map +0 -1
  108. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +0 -44
  109. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +0 -1
  110. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js.map +0 -1
  111. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js.map +0 -1
  112. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js.map +0 -1
  113. /package/bin/{powersync.js → powersync.cjs} +0 -0
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js","mappings":";;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC/sBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;AC7NA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","sources":["webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/FacadeVFS.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/VFS.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js"],"sourcesContent":["// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport * as VFS from './VFS.js';\n\nconst AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;\n\n// Milliseconds since Julian epoch as a BigInt.\n// https://github.com/sqlite/sqlite/blob/e57527c14f7b7cfa6e32eeab5c549d50c4fa3674/src/os_unix.c#L6872-L6882\nconst UNIX_EPOCH = 24405875n * 8640000n;\n\n// Convenience base class for a JavaScript VFS.\n// The raw xOpen, xRead, etc. function signatures receive only C primitives\n// which aren't easy to work with. This class provides corresponding calls\n// like jOpen, jRead, etc., which receive JavaScript-friendlier arguments\n// such as string, Uint8Array, and DataView.\nexport class FacadeVFS extends VFS.Base {\n /**\n * @param {string} name \n * @param {object} module \n */\n constructor(name, module) {\n super(name, module);\n }\n\n /**\n * Override to indicate which methods are asynchronous.\n * @param {string} methodName \n * @returns {boolean}\n */\n hasAsyncMethod(methodName) {\n // The input argument is a string like \"xOpen\", so convert to \"jOpen\".\n // Then check if the method exists and is async.\n const jMethodName = `j${methodName.slice(1)}`;\n return this[jMethodName] instanceof AsyncFunction;\n }\n \n /**\n * Return the filename for a file id for use by mixins.\n * @param {number} pFile \n * @returns {string}\n */\n getFilename(pFile) {\n throw new Error('unimplemented');\n }\n\n /**\n * @param {string?} filename \n * @param {number} pFile \n * @param {number} flags \n * @param {DataView} pOutFlags \n * @returns {number|Promise<number>}\n */\n jOpen(filename, pFile, flags, pOutFlags) {\n return VFS.SQLITE_CANTOPEN;\n }\n\n /**\n * @param {string} filename \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n jDelete(filename, syncDir) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} filename \n * @param {number} flags \n * @param {DataView} pResOut \n * @returns {number|Promise<number>}\n */\n jAccess(filename, flags, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} filename \n * @param {Uint8Array} zOut \n * @returns {number|Promise<number>}\n */\n jFullPathname(filename, zOut) {\n // Copy the filename to the output buffer.\n const { read, written } = new TextEncoder().encodeInto(filename, zOut);\n if (read < filename.length) return VFS.SQLITE_IOERR;\n if (written >= zOut.length) return VFS.SQLITE_IOERR;\n zOut[written] = 0;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {Uint8Array} zBuf \n * @returns {number|Promise<number>}\n */\n jGetLastError(zBuf) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n jClose(pFile) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {Uint8Array} pData \n * @param {number} iOffset \n * @returns {number|Promise<number>}\n */\n jRead(pFile, pData, iOffset) {\n pData.fill(0);\n return VFS.SQLITE_IOERR_SHORT_READ;\n }\n\n /**\n * @param {number} pFile \n * @param {Uint8Array} pData \n * @param {number} iOffset \n * @returns {number|Promise<number>}\n */\n jWrite(pFile, pData, iOffset) {\n return VFS.SQLITE_IOERR_WRITE;\n }\n\n /**\n * @param {number} pFile \n * @param {number} size \n * @returns {number|Promise<number>}\n */\n jTruncate(pFile, size) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n jSync(pFile, flags) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pSize\n * @returns {number|Promise<number>}\n */\n jFileSize(pFile, pSize) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jLock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jUnlock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pResOut \n * @returns {number|Promise<number>}\n */\n jCheckReservedLock(pFile, pResOut) {\n pResOut.setInt32(0, 0, true);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number|Promise<number>}\n */\n jFileControl(pFile, op, pArg) {\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jSectorSize(pFile) {\n return super.xSectorSize(pFile);\n }\n\n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jDeviceCharacteristics(pFile) {\n return 0;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} pFile \n * @param {number} flags \n * @param {number} pOutFlags \n * @returns {number|Promise<number>}\n */\n xOpen(pVfs, zName, pFile, flags, pOutFlags) {\n const filename = this.#decodeFilename(zName, flags);\n const pOutFlagsView = this.#makeTypedDataView('Int32', pOutFlags);\n this['log']?.('jOpen', filename, pFile, '0x' + flags.toString(16));\n return this.jOpen(filename, pFile, flags, pOutFlagsView);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nByte \n * @param {number} pCharOut\n * @returns {number|Promise<number>}\n */\n xRandomness(pVfs, nByte, pCharOut) {\n const randomArray = new Uint8Array(nByte);\n crypto.getRandomValues(randomArray);\n // Copy randomArray to the WebAssembly memory\n const buffer = pCharOut; // Pointer to memory in WebAssembly\n this._module.HEAPU8.set(randomArray, buffer); // Copy randomArray into memory starting at buffer\n return nByte;\n }\n\n /**\n * Gets the current time as milliseconds since Unix epoch\n * @param {number} pVfs pointer to the VFS\n * @param {number} pTime pointer to write the time value\n * @returns {number} SQLite error code\n */\n xCurrentTimeInt64(pVfs, pTime) {\n // Create a DataView to write the current time\n const timeView = this.#makeTypedDataView('BigInt64', pTime);\n \n const currentTime = BigInt(Date.now());\n // Convert the current time to milliseconds since Unix epoch\n const value = UNIX_EPOCH + currentTime;\n \n // Write the time value to the pointer location\n timeView.setBigInt64(0, value, true);\n \n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n xDelete(pVfs, zName, syncDir) {\n const filename = this._module.UTF8ToString(zName);\n this['log']?.('jDelete', filename, syncDir);\n return this.jDelete(filename, syncDir);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} flags \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xAccess(pVfs, zName, flags, pResOut) {\n const filename = this._module.UTF8ToString(zName);\n const pResOutView = this.#makeTypedDataView('Int32', pResOut);\n this['log']?.('jAccess', filename, flags);\n return this.jAccess(filename, flags, pResOutView);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} nOut \n * @param {number} zOut \n * @returns {number|Promise<number>}\n */\n xFullPathname(pVfs, zName, nOut, zOut) {\n const filename = this._module.UTF8ToString(zName);\n const zOutArray = this._module.HEAPU8.subarray(zOut, zOut + nOut);\n this['log']?.('jFullPathname', filename, nOut);\n return this.jFullPathname(filename, zOutArray);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nBuf \n * @param {number} zBuf \n * @returns {number|Promise<number>}\n */\n xGetLastError(pVfs, nBuf, zBuf) {\n const zBufArray = this._module.HEAPU8.subarray(zBuf, zBuf + nBuf);\n this['log']?.('jGetLastError', nBuf);\n return this.jGetLastError(zBufArray);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xClose(pFile) {\n this['log']?.('jClose', pFile);\n return this.jClose(pFile);\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xRead(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n const pDataArray = this.#makeDataArray(pData, iAmt);\n const iOffset = delegalize(iOffsetLo, iOffsetHi);\n this['log']?.('jRead', pFile, iAmt, iOffset);\n return this.jRead(pFile, pDataArray, iOffset);\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xWrite(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n const pDataArray = this.#makeDataArray(pData, iAmt);\n const iOffset = delegalize(iOffsetLo, iOffsetHi);\n this['log']?.('jWrite', pFile, pDataArray, iOffset);\n return this.jWrite(pFile, pDataArray, iOffset);\n }\n\n /**\n * @param {number} pFile \n * @param {number} sizeLo \n * @param {number} sizeHi \n * @returns {number|Promise<number>}\n */\n xTruncate(pFile, sizeLo, sizeHi) {\n const size = delegalize(sizeLo, sizeHi);\n this['log']?.('jTruncate', pFile, size);\n return this.jTruncate(pFile, size);\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n xSync(pFile, flags) {\n this['log']?.('jSync', pFile, flags);\n return this.jSync(pFile, flags);\n }\n\n /**\n * \n * @param {number} pFile \n * @param {number} pSize \n * @returns {number|Promise<number>}\n */\n xFileSize(pFile, pSize) {\n const pSizeView = this.#makeTypedDataView('BigInt64', pSize);\n this['log']?.('jFileSize', pFile);\n return this.jFileSize(pFile, pSizeView);\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xLock(pFile, lockType) {\n this['log']?.('jLock', pFile, lockType);\n return this.jLock(pFile, lockType);\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xUnlock(pFile, lockType) {\n this['log']?.('jUnlock', pFile, lockType);\n return this.jUnlock(pFile, lockType);\n } \n\n /**\n * @param {number} pFile \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xCheckReservedLock(pFile, pResOut) {\n const pResOutView = this.#makeTypedDataView('Int32', pResOut);\n this['log']?.('jCheckReservedLock', pFile);\n return this.jCheckReservedLock(pFile, pResOutView);\n }\n\n /**\n * @param {number} pFile \n * @param {number} op \n * @param {number} pArg \n * @returns {number|Promise<number>}\n */\n xFileControl(pFile, op, pArg) {\n const pArgView = new DataView(\n this._module.HEAPU8.buffer,\n this._module.HEAPU8.byteOffset + pArg);\n this['log']?.('jFileControl', pFile, op, pArgView);\n return this.jFileControl(pFile, op, pArgView);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xSectorSize(pFile) {\n this['log']?.('jSectorSize', pFile);\n return this.jSectorSize(pFile);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xDeviceCharacteristics(pFile) {\n this['log']?.('jDeviceCharacteristics', pFile);\n return this.jDeviceCharacteristics(pFile);\n }\n\n /**\n * Wrapped DataView for pointer arguments.\n * Pointers to a single value are passed using a DataView-like class.\n * This wrapper class prevents use of incorrect type or endianness, and\n * reacquires the underlying buffer when the WebAssembly memory is resized.\n * @param {'Int32'|'BigInt64'} type \n * @param {number} byteOffset \n * @returns {DataView}\n */\n #makeTypedDataView(type, byteOffset) {\n // @ts-ignore\n return new DataViewProxy(this._module, byteOffset, type);\n }\n\n /**\n * Wrapped Uint8Array for buffer arguments.\n * Memory blocks are passed as a Uint8Array-like class. This wrapper\n * class reacquires the underlying buffer when the WebAssembly memory\n * is resized.\n * @param {number} byteOffset \n * @param {number} byteLength \n * @returns {Uint8Array}\n */\n #makeDataArray(byteOffset, byteLength) {\n // @ts-ignore\n return new Uint8ArrayProxy(this._module, byteOffset, byteLength);\n }\n\n #decodeFilename(zName, flags) {\n if (flags & VFS.SQLITE_OPEN_URI) {\n // The first null-terminated string is the URI path. Subsequent\n // strings are query parameter keys and values.\n // https://www.sqlite.org/c3ref/open.html#urifilenamesinsqlite3open\n let pName = zName;\n let state = 1;\n const charCodes = [];\n while (state) {\n const charCode = this._module.HEAPU8[pName++];\n if (charCode) {\n charCodes.push(charCode);\n } else {\n if (!this._module.HEAPU8[pName]) state = null;\n switch (state) {\n case 1: // path\n charCodes.push('?'.charCodeAt(0));\n state = 2;\n break;\n case 2: // key\n charCodes.push('='.charCodeAt(0));\n state = 3;\n break;\n case 3: // value\n charCodes.push('&'.charCodeAt(0));\n state = 2;\n break;\n }\n }\n }\n return new TextDecoder().decode(new Uint8Array(charCodes));\n }\n return zName ? this._module.UTF8ToString(zName) : null;\n }\n}\n\n// Emscripten \"legalizes\" 64-bit integer arguments by passing them as\n// two 32-bit signed integers.\nfunction delegalize(lo32, hi32) {\n return (hi32 * 0x100000000) + lo32 + (lo32 < 0 ? 2**32 : 0);\n}\n\n// This class provides a Uint8Array-like interface for a WebAssembly memory\n// buffer. It is used to access memory blocks passed as arguments to\n// xRead, xWrite, etc. The class reacquires the underlying buffer when the\n// WebAssembly memory is resized, which can happen when the memory is\n// detached and resized by the WebAssembly module.\n//\n// Note that although this class implements the same methods as Uint8Array,\n// it is not a real Uint8Array and passing it to functions that expect\n// a Uint8Array may not work. Use subarray() to get a real Uint8Array\n// if needed.\nclass Uint8ArrayProxy {\n #module;\n\n #_array = new Uint8Array()\n get #array() {\n if (this.#_array.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer so re-create the\n // array with the new buffer.\n this.#_array = this.#module.HEAPU8.subarray(\n this.byteOffset,\n this.byteOffset + this.byteLength);\n }\n return this.#_array;\n }\n\n /**\n * @param {*} module\n * @param {number} byteOffset \n * @param {number} byteLength \n */\n constructor(module, byteOffset, byteLength) {\n this.#module = module;\n this.byteOffset = byteOffset;\n this.length = this.byteLength = byteLength;\n }\n\n get buffer() {\n return this.#array.buffer;\n }\n\n at(index) {\n return this.#array.at(index);\n }\n copyWithin(target, start, end) {\n this.#array.copyWithin(target, start, end);\n }\n entries() {\n return this.#array.entries();\n }\n every(predicate) {\n return this.#array.every(predicate);\n }\n fill(value, start, end) {\n this.#array.fill(value, start, end);\n }\n filter(predicate) {\n return this.#array.filter(predicate);\n }\n find(predicate) {\n return this.#array.find(predicate);\n }\n findIndex(predicate) {\n return this.#array.findIndex(predicate);\n }\n findLast(predicate) {\n return this.#array.findLast(predicate);\n }\n findLastIndex(predicate) {\n return this.#array.findLastIndex(predicate);\n }\n forEach(callback) {\n this.#array.forEach(callback);\n }\n includes(value, start) {\n return this.#array.includes(value, start);\n }\n indexOf(value, start) {\n return this.#array.indexOf(value, start);\n }\n join(separator) {\n return this.#array.join(separator);\n }\n keys() {\n return this.#array.keys();\n }\n lastIndexOf(value, start) {\n return this.#array.lastIndexOf(value, start);\n }\n map(callback) {\n return this.#array.map(callback);\n }\n reduce(callback, initialValue) {\n return this.#array.reduce(callback, initialValue);\n }\n reduceRight(callback, initialValue) {\n return this.#array.reduceRight(callback, initialValue);\n }\n reverse() {\n this.#array.reverse();\n }\n set(array, offset) {\n this.#array.set(array, offset);\n }\n slice(start, end) {\n return this.#array.slice(start, end);\n }\n some(predicate) {\n return this.#array.some(predicate);\n }\n sort(compareFn) {\n this.#array.sort(compareFn);\n }\n subarray(begin, end) {\n return this.#array.subarray(begin, end);\n }\n toLocaleString(locales, options) {\n // @ts-ignore\n return this.#array.toLocaleString(locales, options);\n }\n toReversed() {\n return this.#array.toReversed();\n }\n toSorted(compareFn) {\n return this.#array.toSorted(compareFn);\n }\n toString() {\n return this.#array.toString();\n }\n values() {\n return this.#array.values();\n }\n with(index, value) {\n return this.#array.with(index, value);\n }\n [Symbol.iterator]() {\n return this.#array[Symbol.iterator]();\n }\n}\n\n// This class provides a DataView-like interface for a WebAssembly memory\n// buffer, restricted to either Int32 or BigInt64 types. It also reacquires\n// the underlying buffer when the WebAssembly memory is resized, which can\n// happen when the memory is detached and resized by the WebAssembly module.\nclass DataViewProxy {\n #module;\n #type;\n\n #_view = new DataView(new ArrayBuffer(0));\n get #view() {\n if (this.#_view.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer so re-create the\n // view with the new buffer.\n this.#_view = new DataView(\n this.#module.HEAPU8.buffer,\n this.#module.HEAPU8.byteOffset + this.byteOffset);\n }\n return this.#_view;\n }\n\n /**\n * @param {*} module\n * @param {number} byteOffset \n * @param {'Int32'|'BigInt64'} type\n */\n constructor(module, byteOffset, type) {\n this.#module = module;\n this.byteOffset = byteOffset;\n this.#type = type;\n }\n\n get buffer() {\n return this.#view.buffer;\n }\n get byteLength() {\n return this.#type === 'Int32' ? 4 : 8;\n }\n\n getInt32(byteOffset, littleEndian) {\n if (this.#type !== 'Int32') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n return this.#view.getInt32(byteOffset, littleEndian);\n }\n setInt32(byteOffset, value, littleEndian) {\n if (this.#type !== 'Int32') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n this.#view.setInt32(byteOffset, value, littleEndian);\n }\n getBigInt64(byteOffset, littleEndian) {\n if (this.#type !== 'BigInt64') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n return this.#view.getBigInt64(byteOffset, littleEndian);\n }\n setBigInt64(byteOffset, value, littleEndian) {\n if (this.#type !== 'BigInt64') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n this.#view.setBigInt64(byteOffset, value, littleEndian);\n }\n}","// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport * as VFS from './sqlite-constants.js';\nexport * from './sqlite-constants.js';\n\nconst DEFAULT_SECTOR_SIZE = 512;\n\n// Base class for a VFS.\nexport class Base {\n name;\n mxPathname = 64;\n _module;\n\n /**\n * @param {string} name \n * @param {object} module \n */\n constructor(name, module) {\n this.name = name;\n this._module = module;\n }\n\n /**\n * @returns {void|Promise<void>} \n */\n close() {\n }\n\n /**\n * @returns {boolean|Promise<boolean>}\n */\n isReady() {\n return true;\n }\n\n /**\n * Overload in subclasses to indicate which methods are asynchronous.\n * @param {string} methodName \n * @returns {boolean}\n */\n hasAsyncMethod(methodName) {\n return false;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} pFile \n * @param {number} flags \n * @param {number} pOutFlags \n * @returns {number|Promise<number>}\n */\n xOpen(pVfs, zName, pFile, flags, pOutFlags) {\n return VFS.SQLITE_CANTOPEN;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n xDelete(pVfs, zName, syncDir) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} flags \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xAccess(pVfs, zName, flags, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} nOut \n * @param {number} zOut \n * @returns {number|Promise<number>}\n */\n xFullPathname(pVfs, zName, nOut, zOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nBuf \n * @param {number} zBuf \n * @returns {number|Promise<number>}\n */\n xGetLastError(pVfs, nBuf, zBuf) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xClose(pFile) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xRead(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xWrite(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} sizeLo \n * @param {number} sizeHi \n * @returns {number|Promise<number>}\n */\n xTruncate(pFile, sizeLo, sizeHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n xSync(pFile, flags) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * \n * @param {number} pFile \n * @param {number} pSize \n * @returns {number|Promise<number>}\n */\n xFileSize(pFile, pSize) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xLock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xUnlock(pFile, lockType) {\n return VFS.SQLITE_OK;\n } \n\n /**\n * @param {number} pFile \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xCheckReservedLock(pFile, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} op \n * @param {number} pArg \n * @returns {number|Promise<number>}\n */\n xFileControl(pFile, op, pArg) {\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xSectorSize(pFile) {\n return DEFAULT_SECTOR_SIZE;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xDeviceCharacteristics(pFile) {\n return 0;\n }\n}\n\nexport const FILE_TYPE_MASK = [\n VFS.SQLITE_OPEN_MAIN_DB,\n VFS.SQLITE_OPEN_MAIN_JOURNAL,\n VFS.SQLITE_OPEN_TEMP_DB,\n VFS.SQLITE_OPEN_TEMP_JOURNAL,\n VFS.SQLITE_OPEN_TRANSIENT_DB,\n VFS.SQLITE_OPEN_SUBJOURNAL,\n VFS.SQLITE_OPEN_SUPER_JOURNAL,\n VFS.SQLITE_OPEN_WAL\n].reduce((mask, element) => mask | element);","// Copyright 2023 Roy T. Hashimoto. All Rights Reserved.\nimport { FacadeVFS } from '../FacadeVFS.js';\nimport * as VFS from '../VFS.js';\n\nconst SECTOR_SIZE = 4096;\n\n// Each OPFS file begins with a fixed-size header with metadata. The\n// contents of the file follow immediately after the header.\nconst HEADER_MAX_PATH_SIZE = 512;\nconst HEADER_FLAGS_SIZE = 4;\nconst HEADER_DIGEST_SIZE = 8;\nconst HEADER_CORPUS_SIZE = HEADER_MAX_PATH_SIZE + HEADER_FLAGS_SIZE;\nconst HEADER_OFFSET_FLAGS = HEADER_MAX_PATH_SIZE;\nconst HEADER_OFFSET_DIGEST = HEADER_CORPUS_SIZE;\nconst HEADER_OFFSET_DATA = SECTOR_SIZE;\n\n// These file types are expected to persist in the file system outside\n// a session. Other files will be removed on VFS start.\nconst PERSISTENT_FILE_TYPES =\n VFS.SQLITE_OPEN_MAIN_DB |\n VFS.SQLITE_OPEN_MAIN_JOURNAL |\n VFS.SQLITE_OPEN_SUPER_JOURNAL |\n VFS.SQLITE_OPEN_WAL;\n\nconst DEFAULT_CAPACITY = 6;\n\n/**\n * This VFS uses the updated Access Handle API with all synchronous methods\n * on FileSystemSyncAccessHandle (instead of just read and write). It will\n * work with the regular SQLite WebAssembly build, i.e. the one without\n * Asyncify.\n */\nexport class AccessHandlePoolVFS extends FacadeVFS {\n log = null; //function(...args) { console.log(`[${contextName}]`, ...args) };\n\n // All the OPFS files the VFS uses are contained in one flat directory\n // specified in the constructor. No other files should be written here.\n #directoryPath;\n #directoryHandle;\n\n // The OPFS files all have randomly-generated names that do not match\n // the SQLite files whose data they contain. This map links those names\n // with their respective OPFS access handles.\n #mapAccessHandleToName = new Map();\n\n // When a SQLite file is associated with an OPFS file, that association\n // is kept in #mapPathToAccessHandle. Each access handle is in exactly\n // one of #mapPathToAccessHandle or #availableAccessHandles.\n #mapPathToAccessHandle = new Map();\n #availableAccessHandles = new Set();\n\n #mapIdToFile = new Map();\n\n static async create(name, module) {\n const vfs = new AccessHandlePoolVFS(name, module);\n await vfs.isReady();\n return vfs;\n }\n \n constructor(name, module) {\n super(name, module);\n this.#directoryPath = name;\n }\n\n /**\n * @param {string?} zName \n * @param {number} fileId \n * @param {number} flags \n * @param {DataView} pOutFlags \n * @returns {number}\n */\n jOpen(zName, fileId, flags, pOutFlags) {\n try {\n // First try to open a path that already exists in the file system.\n const path = zName ? this.#getPath(zName) : Math.random().toString(36);\n let accessHandle = this.#mapPathToAccessHandle.get(path);\n if (!accessHandle && (flags & VFS.SQLITE_OPEN_CREATE)) {\n // File not found so try to create it.\n if (this.getSize() < this.getCapacity()) {\n // Choose an unassociated OPFS file from the pool.\n ([accessHandle] = this.#availableAccessHandles.keys());\n this.#setAssociatedPath(accessHandle, path, flags);\n } else {\n // Out of unassociated files. This can be fixed by calling\n // addCapacity() from the application.\n throw new Error('cannot create file');\n }\n }\n if (!accessHandle) {\n throw new Error('file not found');\n }\n // Subsequent methods are only passed the fileId, so make sure we have\n // a way to get the file resources.\n const file = { path, flags, accessHandle };\n this.#mapIdToFile.set(fileId, file);\n\n pOutFlags.setInt32(0, flags, true);\n return VFS.SQLITE_OK;\n } catch (e) {\n console.error(e.message);\n return VFS.SQLITE_CANTOPEN;\n }\n }\n\n /**\n * @param {number} fileId \n * @returns {number}\n */\n jClose(fileId) {\n const file = this.#mapIdToFile.get(fileId);\n if (file) {\n file.accessHandle.flush();\n this.#mapIdToFile.delete(fileId);\n if (file.flags & VFS.SQLITE_OPEN_DELETEONCLOSE) {\n this.#deletePath(file.path);\n }\n }\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} fileId \n * @param {Uint8Array} pData \n * @param {number} iOffset\n * @returns {number}\n */\n jRead(fileId, pData, iOffset) {\n const file = this.#mapIdToFile.get(fileId);\n const nBytes = file.accessHandle.read(\n pData.subarray(),\n { at: HEADER_OFFSET_DATA + iOffset });\n if (nBytes < pData.byteLength) {\n pData.fill(0, nBytes, pData.byteLength);\n return VFS.SQLITE_IOERR_SHORT_READ;\n }\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} fileId \n * @param {Uint8Array} pData \n * @param {number} iOffset\n * @returns {number}\n */\n jWrite(fileId, pData, iOffset) {\n const file = this.#mapIdToFile.get(fileId);\n const nBytes = file.accessHandle.write(\n pData.subarray(),\n { at: HEADER_OFFSET_DATA + iOffset });\n return nBytes === pData.byteLength ? VFS.SQLITE_OK : VFS.SQLITE_IOERR;\n }\n\n /**\n * @param {number} fileId \n * @param {number} iSize \n * @returns {number}\n */\n jTruncate(fileId, iSize) {\n const file = this.#mapIdToFile.get(fileId);\n file.accessHandle.truncate(HEADER_OFFSET_DATA + iSize);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} fileId \n * @param {number} flags \n * @returns {number}\n */\n jSync(fileId, flags) {\n const file = this.#mapIdToFile.get(fileId);\n file.accessHandle.flush();\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} fileId \n * @param {DataView} pSize64 \n * @returns {number}\n */\n jFileSize(fileId, pSize64) {\n const file = this.#mapIdToFile.get(fileId);\n const size = file.accessHandle.getSize() - HEADER_OFFSET_DATA;\n pSize64.setBigInt64(0, BigInt(size), true);\n return VFS.SQLITE_OK;\n }\n\n jSectorSize(fileId) {\n return SECTOR_SIZE;\n }\n\n jDeviceCharacteristics(fileId) {\n return VFS.SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;\n }\n\n /**\n * @param {string} zName \n * @param {number} flags \n * @param {DataView} pResOut \n * @returns {number}\n */\n jAccess(zName, flags, pResOut) {\n const path = this.#getPath(zName);\n pResOut.setInt32(0, this.#mapPathToAccessHandle.has(path) ? 1 : 0, true);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} zName \n * @param {number} syncDir \n * @returns {number}\n */\n jDelete(zName, syncDir) {\n const path = this.#getPath(zName);\n this.#deletePath(path);\n return VFS.SQLITE_OK;\n }\n\n async close() {\n await this.#releaseAccessHandles();\n }\n\n async isReady() {\n if (!this.#directoryHandle) {\n // All files are stored in a single directory.\n let handle = await navigator.storage.getDirectory();\n for (const d of this.#directoryPath.split('/')) {\n if (d) {\n handle = await handle.getDirectoryHandle(d, { create: true });\n }\n }\n this.#directoryHandle = handle;\n\n await this.#acquireAccessHandles();\n if (this.getCapacity() === 0) {\n await this.addCapacity(DEFAULT_CAPACITY);\n }\n }\n return true;\n }\n\n /**\n * Returns the number of SQLite files in the file system.\n * @returns {number}\n */\n getSize() {\n return this.#mapPathToAccessHandle.size;\n }\n\n /**\n * Returns the maximum number of SQLite files the file system can hold.\n * @returns {number}\n */\n getCapacity() {\n return this.#mapAccessHandleToName.size;\n }\n\n /**\n * Increase the capacity of the file system by n.\n * @param {number} n \n * @returns {Promise<number>} \n */\n async addCapacity(n) {\n for (let i = 0; i < n; ++i) {\n const name = Math.random().toString(36).replace('0.', '');\n const handle = await this.#directoryHandle.getFileHandle(name, { create: true });\n const accessHandle = await handle.createSyncAccessHandle();\n this.#mapAccessHandleToName.set(accessHandle, name);\n\n this.#setAssociatedPath(accessHandle, '', 0);\n }\n return n;\n }\n\n /**\n * Decrease the capacity of the file system by n. The capacity cannot be\n * decreased to fewer than the current number of SQLite files in the\n * file system.\n * @param {number} n \n * @returns {Promise<number>}\n */\n async removeCapacity(n) {\n let nRemoved = 0;\n for (const accessHandle of Array.from(this.#availableAccessHandles)) {\n if (nRemoved == n || this.getSize() === this.getCapacity()) return nRemoved;\n\n const name = this.#mapAccessHandleToName.get(accessHandle);\n await accessHandle.close();\n await this.#directoryHandle.removeEntry(name);\n this.#mapAccessHandleToName.delete(accessHandle);\n this.#availableAccessHandles.delete(accessHandle);\n ++nRemoved;\n }\n return nRemoved;\n }\n\n async #acquireAccessHandles() {\n // Enumerate all the files in the directory.\n const files = [];\n for await (const [name, handle] of this.#directoryHandle) {\n if (handle.kind === 'file') {\n files.push([name, handle]);\n }\n }\n\n // Open access handles in parallel, separating associated and unassociated.\n await Promise.all(files.map(async ([name, handle]) => {\n const accessHandle = await handle.createSyncAccessHandle();\n this.#mapAccessHandleToName.set(accessHandle, name);\n const path = this.#getAssociatedPath(accessHandle);\n if (path) {\n this.#mapPathToAccessHandle.set(path, accessHandle);\n } else {\n this.#availableAccessHandles.add(accessHandle);\n }\n }));\n }\n\n #releaseAccessHandles() {\n for (const accessHandle of this.#mapAccessHandleToName.keys()) {\n accessHandle.close();\n }\n this.#mapAccessHandleToName.clear();\n this.#mapPathToAccessHandle.clear();\n this.#availableAccessHandles.clear();\n }\n\n /**\n * Read and return the associated path from an OPFS file header.\n * Empty string is returned for an unassociated OPFS file.\n * @param accessHandle FileSystemSyncAccessHandle\n * @returns {string} path or empty string\n */\n #getAssociatedPath(accessHandle) {\n // Read the path and digest of the path from the file.\n const corpus = new Uint8Array(HEADER_CORPUS_SIZE);\n accessHandle.read(corpus, { at: 0 })\n\n // Delete files not expected to be present.\n const dataView = new DataView(corpus.buffer, corpus.byteOffset);\n const flags = dataView.getUint32(HEADER_OFFSET_FLAGS);\n if (corpus[0] &&\n ((flags & VFS.SQLITE_OPEN_DELETEONCLOSE) ||\n (flags & PERSISTENT_FILE_TYPES) === 0)) {\n console.warn(`Remove file with unexpected flags ${flags.toString(16)}`);\n this.#setAssociatedPath(accessHandle, '', 0);\n return '';\n }\n\n const fileDigest = new Uint32Array(HEADER_DIGEST_SIZE / 4);\n accessHandle.read(fileDigest, { at: HEADER_OFFSET_DIGEST });\n\n // Verify the digest.\n const computedDigest = this.#computeDigest(corpus);\n if (fileDigest.every((value, i) => value === computedDigest[i])) {\n // Good digest. Decode the null-terminated path string.\n const pathBytes = corpus.findIndex(value => value === 0);\n if (pathBytes === 0) {\n // Ensure that unassociated files are empty. Unassociated files are\n // truncated in #setAssociatedPath after the header is written. If\n // an interruption occurs right before the truncation then garbage\n // may remain in the file.\n accessHandle.truncate(HEADER_OFFSET_DATA);\n }\n return new TextDecoder().decode(corpus.subarray(0, pathBytes));\n } else {\n // Bad digest. Repair this header.\n console.warn('Disassociating file with bad digest.');\n this.#setAssociatedPath(accessHandle, '', 0);\n return '';\n }\n }\n\n /**\n * Set the path on an OPFS file header.\n * @param accessHandle FileSystemSyncAccessHandle\n * @param {string} path\n * @param {number} flags\n */\n #setAssociatedPath(accessHandle, path, flags) {\n // Convert the path string to UTF-8.\n const corpus = new Uint8Array(HEADER_CORPUS_SIZE);\n const encodedResult = new TextEncoder().encodeInto(path, corpus);\n if (encodedResult.written >= HEADER_MAX_PATH_SIZE) {\n throw new Error('path too long');\n }\n\n // Add the creation flags.\n const dataView = new DataView(corpus.buffer, corpus.byteOffset);\n dataView.setUint32(HEADER_OFFSET_FLAGS, flags);\n\n // Write the OPFS file header, including the digest.\n const digest = this.#computeDigest(corpus);\n accessHandle.write(corpus, { at: 0 });\n accessHandle.write(digest, { at: HEADER_OFFSET_DIGEST });\n accessHandle.flush();\n\n if (path) {\n this.#mapPathToAccessHandle.set(path, accessHandle);\n this.#availableAccessHandles.delete(accessHandle);\n } else {\n // This OPFS file doesn't represent any SQLite file so it doesn't\n // need to keep any data.\n accessHandle.truncate(HEADER_OFFSET_DATA);\n this.#availableAccessHandles.add(accessHandle);\n }\n }\n\n /**\n * We need a synchronous digest function so can't use WebCrypto.\n * Adapted from https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js\n * @param {Uint8Array} corpus \n * @returns {ArrayBuffer} 64-bit digest\n */\n #computeDigest(corpus) {\n if (!corpus[0]) {\n // Optimization for deleted file.\n return new Uint32Array([0xfecc5f80, 0xaccec037]);\n }\n\n let h1 = 0xdeadbeef;\n let h2 = 0x41c6ce57;\n \n for (const value of corpus) {\n h1 = Math.imul(h1 ^ value, 2654435761);\n h2 = Math.imul(h2 ^ value, 1597334677);\n }\n \n h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);\n h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);\n \n return new Uint32Array([h1 >>> 0, h2 >>> 0]);\n };\n \n /**\n * Convert a bare filename, path, or URL to a UNIX-style path.\n * @param {string|URL} nameOrURL\n * @returns {string} path\n */\n #getPath(nameOrURL) {\n const url = typeof nameOrURL === 'string' ?\n new URL(nameOrURL, 'file://localhost/') :\n nameOrURL;\n return url.pathname;\n }\n\n /**\n * Remove the association between a path and an OPFS file.\n * @param {string} path \n */\n #deletePath(path) {\n const accessHandle = this.#mapPathToAccessHandle.get(path);\n if (accessHandle) {\n // Un-associate the SQLite path from the OPFS file.\n this.#mapPathToAccessHandle.delete(path);\n this.#setAssociatedPath(accessHandle, '', 0);\n }\n }\n}"],"names":[],"ignoreList":[],"sourceRoot":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js","mappings":";;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC/sBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;AC7NA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;AC1ZA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","sources":["webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/FacadeVFS.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/VFS.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/WebLocksMixin.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js"],"sourcesContent":["// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport * as VFS from './VFS.js';\n\nconst AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;\n\n// Milliseconds since Julian epoch as a BigInt.\n// https://github.com/sqlite/sqlite/blob/e57527c14f7b7cfa6e32eeab5c549d50c4fa3674/src/os_unix.c#L6872-L6882\nconst UNIX_EPOCH = 24405875n * 8640000n;\n\n// Convenience base class for a JavaScript VFS.\n// The raw xOpen, xRead, etc. function signatures receive only C primitives\n// which aren't easy to work with. This class provides corresponding calls\n// like jOpen, jRead, etc., which receive JavaScript-friendlier arguments\n// such as string, Uint8Array, and DataView.\nexport class FacadeVFS extends VFS.Base {\n /**\n * @param {string} name \n * @param {object} module \n */\n constructor(name, module) {\n super(name, module);\n }\n\n /**\n * Override to indicate which methods are asynchronous.\n * @param {string} methodName \n * @returns {boolean}\n */\n hasAsyncMethod(methodName) {\n // The input argument is a string like \"xOpen\", so convert to \"jOpen\".\n // Then check if the method exists and is async.\n const jMethodName = `j${methodName.slice(1)}`;\n return this[jMethodName] instanceof AsyncFunction;\n }\n \n /**\n * Return the filename for a file id for use by mixins.\n * @param {number} pFile \n * @returns {string}\n */\n getFilename(pFile) {\n throw new Error('unimplemented');\n }\n\n /**\n * @param {string?} filename \n * @param {number} pFile \n * @param {number} flags \n * @param {DataView} pOutFlags \n * @returns {number|Promise<number>}\n */\n jOpen(filename, pFile, flags, pOutFlags) {\n return VFS.SQLITE_CANTOPEN;\n }\n\n /**\n * @param {string} filename \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n jDelete(filename, syncDir) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} filename \n * @param {number} flags \n * @param {DataView} pResOut \n * @returns {number|Promise<number>}\n */\n jAccess(filename, flags, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} filename \n * @param {Uint8Array} zOut \n * @returns {number|Promise<number>}\n */\n jFullPathname(filename, zOut) {\n // Copy the filename to the output buffer.\n const { read, written } = new TextEncoder().encodeInto(filename, zOut);\n if (read < filename.length) return VFS.SQLITE_IOERR;\n if (written >= zOut.length) return VFS.SQLITE_IOERR;\n zOut[written] = 0;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {Uint8Array} zBuf \n * @returns {number|Promise<number>}\n */\n jGetLastError(zBuf) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n jClose(pFile) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {Uint8Array} pData \n * @param {number} iOffset \n * @returns {number|Promise<number>}\n */\n jRead(pFile, pData, iOffset) {\n pData.fill(0);\n return VFS.SQLITE_IOERR_SHORT_READ;\n }\n\n /**\n * @param {number} pFile \n * @param {Uint8Array} pData \n * @param {number} iOffset \n * @returns {number|Promise<number>}\n */\n jWrite(pFile, pData, iOffset) {\n return VFS.SQLITE_IOERR_WRITE;\n }\n\n /**\n * @param {number} pFile \n * @param {number} size \n * @returns {number|Promise<number>}\n */\n jTruncate(pFile, size) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n jSync(pFile, flags) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pSize\n * @returns {number|Promise<number>}\n */\n jFileSize(pFile, pSize) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jLock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jUnlock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pResOut \n * @returns {number|Promise<number>}\n */\n jCheckReservedLock(pFile, pResOut) {\n pResOut.setInt32(0, 0, true);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number|Promise<number>}\n */\n jFileControl(pFile, op, pArg) {\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jSectorSize(pFile) {\n return super.xSectorSize(pFile);\n }\n\n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jDeviceCharacteristics(pFile) {\n return 0;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} pFile \n * @param {number} flags \n * @param {number} pOutFlags \n * @returns {number|Promise<number>}\n */\n xOpen(pVfs, zName, pFile, flags, pOutFlags) {\n const filename = this.#decodeFilename(zName, flags);\n const pOutFlagsView = this.#makeTypedDataView('Int32', pOutFlags);\n this['log']?.('jOpen', filename, pFile, '0x' + flags.toString(16));\n return this.jOpen(filename, pFile, flags, pOutFlagsView);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nByte \n * @param {number} pCharOut\n * @returns {number|Promise<number>}\n */\n xRandomness(pVfs, nByte, pCharOut) {\n const randomArray = new Uint8Array(nByte);\n crypto.getRandomValues(randomArray);\n // Copy randomArray to the WebAssembly memory\n const buffer = pCharOut; // Pointer to memory in WebAssembly\n this._module.HEAPU8.set(randomArray, buffer); // Copy randomArray into memory starting at buffer\n return nByte;\n }\n\n /**\n * Gets the current time as milliseconds since Unix epoch\n * @param {number} pVfs pointer to the VFS\n * @param {number} pTime pointer to write the time value\n * @returns {number} SQLite error code\n */\n xCurrentTimeInt64(pVfs, pTime) {\n // Create a DataView to write the current time\n const timeView = this.#makeTypedDataView('BigInt64', pTime);\n \n const currentTime = BigInt(Date.now());\n // Convert the current time to milliseconds since Unix epoch\n const value = UNIX_EPOCH + currentTime;\n \n // Write the time value to the pointer location\n timeView.setBigInt64(0, value, true);\n \n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n xDelete(pVfs, zName, syncDir) {\n const filename = this._module.UTF8ToString(zName);\n this['log']?.('jDelete', filename, syncDir);\n return this.jDelete(filename, syncDir);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} flags \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xAccess(pVfs, zName, flags, pResOut) {\n const filename = this._module.UTF8ToString(zName);\n const pResOutView = this.#makeTypedDataView('Int32', pResOut);\n this['log']?.('jAccess', filename, flags);\n return this.jAccess(filename, flags, pResOutView);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} nOut \n * @param {number} zOut \n * @returns {number|Promise<number>}\n */\n xFullPathname(pVfs, zName, nOut, zOut) {\n const filename = this._module.UTF8ToString(zName);\n const zOutArray = this._module.HEAPU8.subarray(zOut, zOut + nOut);\n this['log']?.('jFullPathname', filename, nOut);\n return this.jFullPathname(filename, zOutArray);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nBuf \n * @param {number} zBuf \n * @returns {number|Promise<number>}\n */\n xGetLastError(pVfs, nBuf, zBuf) {\n const zBufArray = this._module.HEAPU8.subarray(zBuf, zBuf + nBuf);\n this['log']?.('jGetLastError', nBuf);\n return this.jGetLastError(zBufArray);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xClose(pFile) {\n this['log']?.('jClose', pFile);\n return this.jClose(pFile);\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xRead(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n const pDataArray = this.#makeDataArray(pData, iAmt);\n const iOffset = delegalize(iOffsetLo, iOffsetHi);\n this['log']?.('jRead', pFile, iAmt, iOffset);\n return this.jRead(pFile, pDataArray, iOffset);\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xWrite(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n const pDataArray = this.#makeDataArray(pData, iAmt);\n const iOffset = delegalize(iOffsetLo, iOffsetHi);\n this['log']?.('jWrite', pFile, pDataArray, iOffset);\n return this.jWrite(pFile, pDataArray, iOffset);\n }\n\n /**\n * @param {number} pFile \n * @param {number} sizeLo \n * @param {number} sizeHi \n * @returns {number|Promise<number>}\n */\n xTruncate(pFile, sizeLo, sizeHi) {\n const size = delegalize(sizeLo, sizeHi);\n this['log']?.('jTruncate', pFile, size);\n return this.jTruncate(pFile, size);\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n xSync(pFile, flags) {\n this['log']?.('jSync', pFile, flags);\n return this.jSync(pFile, flags);\n }\n\n /**\n * \n * @param {number} pFile \n * @param {number} pSize \n * @returns {number|Promise<number>}\n */\n xFileSize(pFile, pSize) {\n const pSizeView = this.#makeTypedDataView('BigInt64', pSize);\n this['log']?.('jFileSize', pFile);\n return this.jFileSize(pFile, pSizeView);\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xLock(pFile, lockType) {\n this['log']?.('jLock', pFile, lockType);\n return this.jLock(pFile, lockType);\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xUnlock(pFile, lockType) {\n this['log']?.('jUnlock', pFile, lockType);\n return this.jUnlock(pFile, lockType);\n } \n\n /**\n * @param {number} pFile \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xCheckReservedLock(pFile, pResOut) {\n const pResOutView = this.#makeTypedDataView('Int32', pResOut);\n this['log']?.('jCheckReservedLock', pFile);\n return this.jCheckReservedLock(pFile, pResOutView);\n }\n\n /**\n * @param {number} pFile \n * @param {number} op \n * @param {number} pArg \n * @returns {number|Promise<number>}\n */\n xFileControl(pFile, op, pArg) {\n const pArgView = new DataView(\n this._module.HEAPU8.buffer,\n this._module.HEAPU8.byteOffset + pArg);\n this['log']?.('jFileControl', pFile, op, pArgView);\n return this.jFileControl(pFile, op, pArgView);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xSectorSize(pFile) {\n this['log']?.('jSectorSize', pFile);\n return this.jSectorSize(pFile);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xDeviceCharacteristics(pFile) {\n this['log']?.('jDeviceCharacteristics', pFile);\n return this.jDeviceCharacteristics(pFile);\n }\n\n /**\n * Wrapped DataView for pointer arguments.\n * Pointers to a single value are passed using a DataView-like class.\n * This wrapper class prevents use of incorrect type or endianness, and\n * reacquires the underlying buffer when the WebAssembly memory is resized.\n * @param {'Int32'|'BigInt64'} type \n * @param {number} byteOffset \n * @returns {DataView}\n */\n #makeTypedDataView(type, byteOffset) {\n // @ts-ignore\n return new DataViewProxy(this._module, byteOffset, type);\n }\n\n /**\n * Wrapped Uint8Array for buffer arguments.\n * Memory blocks are passed as a Uint8Array-like class. This wrapper\n * class reacquires the underlying buffer when the WebAssembly memory\n * is resized.\n * @param {number} byteOffset \n * @param {number} byteLength \n * @returns {Uint8Array}\n */\n #makeDataArray(byteOffset, byteLength) {\n // @ts-ignore\n return new Uint8ArrayProxy(this._module, byteOffset, byteLength);\n }\n\n #decodeFilename(zName, flags) {\n if (flags & VFS.SQLITE_OPEN_URI) {\n // The first null-terminated string is the URI path. Subsequent\n // strings are query parameter keys and values.\n // https://www.sqlite.org/c3ref/open.html#urifilenamesinsqlite3open\n let pName = zName;\n let state = 1;\n const charCodes = [];\n while (state) {\n const charCode = this._module.HEAPU8[pName++];\n if (charCode) {\n charCodes.push(charCode);\n } else {\n if (!this._module.HEAPU8[pName]) state = null;\n switch (state) {\n case 1: // path\n charCodes.push('?'.charCodeAt(0));\n state = 2;\n break;\n case 2: // key\n charCodes.push('='.charCodeAt(0));\n state = 3;\n break;\n case 3: // value\n charCodes.push('&'.charCodeAt(0));\n state = 2;\n break;\n }\n }\n }\n return new TextDecoder().decode(new Uint8Array(charCodes));\n }\n return zName ? this._module.UTF8ToString(zName) : null;\n }\n}\n\n// Emscripten \"legalizes\" 64-bit integer arguments by passing them as\n// two 32-bit signed integers.\nfunction delegalize(lo32, hi32) {\n return (hi32 * 0x100000000) + lo32 + (lo32 < 0 ? 2**32 : 0);\n}\n\n// This class provides a Uint8Array-like interface for a WebAssembly memory\n// buffer. It is used to access memory blocks passed as arguments to\n// xRead, xWrite, etc. The class reacquires the underlying buffer when the\n// WebAssembly memory is resized, which can happen when the memory is\n// detached and resized by the WebAssembly module.\n//\n// Note that although this class implements the same methods as Uint8Array,\n// it is not a real Uint8Array and passing it to functions that expect\n// a Uint8Array may not work. Use subarray() to get a real Uint8Array\n// if needed.\nclass Uint8ArrayProxy {\n #module;\n\n #_array = new Uint8Array()\n get #array() {\n if (this.#_array.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer so re-create the\n // array with the new buffer.\n this.#_array = this.#module.HEAPU8.subarray(\n this.byteOffset,\n this.byteOffset + this.byteLength);\n }\n return this.#_array;\n }\n\n /**\n * @param {*} module\n * @param {number} byteOffset \n * @param {number} byteLength \n */\n constructor(module, byteOffset, byteLength) {\n this.#module = module;\n this.byteOffset = byteOffset;\n this.length = this.byteLength = byteLength;\n }\n\n get buffer() {\n return this.#array.buffer;\n }\n\n at(index) {\n return this.#array.at(index);\n }\n copyWithin(target, start, end) {\n this.#array.copyWithin(target, start, end);\n }\n entries() {\n return this.#array.entries();\n }\n every(predicate) {\n return this.#array.every(predicate);\n }\n fill(value, start, end) {\n this.#array.fill(value, start, end);\n }\n filter(predicate) {\n return this.#array.filter(predicate);\n }\n find(predicate) {\n return this.#array.find(predicate);\n }\n findIndex(predicate) {\n return this.#array.findIndex(predicate);\n }\n findLast(predicate) {\n return this.#array.findLast(predicate);\n }\n findLastIndex(predicate) {\n return this.#array.findLastIndex(predicate);\n }\n forEach(callback) {\n this.#array.forEach(callback);\n }\n includes(value, start) {\n return this.#array.includes(value, start);\n }\n indexOf(value, start) {\n return this.#array.indexOf(value, start);\n }\n join(separator) {\n return this.#array.join(separator);\n }\n keys() {\n return this.#array.keys();\n }\n lastIndexOf(value, start) {\n return this.#array.lastIndexOf(value, start);\n }\n map(callback) {\n return this.#array.map(callback);\n }\n reduce(callback, initialValue) {\n return this.#array.reduce(callback, initialValue);\n }\n reduceRight(callback, initialValue) {\n return this.#array.reduceRight(callback, initialValue);\n }\n reverse() {\n this.#array.reverse();\n }\n set(array, offset) {\n this.#array.set(array, offset);\n }\n slice(start, end) {\n return this.#array.slice(start, end);\n }\n some(predicate) {\n return this.#array.some(predicate);\n }\n sort(compareFn) {\n this.#array.sort(compareFn);\n }\n subarray(begin, end) {\n return this.#array.subarray(begin, end);\n }\n toLocaleString(locales, options) {\n // @ts-ignore\n return this.#array.toLocaleString(locales, options);\n }\n toReversed() {\n return this.#array.toReversed();\n }\n toSorted(compareFn) {\n return this.#array.toSorted(compareFn);\n }\n toString() {\n return this.#array.toString();\n }\n values() {\n return this.#array.values();\n }\n with(index, value) {\n return this.#array.with(index, value);\n }\n [Symbol.iterator]() {\n return this.#array[Symbol.iterator]();\n }\n}\n\n// This class provides a DataView-like interface for a WebAssembly memory\n// buffer, restricted to either Int32 or BigInt64 types. It also reacquires\n// the underlying buffer when the WebAssembly memory is resized, which can\n// happen when the memory is detached and resized by the WebAssembly module.\nclass DataViewProxy {\n #module;\n #type;\n\n #_view = new DataView(new ArrayBuffer(0));\n get #view() {\n if (this.#_view.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer so re-create the\n // view with the new buffer.\n this.#_view = new DataView(\n this.#module.HEAPU8.buffer,\n this.#module.HEAPU8.byteOffset + this.byteOffset);\n }\n return this.#_view;\n }\n\n /**\n * @param {*} module\n * @param {number} byteOffset \n * @param {'Int32'|'BigInt64'} type\n */\n constructor(module, byteOffset, type) {\n this.#module = module;\n this.byteOffset = byteOffset;\n this.#type = type;\n }\n\n get buffer() {\n return this.#view.buffer;\n }\n get byteLength() {\n return this.#type === 'Int32' ? 4 : 8;\n }\n\n getInt32(byteOffset, littleEndian) {\n if (this.#type !== 'Int32') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n return this.#view.getInt32(byteOffset, littleEndian);\n }\n setInt32(byteOffset, value, littleEndian) {\n if (this.#type !== 'Int32') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n this.#view.setInt32(byteOffset, value, littleEndian);\n }\n getBigInt64(byteOffset, littleEndian) {\n if (this.#type !== 'BigInt64') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n return this.#view.getBigInt64(byteOffset, littleEndian);\n }\n setBigInt64(byteOffset, value, littleEndian) {\n if (this.#type !== 'BigInt64') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n this.#view.setBigInt64(byteOffset, value, littleEndian);\n }\n}","// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport * as VFS from './sqlite-constants.js';\nexport * from './sqlite-constants.js';\n\nconst DEFAULT_SECTOR_SIZE = 512;\n\n// Base class for a VFS.\nexport class Base {\n name;\n mxPathname = 64;\n _module;\n\n /**\n * @param {string} name \n * @param {object} module \n */\n constructor(name, module) {\n this.name = name;\n this._module = module;\n }\n\n /**\n * @returns {void|Promise<void>} \n */\n close() {\n }\n\n /**\n * @returns {boolean|Promise<boolean>}\n */\n isReady() {\n return true;\n }\n\n /**\n * Overload in subclasses to indicate which methods are asynchronous.\n * @param {string} methodName \n * @returns {boolean}\n */\n hasAsyncMethod(methodName) {\n return false;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} pFile \n * @param {number} flags \n * @param {number} pOutFlags \n * @returns {number|Promise<number>}\n */\n xOpen(pVfs, zName, pFile, flags, pOutFlags) {\n return VFS.SQLITE_CANTOPEN;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n xDelete(pVfs, zName, syncDir) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} flags \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xAccess(pVfs, zName, flags, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} nOut \n * @param {number} zOut \n * @returns {number|Promise<number>}\n */\n xFullPathname(pVfs, zName, nOut, zOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nBuf \n * @param {number} zBuf \n * @returns {number|Promise<number>}\n */\n xGetLastError(pVfs, nBuf, zBuf) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xClose(pFile) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xRead(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xWrite(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} sizeLo \n * @param {number} sizeHi \n * @returns {number|Promise<number>}\n */\n xTruncate(pFile, sizeLo, sizeHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n xSync(pFile, flags) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * \n * @param {number} pFile \n * @param {number} pSize \n * @returns {number|Promise<number>}\n */\n xFileSize(pFile, pSize) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xLock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xUnlock(pFile, lockType) {\n return VFS.SQLITE_OK;\n } \n\n /**\n * @param {number} pFile \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xCheckReservedLock(pFile, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} op \n * @param {number} pArg \n * @returns {number|Promise<number>}\n */\n xFileControl(pFile, op, pArg) {\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xSectorSize(pFile) {\n return DEFAULT_SECTOR_SIZE;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xDeviceCharacteristics(pFile) {\n return 0;\n }\n}\n\nexport const FILE_TYPE_MASK = [\n VFS.SQLITE_OPEN_MAIN_DB,\n VFS.SQLITE_OPEN_MAIN_JOURNAL,\n VFS.SQLITE_OPEN_TEMP_DB,\n VFS.SQLITE_OPEN_TEMP_JOURNAL,\n VFS.SQLITE_OPEN_TRANSIENT_DB,\n VFS.SQLITE_OPEN_SUBJOURNAL,\n VFS.SQLITE_OPEN_SUPER_JOURNAL,\n VFS.SQLITE_OPEN_WAL\n].reduce((mask, element) => mask | element);","import * as VFS from './VFS.js';\n\n// Options for navigator.locks.request().\n/** @type {LockOptions} */ const SHARED = { mode: 'shared' };\n/** @type {LockOptions} */ const POLL_SHARED = { ifAvailable: true, mode: 'shared' };\n/** @type {LockOptions} */ const POLL_EXCLUSIVE = { ifAvailable: true, mode: 'exclusive' };\n\nconst POLICIES = ['exclusive', 'shared', 'shared+hint'];\n\n/**\n * @typedef LockState\n * @property {string} baseName\n * @property {number} type\n * @property {boolean} writeHint\n * \n * These properties are functions that release a specific lock.\n * @property {(() => void)?} [gate]\n * @property {(() => void)?} [access]\n * @property {(() => void)?} [reserved]\n * @property {(() => void)?} [hint]\n */\n\n/**\n * Mix-in for FacadeVFS that implements the SQLite VFS locking protocol.\n * @param {*} superclass FacadeVFS (or subclass)\n * @returns \n */\nexport const WebLocksMixin = superclass => class extends superclass {\n #options = {\n lockPolicy: 'exclusive',\n lockTimeout: Infinity\n };\n\n /** @type {Map<number, LockState>} */ #mapIdToState = new Map();\n\n constructor(name, module, options) {\n super(name, module, options);\n Object.assign(this.#options, options);\n if (POLICIES.indexOf(this.#options.lockPolicy) === -1) {\n throw new Error(`WebLocksMixin: invalid lock mode: ${options.lockPolicy}`);\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} lockType \n * @returns {Promise<number>}\n */\n async jLock(fileId, lockType) {\n try {\n const lockState = this.#getLockState(fileId);\n if (lockType <= lockState.type) return VFS.SQLITE_OK;\n \n switch (this.#options.lockPolicy) {\n case 'exclusive':\n return await this.#lockExclusive(lockState, lockType);\n case 'shared':\n case 'shared+hint':\n return await this.#lockShared(lockState, lockType);\n }\n } catch (e) {\n console.error('WebLocksMixin: lock error', e);\n return VFS.SQLITE_IOERR_LOCK;\n }\n }\n \n /**\n * @param {number} fileId \n * @param {number} lockType \n * @returns {Promise<number>}\n */\n async jUnlock(fileId, lockType) {\n try {\n const lockState = this.#getLockState(fileId);\n if (!(lockType < lockState.type)) return VFS.SQLITE_OK;\n \n switch (this.#options.lockPolicy) {\n case 'exclusive':\n return await this.#unlockExclusive(lockState, lockType);\n case 'shared':\n case 'shared+hint':\n return await this.#unlockShared(lockState, lockType);\n }\n } catch (e) {\n console.error('WebLocksMixin: unlock error', e);\n return VFS.SQLITE_IOERR_UNLOCK;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {DataView} pResOut \n * @returns {Promise<number>}\n */\n async jCheckReservedLock(fileId, pResOut) {\n try {\n const lockState = this.#getLockState(fileId);\n switch (this.#options.lockPolicy) {\n case 'exclusive':\n return this.#checkReservedExclusive(lockState, pResOut);\n case 'shared':\n case 'shared+hint':\n return await this.#checkReservedShared(lockState, pResOut);\n }\n } catch (e) {\n console.error('WebLocksMixin: check reserved lock error', e);\n return VFS.SQLITE_IOERR_CHECKRESERVEDLOCK;\n }\n pResOut.setInt32(0, 0, true);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} fileId\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number|Promise<number>}\n */\n jFileControl(fileId, op, pArg) {\n if (op === WebLocksMixin.WRITE_HINT_OP_CODE &&\n this.#options.lockPolicy === 'shared+hint'){\n const lockState = this.#getLockState(fileId);\n lockState.writeHint = true;\n }\n return VFS.SQLITE_NOTFOUND;\n }\n\n #getLockState(fileId) {\n let lockState = this.#mapIdToState.get(fileId);\n if (!lockState) {\n // The state doesn't exist yet so create it.\n const name = this.getFilename(fileId);\n lockState = {\n baseName: name,\n type: VFS.SQLITE_LOCK_NONE,\n writeHint: false\n };\n this.#mapIdToState.set(fileId, lockState);\n }\n return lockState\n }\n\n /**\n * @param {LockState} lockState \n * @param {number} lockType \n * @returns \n */\n async #lockExclusive(lockState, lockType) {\n if (!lockState.access) {\n if (!await this.#acquire(lockState, 'access')) {\n return VFS.SQLITE_BUSY;\n }\n console.assert(!!lockState.access);\n }\n lockState.type = lockType;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {LockState} lockState \n * @param {number} lockType \n * @returns {number}\n */\n #unlockExclusive(lockState, lockType) {\n if (lockType === VFS.SQLITE_LOCK_NONE) {\n lockState.access?.();\n console.assert(!lockState.access);\n }\n lockState.type = lockType;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {LockState} lockState \n * @param {DataView} pResOut \n * @returns {number}\n */\n #checkReservedExclusive(lockState, pResOut) {\n pResOut.setInt32(0, 0, true);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {LockState} lockState \n * @param {number} lockType \n * @returns \n */\n async #lockShared(lockState, lockType) {\n switch (lockState.type) {\n case VFS.SQLITE_LOCK_NONE:\n switch (lockType) {\n case VFS.SQLITE_LOCK_SHARED:\n if (lockState.writeHint) {\n // xFileControl() has hinted that this transaction will\n // write. Acquire the hint lock, which is required to reach\n // the RESERVED state.\n if (!await this.#acquire(lockState, 'hint')) {\n // Timeout before lock acquired.\n return VFS.SQLITE_BUSY;\n }\n }\n\n // Must have the gate lock to request the access lock.\n if (!await this.#acquire(lockState, 'gate', SHARED)) {\n // Timeout before lock acquired.\n lockState.hint?.();\n return VFS.SQLITE_BUSY;\n }\n await this.#acquire(lockState, 'access', SHARED);\n lockState.gate();\n console.assert(!lockState.gate);\n console.assert(!!lockState.access);\n console.assert(!lockState.reserved);\n break;\n\n default:\n throw new Error('unsupported lock transition');\n }\n break;\n case VFS.SQLITE_LOCK_SHARED:\n switch (lockType) {\n case VFS.SQLITE_LOCK_RESERVED:\n if (this.#options.lockPolicy === 'shared+hint') {\n // Ideally we should already have the hint lock, but if not\n // poll for it here.\n if (!lockState.hint &&\n !await this.#acquire(lockState, 'hint', POLL_EXCLUSIVE)) {\n // Another connection has the hint lock so this is a\n // deadlock. This connection must retry.\n return VFS.SQLITE_BUSY;\n }\n }\n\n // Poll for the reserved lock. This should always succeed\n // if all clients use the 'shared+hint' policy.\n if (!await this.#acquire(lockState, 'reserved', POLL_EXCLUSIVE)) {\n // This is a deadlock. The connection holding the reserved\n // lock blocks us, and it can't acquire an exclusive access\n // lock because we hold a shared access lock. This connection\n // must retry.\n lockState.hint?.();\n return VFS.SQLITE_BUSY;\n }\n lockState.access();\n console.assert(!lockState.gate);\n console.assert(!lockState.access);\n console.assert(!!lockState.reserved);\n break;\n\n case VFS.SQLITE_LOCK_EXCLUSIVE:\n // Jumping directly from SHARED to EXCLUSIVE without passing\n // through RESERVED is only done with a hot journal.\n if (!await this.#acquire(lockState, 'gate')) {\n // Timeout before lock acquired.\n return VFS.SQLITE_BUSY;\n }\n lockState.access();\n if (!await this.#acquire(lockState, 'access')) {\n // Timeout before lock acquired.\n lockState.gate();\n return VFS.SQLITE_BUSY;\n }\n console.assert(!!lockState.gate);\n console.assert(!!lockState.access);\n console.assert(!lockState.reserved);\n break;\n\n default:\n throw new Error('unsupported lock transition');\n }\n break;\n case VFS.SQLITE_LOCK_RESERVED:\n switch (lockType) {\n case VFS.SQLITE_LOCK_EXCLUSIVE:\n // Prevent other connections from entering the SHARED state.\n if (!await this.#acquire(lockState, 'gate')) {\n // Timeout before lock acquired.\n return VFS.SQLITE_BUSY;\n }\n\n // Block until all other connections exit the SHARED state.\n if (!await this.#acquire(lockState, 'access')) {\n // Timeout before lock acquired.\n lockState.gate();\n return VFS.SQLITE_BUSY;\n }\n console.assert(!!lockState.gate);\n console.assert(!!lockState.access);\n console.assert(!!lockState.reserved);\n break;\n\n default:\n throw new Error('unsupported lock transition');\n }\n break;\n }\n lockState.type = lockType;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {LockState} lockState \n * @param {number} lockType \n * @returns \n */\n async #unlockShared(lockState, lockType) {\n // lockType can only be SQLITE_LOCK_SHARED or SQLITE_LOCK_NONE.\n if (lockType === VFS.SQLITE_LOCK_NONE) {\n lockState.access?.();\n lockState.gate?.();\n lockState.reserved?.();\n lockState.hint?.();\n lockState.writeHint = false;\n console.assert(!lockState.access);\n console.assert(!lockState.gate);\n console.assert(!lockState.reserved);\n console.assert(!lockState.hint);\n } else { // lockType === VFS.SQLITE_LOCK_SHARED\n switch (lockState.type) {\n case VFS.SQLITE_LOCK_EXCLUSIVE:\n // Release our exclusive access lock and reacquire it with a\n // shared lock. This should always succeed because we hold\n // the gate lock.\n lockState.access();\n await this.#acquire(lockState, 'access', SHARED);\n\n // Release our gate and reserved locks. We might not have a\n // reserved lock if we were handling a hot journal.\n lockState.gate();\n lockState.reserved?.();\n lockState.hint?.();\n console.assert(!!lockState.access);\n console.assert(!lockState.gate);\n console.assert(!lockState.reserved);\n break;\n\n case VFS.SQLITE_LOCK_RESERVED:\n // This transition is rare, probably only on an I/O error\n // while writing to a journal file.\n await this.#acquire(lockState, 'access', SHARED);\n lockState.reserved();\n lockState.hint?.();\n console.assert(!!lockState.access);\n console.assert(!lockState.gate);\n console.assert(!lockState.reserved);\n break;\n }\n }\n lockState.type = lockType;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {LockState} lockState \n * @param {DataView} pResOut \n * @returns {Promise<number>}\n */\n async #checkReservedShared(lockState, pResOut) {\n if (await this.#acquire(lockState, 'reserved', POLL_SHARED)) {\n // We were able to get the lock so it was not reserved.\n lockState.reserved();\n pResOut.setInt32(0, 0, true);\n } else {\n pResOut.setInt32(0, 1, true);\n }\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {LockState} lockState \n * @param {'gate'|'access'|'reserved'|'hint'} name\n * @param {LockOptions} options \n * @returns {Promise<boolean>}\n */\n #acquire(lockState, name, options = {}) {\n console.assert(!lockState[name]);\n return new Promise(resolve => {\n if (!options.ifAvailable && this.#options.lockTimeout < Infinity) {\n // Add a timeout to the lock request.\n const controller = new AbortController();\n options = Object.assign({}, options, { signal: controller.signal });\n setTimeout(() => {\n controller.abort();\n resolve?.(false);\n }, this.#options.lockTimeout);\n }\n\n const lockName = `lock##${lockState.baseName}##${name}`;\n navigator.locks.request(lockName, options, lock => {\n if (lock) {\n return new Promise(release => {\n lockState[name] = () => {\n release();\n lockState[name] = null;\n };\n resolve(true);\n resolve = null;\n });\n } else {\n lockState[name] = null;\n resolve(false);\n resolve = null;\n }\n }).catch(e => {\n if (e.name !== 'AbortError') throw e;\n });\n });\n }\n}\n\nWebLocksMixin.WRITE_HINT_OP_CODE = -9999;","// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport { FacadeVFS } from '../FacadeVFS.js';\nimport * as VFS from '../VFS.js';\nimport { WebLocksMixin } from '../WebLocksMixin.js';\n\nconst RETRYABLE_ERRORS = new Set([\n 'TransactionInactiveError',\n 'InvalidStateError'\n]);\n\n/**\n * @typedef Metadata\n * @property {string} name\n * @property {number} fileSize\n * @property {number} version\n * @property {number} [pendingVersion]\n */\n\nclass File {\n /** @type {string} */ path;\n /** @type {number} */ flags;\n\n /** @type {Metadata} */ metadata;\n /** @type {number} */ fileSize = 0;\n\n /** @type {boolean} */ needsMetadataSync = false;\n /** @type {Metadata} */ rollback = null;\n /** @type {Set<number>} */ changedPages = new Set();\n\n /** @type {string} */ synchronous = 'full';\n /** @type {IDBTransactionOptions} */ txOptions = { durability: 'strict' };\n\n constructor(path, flags, metadata) {\n this.path = path;\n this.flags = flags;\n this.metadata = metadata;\n }\n}\n\nexport class IDBBatchAtomicVFS extends WebLocksMixin(FacadeVFS) {\n /** @type {Map<number, File>} */ mapIdToFile = new Map();\n lastError = null;\n\n log = null; // console.log\n\n /** @type {Promise} */ #isReady;\n /** @type {IDBContext} */ #idb;\n\n static async create(name, module, options) {\n const vfs = new IDBBatchAtomicVFS(name, module, options);\n await vfs.isReady();\n return vfs;\n }\n\n constructor(name, module, options = {}) {\n super(name, module, options);\n this.#isReady = this.#initialize(options.idbName ?? name);\n }\n\n async #initialize(name) {\n this.#idb = await IDBContext.create(name);\n }\n\n close() {\n this.#idb.close();\n }\n \n async isReady() {\n await super.isReady();\n await this.#isReady;\n }\n\n getFilename(fileId) {\n const pathname = this.mapIdToFile.get(fileId).path;\n return `IDB(${this.name}):${pathname}`\n }\n \n /**\n * @param {string?} zName \n * @param {number} fileId \n * @param {number} flags \n * @param {DataView} pOutFlags \n * @returns {Promise<number>}\n */\n async jOpen(zName, fileId, flags, pOutFlags) {\n try {\n const url = new URL(zName || Math.random().toString(36).slice(2), 'file://');\n const path = url.pathname;\n\n let meta = await this.#idb.q(({ metadata }) => metadata.get(path));\n if (!meta && (flags & VFS.SQLITE_OPEN_CREATE)) {\n meta = {\n name: path,\n fileSize: 0,\n version: 0\n };\n await this.#idb.q(({ metadata }) => metadata.put(meta), 'rw');\n }\n \n if (!meta) {\n throw new Error(`File ${path} not found`);\n }\n\n const file = new File(path, flags, meta);\n this.mapIdToFile.set(fileId, file);\n pOutFlags.setInt32(0, flags, true);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_CANTOPEN;\n }\n }\n\n /**\n * @param {string} zName \n * @param {number} syncDir \n * @returns {Promise<number>}\n */\n async jDelete(zName, syncDir) {\n try {\n const url = new URL(zName, 'file://');\n const path = url.pathname;\n\n this.#idb.q(({ metadata, blocks }) => {\n const range = IDBKeyRange.bound([path, -Infinity], [path, Infinity]);\n blocks.delete(range);\n metadata.delete(path);\n }, 'rw');\n\n if (syncDir) {\n await this.#idb.sync(false);\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_DELETE;\n }\n }\n\n /**\n * @param {string} zName \n * @param {number} flags \n * @param {DataView} pResOut \n * @returns {Promise<number>}\n */\n async jAccess(zName, flags, pResOut) {\n try {\n const url = new URL(zName, 'file://');\n const path = url.pathname;\n\n const meta = await this.#idb.q(({ metadata }) => metadata.get(path));\n pResOut.setInt32(0, meta ? 1 : 0, true);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_ACCESS;\n } \n }\n\n /**\n * @param {number} fileId \n * @returns {Promise<number>}\n */\n async jClose(fileId) {\n try {\n const file = this.mapIdToFile.get(fileId);\n this.mapIdToFile.delete(fileId);\n\n if (file.flags & VFS.SQLITE_OPEN_DELETEONCLOSE) {\n await this.#idb.q(({ metadata, blocks }) => {\n metadata.delete(file.path);\n blocks.delete(IDBKeyRange.bound([file.path, 0], [file.path, Infinity]));\n }, 'rw'); \n }\n\n if (file.needsMetadataSync) {\n this.#idb.q(({ metadata }) => metadata.put(file.metadata), 'rw');\n }\n await this.#idb.sync(file.synchronous === 'full');\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_CLOSE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {Uint8Array} pData \n * @param {number} iOffset\n * @returns {Promise<number>}\n */\n async jRead(fileId, pData, iOffset) {\n try {\n const file = this.mapIdToFile.get(fileId);\n\n let pDataOffset = 0;\n while (pDataOffset < pData.byteLength) {\n // Fetch the IndexedDB block for this file location.\n const fileOffset = iOffset + pDataOffset;\n const block = await this.#idb.q(({ blocks }) => {\n const range = IDBKeyRange.bound([file.path, -fileOffset], [file.path, Infinity]);\n return blocks.get(range);\n }); \n \n if (!block || block.data.byteLength - block.offset <= fileOffset) {\n pData.fill(0, pDataOffset);\n return VFS.SQLITE_IOERR_SHORT_READ;\n }\n\n // Copy block data.\n const dst = pData.subarray(pDataOffset);\n const srcOffset = fileOffset + block.offset;\n const nBytesToCopy = Math.min(\n Math.max(block.data.byteLength - srcOffset, 0),\n dst.byteLength);\n dst.set(block.data.subarray(srcOffset, srcOffset + nBytesToCopy));\n pDataOffset += nBytesToCopy;\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_READ;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {Uint8Array} pData \n * @param {number} iOffset\n * @returns {number}\n */\n jWrite(fileId, pData, iOffset) {\n try {\n const file = this.mapIdToFile.get(fileId);\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n if (!file.rollback) {\n // Begin a new write transaction.\n // Add pendingVersion to the metadata in IndexedDB. If we crash\n // during the transaction, this lets subsequent connections\n // know to remove blocks from the failed transaction.\n const pending = Object.assign(\n { pendingVersion: file.metadata.version - 1 },\n file.metadata);\n this.#idb.q(({ metadata }) => metadata.put(pending), 'rw', file.txOptions);\n\n file.rollback = Object.assign({}, file.metadata);\n file.metadata.version--;\n }\n }\n\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n file.changedPages.add(iOffset);\n }\n\n const data = pData.slice();\n const version = file.metadata.version;\n const isOverwrite = iOffset < file.metadata.fileSize;\n if (!isOverwrite ||\n file.flags & VFS.SQLITE_OPEN_MAIN_DB ||\n file.flags & VFS.SQLITE_OPEN_TEMP_DB) {\n const block = {\n path: file.path,\n offset: -iOffset,\n version: version,\n data: pData.slice()\n };\n this.#idb.q(({ blocks }) => {\n blocks.put(block);\n file.changedPages.add(iOffset);\n }, 'rw', file.txOptions);\n } else {\n this.#idb.q(async ({ blocks }) => {\n // Read the existing block.\n const range = IDBKeyRange.bound(\n [file.path, -iOffset],\n [file.path, Infinity]);\n const block = await blocks.get(range);\n\n // Modify the block data.\n // @ts-ignore\n block.data.subarray(iOffset + block.offset).set(data);\n\n // Write back.\n blocks.put(block);\n }, 'rw', file.txOptions);\n\n }\n\n if (file.metadata.fileSize < iOffset + pData.length) {\n file.metadata.fileSize = iOffset + pData.length;\n file.needsMetadataSync = true;\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_WRITE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} iSize \n * @returns {number}\n */\n jTruncate(fileId, iSize) {\n try {\n const file = this.mapIdToFile.get(fileId);\n if (iSize < file.metadata.fileSize) {\n this.#idb.q(({ blocks }) => {\n const range = IDBKeyRange.bound(\n [file.path, -Infinity],\n [file.path, -iSize, Infinity]);\n blocks.delete(range);\n }, 'rw', file.txOptions);\n file.metadata.fileSize = iSize;\n file.needsMetadataSync = true;\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_TRUNCATE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} flags \n * @returns {Promise<number>}\n */\n async jSync(fileId, flags) {\n try {\n const file = this.mapIdToFile.get(fileId);\n if (file.needsMetadataSync) {\n this.#idb.q(({ metadata }) => metadata.put(file.metadata), 'rw', file.txOptions);\n file.needsMetadataSync = false;\n }\n\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n // Sync is only needed here for durability. Visibility for other\n // connections is ensured in jUnlock().\n if (file.synchronous === 'full') {\n await this.#idb.sync(true);\n }\n } else {\n await this.#idb.sync(file.synchronous === 'full');\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_FSYNC;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {DataView} pSize64 \n * @returns {number}\n */\n jFileSize(fileId, pSize64) {\n try {\n const file = this.mapIdToFile.get(fileId);\n pSize64.setBigInt64(0, BigInt(file.metadata.fileSize), true);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_FSTAT;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} lockType \n * @returns {Promise<number>}\n */\n async jLock(fileId, lockType) {\n // Call the actual lock implementation.\n const file = this.mapIdToFile.get(fileId);\n const result = await super.jLock(fileId, lockType);\n\n if (lockType === VFS.SQLITE_LOCK_SHARED) {\n // Update metadata.\n file.metadata = await this.#idb.q(async ({ metadata, blocks }) => {\n // @ts-ignore\n /** @type {Metadata} */ const m = await metadata.get(file.path);\n if (m.pendingVersion) {\n console.warn(`removing failed transaction ${m.pendingVersion}`);\n await new Promise((resolve, reject) => {\n const range = IDBKeyRange.bound([m.name, -Infinity], [m.name, Infinity]);\n const request = blocks.openCursor(range);\n request.onsuccess = () => {\n const cursor = request.result;\n if (cursor) {\n const block = cursor.value;\n if (block.version < m.version) {\n cursor.delete();\n }\n cursor.continue();\n } else {\n resolve();\n }\n };\n request.onerror = () => reject(request.error);\n })\n\n delete m.pendingVersion;\n metadata.put(m);\n }\n return m;\n }, 'rw', file.txOptions);\n }\n return result;\n }\n\n /**\n * @param {number} fileId \n * @param {number} lockType \n * @returns {Promise<number>}\n */\n async jUnlock(fileId, lockType) {\n if (lockType === VFS.SQLITE_LOCK_NONE) {\n const file = this.mapIdToFile.get(fileId);\n await this.#idb.sync(file.synchronous === 'full');\n }\n\n // Call the actual unlock implementation.\n return super.jUnlock(fileId, lockType);\n }\n\n /**\n * @param {number} fileId\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number|Promise<number>}\n */\n jFileControl(fileId, op, pArg) {\n try {\n const file = this.mapIdToFile.get(fileId);\n switch (op) {\n case VFS.SQLITE_FCNTL_PRAGMA:\n const key = extractString(pArg, 4);\n const value = extractString(pArg, 8);\n this.log?.('xFileControl', file.path, 'PRAGMA', key, value);\n const setPragmaResponse = response => {\n const encoded = new TextEncoder().encode(response);\n const out = this._module._sqlite3_malloc(encoded.byteLength);\n const outArray = this._module.HEAPU8.subarray(out, out + encoded.byteLength);\n outArray.set(encoded);\n pArg.setUint32(0, out, true);\n return VFS.SQLITE_ERROR;\n };\n switch (key.toLowerCase()) {\n case 'page_size':\n if (file.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n // Don't allow changing the page size.\n if (value && file.metadata.fileSize) {\n return VFS.SQLITE_ERROR;\n }\n }\n break;\n case 'synchronous':\n if (value) {\n switch (value.toLowerCase()) {\n case '0':\n case 'off':\n file.synchronous = 'off';\n file.txOptions = { durability: 'relaxed' };\n break;\n case '1':\n case 'normal':\n file.synchronous = 'normal';\n file.txOptions = { durability: 'relaxed' };\n break;\n case '2':\n case '3':\n case 'full':\n case 'extra':\n file.synchronous = 'full';\n file.txOptions = { durability: 'strict' };\n break;\n }\n }\n break;\n case 'write_hint':\n return super.jFileControl(fileId, WebLocksMixin.WRITE_HINT_OP_CODE, null);\n }\n break;\n case VFS.SQLITE_FCNTL_SYNC:\n this.log?.('xFileControl', file.path, 'SYNC');\n if (file.rollback) {\n const commitMetadata = Object.assign({}, file.metadata);\n const prevFileSize = file.rollback.fileSize\n this.#idb.q(({ metadata, blocks }) => {\n metadata.put(commitMetadata);\n\n // Remove old page versions.\n for (const offset of file.changedPages) {\n if (offset < prevFileSize) {\n const range = IDBKeyRange.bound(\n [file.path, -offset, commitMetadata.version],\n [file.path, -offset, Infinity],\n true);\n blocks.delete(range);\n }\n }\n file.changedPages.clear();\n }, 'rw', file.txOptions);\n file.needsMetadataSync = false;\n file.rollback = null;\n }\n break;\n case VFS.SQLITE_FCNTL_BEGIN_ATOMIC_WRITE:\n // Every write transaction is atomic, so this is a no-op.\n this.log?.('xFileControl', file.path, 'BEGIN_ATOMIC_WRITE');\n return VFS.SQLITE_OK;\n case VFS.SQLITE_FCNTL_COMMIT_ATOMIC_WRITE:\n // Every write transaction is atomic, so this is a no-op.\n this.log?.('xFileControl', file.path, 'COMMIT_ATOMIC_WRITE');\n return VFS.SQLITE_OK;\n case VFS.SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE:\n this.log?.('xFileControl', file.path, 'ROLLBACK_ATOMIC_WRITE');\n file.metadata = file.rollback;\n const rollbackMetadata = Object.assign({}, file.metadata);\n this.#idb.q(({ metadata, blocks }) => {\n metadata.put(rollbackMetadata);\n\n // Remove pages.\n for (const offset of file.changedPages) {\n blocks.delete([file.path, -offset, rollbackMetadata.version - 1]);\n }\n file.changedPages.clear();\n }, 'rw', file.txOptions);\n file.needsMetadataSync = false;\n file.rollback = null;\n return VFS.SQLITE_OK;\n }\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR;\n }\n return super.jFileControl(fileId, op, pArg);\n }\n \n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jDeviceCharacteristics(pFile) {\n return 0\n | VFS.SQLITE_IOCAP_BATCH_ATOMIC\n | VFS.SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;\n }\n\n /**\n * @param {Uint8Array} zBuf \n * @returns {number|Promise<number>}\n */\n jGetLastError(zBuf) {\n if (this.lastError) {\n console.error(this.lastError);\n const outputArray = zBuf.subarray(0, zBuf.byteLength - 1);\n const { written } = new TextEncoder().encodeInto(this.lastError.message, outputArray);\n zBuf[written] = 0;\n }\n return VFS.SQLITE_OK\n }\n}\n\nfunction extractString(dataView, offset) {\n const p = dataView.getUint32(offset, true);\n if (p) {\n const chars = new Uint8Array(dataView.buffer, p);\n return new TextDecoder().decode(chars.subarray(0, chars.indexOf(0)));\n }\n return null;\n}\n\nexport class IDBContext {\n /** @type {IDBDatabase} */ #database;\n\n /** @type {Promise} */ #chain = null;\n /** @type {Promise<any>} */ #txComplete = Promise.resolve();\n /** @type {IDBRequest?} */ #request = null;\n /** @type {WeakSet<IDBTransaction>} */ #txPending = new WeakSet();\n \n log = null;\n\n static async create(name) {\n const database = await new Promise((resolve, reject) => {\n const request = indexedDB.open(name, 6);\n request.onupgradeneeded = async event => {\n const db = request.result;\n if (event.oldVersion) {\n console.log(`Upgrading IndexedDB from version ${event.oldVersion}`);\n }\n switch (event.oldVersion) {\n case 0:\n // Start with the original schema.\n db.createObjectStore('blocks', { keyPath: ['path', 'offset', 'version']})\n .createIndex('version', ['path', 'version']);\n // fall through intentionally\n case 5:\n const tx = request.transaction;\n const blocks = tx.objectStore('blocks');\n blocks.deleteIndex('version');\n const metadata = db.createObjectStore('metadata', { keyPath: 'name' });\n\n await new Promise((resolve, reject) => {\n // Iterate over all the blocks.\n let lastBlock = {};\n const request = tx.objectStore('blocks').openCursor();\n request.onsuccess = () => {\n const cursor = request.result;\n if (cursor) {\n const block = cursor.value;\n if (typeof block.offset !== 'number' ||\n (block.path === lastBlock.path && block.offset === lastBlock.offset)) {\n // Remove superceded block (or the \"purge\" info).\n cursor.delete();\n } else if (block.offset === 0) {\n // Move metadata to its own store.\n metadata.put({\n name: block.path,\n fileSize: block.fileSize,\n version: block.version\n });\n\n delete block.fileSize;\n cursor.update(block);\n }\n lastBlock = block;\n cursor.continue();\n } else {\n resolve();\n }\n };\n request.onerror = () => reject(request.error);\n });\n break;\n }\n };\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error);\n });\n return new IDBContext(database);\n }\n\n constructor(database) {\n this.#database = database;\n }\n\n close() {\n this.#database.close();\n }\n\n /**\n * @param {(stores: Object.<string, IDBObjectStore>) => any} f \n * @param {'ro'|'rw'} mode \n * @returns {Promise<any>}\n */\n q(f, mode = 'ro', options = {}) {\n /** @type {IDBTransactionMode} */\n const txMode = mode === 'ro' ? 'readonly' : 'readwrite';\n const txOptions = Object.assign({\n /** @type {IDBTransactionDurability} */ durability: 'default'\n }, options);\n\n // Ensure that queries run sequentially. If any function rejects,\n // or any request has an error, or the transaction does not commit,\n // then no subsequent functions will run until sync() or reset().\n this.#chain = (this.#chain || Promise.resolve())\n .then(() => this.#q(f, txMode, txOptions));\n return this.#chain;\n }\n\n /**\n * @param {(stores: Object.<string, IDBObjectStore>) => any} f \n * @param {IDBTransactionMode} mode \n * @param {IDBTransactionOptions} options\n * @returns {Promise<any>}\n */\n async #q(f, mode, options) {\n /** @type {IDBTransaction} */ let tx;\n if (this.#request &&\n this.#txPending.has(this.#request.transaction) &&\n this.#request.transaction.mode >= mode &&\n this.#request.transaction.durability === options.durability) {\n // The previous request transaction is compatible and has\n // not yet completed.\n tx = this.#request.transaction;\n\n // If the previous request is pending, wait for it to complete.\n // This ensures that the transaction will be active.\n if (this.#request.readyState === 'pending') {\n await new Promise(resolve => {\n this.#request.addEventListener('success', resolve, { once: true });\n this.#request.addEventListener('error', resolve, { once: true });\n });\n }\n }\n\n for (let i = 0; i < 2; ++i) {\n if (!tx) {\n // The current transaction is missing or doesn't match so\n // replace it with a new one. wait for the previous\n // transaction to complete so the lifetimes do not overlap.\n await this.#txComplete;\n\n // Create the new transaction.\n // @ts-ignore\n tx = this.#database.transaction(this.#database.objectStoreNames, mode, options);\n this.log?.('IDBTransaction open', mode);\n this.#txPending.add(tx);\n this.#txComplete = new Promise((resolve, reject) => {\n tx.addEventListener('complete', () => {\n this.log?.('IDBTransaction complete');\n this.#txPending.delete(tx);\n resolve();\n });\n tx.addEventListener('abort', () => {\n this.#txPending.delete(tx);\n reject(new Error('transaction aborted'));\n });\n });\n }\n\n try {\n // @ts-ignore\n // Create object store proxies.\n const objectStores = [...tx.objectStoreNames].map(name => {\n return [name, this.proxyStoreOrIndex(tx.objectStore(name))];\n });\n\n // Execute the function.\n return await f(Object.fromEntries(objectStores));\n } catch (e) {\n // Use a new transaction if this one was inactive. This will\n // happen if the last request in the transaction completed\n // in a previous task but the transaction has not yet committed.\n if (!i && RETRYABLE_ERRORS.has(e.name)) {\n this.log?.(`${e.name}, retrying`);\n tx = null;\n continue;\n }\n throw e;\n }\n }\n }\n\n /**\n * Object store methods that return an IDBRequest, except for cursor\n * creation, are wrapped to return a Promise. In addition, the\n * request is used internally for chaining.\n * @param {IDBObjectStore} objectStore \n * @returns \n */\n proxyStoreOrIndex(objectStore) {\n return new Proxy(objectStore, {\n get: (target, property, receiver) => {\n const result = Reflect.get(target, property, receiver);\n if (typeof result === 'function') {\n return (...args) => {\n const maybeRequest = Reflect.apply(result, target, args);\n // @ts-ignore\n if (maybeRequest instanceof IDBRequest && !property.endsWith('Cursor')) {\n // // Debug logging.\n // this.log?.(`${target.name}.${String(property)}`, args);\n // maybeRequest.addEventListener('success', () => {\n // this.log?.(`${target.name}.${String(property)} success`, maybeRequest.result);\n // });\n // maybeRequest.addEventListener('error', () => {\n // this.log?.(`${target.name}.${String(property)} error`, maybeRequest.error);\n // });\n \n // Save the request.\n this.#request = maybeRequest;\n\n // Abort the transaction on error.\n maybeRequest.addEventListener('error', () => {\n console.error(maybeRequest.error);\n maybeRequest.transaction.abort();\n }, { once: true }); \n\n // Return a Promise.\n return wrap(maybeRequest);\n }\n return maybeRequest;\n }\n }\n return result;\n }\n });\n }\n\n /**\n * @param {boolean} durable \n */\n async sync(durable) {\n if (this.#chain) {\n // This waits for all IndexedDB calls to be made.\n await this.#chain;\n if (durable) {\n // This waits for the final transaction to commit.\n await this.#txComplete;\n }\n this.reset();\n }\n }\n\n reset() {\n this.#chain = null;\n this.#txComplete = Promise.resolve();\n this.#request = null;\n }\n}\n\n/**\n * @param {IDBRequest} request \n * @returns {Promise}\n */\nfunction wrap(request) {\n return new Promise((resolve, reject) => {\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error);\n });\n}\n\n"],"names":[],"ignoreList":[],"sourceRoot":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js","mappings":";;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC/sBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;;;;;;;;;;;;;AC7NA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","sources":["webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/FacadeVFS.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/VFS.js","webpack://sdk_web/../../node_modules/@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js"],"sourcesContent":["// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport * as VFS from './VFS.js';\n\nconst AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;\n\n// Milliseconds since Julian epoch as a BigInt.\n// https://github.com/sqlite/sqlite/blob/e57527c14f7b7cfa6e32eeab5c549d50c4fa3674/src/os_unix.c#L6872-L6882\nconst UNIX_EPOCH = 24405875n * 8640000n;\n\n// Convenience base class for a JavaScript VFS.\n// The raw xOpen, xRead, etc. function signatures receive only C primitives\n// which aren't easy to work with. This class provides corresponding calls\n// like jOpen, jRead, etc., which receive JavaScript-friendlier arguments\n// such as string, Uint8Array, and DataView.\nexport class FacadeVFS extends VFS.Base {\n /**\n * @param {string} name \n * @param {object} module \n */\n constructor(name, module) {\n super(name, module);\n }\n\n /**\n * Override to indicate which methods are asynchronous.\n * @param {string} methodName \n * @returns {boolean}\n */\n hasAsyncMethod(methodName) {\n // The input argument is a string like \"xOpen\", so convert to \"jOpen\".\n // Then check if the method exists and is async.\n const jMethodName = `j${methodName.slice(1)}`;\n return this[jMethodName] instanceof AsyncFunction;\n }\n \n /**\n * Return the filename for a file id for use by mixins.\n * @param {number} pFile \n * @returns {string}\n */\n getFilename(pFile) {\n throw new Error('unimplemented');\n }\n\n /**\n * @param {string?} filename \n * @param {number} pFile \n * @param {number} flags \n * @param {DataView} pOutFlags \n * @returns {number|Promise<number>}\n */\n jOpen(filename, pFile, flags, pOutFlags) {\n return VFS.SQLITE_CANTOPEN;\n }\n\n /**\n * @param {string} filename \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n jDelete(filename, syncDir) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} filename \n * @param {number} flags \n * @param {DataView} pResOut \n * @returns {number|Promise<number>}\n */\n jAccess(filename, flags, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {string} filename \n * @param {Uint8Array} zOut \n * @returns {number|Promise<number>}\n */\n jFullPathname(filename, zOut) {\n // Copy the filename to the output buffer.\n const { read, written } = new TextEncoder().encodeInto(filename, zOut);\n if (read < filename.length) return VFS.SQLITE_IOERR;\n if (written >= zOut.length) return VFS.SQLITE_IOERR;\n zOut[written] = 0;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {Uint8Array} zBuf \n * @returns {number|Promise<number>}\n */\n jGetLastError(zBuf) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n jClose(pFile) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {Uint8Array} pData \n * @param {number} iOffset \n * @returns {number|Promise<number>}\n */\n jRead(pFile, pData, iOffset) {\n pData.fill(0);\n return VFS.SQLITE_IOERR_SHORT_READ;\n }\n\n /**\n * @param {number} pFile \n * @param {Uint8Array} pData \n * @param {number} iOffset \n * @returns {number|Promise<number>}\n */\n jWrite(pFile, pData, iOffset) {\n return VFS.SQLITE_IOERR_WRITE;\n }\n\n /**\n * @param {number} pFile \n * @param {number} size \n * @returns {number|Promise<number>}\n */\n jTruncate(pFile, size) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n jSync(pFile, flags) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pSize\n * @returns {number|Promise<number>}\n */\n jFileSize(pFile, pSize) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jLock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n jUnlock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {DataView} pResOut \n * @returns {number|Promise<number>}\n */\n jCheckReservedLock(pFile, pResOut) {\n pResOut.setInt32(0, 0, true);\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number|Promise<number>}\n */\n jFileControl(pFile, op, pArg) {\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jSectorSize(pFile) {\n return super.xSectorSize(pFile);\n }\n\n /**\n * @param {number} pFile\n * @returns {number|Promise<number>}\n */\n jDeviceCharacteristics(pFile) {\n return 0;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} pFile \n * @param {number} flags \n * @param {number} pOutFlags \n * @returns {number|Promise<number>}\n */\n xOpen(pVfs, zName, pFile, flags, pOutFlags) {\n const filename = this.#decodeFilename(zName, flags);\n const pOutFlagsView = this.#makeTypedDataView('Int32', pOutFlags);\n this['log']?.('jOpen', filename, pFile, '0x' + flags.toString(16));\n return this.jOpen(filename, pFile, flags, pOutFlagsView);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nByte \n * @param {number} pCharOut\n * @returns {number|Promise<number>}\n */\n xRandomness(pVfs, nByte, pCharOut) {\n const randomArray = new Uint8Array(nByte);\n crypto.getRandomValues(randomArray);\n // Copy randomArray to the WebAssembly memory\n const buffer = pCharOut; // Pointer to memory in WebAssembly\n this._module.HEAPU8.set(randomArray, buffer); // Copy randomArray into memory starting at buffer\n return nByte;\n }\n\n /**\n * Gets the current time as milliseconds since Unix epoch\n * @param {number} pVfs pointer to the VFS\n * @param {number} pTime pointer to write the time value\n * @returns {number} SQLite error code\n */\n xCurrentTimeInt64(pVfs, pTime) {\n // Create a DataView to write the current time\n const timeView = this.#makeTypedDataView('BigInt64', pTime);\n \n const currentTime = BigInt(Date.now());\n // Convert the current time to milliseconds since Unix epoch\n const value = UNIX_EPOCH + currentTime;\n \n // Write the time value to the pointer location\n timeView.setBigInt64(0, value, true);\n \n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n xDelete(pVfs, zName, syncDir) {\n const filename = this._module.UTF8ToString(zName);\n this['log']?.('jDelete', filename, syncDir);\n return this.jDelete(filename, syncDir);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} flags \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xAccess(pVfs, zName, flags, pResOut) {\n const filename = this._module.UTF8ToString(zName);\n const pResOutView = this.#makeTypedDataView('Int32', pResOut);\n this['log']?.('jAccess', filename, flags);\n return this.jAccess(filename, flags, pResOutView);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} nOut \n * @param {number} zOut \n * @returns {number|Promise<number>}\n */\n xFullPathname(pVfs, zName, nOut, zOut) {\n const filename = this._module.UTF8ToString(zName);\n const zOutArray = this._module.HEAPU8.subarray(zOut, zOut + nOut);\n this['log']?.('jFullPathname', filename, nOut);\n return this.jFullPathname(filename, zOutArray);\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nBuf \n * @param {number} zBuf \n * @returns {number|Promise<number>}\n */\n xGetLastError(pVfs, nBuf, zBuf) {\n const zBufArray = this._module.HEAPU8.subarray(zBuf, zBuf + nBuf);\n this['log']?.('jGetLastError', nBuf);\n return this.jGetLastError(zBufArray);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xClose(pFile) {\n this['log']?.('jClose', pFile);\n return this.jClose(pFile);\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xRead(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n const pDataArray = this.#makeDataArray(pData, iAmt);\n const iOffset = delegalize(iOffsetLo, iOffsetHi);\n this['log']?.('jRead', pFile, iAmt, iOffset);\n return this.jRead(pFile, pDataArray, iOffset);\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xWrite(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n const pDataArray = this.#makeDataArray(pData, iAmt);\n const iOffset = delegalize(iOffsetLo, iOffsetHi);\n this['log']?.('jWrite', pFile, pDataArray, iOffset);\n return this.jWrite(pFile, pDataArray, iOffset);\n }\n\n /**\n * @param {number} pFile \n * @param {number} sizeLo \n * @param {number} sizeHi \n * @returns {number|Promise<number>}\n */\n xTruncate(pFile, sizeLo, sizeHi) {\n const size = delegalize(sizeLo, sizeHi);\n this['log']?.('jTruncate', pFile, size);\n return this.jTruncate(pFile, size);\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n xSync(pFile, flags) {\n this['log']?.('jSync', pFile, flags);\n return this.jSync(pFile, flags);\n }\n\n /**\n * \n * @param {number} pFile \n * @param {number} pSize \n * @returns {number|Promise<number>}\n */\n xFileSize(pFile, pSize) {\n const pSizeView = this.#makeTypedDataView('BigInt64', pSize);\n this['log']?.('jFileSize', pFile);\n return this.jFileSize(pFile, pSizeView);\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xLock(pFile, lockType) {\n this['log']?.('jLock', pFile, lockType);\n return this.jLock(pFile, lockType);\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xUnlock(pFile, lockType) {\n this['log']?.('jUnlock', pFile, lockType);\n return this.jUnlock(pFile, lockType);\n } \n\n /**\n * @param {number} pFile \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xCheckReservedLock(pFile, pResOut) {\n const pResOutView = this.#makeTypedDataView('Int32', pResOut);\n this['log']?.('jCheckReservedLock', pFile);\n return this.jCheckReservedLock(pFile, pResOutView);\n }\n\n /**\n * @param {number} pFile \n * @param {number} op \n * @param {number} pArg \n * @returns {number|Promise<number>}\n */\n xFileControl(pFile, op, pArg) {\n const pArgView = new DataView(\n this._module.HEAPU8.buffer,\n this._module.HEAPU8.byteOffset + pArg);\n this['log']?.('jFileControl', pFile, op, pArgView);\n return this.jFileControl(pFile, op, pArgView);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xSectorSize(pFile) {\n this['log']?.('jSectorSize', pFile);\n return this.jSectorSize(pFile);\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xDeviceCharacteristics(pFile) {\n this['log']?.('jDeviceCharacteristics', pFile);\n return this.jDeviceCharacteristics(pFile);\n }\n\n /**\n * Wrapped DataView for pointer arguments.\n * Pointers to a single value are passed using a DataView-like class.\n * This wrapper class prevents use of incorrect type or endianness, and\n * reacquires the underlying buffer when the WebAssembly memory is resized.\n * @param {'Int32'|'BigInt64'} type \n * @param {number} byteOffset \n * @returns {DataView}\n */\n #makeTypedDataView(type, byteOffset) {\n // @ts-ignore\n return new DataViewProxy(this._module, byteOffset, type);\n }\n\n /**\n * Wrapped Uint8Array for buffer arguments.\n * Memory blocks are passed as a Uint8Array-like class. This wrapper\n * class reacquires the underlying buffer when the WebAssembly memory\n * is resized.\n * @param {number} byteOffset \n * @param {number} byteLength \n * @returns {Uint8Array}\n */\n #makeDataArray(byteOffset, byteLength) {\n // @ts-ignore\n return new Uint8ArrayProxy(this._module, byteOffset, byteLength);\n }\n\n #decodeFilename(zName, flags) {\n if (flags & VFS.SQLITE_OPEN_URI) {\n // The first null-terminated string is the URI path. Subsequent\n // strings are query parameter keys and values.\n // https://www.sqlite.org/c3ref/open.html#urifilenamesinsqlite3open\n let pName = zName;\n let state = 1;\n const charCodes = [];\n while (state) {\n const charCode = this._module.HEAPU8[pName++];\n if (charCode) {\n charCodes.push(charCode);\n } else {\n if (!this._module.HEAPU8[pName]) state = null;\n switch (state) {\n case 1: // path\n charCodes.push('?'.charCodeAt(0));\n state = 2;\n break;\n case 2: // key\n charCodes.push('='.charCodeAt(0));\n state = 3;\n break;\n case 3: // value\n charCodes.push('&'.charCodeAt(0));\n state = 2;\n break;\n }\n }\n }\n return new TextDecoder().decode(new Uint8Array(charCodes));\n }\n return zName ? this._module.UTF8ToString(zName) : null;\n }\n}\n\n// Emscripten \"legalizes\" 64-bit integer arguments by passing them as\n// two 32-bit signed integers.\nfunction delegalize(lo32, hi32) {\n return (hi32 * 0x100000000) + lo32 + (lo32 < 0 ? 2**32 : 0);\n}\n\n// This class provides a Uint8Array-like interface for a WebAssembly memory\n// buffer. It is used to access memory blocks passed as arguments to\n// xRead, xWrite, etc. The class reacquires the underlying buffer when the\n// WebAssembly memory is resized, which can happen when the memory is\n// detached and resized by the WebAssembly module.\n//\n// Note that although this class implements the same methods as Uint8Array,\n// it is not a real Uint8Array and passing it to functions that expect\n// a Uint8Array may not work. Use subarray() to get a real Uint8Array\n// if needed.\nclass Uint8ArrayProxy {\n #module;\n\n #_array = new Uint8Array()\n get #array() {\n if (this.#_array.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer so re-create the\n // array with the new buffer.\n this.#_array = this.#module.HEAPU8.subarray(\n this.byteOffset,\n this.byteOffset + this.byteLength);\n }\n return this.#_array;\n }\n\n /**\n * @param {*} module\n * @param {number} byteOffset \n * @param {number} byteLength \n */\n constructor(module, byteOffset, byteLength) {\n this.#module = module;\n this.byteOffset = byteOffset;\n this.length = this.byteLength = byteLength;\n }\n\n get buffer() {\n return this.#array.buffer;\n }\n\n at(index) {\n return this.#array.at(index);\n }\n copyWithin(target, start, end) {\n this.#array.copyWithin(target, start, end);\n }\n entries() {\n return this.#array.entries();\n }\n every(predicate) {\n return this.#array.every(predicate);\n }\n fill(value, start, end) {\n this.#array.fill(value, start, end);\n }\n filter(predicate) {\n return this.#array.filter(predicate);\n }\n find(predicate) {\n return this.#array.find(predicate);\n }\n findIndex(predicate) {\n return this.#array.findIndex(predicate);\n }\n findLast(predicate) {\n return this.#array.findLast(predicate);\n }\n findLastIndex(predicate) {\n return this.#array.findLastIndex(predicate);\n }\n forEach(callback) {\n this.#array.forEach(callback);\n }\n includes(value, start) {\n return this.#array.includes(value, start);\n }\n indexOf(value, start) {\n return this.#array.indexOf(value, start);\n }\n join(separator) {\n return this.#array.join(separator);\n }\n keys() {\n return this.#array.keys();\n }\n lastIndexOf(value, start) {\n return this.#array.lastIndexOf(value, start);\n }\n map(callback) {\n return this.#array.map(callback);\n }\n reduce(callback, initialValue) {\n return this.#array.reduce(callback, initialValue);\n }\n reduceRight(callback, initialValue) {\n return this.#array.reduceRight(callback, initialValue);\n }\n reverse() {\n this.#array.reverse();\n }\n set(array, offset) {\n this.#array.set(array, offset);\n }\n slice(start, end) {\n return this.#array.slice(start, end);\n }\n some(predicate) {\n return this.#array.some(predicate);\n }\n sort(compareFn) {\n this.#array.sort(compareFn);\n }\n subarray(begin, end) {\n return this.#array.subarray(begin, end);\n }\n toLocaleString(locales, options) {\n // @ts-ignore\n return this.#array.toLocaleString(locales, options);\n }\n toReversed() {\n return this.#array.toReversed();\n }\n toSorted(compareFn) {\n return this.#array.toSorted(compareFn);\n }\n toString() {\n return this.#array.toString();\n }\n values() {\n return this.#array.values();\n }\n with(index, value) {\n return this.#array.with(index, value);\n }\n [Symbol.iterator]() {\n return this.#array[Symbol.iterator]();\n }\n}\n\n// This class provides a DataView-like interface for a WebAssembly memory\n// buffer, restricted to either Int32 or BigInt64 types. It also reacquires\n// the underlying buffer when the WebAssembly memory is resized, which can\n// happen when the memory is detached and resized by the WebAssembly module.\nclass DataViewProxy {\n #module;\n #type;\n\n #_view = new DataView(new ArrayBuffer(0));\n get #view() {\n if (this.#_view.buffer.byteLength === 0) {\n // WebAssembly memory resize detached the buffer so re-create the\n // view with the new buffer.\n this.#_view = new DataView(\n this.#module.HEAPU8.buffer,\n this.#module.HEAPU8.byteOffset + this.byteOffset);\n }\n return this.#_view;\n }\n\n /**\n * @param {*} module\n * @param {number} byteOffset \n * @param {'Int32'|'BigInt64'} type\n */\n constructor(module, byteOffset, type) {\n this.#module = module;\n this.byteOffset = byteOffset;\n this.#type = type;\n }\n\n get buffer() {\n return this.#view.buffer;\n }\n get byteLength() {\n return this.#type === 'Int32' ? 4 : 8;\n }\n\n getInt32(byteOffset, littleEndian) {\n if (this.#type !== 'Int32') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n return this.#view.getInt32(byteOffset, littleEndian);\n }\n setInt32(byteOffset, value, littleEndian) {\n if (this.#type !== 'Int32') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n this.#view.setInt32(byteOffset, value, littleEndian);\n }\n getBigInt64(byteOffset, littleEndian) {\n if (this.#type !== 'BigInt64') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n return this.#view.getBigInt64(byteOffset, littleEndian);\n }\n setBigInt64(byteOffset, value, littleEndian) {\n if (this.#type !== 'BigInt64') {\n throw new Error('invalid type');\n }\n if (!littleEndian) throw new Error('must be little endian');\n this.#view.setBigInt64(byteOffset, value, littleEndian);\n }\n}","// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport * as VFS from './sqlite-constants.js';\nexport * from './sqlite-constants.js';\n\nconst DEFAULT_SECTOR_SIZE = 512;\n\n// Base class for a VFS.\nexport class Base {\n name;\n mxPathname = 64;\n _module;\n\n /**\n * @param {string} name \n * @param {object} module \n */\n constructor(name, module) {\n this.name = name;\n this._module = module;\n }\n\n /**\n * @returns {void|Promise<void>} \n */\n close() {\n }\n\n /**\n * @returns {boolean|Promise<boolean>}\n */\n isReady() {\n return true;\n }\n\n /**\n * Overload in subclasses to indicate which methods are asynchronous.\n * @param {string} methodName \n * @returns {boolean}\n */\n hasAsyncMethod(methodName) {\n return false;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} pFile \n * @param {number} flags \n * @param {number} pOutFlags \n * @returns {number|Promise<number>}\n */\n xOpen(pVfs, zName, pFile, flags, pOutFlags) {\n return VFS.SQLITE_CANTOPEN;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} syncDir \n * @returns {number|Promise<number>}\n */\n xDelete(pVfs, zName, syncDir) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} flags \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xAccess(pVfs, zName, flags, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} zName \n * @param {number} nOut \n * @param {number} zOut \n * @returns {number|Promise<number>}\n */\n xFullPathname(pVfs, zName, nOut, zOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pVfs \n * @param {number} nBuf \n * @param {number} zBuf \n * @returns {number|Promise<number>}\n */\n xGetLastError(pVfs, nBuf, zBuf) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xClose(pFile) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xRead(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} pData \n * @param {number} iAmt \n * @param {number} iOffsetLo \n * @param {number} iOffsetHi \n * @returns {number|Promise<number>}\n */\n xWrite(pFile, pData, iAmt, iOffsetLo, iOffsetHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} sizeLo \n * @param {number} sizeHi \n * @returns {number|Promise<number>}\n */\n xTruncate(pFile, sizeLo, sizeHi) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} flags \n * @returns {number|Promise<number>}\n */\n xSync(pFile, flags) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * \n * @param {number} pFile \n * @param {number} pSize \n * @returns {number|Promise<number>}\n */\n xFileSize(pFile, pSize) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xLock(pFile, lockType) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} lockType \n * @returns {number|Promise<number>}\n */\n xUnlock(pFile, lockType) {\n return VFS.SQLITE_OK;\n } \n\n /**\n * @param {number} pFile \n * @param {number} pResOut \n * @returns {number|Promise<number>}\n */\n xCheckReservedLock(pFile, pResOut) {\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} pFile \n * @param {number} op \n * @param {number} pArg \n * @returns {number|Promise<number>}\n */\n xFileControl(pFile, op, pArg) {\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xSectorSize(pFile) {\n return DEFAULT_SECTOR_SIZE;\n }\n\n /**\n * @param {number} pFile \n * @returns {number|Promise<number>}\n */\n xDeviceCharacteristics(pFile) {\n return 0;\n }\n}\n\nexport const FILE_TYPE_MASK = [\n VFS.SQLITE_OPEN_MAIN_DB,\n VFS.SQLITE_OPEN_MAIN_JOURNAL,\n VFS.SQLITE_OPEN_TEMP_DB,\n VFS.SQLITE_OPEN_TEMP_JOURNAL,\n VFS.SQLITE_OPEN_TRANSIENT_DB,\n VFS.SQLITE_OPEN_SUBJOURNAL,\n VFS.SQLITE_OPEN_SUPER_JOURNAL,\n VFS.SQLITE_OPEN_WAL\n].reduce((mask, element) => mask | element);","// Copyright 2024 Roy T. Hashimoto. All Rights Reserved.\nimport { FacadeVFS } from '../FacadeVFS.js';\nimport * as VFS from '../VFS.js';\n\nconst DEFAULT_TEMPORARY_FILES = 10;\nconst LOCK_NOTIFY_INTERVAL = 1000;\n\nconst DB_RELATED_FILE_SUFFIXES = ['', '-journal', '-wal'];\n\nconst finalizationRegistry = new FinalizationRegistry(releaser => releaser());\n\nclass File {\n /** @type {string} */ path\n /** @type {number} */ flags;\n /** @type {FileSystemSyncAccessHandle} */ accessHandle;\n\n /** @type {PersistentFile?} */ persistentFile;\n\n constructor(path, flags) {\n this.path = path;\n this.flags = flags;\n }\n}\n\nclass PersistentFile {\n /** @type {FileSystemFileHandle} */ fileHandle\n /** @type {FileSystemSyncAccessHandle} */ accessHandle = null\n\n // The following properties are for main database files.\n\n /** @type {boolean} */ isLockBusy = false;\n /** @type {boolean} */ isFileLocked = false;\n /** @type {boolean} */ isRequestInProgress = false;\n /** @type {function} */ handleLockReleaser = null;\n\n /** @type {BroadcastChannel} */ handleRequestChannel;\n /** @type {boolean} */ isHandleRequested = false;\n\n constructor(fileHandle) {\n this.fileHandle = fileHandle;\n }\n}\n\nexport class OPFSCoopSyncVFS extends FacadeVFS {\n /** @type {Map<number, File>} */ mapIdToFile = new Map();\n\n lastError = null;\n log = null; //function(...args) { console.log(`[${contextName}]`, ...args) };\n \n /** @type {Map<string, PersistentFile>} */ persistentFiles = new Map();\n /** @type {Map<string, FileSystemSyncAccessHandle>} */ boundAccessHandles = new Map();\n /** @type {Set<FileSystemSyncAccessHandle>} */ unboundAccessHandles = new Set();\n /** @type {Set<string>} */ accessiblePaths = new Set();\n releaser = null;\n\n static async create(name, module) {\n const vfs = new OPFSCoopSyncVFS(name, module);\n await Promise.all([\n vfs.isReady(),\n vfs.#initialize(DEFAULT_TEMPORARY_FILES),\n ]);\n return vfs;\n }\n\n constructor(name, module) {\n super(name, module);\n }\n\n async #initialize(nTemporaryFiles) {\n // Delete temporary directories no longer in use.\n const root = await navigator.storage.getDirectory();\n // @ts-ignore\n for await (const entry of root.values()) {\n if (entry.kind === 'directory' && entry.name.startsWith('.ahp-')) {\n // A lock with the same name as the directory protects it from\n // being deleted.\n await navigator.locks.request(entry.name, { ifAvailable: true }, async lock => {\n if (lock) {\n this.log?.(`Deleting temporary directory ${entry.name}`);\n await root.removeEntry(entry.name, { recursive: true });\n } else {\n this.log?.(`Temporary directory ${entry.name} is in use`);\n }\n });\n }\n }\n\n // Create our temporary directory.\n const tmpDirName = `.ahp-${Math.random().toString(36).slice(2)}`;\n this.releaser = await new Promise(resolve => {\n navigator.locks.request(tmpDirName, () => {\n return new Promise(release => {\n resolve(release);\n });\n });\n });\n finalizationRegistry.register(this, this.releaser);\n const tmpDir = await root.getDirectoryHandle(tmpDirName, { create: true });\n\n // Populate temporary directory.\n for (let i = 0; i < nTemporaryFiles; i++) {\n const tmpFile = await tmpDir.getFileHandle(`${i}.tmp`, { create: true });\n const tmpAccessHandle = await tmpFile.createSyncAccessHandle();\n this.unboundAccessHandles.add(tmpAccessHandle);\n }\n }\n\n /**\n * @param {string?} zName \n * @param {number} fileId \n * @param {number} flags \n * @param {DataView} pOutFlags \n * @returns {number}\n */\n jOpen(zName, fileId, flags, pOutFlags) {\n try {\n const url = new URL(zName || Math.random().toString(36).slice(2), 'file://');\n const path = url.pathname;\n\n if (flags & VFS.SQLITE_OPEN_MAIN_DB) {\n const persistentFile = this.persistentFiles.get(path);\n if (persistentFile?.isRequestInProgress) {\n // Should not reach here unless SQLite itself retries an open.\n // Otherwise, asynchronous operations started on a previous\n // open try should have completed.\n return VFS.SQLITE_BUSY;\n } else if (!persistentFile) {\n // This is the usual starting point for opening a database.\n // Register a Promise that resolves when the database and related\n // files are ready to be used.\n this.log?.(`creating persistent file for ${path}`);\n const create = !!(flags & VFS.SQLITE_OPEN_CREATE);\n this._module.retryOps.push((async () => {\n try {\n // Get the path directory handle.\n let dirHandle = await navigator.storage.getDirectory();\n const directories = path.split('/').filter(d => d);\n const filename = directories.pop();\n for (const directory of directories) {\n dirHandle = await dirHandle.getDirectoryHandle(directory, { create });\n }\n\n // Get file handles for the database and related files,\n // and create persistent file instances.\n for (const suffix of DB_RELATED_FILE_SUFFIXES) {\n const fileHandle = await dirHandle.getFileHandle(filename + suffix, { create });\n await this.#createPersistentFile(fileHandle);\n }\n\n // Get access handles for the files.\n const file = new File(path, flags);\n file.persistentFile = this.persistentFiles.get(path);\n await this.#requestAccessHandle(file);\n } catch (e) {\n // Use an invalid persistent file to signal this error\n // for the retried open.\n const persistentFile = new PersistentFile(null);\n this.persistentFiles.set(path, persistentFile);\n console.error(e);\n }\n })());\n return VFS.SQLITE_BUSY;\n } else if (!persistentFile.fileHandle) {\n // The asynchronous open operation failed.\n this.persistentFiles.delete(path);\n return VFS.SQLITE_CANTOPEN;\n } else if (!persistentFile.accessHandle) {\n // This branch is reached if the database was previously opened\n // and closed.\n this._module.retryOps.push((async () => {\n const file = new File(path, flags);\n file.persistentFile = this.persistentFiles.get(path);\n await this.#requestAccessHandle(file);\n })());\n return VFS.SQLITE_BUSY;\n }\n }\n\n if (!this.accessiblePaths.has(path) &&\n !(flags & VFS.SQLITE_OPEN_CREATE)) {\n throw new Error(`File ${path} not found`);\n }\n\n const file = new File(path, flags);\n this.mapIdToFile.set(fileId, file);\n\n if (this.persistentFiles.has(path)) {\n file.persistentFile = this.persistentFiles.get(path);\n } else if (this.boundAccessHandles.has(path)) {\n // This temporary file was previously created and closed. Reopen\n // the same access handle.\n file.accessHandle = this.boundAccessHandles.get(path);\n } else if (this.unboundAccessHandles.size) {\n // Associate an unbound access handle to this file.\n file.accessHandle = this.unboundAccessHandles.values().next().value;\n file.accessHandle.truncate(0);\n this.unboundAccessHandles.delete(file.accessHandle);\n this.boundAccessHandles.set(path, file.accessHandle);\n }\n this.accessiblePaths.add(path);\n \n pOutFlags.setInt32(0, flags, true);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_CANTOPEN;\n }\n }\n\n /**\n * @param {string} zName \n * @param {number} syncDir \n * @returns {number}\n */\n jDelete(zName, syncDir) {\n try {\n const url = new URL(zName, 'file://');\n const path = url.pathname;\n if (this.persistentFiles.has(path)) {\n const persistentFile = this.persistentFiles.get(path);\n persistentFile.accessHandle.truncate(0);\n } else {\n this.boundAccessHandles.get(path)?.truncate(0);\n }\n this.accessiblePaths.delete(path);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_DELETE;\n }\n }\n\n /**\n * @param {string} zName \n * @param {number} flags \n * @param {DataView} pResOut \n * @returns {number}\n */\n jAccess(zName, flags, pResOut) {\n try {\n const url = new URL(zName, 'file://');\n const path = url.pathname;\n pResOut.setInt32(0, this.accessiblePaths.has(path) ? 1 : 0, true);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_ACCESS;\n } \n }\n\n /**\n * @param {number} fileId \n * @returns {number}\n */\n jClose(fileId) {\n try {\n const file = this.mapIdToFile.get(fileId);\n this.mapIdToFile.delete(fileId);\n\n if (file?.flags & VFS.SQLITE_OPEN_MAIN_DB) {\n if (file.persistentFile?.handleLockReleaser) {\n this.#releaseAccessHandle(file);\n }\n } else if (file?.flags & VFS.SQLITE_OPEN_DELETEONCLOSE) {\n file.accessHandle.truncate(0);\n this.accessiblePaths.delete(file.path);\n if (!this.persistentFiles.has(file.path)) {\n this.boundAccessHandles.delete(file.path);\n this.unboundAccessHandles.add(file.accessHandle);\n }\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_CLOSE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {Uint8Array} pData \n * @param {number} iOffset\n * @returns {number}\n */\n jRead(fileId, pData, iOffset) {\n try {\n const file = this.mapIdToFile.get(fileId);\n\n // On Chrome (at least), passing pData to accessHandle.read() is\n // an error because pData is a Proxy of a Uint8Array. Calling\n // subarray() produces a real Uint8Array and that works.\n const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n const bytesRead = accessHandle.read(pData.subarray(), { at: iOffset });\n\n // Opening a database file performs one read without a xLock call.\n if ((file.flags & VFS.SQLITE_OPEN_MAIN_DB) && !file.persistentFile.isFileLocked) {\n this.#releaseAccessHandle(file);\n }\n\n if (bytesRead < pData.byteLength) {\n pData.fill(0, bytesRead);\n return VFS.SQLITE_IOERR_SHORT_READ;\n }\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_READ;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {Uint8Array} pData \n * @param {number} iOffset\n * @returns {number}\n */\n jWrite(fileId, pData, iOffset) {\n try {\n const file = this.mapIdToFile.get(fileId);\n\n // On Chrome (at least), passing pData to accessHandle.write() is\n // an error because pData is a Proxy of a Uint8Array. Calling\n // subarray() produces a real Uint8Array and that works.\n const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n const nBytes = accessHandle.write(pData.subarray(), { at: iOffset });\n if (nBytes !== pData.byteLength) throw new Error('short write');\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_WRITE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} iSize \n * @returns {number}\n */\n jTruncate(fileId, iSize) {\n try {\n const file = this.mapIdToFile.get(fileId);\n const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n accessHandle.truncate(iSize);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_TRUNCATE;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} flags \n * @returns {number}\n */\n jSync(fileId, flags) {\n try {\n const file = this.mapIdToFile.get(fileId);\n const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n accessHandle.flush();\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_FSYNC;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {DataView} pSize64 \n * @returns {number}\n */\n jFileSize(fileId, pSize64) {\n try {\n const file = this.mapIdToFile.get(fileId);\n const accessHandle = file.accessHandle || file.persistentFile.accessHandle;\n const size = accessHandle.getSize();\n pSize64.setBigInt64(0, BigInt(size), true);\n return VFS.SQLITE_OK;\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR_FSTAT;\n }\n }\n\n /**\n * @param {number} fileId \n * @param {number} lockType \n * @returns {number}\n */\n jLock(fileId, lockType) {\n const file = this.mapIdToFile.get(fileId);\n if (file.persistentFile.isRequestInProgress) {\n file.persistentFile.isLockBusy = true;\n return VFS.SQLITE_BUSY;\n }\n\n file.persistentFile.isFileLocked = true;\n if (!file.persistentFile.handleLockReleaser) {\n // Start listening for notifications from other connections.\n // This is before we actually get access handles, but waiting to\n // listen until then allows a race condition where notifications\n // are missed. \n file.persistentFile.handleRequestChannel.onmessage = () => {\n this.log?.(`received notification for ${file.path}`);\n if (file.persistentFile.isFileLocked) {\n // We're still using the access handle, so mark it to be\n // released when we're done.\n file.persistentFile.isHandleRequested = true;\n } else {\n // Release the access handles immediately.\n this.#releaseAccessHandle(file);\n }\n file.persistentFile.handleRequestChannel.onmessage = null;\n };\n\n this.#requestAccessHandle(file);\n this.log?.('returning SQLITE_BUSY');\n file.persistentFile.isLockBusy = true;\n return VFS.SQLITE_BUSY;\n }\n file.persistentFile.isLockBusy = false;\n return VFS.SQLITE_OK;\n }\n\n /**\n * @param {number} fileId \n * @param {number} lockType \n * @returns {number}\n */\n jUnlock(fileId, lockType) {\n const file = this.mapIdToFile.get(fileId);\n if (lockType === VFS.SQLITE_LOCK_NONE) {\n // Don't change any state if this unlock is because xLock returned\n // SQLITE_BUSY.\n if (!file.persistentFile.isLockBusy) {\n if (file.persistentFile.isHandleRequested) {\n // Another connection wants the access handle.\n this.#releaseAccessHandle(file);\n file.persistentFile.isHandleRequested = false;\n }\n file.persistentFile.isFileLocked = false;\n }\n }\n return VFS.SQLITE_OK;\n }\n \n /**\n * @param {number} fileId\n * @param {number} op\n * @param {DataView} pArg\n * @returns {number|Promise<number>}\n */\n jFileControl(fileId, op, pArg) {\n try {\n const file = this.mapIdToFile.get(fileId);\n switch (op) {\n case VFS.SQLITE_FCNTL_PRAGMA:\n const key = extractString(pArg, 4);\n const value = extractString(pArg, 8);\n this.log?.('xFileControl', file.path, 'PRAGMA', key, value);\n switch (key.toLowerCase()) {\n case 'journal_mode':\n if (value &&\n !['off', 'memory', 'delete', 'wal'].includes(value.toLowerCase())) {\n throw new Error('journal_mode must be \"off\", \"memory\", \"delete\", or \"wal\"');\n }\n break;\n }\n break;\n }\n } catch (e) {\n this.lastError = e;\n return VFS.SQLITE_IOERR;\n }\n return VFS.SQLITE_NOTFOUND;\n }\n\n /**\n * @param {Uint8Array} zBuf \n * @returns \n */\n jGetLastError(zBuf) {\n if (this.lastError) {\n console.error(this.lastError);\n const outputArray = zBuf.subarray(0, zBuf.byteLength - 1);\n const { written } = new TextEncoder().encodeInto(this.lastError.message, outputArray);\n zBuf[written] = 0;\n }\n return VFS.SQLITE_OK\n }\n\n /**\n * @param {FileSystemFileHandle} fileHandle \n * @returns {Promise<PersistentFile>}\n */\n async #createPersistentFile(fileHandle) {\n const persistentFile = new PersistentFile(fileHandle);\n const root = await navigator.storage.getDirectory();\n const relativePath = await root.resolve(fileHandle);\n const path = `/${relativePath.join('/')}`;\n persistentFile.handleRequestChannel = new BroadcastChannel(`ahp:${path}`);\n this.persistentFiles.set(path, persistentFile);\n\n const f = await fileHandle.getFile();\n if (f.size) {\n this.accessiblePaths.add(path);\n }\n return persistentFile;\n }\n\n /**\n * @param {File} file \n */\n #requestAccessHandle(file) {\n console.assert(!file.persistentFile.handleLockReleaser);\n if (!file.persistentFile.isRequestInProgress) {\n file.persistentFile.isRequestInProgress = true;\n this._module.retryOps.push((async () => {\n // Acquire the Web Lock.\n file.persistentFile.handleLockReleaser = await this.#acquireLock(file.persistentFile);\n try {\n // Get access handles for the database and releated files in parallel.\n this.log?.(`creating access handles for ${file.path}`)\n await Promise.all(DB_RELATED_FILE_SUFFIXES.map(async suffix => {\n const persistentFile = this.persistentFiles.get(file.path + suffix);\n if (persistentFile) {\n persistentFile.accessHandle =\n await persistentFile.fileHandle.createSyncAccessHandle();\n }\n }));\n } catch (e) {\n this.log?.(`failed to create access handles for ${file.path}`, e);\n // Close any of the potentially opened access handles\n this.#releaseAccessHandle(file);\n throw e;\n } finally {\n file.persistentFile.isRequestInProgress = false;\n }\n })());\n return this._module.retryOps.at(-1);\n }\n return Promise.resolve();\n }\n\n /**\n * @param {File} file \n */\n async #releaseAccessHandle(file) {\n DB_RELATED_FILE_SUFFIXES.forEach(async suffix => {\n const persistentFile = this.persistentFiles.get(file.path + suffix);\n if (persistentFile) {\n persistentFile.accessHandle?.close();\n persistentFile.accessHandle = null;\n }\n });\n this.log?.(`access handles closed for ${file.path}`)\n\n file.persistentFile.handleLockReleaser?.();\n file.persistentFile.handleLockReleaser = null;\n this.log?.(`lock released for ${file.path}`)\n }\n\n /**\n * @param {PersistentFile} persistentFile \n * @returns {Promise<function>} lock releaser\n */\n #acquireLock(persistentFile) {\n return new Promise(resolve => {\n // Tell other connections we want the access handle.\n const lockName = persistentFile.handleRequestChannel.name;\n const notify = () => {\n this.log?.(`notifying for ${lockName}`);\n persistentFile.handleRequestChannel.postMessage(null);\n }\n const notifyId = setInterval(notify, LOCK_NOTIFY_INTERVAL);\n setTimeout(notify);\n\n this.log?.(`lock requested: ${lockName}`)\n navigator.locks.request(lockName, lock => {\n // We have the lock. Stop asking other connections for it.\n this.log?.(`lock acquired: ${lockName}`, lock);\n clearInterval(notifyId);\n return new Promise(resolve);\n });\n });\n }\n}\n\nfunction extractString(dataView, offset) {\n const p = dataView.getUint32(offset, true);\n if (p) {\n const chars = new Uint8Array(dataView.buffer, p);\n return new TextDecoder().decode(chars.subarray(0, chars.indexOf(0)));\n }\n return null;\n}"],"names":[],"ignoreList":[],"sourceRoot":""}
File without changes