@powersync/web 1.37.2 → 1.38.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 (76) hide show
  1. package/dist/2075a31bb151adbb9767.wasm +0 -0
  2. package/dist/3322bc84de986b63c2cd.wasm +0 -0
  3. package/dist/8e97452e297be23b5e50.wasm +0 -0
  4. package/dist/fbc178b70d530e8ce02b.wasm +0 -0
  5. package/dist/index.umd.js +4289 -4786
  6. package/dist/index.umd.js.map +1 -1
  7. package/dist/worker/SharedSyncImplementation.umd.js +165 -899
  8. package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
  9. package/dist/worker/WASQLiteDB.umd.js +227 -838
  10. package/dist/worker/WASQLiteDB.umd.js.map +1 -1
  11. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-9af0a7.umd.js +31 -0
  12. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-9af0a7.umd.js.map +1 -0
  13. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-bbf5a9.umd.js +31 -0
  14. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-bbf5a9.umd.js.map +1 -0
  15. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-c26e0f.umd.js +31 -0
  16. package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-cc5fcc.umd.js.map → node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-c26e0f.umd.js.map} +1 -1
  17. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +31 -0
  18. package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map → node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map} +1 -1
  19. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-2fb422.umd.js +3562 -0
  20. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-2fb422.umd.js.map +1 -0
  21. package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-0df390.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-96fb23.umd.js} +16 -16
  22. package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-0df390.umd.js.map → node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-96fb23.umd.js.map} +1 -1
  23. package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-151024.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-c89911.umd.js} +12 -12
  24. package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-151024.umd.js.map → node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-c89911.umd.js.map} +1 -1
  25. package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-c01ef0.umd.js → node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-ec4eb1.umd.js} +14 -14
  26. package/dist/worker/{node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-c01ef0.umd.js.map → node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-ec4eb1.umd.js.map} +1 -1
  27. package/lib/package.json +4 -7
  28. package/lib/src/db/PowerSyncDatabase.d.ts +1 -1
  29. package/lib/src/db/PowerSyncDatabase.js +0 -8
  30. package/lib/src/db/adapters/AsyncWebAdapter.d.ts +13 -3
  31. package/lib/src/db/adapters/AsyncWebAdapter.js +115 -21
  32. package/lib/src/db/adapters/wa-sqlite/DatabaseServer.js +1 -2
  33. package/lib/src/db/adapters/wa-sqlite/RawSqliteConnection.js +1 -1
  34. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +16 -2
  35. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +60 -38
  36. package/lib/src/db/adapters/wa-sqlite/vfs.d.ts +7 -18
  37. package/lib/src/db/adapters/wa-sqlite/vfs.js +34 -49
  38. package/lib/src/db/sync/SSRWebStreamingSyncImplementation.d.ts +4 -0
  39. package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js +4 -0
  40. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +0 -1
  41. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +0 -3
  42. package/lib/src/db/sync/WebRemote.d.ts +1 -3
  43. package/lib/src/db/sync/WebRemote.js +0 -12
  44. package/lib/src/worker/db/MultiDatabaseServer.js +4 -1
  45. package/lib/src/worker/db/open-worker-database.js +2 -2
  46. package/lib/src/worker/sync/SharedSyncImplementation.d.ts +0 -2
  47. package/lib/src/worker/sync/SharedSyncImplementation.js +4 -16
  48. package/lib/src/worker/sync/WorkerClient.d.ts +0 -1
  49. package/lib/src/worker/sync/WorkerClient.js +0 -3
  50. package/lib/tsconfig.tsbuildinfo +1 -1
  51. package/package.json +5 -8
  52. package/src/db/PowerSyncDatabase.ts +1 -9
  53. package/src/db/adapters/AsyncWebAdapter.ts +138 -22
  54. package/src/db/adapters/wa-sqlite/DatabaseServer.ts +4 -2
  55. package/src/db/adapters/wa-sqlite/RawSqliteConnection.ts +4 -1
  56. package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +89 -44
  57. package/src/db/adapters/wa-sqlite/vfs.ts +33 -49
  58. package/src/db/sync/SSRWebStreamingSyncImplementation.ts +5 -0
  59. package/src/db/sync/SharedWebStreamingSyncImplementation.ts +0 -4
  60. package/src/db/sync/WebRemote.ts +0 -16
  61. package/src/worker/db/MultiDatabaseServer.ts +4 -1
  62. package/src/worker/db/open-worker-database.ts +2 -2
  63. package/src/worker/sync/SharedSyncImplementation.ts +4 -18
  64. package/src/worker/sync/WorkerClient.ts +0 -4
  65. package/dist/26d61ca9f5694d064635.wasm +0 -0
  66. package/dist/b4c6283dc473b6b3fd24.wasm +0 -0
  67. package/dist/c78985091a0b22aaef03.wasm +0 -0
  68. package/dist/ca59e199e1138b553fad.wasm +0 -0
  69. package/dist/worker/node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js +0 -4646
  70. package/dist/worker/node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js.map +0 -1
  71. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-b9c070.umd.js +0 -31
  72. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-b9c070.umd.js.map +0 -1
  73. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-c99c07.umd.js +0 -31
  74. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-c99c07.umd.js.map +0 -1
  75. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-cc5fcc.umd.js +0 -31
  76. package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +0 -31
@@ -384,9 +384,9 @@ function generateUUID() {
384
384
 
385
385
  /***/ },
386
386
 
387
- /***/ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-api.js"
387
+ /***/ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-api.js"
388
388
  /*!*******************************************************************************************************************!*\
389
- !*** ../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-api.js ***!
389
+ !*** ../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-api.js ***!
390
390
  \*******************************************************************************************************************/
391
391
  (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
392
392
 
@@ -627,7 +627,7 @@ __webpack_require__.r(__webpack_exports__);
627
627
  /* harmony export */ SQLITE_WARNING: () => (/* reexport safe */ _sqlite_constants_js__WEBPACK_IMPORTED_MODULE_0__.SQLITE_WARNING),
628
628
  /* harmony export */ SQLiteError: () => (/* binding */ SQLiteError)
629
629
  /* harmony export */ });
630
- /* harmony import */ var _sqlite_constants_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./sqlite-constants.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-constants.js");
630
+ /* harmony import */ var _sqlite_constants_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./sqlite-constants.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-constants.js");
631
631
  // Copyright 2021 Roy T. Hashimoto. All Rights Reserved.
632
632
 
633
633
 
@@ -669,6 +669,7 @@ function Factory(Module) {
669
669
  /** @type {SQLiteAPI} */ const sqlite3 = {};
670
670
 
671
671
  Module.retryOps = [];
672
+ Module.pendingOps = [];
672
673
  const sqliteFreeAddress = Module._getSqliteFree();
673
674
 
674
675
  // Allocate some space for 32-bit returned values.
@@ -1524,7 +1525,7 @@ function Factory(Module) {
1524
1525
  // succeed.
1525
1526
  async function retry(f) {
1526
1527
  let rc;
1527
- do {
1528
+ for (let retryCount = 0; retryCount < 2; ++retryCount) {
1528
1529
  // Wait for all pending retry operations to complete. This is
1529
1530
  // normally empty on the first loop iteration.
1530
1531
  if (Module.retryOps.length) {
@@ -1536,9 +1537,20 @@ function Factory(Module) {
1536
1537
  }
1537
1538
 
1538
1539
  rc = await f();
1539
-
1540
- // Retry on failure with new pending retry operations.
1541
- } while (rc && Module.retryOps.length);
1540
+ if (rc === _sqlite_constants_js__WEBPACK_IMPORTED_MODULE_0__.SQLITE_OK || Module.retryOps.length === 0) {
1541
+ if (Module.pendingOps.length) {
1542
+ try {
1543
+ await Promise.all(Module.pendingOps);
1544
+ } catch (e) {
1545
+ console.error('Error in pendingOps:', e);
1546
+ return e.code || _sqlite_constants_js__WEBPACK_IMPORTED_MODULE_0__.SQLITE_ERROR;
1547
+ } finally {
1548
+ Module.pendingOps = [];
1549
+ }
1550
+ }
1551
+ return rc;
1552
+ }
1553
+ }
1542
1554
  return rc;
1543
1555
  }
1544
1556
 
@@ -1579,9 +1591,9 @@ function decl(s) {
1579
1591
 
1580
1592
  /***/ },
1581
1593
 
1582
- /***/ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-constants.js"
1594
+ /***/ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-constants.js"
1583
1595
  /*!*************************************************************************************************************************!*\
1584
- !*** ../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-constants.js ***!
1596
+ !*** ../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-constants.js ***!
1585
1597
  \*************************************************************************************************************************/
1586
1598
  (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
1587
1599
 
@@ -2332,8 +2344,7 @@ class DatabaseServer {
2332
2344
  },
2333
2345
  requestAccess: async (write, timeoutMs) => {
2334
2346
  requireOpen();
2335
- // TODO: Support timeouts, they don't seem to be supported by the async-mutex package.
2336
- const lease = await this.#inner.acquireConnection();
2347
+ const lease = await this.#inner.acquireConnection(timeoutMs != null ? AbortSignal.timeout(timeoutMs) : undefined);
2337
2348
  if (!isOpen) {
2338
2349
  // Race between requestAccess and close(), the connection was closed while we tried to acquire a lease.
2339
2350
  await lease.returnLease();
@@ -2405,7 +2416,7 @@ __webpack_require__.r(__webpack_exports__);
2405
2416
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
2406
2417
  /* harmony export */ RawSqliteConnection: () => (/* binding */ RawSqliteConnection)
2407
2418
  /* harmony export */ });
2408
- /* harmony import */ var _journeyapps_wa_sqlite__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @journeyapps/wa-sqlite */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-api.js");
2419
+ /* harmony import */ var _journeyapps_wa_sqlite__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @journeyapps/wa-sqlite */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-api.js");
2409
2420
  /* harmony import */ var _vfs_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./vfs.js */ "./lib/src/db/adapters/wa-sqlite/vfs.js");
2410
2421
 
2411
2422
 
@@ -2432,7 +2443,7 @@ class RawSqliteConnection {
2432
2443
  }
2433
2444
  async init() {
2434
2445
  const api = (this._sqliteAPI = await this.openSQLiteAPI());
2435
- this.db = await api.open_v2(this.options.dbFilename);
2446
+ this.db = await api.open_v2(this.options.dbFilename, this.options.isReadOnly ? 1 /* SQLITE_OPEN_READONLY */ : 6 /* SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE */);
2436
2447
  await this.executeRaw(`PRAGMA temp_store = ${this.options.temporaryStorage};`);
2437
2448
  if (this.options.encryptionKey) {
2438
2449
  const escapedKey = this.options.encryptionKey.replace("'", "''");
@@ -2566,11 +2577,7 @@ class RawSqliteConnection {
2566
2577
 
2567
2578
  __webpack_require__.r(__webpack_exports__);
2568
2579
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
2569
- /* harmony export */ AsyncWASQLiteModuleFactory: () => (/* binding */ AsyncWASQLiteModuleFactory),
2570
2580
  /* harmony export */ DEFAULT_MODULE_FACTORIES: () => (/* binding */ DEFAULT_MODULE_FACTORIES),
2571
- /* harmony export */ MultiCipherAsyncWASQLiteModuleFactory: () => (/* binding */ MultiCipherAsyncWASQLiteModuleFactory),
2572
- /* harmony export */ MultiCipherSyncWASQLiteModuleFactory: () => (/* binding */ MultiCipherSyncWASQLiteModuleFactory),
2573
- /* harmony export */ SyncWASQLiteModuleFactory: () => (/* binding */ SyncWASQLiteModuleFactory),
2574
2581
  /* harmony export */ WASQLiteVFS: () => (/* binding */ WASQLiteVFS),
2575
2582
  /* harmony export */ vfsRequiresDedicatedWorkers: () => (/* binding */ vfsRequiresDedicatedWorkers)
2576
2583
  /* harmony export */ });
@@ -2582,51 +2589,38 @@ var WASQLiteVFS;
2582
2589
  WASQLiteVFS["IDBBatchAtomicVFS"] = "IDBBatchAtomicVFS";
2583
2590
  WASQLiteVFS["OPFSCoopSyncVFS"] = "OPFSCoopSyncVFS";
2584
2591
  WASQLiteVFS["AccessHandlePoolVFS"] = "AccessHandlePoolVFS";
2592
+ WASQLiteVFS["OPFSWriteAheadVFS"] = "OPFSWriteAheadVFS";
2585
2593
  })(WASQLiteVFS || (WASQLiteVFS = {}));
2586
2594
  function vfsRequiresDedicatedWorkers(vfs) {
2587
2595
  return vfs != WASQLiteVFS.IDBBatchAtomicVFS;
2588
2596
  }
2589
- /**
2590
- * @internal
2591
- */
2592
- const AsyncWASQLiteModuleFactory = async () => {
2593
- const { default: factory } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-cc5fcc").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs"));
2594
- return factory();
2595
- };
2596
- /**
2597
- * @internal
2598
- */
2599
- const MultiCipherAsyncWASQLiteModuleFactory = async () => {
2600
- const { default: factory } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-c99c07").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/dist/mc-wa-sqlite-async.mjs */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/dist/mc-wa-sqlite-async.mjs"));
2601
- return factory();
2602
- };
2603
- /**
2604
- * @internal
2605
- */
2606
- const SyncWASQLiteModuleFactory = async () => {
2607
- const { default: factory } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/dist/wa-sqlite.mjs */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/dist/wa-sqlite.mjs"));
2608
- return factory();
2609
- };
2610
- /**
2611
- * @internal
2612
- */
2613
- const MultiCipherSyncWASQLiteModuleFactory = async () => {
2614
- const { default: factory } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-b9c070").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/dist/mc-wa-sqlite.mjs */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/dist/mc-wa-sqlite.mjs"));
2615
- return factory();
2616
- };
2597
+ async function asyncModuleFactory(encryptionKey) {
2598
+ if (encryptionKey) {
2599
+ const { default: factory } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-9af0a7").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/dist/mc-wa-sqlite-async.mjs */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/dist/mc-wa-sqlite-async.mjs"));
2600
+ return factory();
2601
+ }
2602
+ else {
2603
+ const { default: factory } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-c26e0f").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs"));
2604
+ return factory();
2605
+ }
2606
+ }
2607
+ async function syncModuleFactory(encryptionKey) {
2608
+ if (encryptionKey) {
2609
+ const { default: factory } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-bbf5a9").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/dist/mc-wa-sqlite.mjs */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/dist/mc-wa-sqlite.mjs"));
2610
+ return factory();
2611
+ }
2612
+ else {
2613
+ const { default: factory } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/dist/wa-sqlite.mjs */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/dist/wa-sqlite.mjs"));
2614
+ return factory();
2615
+ }
2616
+ }
2617
2617
  /**
2618
2618
  * @internal
2619
2619
  */
2620
2620
  const DEFAULT_MODULE_FACTORIES = {
2621
2621
  [WASQLiteVFS.IDBBatchAtomicVFS]: async (options) => {
2622
- let module;
2623
- if (options.encryptionKey) {
2624
- module = await MultiCipherAsyncWASQLiteModuleFactory();
2625
- }
2626
- else {
2627
- module = await AsyncWASQLiteModuleFactory();
2628
- }
2629
- const { IDBBatchAtomicVFS } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-0df390").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js"));
2622
+ const module = await asyncModuleFactory(options.encryptionKey);
2623
+ const { IDBBatchAtomicVFS } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-96fb23").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js"));
2630
2624
  return {
2631
2625
  module,
2632
2626
  // @ts-expect-error The types for this static method are missing upstream
@@ -2634,35 +2628,33 @@ const DEFAULT_MODULE_FACTORIES = {
2634
2628
  };
2635
2629
  },
2636
2630
  [WASQLiteVFS.AccessHandlePoolVFS]: async (options) => {
2637
- let module;
2638
- if (options.encryptionKey) {
2639
- module = await MultiCipherSyncWASQLiteModuleFactory();
2640
- }
2641
- else {
2642
- module = await SyncWASQLiteModuleFactory();
2643
- }
2631
+ const module = await syncModuleFactory(options.encryptionKey);
2644
2632
  // @ts-expect-error The types for this static method are missing upstream
2645
- const { AccessHandlePoolVFS } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-151024").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js"));
2633
+ const { AccessHandlePoolVFS } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-c89911").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js"));
2646
2634
  return {
2647
2635
  module,
2648
2636
  vfs: await AccessHandlePoolVFS.create(options.dbFileName, module)
2649
2637
  };
2650
2638
  },
2651
2639
  [WASQLiteVFS.OPFSCoopSyncVFS]: async (options) => {
2652
- let module;
2653
- if (options.encryptionKey) {
2654
- module = await MultiCipherSyncWASQLiteModuleFactory();
2655
- }
2656
- else {
2657
- module = await SyncWASQLiteModuleFactory();
2658
- }
2640
+ const module = await syncModuleFactory(options.encryptionKey);
2659
2641
  // @ts-expect-error The types for this static method are missing upstream
2660
- const { OPFSCoopSyncVFS } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_5_0_node_modules_journeyapps_wa-sqlite_src_examples-c01ef0").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js"));
2642
+ const { OPFSCoopSyncVFS } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-ec4eb1").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js"));
2661
2643
  const vfs = await OPFSCoopSyncVFS.create(options.dbFileName, module);
2662
2644
  return {
2663
2645
  module,
2664
2646
  vfs
2665
2647
  };
2648
+ },
2649
+ [WASQLiteVFS.OPFSWriteAheadVFS]: async (options) => {
2650
+ const module = await syncModuleFactory(options.encryptionKey);
2651
+ // @ts-expect-error The types for this static method are missing upstream
2652
+ const { OPFSWriteAheadVFS } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-2fb422").then(__webpack_require__.bind(__webpack_require__, /*! @journeyapps/wa-sqlite/src/examples/OPFSWriteAheadVFS.js */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/examples/OPFSWriteAheadVFS.js"));
2653
+ const vfs = await OPFSWriteAheadVFS.create(options.dbFileName, module, {});
2654
+ return {
2655
+ module,
2656
+ vfs
2657
+ };
2666
2658
  }
2667
2659
  };
2668
2660
 
@@ -2756,7 +2748,10 @@ class MultiDatabaseServer {
2756
2748
  const { dbFilename } = options;
2757
2749
  let server = this.activeDatabases.get(dbFilename);
2758
2750
  if (server == null) {
2759
- const needsNavigatorLocks = !isSharedWorker;
2751
+ // We don't need navigator locks for shared workers because all queries run in this shared worker exclusively.
2752
+ // For read-only connections, we use a VFS that supports concurrent reads (so a single lock on the connection is
2753
+ // fine).
2754
+ const needsNavigatorLocks = !(isSharedWorker || options.isReadOnly);
2760
2755
  const connection = new _db_adapters_wa_sqlite_RawSqliteConnection_js__WEBPACK_IMPORTED_MODULE_3__.RawSqliteConnection(options);
2761
2756
  const withSafeConcurrency = new _db_adapters_wa_sqlite_ConcurrentConnection_js__WEBPACK_IMPORTED_MODULE_4__.ConcurrentSqliteConnection(connection, needsNavigatorLocks);
2762
2757
  // Initializing the RawSqliteConnection will run some pragmas that might write to the database file, so we want
@@ -2862,9 +2857,6 @@ __webpack_require__.r(__webpack_exports__);
2862
2857
  /* harmony export */ MEMORY_TRIGGER_CLAIM_MANAGER: () => (/* binding */ MEMORY_TRIGGER_CLAIM_MANAGER),
2863
2858
  /* harmony export */ Mutex: () => (/* binding */ Mutex),
2864
2859
  /* harmony export */ OnChangeQueryProcessor: () => (/* binding */ OnChangeQueryProcessor),
2865
- /* harmony export */ OpType: () => (/* binding */ OpType),
2866
- /* harmony export */ OpTypeEnum: () => (/* binding */ OpTypeEnum),
2867
- /* harmony export */ OplogEntry: () => (/* binding */ OplogEntry),
2868
2860
  /* harmony export */ PSInternalTable: () => (/* binding */ PSInternalTable),
2869
2861
  /* harmony export */ PowerSyncControlCommand: () => (/* binding */ PowerSyncControlCommand),
2870
2862
  /* harmony export */ RowUpdateType: () => (/* binding */ RowUpdateType),
@@ -2872,8 +2864,6 @@ __webpack_require__.r(__webpack_exports__);
2872
2864
  /* harmony export */ Semaphore: () => (/* binding */ Semaphore),
2873
2865
  /* harmony export */ SqliteBucketStorage: () => (/* binding */ SqliteBucketStorage),
2874
2866
  /* harmony export */ SyncClientImplementation: () => (/* binding */ SyncClientImplementation),
2875
- /* harmony export */ SyncDataBatch: () => (/* binding */ SyncDataBatch),
2876
- /* harmony export */ SyncDataBucket: () => (/* binding */ SyncDataBucket),
2877
2867
  /* harmony export */ SyncProgress: () => (/* binding */ SyncProgress),
2878
2868
  /* harmony export */ SyncStatus: () => (/* binding */ SyncStatus),
2879
2869
  /* harmony export */ SyncStreamConnectionMethod: () => (/* binding */ SyncStreamConnectionMethod),
@@ -2891,18 +2881,10 @@ __webpack_require__.r(__webpack_exports__);
2891
2881
  /* harmony export */ createLogger: () => (/* binding */ createLogger),
2892
2882
  /* harmony export */ extractTableUpdates: () => (/* binding */ extractTableUpdates),
2893
2883
  /* harmony export */ isBatchedUpdateNotification: () => (/* binding */ isBatchedUpdateNotification),
2894
- /* harmony export */ isContinueCheckpointRequest: () => (/* binding */ isContinueCheckpointRequest),
2895
2884
  /* harmony export */ isDBAdapter: () => (/* binding */ isDBAdapter),
2896
2885
  /* harmony export */ isPowerSyncDatabaseOptionsWithSettings: () => (/* binding */ isPowerSyncDatabaseOptionsWithSettings),
2897
2886
  /* harmony export */ isSQLOpenFactory: () => (/* binding */ isSQLOpenFactory),
2898
2887
  /* harmony export */ isSQLOpenOptions: () => (/* binding */ isSQLOpenOptions),
2899
- /* harmony export */ isStreamingKeepalive: () => (/* binding */ isStreamingKeepalive),
2900
- /* harmony export */ isStreamingSyncCheckpoint: () => (/* binding */ isStreamingSyncCheckpoint),
2901
- /* harmony export */ isStreamingSyncCheckpointComplete: () => (/* binding */ isStreamingSyncCheckpointComplete),
2902
- /* harmony export */ isStreamingSyncCheckpointDiff: () => (/* binding */ isStreamingSyncCheckpointDiff),
2903
- /* harmony export */ isStreamingSyncCheckpointPartiallyComplete: () => (/* binding */ isStreamingSyncCheckpointPartiallyComplete),
2904
- /* harmony export */ isStreamingSyncData: () => (/* binding */ isStreamingSyncData),
2905
- /* harmony export */ isSyncNewCheckpointRequest: () => (/* binding */ isSyncNewCheckpointRequest),
2906
2888
  /* harmony export */ parseQuery: () => (/* binding */ parseQuery),
2907
2889
  /* harmony export */ runOnSchemaChange: () => (/* binding */ runOnSchemaChange),
2908
2890
  /* harmony export */ sanitizeSQL: () => (/* binding */ sanitizeSQL),
@@ -4880,8 +4862,15 @@ class BaseTransaction {
4880
4862
  class TransactionImplementation extends DBGetUtilsDefaultMixin(BaseTransaction) {
4881
4863
  static async runWith(ctx, fn) {
4882
4864
  let tx = new TransactionImplementation(ctx);
4865
+ // For write transactions, use BEGIN IMMEDIATE to immediately obtain a write lock on the database (instead of doing
4866
+ // that on the first statement). If we have a genuine read-only connection, we also use BEGIN IMMEDIATE there: In
4867
+ // WAL mode, that ensures we pin the current state of the database (instead of the state at the first statement in
4868
+ // the transaction). But if we have a "fake" read-only connection implemented through `pragma query_only = true`, we
4869
+ // can't use this trick because it would attempt to lock the connection. So there, we use a regular `BEGIN`
4870
+ // statement.
4871
+ const useBeginImmediate = ctx.connectionType != 'queryOnly';
4883
4872
  try {
4884
- await ctx.execute('BEGIN IMMEDIATE');
4873
+ await ctx.execute(useBeginImmediate ? 'BEGIN IMMEDIATE' : 'BEGIN');
4885
4874
  const result = await fn(tx);
4886
4875
  await tx.commit();
4887
4876
  return result;
@@ -5040,16 +5029,12 @@ class SyncStatus {
5040
5029
  *
5041
5030
  * This returns null when the database is currently being opened and we don't have reliable information about all
5042
5031
  * included streams yet.
5043
- *
5044
- * @experimental Sync streams are currently in alpha.
5045
5032
  */
5046
5033
  get syncStreams() {
5047
5034
  return this.options.dataFlow?.internalStreamSubscriptions?.map((core) => new SyncStreamStatusView(this, core));
5048
5035
  }
5049
5036
  /**
5050
5037
  * If the `stream` appears in {@link syncStreams}, returns the current status for that stream.
5051
- *
5052
- * @experimental Sync streams are currently in alpha.
5053
5038
  */
5054
5039
  forStream(stream) {
5055
5040
  const asJson = JSON.stringify(stream.parameters);
@@ -5632,7 +5617,7 @@ class SyncStreamSubscriptionHandle {
5632
5617
  constructor(subscription) {
5633
5618
  this.subscription = subscription;
5634
5619
  subscription.refcount++;
5635
- _finalizer?.register(this, subscription);
5620
+ _finalizer?.register(this, subscription, this);
5636
5621
  }
5637
5622
  get name() {
5638
5623
  return this.subscription.name;
@@ -6221,6 +6206,10 @@ var PowerSyncControlCommand;
6221
6206
  PowerSyncControlCommand["NOTIFY_TOKEN_REFRESHED"] = "refreshed_token";
6222
6207
  PowerSyncControlCommand["NOTIFY_CRUD_UPLOAD_COMPLETED"] = "completed_upload";
6223
6208
  PowerSyncControlCommand["UPDATE_SUBSCRIPTIONS"] = "update_subscriptions";
6209
+ /**
6210
+ * An `established` or `end` event for response streams.
6211
+ */
6212
+ PowerSyncControlCommand["CONNECTION_STATE"] = "connection";
6224
6213
  })(PowerSyncControlCommand || (PowerSyncControlCommand = {}));
6225
6214
 
6226
6215
  /**
@@ -6402,103 +6391,6 @@ class AbortOperation extends Error {
6402
6391
  }
6403
6392
  }
6404
6393
 
6405
- var OpTypeEnum;
6406
- (function (OpTypeEnum) {
6407
- OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
6408
- OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
6409
- OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
6410
- OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
6411
- })(OpTypeEnum || (OpTypeEnum = {}));
6412
- /**
6413
- * Used internally for sync buckets.
6414
- */
6415
- class OpType {
6416
- value;
6417
- static fromJSON(jsonValue) {
6418
- return new OpType(OpTypeEnum[jsonValue]);
6419
- }
6420
- constructor(value) {
6421
- this.value = value;
6422
- }
6423
- toJSON() {
6424
- return Object.entries(OpTypeEnum).find(([, value]) => value === this.value)[0];
6425
- }
6426
- }
6427
-
6428
- class OplogEntry {
6429
- op_id;
6430
- op;
6431
- checksum;
6432
- subkey;
6433
- object_type;
6434
- object_id;
6435
- data;
6436
- static fromRow(row) {
6437
- return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
6438
- }
6439
- constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
6440
- this.op_id = op_id;
6441
- this.op = op;
6442
- this.checksum = checksum;
6443
- this.subkey = subkey;
6444
- this.object_type = object_type;
6445
- this.object_id = object_id;
6446
- this.data = data;
6447
- }
6448
- toJSON(fixedKeyEncoding = false) {
6449
- return {
6450
- op_id: this.op_id,
6451
- op: this.op.toJSON(),
6452
- object_type: this.object_type,
6453
- object_id: this.object_id,
6454
- checksum: this.checksum,
6455
- data: this.data,
6456
- // Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
6457
- // but we need to migrate gradually to not break existing databases.
6458
- subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
6459
- };
6460
- }
6461
- }
6462
-
6463
- class SyncDataBucket {
6464
- bucket;
6465
- data;
6466
- has_more;
6467
- after;
6468
- next_after;
6469
- static fromRow(row) {
6470
- return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
6471
- }
6472
- constructor(bucket, data,
6473
- /**
6474
- * True if the response does not contain all the data for this bucket, and another request must be made.
6475
- */
6476
- has_more,
6477
- /**
6478
- * The `after` specified in the request.
6479
- */
6480
- after,
6481
- /**
6482
- * Use this for the next request.
6483
- */
6484
- next_after) {
6485
- this.bucket = bucket;
6486
- this.data = data;
6487
- this.has_more = has_more;
6488
- this.after = after;
6489
- this.next_after = next_after;
6490
- }
6491
- toJSON(fixedKeyEncoding = false) {
6492
- return {
6493
- bucket: this.bucket,
6494
- has_more: this.has_more,
6495
- after: this.after,
6496
- next_after: this.next_after,
6497
- data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
6498
- };
6499
- }
6500
- }
6501
-
6502
6394
  var buffer = {};
6503
6395
 
6504
6396
  var base64Js = {};
@@ -13654,7 +13546,7 @@ function requireDist () {
13654
13546
 
13655
13547
  var distExports = requireDist();
13656
13548
 
13657
- var version = "1.52.0";
13549
+ var version = "1.53.0";
13658
13550
  var PACKAGE = {
13659
13551
  version: version};
13660
13552
 
@@ -13824,22 +13716,6 @@ const doneResult = { done: true, value: undefined };
13824
13716
  function valueResult(value) {
13825
13717
  return { done: false, value };
13826
13718
  }
13827
- /**
13828
- * A variant of {@link Array.map} for async iterators.
13829
- */
13830
- function map(source, map) {
13831
- return {
13832
- next: async () => {
13833
- const value = await source.next();
13834
- if (value.done) {
13835
- return value;
13836
- }
13837
- else {
13838
- return { value: map(value.value) };
13839
- }
13840
- }
13841
- };
13842
- }
13843
13719
  /**
13844
13720
  * Expands a source async iterator by allowing to inject events asynchronously.
13845
13721
  *
@@ -14211,22 +14087,12 @@ class AbstractRemote {
14211
14087
  * Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
14212
14088
  *
14213
14089
  * The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
14214
- *
14215
- * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
14216
- * (required for compatibility with older sync services).
14217
14090
  */
14218
- async socketStreamRaw(options, bson) {
14091
+ async socketStreamRaw(options) {
14219
14092
  const { path, fetchStrategy = FetchStrategy.Buffered } = options;
14220
- const mimeType = bson == null ? 'application/json' : 'application/bson';
14093
+ const mimeType = 'application/json';
14221
14094
  function toBuffer(js) {
14222
- let contents;
14223
- if (bson != null) {
14224
- contents = bson.serialize(js);
14225
- }
14226
- else {
14227
- contents = JSON.stringify(js);
14228
- }
14229
- return bufferExports.Buffer.from(contents);
14095
+ return bufferExports.Buffer.from(JSON.stringify(js));
14230
14096
  }
14231
14097
  const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
14232
14098
  const request = await this.buildRequest(path);
@@ -14527,31 +14393,8 @@ function coreStatusToJs(status) {
14527
14393
  priorityStatusEntries: status.priority_status.map(priorityToJs)
14528
14394
  };
14529
14395
  }
14530
-
14531
- function isStreamingSyncData(line) {
14532
- return line.data != null;
14533
- }
14534
- function isStreamingKeepalive(line) {
14535
- return line.token_expires_in != null;
14536
- }
14537
- function isStreamingSyncCheckpoint(line) {
14538
- return line.checkpoint != null;
14539
- }
14540
- function isStreamingSyncCheckpointComplete(line) {
14541
- return line.checkpoint_complete != null;
14542
- }
14543
- function isStreamingSyncCheckpointPartiallyComplete(line) {
14544
- return line.partial_checkpoint_complete != null;
14545
- }
14546
- function isStreamingSyncCheckpointDiff(line) {
14547
- return line.checkpoint_diff != null;
14548
- }
14549
- function isContinueCheckpointRequest(request) {
14550
- return (Array.isArray(request.buckets) &&
14551
- typeof request.checkpoint_token == 'string');
14552
- }
14553
- function isSyncNewCheckpointRequest(request) {
14554
- return typeof request.request_checkpoint == 'object';
14396
+ function isInterruptingInstruction(instruction) {
14397
+ return 'EstablishSyncStream' in instruction || 'CloseSyncStream' in instruction;
14555
14398
  }
14556
14399
 
14557
14400
  var LockType;
@@ -14566,35 +14409,21 @@ var SyncStreamConnectionMethod;
14566
14409
  })(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
14567
14410
  var SyncClientImplementation;
14568
14411
  (function (SyncClientImplementation) {
14569
- /**
14570
- * Decodes and handles sync lines received from the sync service in JavaScript.
14571
- *
14572
- * This is the default option.
14573
- *
14574
- * @deprecated We recommend the {@link RUST} client implementation for all apps. If you have issues with
14575
- * the Rust client, please file an issue or reach out to us. The JavaScript client will be removed in a future
14576
- * version of the PowerSync SDK.
14577
- */
14578
- SyncClientImplementation["JAVASCRIPT"] = "js";
14579
14412
  /**
14580
14413
  * This implementation offloads the sync line decoding and handling into the PowerSync
14581
14414
  * core extension.
14582
14415
  *
14583
- * This option is more performant than the {@link JAVASCRIPT} client, enabled by default and the
14584
- * recommended client implementation for all apps.
14416
+ * This is the only option, as an older JavaScript client implementation has been removed from the SDK.
14585
14417
  *
14586
14418
  * ## Compatibility warning
14587
14419
  *
14588
14420
  * The Rust sync client stores sync data in a format that is slightly different than the one used
14589
- * by the old {@link JAVASCRIPT} implementation. When adopting the {@link RUST} client on existing
14590
- * databases, the PowerSync SDK will migrate the format automatically.
14591
- * Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
14592
- * the version introducing {@link RUST} as an option) also supports the new format, so you can switch
14593
- * back to {@link JAVASCRIPT} later.
14421
+ * by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
14422
+ * migrate the format automatically.
14594
14423
  *
14595
- * __However__: Upgrading the SDK version, then adopting {@link RUST} as a sync client and later
14596
- * downgrading the SDK to an older version (necessarily using the JavaScript-based implementation then)
14597
- * can lead to sync issues.
14424
+ * SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
14425
+ * implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
14426
+ * possible anymore. Problematic SDK versions have been released before 2025-06-09.
14598
14427
  */
14599
14428
  SyncClientImplementation["RUST"] = "rust";
14600
14429
  })(SyncClientImplementation || (SyncClientImplementation = {}));
@@ -14617,13 +14446,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
14617
14446
  serializedSchema: undefined,
14618
14447
  includeDefaultStreams: true
14619
14448
  };
14620
- // The priority we assume when we receive checkpoint lines where no priority is set.
14621
- // This is the default priority used by the sync service, but can be set to an arbitrary
14622
- // value since sync services without priorities also won't send partial sync completion
14623
- // messages.
14624
- const FALLBACK_PRIORITY = 3;
14625
14449
  class AbstractStreamingSyncImplementation extends BaseObserver {
14626
- _lastSyncedAt;
14627
14450
  options;
14628
14451
  abortController;
14629
14452
  // In rare cases, mostly for tests, uploads can be triggered without being properly connected.
@@ -14633,6 +14456,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
14633
14456
  streamingSyncPromise;
14634
14457
  logger;
14635
14458
  activeStreams;
14459
+ connectionMayHaveChanged = false;
14636
14460
  isUploadingCrud = false;
14637
14461
  notifyCompletedUploads;
14638
14462
  handleActiveStreamsChange;
@@ -14712,9 +14536,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
14712
14536
  this.crudUpdateListener = undefined;
14713
14537
  this.uploadAbortController?.abort();
14714
14538
  }
14715
- async hasCompletedSync() {
14716
- return this.options.adapter.hasCompletedSync();
14717
- }
14718
14539
  async getWriteCheckpoint() {
14719
14540
  const clientId = await this.options.adapter.getClientId();
14720
14541
  let path = `/write-checkpoint2.json?client_id=${clientId}`;
@@ -14796,7 +14617,7 @@ The next upload iteration will be delayed.`);
14796
14617
  });
14797
14618
  }
14798
14619
  }
14799
- this.uploadAbortController = null;
14620
+ this.uploadAbortController = undefined;
14800
14621
  }
14801
14622
  });
14802
14623
  }
@@ -14912,6 +14733,11 @@ The next upload iteration will be delayed.`);
14912
14733
  shouldDelayRetry = false;
14913
14734
  // A disconnect was requested, we should not delay since there is no explicit retry
14914
14735
  }
14736
+ else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
14737
+ this.connectionMayHaveChanged = false;
14738
+ this.logger.info('Sync error after changed connection, retrying immediately');
14739
+ shouldDelayRetry = false;
14740
+ }
14915
14741
  else {
14916
14742
  this.logger.error(ex);
14917
14743
  }
@@ -14942,17 +14768,14 @@ The next upload iteration will be delayed.`);
14942
14768
  // Mark as disconnected if here
14943
14769
  this.updateSyncStatus({ connected: false, connecting: false });
14944
14770
  }
14945
- async collectLocalBucketState() {
14946
- const bucketEntries = await this.options.adapter.getBucketStates();
14947
- const req = bucketEntries.map((entry) => ({
14948
- name: entry.bucket,
14949
- after: entry.op_id
14950
- }));
14951
- const localDescriptions = new Map();
14952
- for (const entry of bucketEntries) {
14953
- localDescriptions.set(entry.bucket, null);
14954
- }
14955
- return [req, localDescriptions];
14771
+ markConnectionMayHaveChanged() {
14772
+ // By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
14773
+ // an active sync iteration on the connection in use.
14774
+ this.connectionMayHaveChanged = true;
14775
+ // This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
14776
+ // make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
14777
+ // iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
14778
+ this.handleActiveStreamsChange?.();
14956
14779
  }
14957
14780
  /**
14958
14781
  * Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
@@ -14993,328 +14816,98 @@ The next upload iteration will be delayed.`);
14993
14816
  if (invalidMetadata.length > 0) {
14994
14817
  throw new Error(`Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`);
14995
14818
  }
14996
- const clientImplementation = resolvedOptions.clientImplementation;
14997
- this.updateSyncStatus({ clientImplementation });
14998
- if (clientImplementation == SyncClientImplementation.JAVASCRIPT) {
14999
- await this.legacyStreamingSyncIteration(signal, resolvedOptions);
15000
- return null;
15001
- }
15002
- else {
15003
- await this.requireKeyFormat(true);
15004
- return await this.rustSyncIteration(signal, resolvedOptions);
15005
- }
14819
+ await this.requireKeyFormat(true);
14820
+ return await this.rustSyncIteration(signal, resolvedOptions);
15006
14821
  }
15007
14822
  });
15008
14823
  }
15009
- async receiveSyncLines(data) {
15010
- const { options, connection, bson } = data;
14824
+ receiveSyncLines(data) {
14825
+ const { options, connection } = data;
15011
14826
  const remote = this.options.remote;
15012
- if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
15013
- return await remote.fetchStream(options);
15014
- }
15015
- else {
15016
- return await this.options.remote.socketStreamRaw({
15017
- ...options,
15018
- ...{ fetchStrategy: connection.fetchStrategy }
15019
- }, bson);
15020
- }
15021
- }
15022
- async legacyStreamingSyncIteration(signal, resolvedOptions) {
15023
- const rawTables = resolvedOptions.serializedSchema?.raw_tables;
15024
- if (rawTables != null && rawTables.length) {
15025
- this.logger.warn('Raw tables require the Rust-based sync client. The JS client will ignore them.');
15026
- }
15027
- if (this.activeStreams.length) {
15028
- this.logger.error('Sync streams require `clientImplementation: SyncClientImplementation.RUST` when connecting.');
15029
- }
15030
- this.logger.debug('Streaming sync iteration started');
15031
- this.options.adapter.startSession();
15032
- let [req, bucketMap] = await this.collectLocalBucketState();
15033
- let targetCheckpoint = null;
15034
- // A checkpoint that has been validated but not applied (e.g. due to pending local writes)
15035
- let pendingValidatedCheckpoint = null;
15036
- const clientId = await this.options.adapter.getClientId();
15037
- const usingFixedKeyFormat = await this.requireKeyFormat(false);
15038
- this.logger.debug('Requesting stream from server');
15039
- const syncOptions = {
15040
- path: '/sync/stream',
15041
- abortSignal: signal,
15042
- data: {
15043
- buckets: req,
15044
- include_checksum: true,
15045
- raw_data: true,
15046
- parameters: resolvedOptions.params,
15047
- app_metadata: resolvedOptions.appMetadata,
15048
- client_id: clientId
15049
- }
15050
- };
15051
- const bson = await this.options.remote.getBSON();
15052
- const source = await this.receiveSyncLines({
15053
- options: syncOptions,
15054
- connection: resolvedOptions,
15055
- bson
15056
- });
15057
- const stream = injectable(map(source, (line) => {
15058
- if (typeof line == 'string') {
15059
- return JSON.parse(line);
14827
+ const openInner = async () => {
14828
+ if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
14829
+ return await remote.fetchStream(options);
15060
14830
  }
15061
14831
  else {
15062
- return bson.deserialize(line);
14832
+ return await this.options.remote.socketStreamRaw({
14833
+ ...options,
14834
+ ...{ fetchStrategy: connection.fetchStrategy }
14835
+ });
15063
14836
  }
15064
- }));
15065
- this.logger.debug('Stream established. Processing events');
15066
- this.notifyCompletedUploads = () => {
15067
- stream.inject({ crud_upload_completed: null });
15068
14837
  };
15069
- while (true) {
15070
- const { value: line, done } = await stream.next();
15071
- if (done) {
15072
- // The stream has closed while waiting
15073
- return;
15074
- }
15075
- if ('crud_upload_completed' in line) {
15076
- if (pendingValidatedCheckpoint != null) {
15077
- const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
15078
- if (applied) {
15079
- pendingValidatedCheckpoint = null;
15080
- }
15081
- else if (endIteration) {
15082
- break;
15083
- }
14838
+ let inner;
14839
+ let done = false;
14840
+ return {
14841
+ async next() {
14842
+ if (done) {
14843
+ return doneResult;
15084
14844
  }
15085
- continue;
15086
- }
15087
- // A connection is active and messages are being received
15088
- if (!this.syncStatus.connected) {
15089
- // There is a connection now
15090
- Promise.resolve().then(() => this.triggerCrudUpload());
15091
- this.updateSyncStatus({
15092
- connected: true
15093
- });
15094
- }
15095
- if (isStreamingSyncCheckpoint(line)) {
15096
- targetCheckpoint = line.checkpoint;
15097
- // New checkpoint - existing validated checkpoint is no longer valid
15098
- pendingValidatedCheckpoint = null;
15099
- const bucketsToDelete = new Set(bucketMap.keys());
15100
- const newBuckets = new Map();
15101
- for (const checksum of line.checkpoint.buckets) {
15102
- newBuckets.set(checksum.bucket, {
15103
- name: checksum.bucket,
15104
- priority: checksum.priority ?? FALLBACK_PRIORITY
14845
+ else if (inner == null) {
14846
+ inner = await openInner();
14847
+ // We're connected here, so we can tell the core extension about it.
14848
+ return valueResult({
14849
+ command: PowerSyncControlCommand.CONNECTION_STATE,
14850
+ payload: 'established'
15105
14851
  });
15106
- bucketsToDelete.delete(checksum.bucket);
15107
- }
15108
- if (bucketsToDelete.size > 0) {
15109
- this.logger.debug('Removing buckets', [...bucketsToDelete]);
15110
- }
15111
- bucketMap = newBuckets;
15112
- await this.options.adapter.removeBuckets([...bucketsToDelete]);
15113
- await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
15114
- await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
15115
- }
15116
- else if (isStreamingSyncCheckpointComplete(line)) {
15117
- const result = await this.applyCheckpoint(targetCheckpoint);
15118
- if (result.endIteration) {
15119
- return;
15120
- }
15121
- else if (!result.applied) {
15122
- // "Could not apply checkpoint due to local data". We need to retry after
15123
- // finishing uploads.
15124
- pendingValidatedCheckpoint = targetCheckpoint;
15125
- }
15126
- else {
15127
- // Nothing to retry later. This would likely already be null from the last
15128
- // checksum or checksum_diff operation, but we make sure.
15129
- pendingValidatedCheckpoint = null;
15130
- }
15131
- }
15132
- else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
15133
- const priority = line.partial_checkpoint_complete.priority;
15134
- this.logger.debug('Partial checkpoint complete', priority);
15135
- const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint, priority);
15136
- if (!result.checkpointValid) {
15137
- // This means checksums failed. Start again with a new checkpoint.
15138
- // TODO: better back-off
15139
- await new Promise((resolve) => setTimeout(resolve, 50));
15140
- return;
15141
14852
  }
15142
- else if (!result.ready) ;
15143
14853
  else {
15144
- // We'll keep on downloading, but can report that this priority is synced now.
15145
- this.logger.debug('partial checkpoint validation succeeded');
15146
- // All states with a higher priority can be deleted since this partial sync includes them.
15147
- const priorityStates = this.syncStatus.priorityStatusEntries.filter((s) => s.priority <= priority);
15148
- priorityStates.push({
15149
- priority,
15150
- lastSyncedAt: new Date(),
15151
- hasSynced: true
15152
- });
15153
- this.updateSyncStatus({
15154
- connected: true,
15155
- priorityStatusEntries: priorityStates
15156
- });
15157
- }
15158
- }
15159
- else if (isStreamingSyncCheckpointDiff(line)) {
15160
- // TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
15161
- if (targetCheckpoint == null) {
15162
- throw new Error('Checkpoint diff without previous checkpoint');
15163
- }
15164
- // New checkpoint - existing validated checkpoint is no longer valid
15165
- pendingValidatedCheckpoint = null;
15166
- const diff = line.checkpoint_diff;
15167
- const newBuckets = new Map();
15168
- for (const checksum of targetCheckpoint.buckets) {
15169
- newBuckets.set(checksum.bucket, checksum);
15170
- }
15171
- for (const checksum of diff.updated_buckets) {
15172
- newBuckets.set(checksum.bucket, checksum);
15173
- }
15174
- for (const bucket of diff.removed_buckets) {
15175
- newBuckets.delete(bucket);
15176
- }
15177
- const newCheckpoint = {
15178
- last_op_id: diff.last_op_id,
15179
- buckets: [...newBuckets.values()],
15180
- write_checkpoint: diff.write_checkpoint
15181
- };
15182
- targetCheckpoint = newCheckpoint;
15183
- await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
15184
- bucketMap = new Map();
15185
- newBuckets.forEach((checksum, name) => bucketMap.set(name, {
15186
- name: checksum.bucket,
15187
- priority: checksum.priority ?? FALLBACK_PRIORITY
15188
- }));
15189
- const bucketsToDelete = diff.removed_buckets;
15190
- if (bucketsToDelete.length > 0) {
15191
- this.logger.debug('Remove buckets', bucketsToDelete);
15192
- }
15193
- await this.options.adapter.removeBuckets(bucketsToDelete);
15194
- await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
15195
- }
15196
- else if (isStreamingSyncData(line)) {
15197
- const { data } = line;
15198
- const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
15199
- let updatedProgress = null;
15200
- if (previousProgress) {
15201
- updatedProgress = { ...previousProgress };
15202
- const progressForBucket = updatedProgress[data.bucket];
15203
- if (progressForBucket) {
15204
- updatedProgress[data.bucket] = {
15205
- ...progressForBucket,
15206
- since_last: progressForBucket.since_last + data.data.length
15207
- };
14854
+ const event = await inner.next();
14855
+ if (event.done) {
14856
+ done = true;
14857
+ return valueResult({ command: PowerSyncControlCommand.CONNECTION_STATE, payload: 'end' });
15208
14858
  }
15209
- }
15210
- this.updateSyncStatus({
15211
- dataFlow: {
15212
- downloading: true,
15213
- downloadProgress: updatedProgress
14859
+ else {
14860
+ return valueResult({
14861
+ command: typeof event.value == 'string'
14862
+ ? PowerSyncControlCommand.PROCESS_TEXT_LINE
14863
+ : PowerSyncControlCommand.PROCESS_BSON_LINE,
14864
+ payload: event.value
14865
+ });
15214
14866
  }
15215
- });
15216
- await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
15217
- }
15218
- else if (isStreamingKeepalive(line)) {
15219
- const remaining_seconds = line.token_expires_in;
15220
- if (remaining_seconds == 0) {
15221
- // Connection would be closed automatically right after this
15222
- this.logger.debug('Token expiring; reconnect');
15223
- /**
15224
- * For a rare case where the backend connector does not update the token
15225
- * (uses the same one), this should have some delay.
15226
- */
15227
- await this.delayRetry();
15228
- return;
15229
- }
15230
- else if (remaining_seconds < 30) {
15231
- this.logger.debug('Token will expire soon; reconnect');
15232
- // Pre-emptively refresh the token
15233
- this.options.remote.invalidateCredentials();
15234
- return;
15235
14867
  }
15236
- this.triggerCrudUpload();
15237
14868
  }
15238
- else {
15239
- this.logger.debug('Received unknown sync line', line);
15240
- }
15241
- }
15242
- this.logger.debug('Stream input empty');
15243
- // Connection closed. Likely due to auth issue.
15244
- return;
14869
+ };
15245
14870
  }
15246
14871
  async rustSyncIteration(signal, resolvedOptions) {
15247
14872
  const syncImplementation = this;
15248
14873
  const adapter = this.options.adapter;
15249
14874
  const remote = this.options.remote;
15250
- const controller = new AbortController();
15251
- const abort = () => {
15252
- return controller.abort(signal.reason);
15253
- };
15254
- signal.addEventListener('abort', abort);
15255
- let receivingLines = null;
15256
- let hadSyncLine = false;
15257
14875
  let hideDisconnectOnRestart = false;
14876
+ let notifyTokenRefreshed;
15258
14877
  if (signal.aborted) {
15259
14878
  throw new AbortOperation('Connection request has been aborted');
15260
14879
  }
15261
- // Pending sync lines received from the service, as well as local events that trigger a powersync_control
15262
- // invocation (local events include refreshed tokens and completed uploads).
15263
- // This is a single data stream so that we can handle all control calls from a single place.
15264
- let controlInvocations = null;
15265
- async function connect(instr) {
15266
- const syncOptions = {
15267
- path: '/sync/stream',
15268
- abortSignal: controller.signal,
15269
- data: instr.request
14880
+ function startCommand() {
14881
+ const options = {
14882
+ parameters: resolvedOptions.params,
14883
+ app_metadata: resolvedOptions.appMetadata,
14884
+ active_streams: syncImplementation.activeStreams,
14885
+ include_defaults: resolvedOptions.includeDefaultStreams
15270
14886
  };
15271
- controlInvocations = injectable(map(await syncImplementation.receiveSyncLines({
15272
- options: syncOptions,
15273
- connection: resolvedOptions
15274
- }), (line) => {
15275
- if (typeof line == 'string') {
15276
- return {
15277
- command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
15278
- payload: line
15279
- };
15280
- }
15281
- else {
15282
- return {
15283
- command: PowerSyncControlCommand.PROCESS_BSON_LINE,
15284
- payload: line
15285
- };
15286
- }
15287
- }));
15288
- // The rust client will set connected: true after the first sync line because that's when it gets invoked, but
15289
- // we're already connected here and can report that.
15290
- syncImplementation.updateSyncStatus({ connected: true });
15291
- try {
15292
- while (true) {
15293
- let event = await controlInvocations.next();
15294
- if (event.done) {
15295
- break;
15296
- }
15297
- const line = event.value;
15298
- await control(line.command, line.payload);
15299
- if (!hadSyncLine) {
15300
- syncImplementation.triggerCrudUpload();
15301
- hadSyncLine = true;
15302
- }
15303
- }
15304
- }
15305
- finally {
15306
- abort();
15307
- signal.removeEventListener('abort', abort);
14887
+ if (resolvedOptions.serializedSchema) {
14888
+ options.schema = resolvedOptions.serializedSchema;
15308
14889
  }
14890
+ return invokePowerSyncControl(PowerSyncControlCommand.START, JSON.stringify(options));
15309
14891
  }
15310
14892
  async function stop() {
15311
- await control(PowerSyncControlCommand.STOP);
14893
+ const instructions = await invokePowerSyncControl(PowerSyncControlCommand.STOP);
14894
+ for (const instruction of instructions) {
14895
+ // We don't need to handle interrupting instructions since we're unconditionally ending the sync iteration at
14896
+ // this point.
14897
+ if (isInterruptingInstruction(instruction))
14898
+ continue;
14899
+ await handleInstruction(instruction);
14900
+ }
15312
14901
  }
15313
- async function control(op, payload) {
14902
+ async function invokePowerSyncControl(op, payload) {
15314
14903
  const rawResponse = await adapter.control(op, payload ?? null);
15315
14904
  const logger = syncImplementation.logger;
15316
14905
  logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
15317
- await handleInstructions(JSON.parse(rawResponse));
14906
+ if (op != PowerSyncControlCommand.STOP) {
14907
+ // Evidently we have a working connection here, otherwise powersync_control would have failed.
14908
+ syncImplementation.connectionMayHaveChanged = false;
14909
+ }
14910
+ return JSON.parse(rawResponse);
15318
14911
  }
15319
14912
  async function handleInstruction(instruction) {
15320
14913
  if ('LogLine' in instruction) {
@@ -15333,13 +14926,6 @@ The next upload iteration will be delayed.`);
15333
14926
  else if ('UpdateSyncStatus' in instruction) {
15334
14927
  syncImplementation.updateSyncStatus(coreStatusToJs(instruction.UpdateSyncStatus.status));
15335
14928
  }
15336
- else if ('EstablishSyncStream' in instruction) {
15337
- if (receivingLines != null) {
15338
- // Already connected, this shouldn't happen during a single iteration.
15339
- throw 'Unexpected request to establish sync stream, already connected';
15340
- }
15341
- receivingLines = connect(instruction.EstablishSyncStream);
15342
- }
15343
14929
  else if ('FetchCredentials' in instruction) {
15344
14930
  if (instruction.FetchCredentials.did_expire) {
15345
14931
  remote.invalidateCredentials();
@@ -15348,16 +14934,12 @@ The next upload iteration will be delayed.`);
15348
14934
  remote.invalidateCredentials();
15349
14935
  // Restart iteration after the credentials have been refreshed.
15350
14936
  remote.fetchCredentials().then((_) => {
15351
- controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
14937
+ notifyTokenRefreshed?.();
15352
14938
  }, (err) => {
15353
14939
  syncImplementation.logger.warn('Could not prefetch credentials', err);
15354
14940
  });
15355
14941
  }
15356
14942
  }
15357
- else if ('CloseSyncStream' in instruction) {
15358
- controller.abort();
15359
- hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
15360
- }
15361
14943
  else if ('FlushFileSystem' in instruction) ;
15362
14944
  else if ('DidCompleteSync' in instruction) {
15363
14945
  syncImplementation.updateSyncStatus({
@@ -15367,101 +14949,83 @@ The next upload iteration will be delayed.`);
15367
14949
  });
15368
14950
  }
15369
14951
  }
15370
- async function handleInstructions(instructions) {
15371
- for (const instr of instructions) {
15372
- await handleInstruction(instr);
15373
- }
15374
- }
15375
14952
  try {
15376
- const options = {
15377
- parameters: resolvedOptions.params,
15378
- app_metadata: resolvedOptions.appMetadata,
15379
- active_streams: this.activeStreams,
15380
- include_defaults: resolvedOptions.includeDefaultStreams
15381
- };
15382
- if (resolvedOptions.serializedSchema) {
15383
- options.schema = resolvedOptions.serializedSchema;
14953
+ const defaultResult = { immediateRestart: false };
14954
+ // Pending sync lines received from the service, as well as local events that trigger a powersync_control
14955
+ // invocation (local events include refreshed tokens and completed uploads).
14956
+ // This is a single data stream so that we can handle all control calls from a single place.
14957
+ let controlInvocations = null;
14958
+ for (const startInstruction of await startCommand()) {
14959
+ if ('EstablishSyncStream' in startInstruction) {
14960
+ const syncOptions = {
14961
+ path: '/sync/stream',
14962
+ abortSignal: signal,
14963
+ data: startInstruction.EstablishSyncStream.request
14964
+ };
14965
+ controlInvocations = injectable(syncImplementation.receiveSyncLines({
14966
+ options: syncOptions,
14967
+ connection: resolvedOptions
14968
+ }));
14969
+ }
14970
+ else if ('CloseSyncStream' in startInstruction) {
14971
+ return defaultResult;
14972
+ }
14973
+ else {
14974
+ await handleInstruction(startInstruction);
14975
+ }
15384
14976
  }
15385
- await control(PowerSyncControlCommand.START, JSON.stringify(options));
14977
+ if (controlInvocations == null)
14978
+ return defaultResult;
15386
14979
  this.notifyCompletedUploads = () => {
15387
- controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
14980
+ controlInvocations.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
15388
14981
  };
15389
14982
  this.handleActiveStreamsChange = () => {
15390
- controlInvocations?.inject({
14983
+ controlInvocations.inject({
15391
14984
  command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
15392
14985
  payload: JSON.stringify(this.activeStreams)
15393
14986
  });
15394
14987
  };
15395
- await receivingLines;
14988
+ notifyTokenRefreshed = () => {
14989
+ controlInvocations.inject({
14990
+ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED
14991
+ });
14992
+ };
14993
+ let hadSyncLine = false;
14994
+ loop: while (true) {
14995
+ const { done, value } = await controlInvocations.next();
14996
+ if (done)
14997
+ break;
14998
+ if (!hadSyncLine) {
14999
+ // Trigger a local CRUD upload when the first sync line has been received, this allows uploading local changes
15000
+ // that have been made while offline or disconnected.
15001
+ if (value.command == PowerSyncControlCommand.PROCESS_TEXT_LINE ||
15002
+ value.command == PowerSyncControlCommand.PROCESS_BSON_LINE) {
15003
+ hadSyncLine = true;
15004
+ this.triggerCrudUpload?.();
15005
+ }
15006
+ }
15007
+ const instructions = await invokePowerSyncControl(value.command, value.payload);
15008
+ for (const instruction of instructions) {
15009
+ if ('EstablishSyncStream' in instruction) {
15010
+ throw new Error('Received EstablishSyncStream while already connected.');
15011
+ }
15012
+ else if ('CloseSyncStream' in instruction) {
15013
+ hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
15014
+ break loop;
15015
+ }
15016
+ else {
15017
+ await handleInstruction(instruction);
15018
+ }
15019
+ }
15020
+ }
15396
15021
  }
15397
15022
  finally {
15398
15023
  this.notifyCompletedUploads = this.handleActiveStreamsChange = undefined;
15024
+ notifyTokenRefreshed = undefined;
15399
15025
  await stop();
15400
15026
  }
15401
15027
  return { immediateRestart: hideDisconnectOnRestart };
15402
15028
  }
15403
- async updateSyncStatusForStartingCheckpoint(checkpoint) {
15404
- const localProgress = await this.options.adapter.getBucketOperationProgress();
15405
- const progress = {};
15406
- let invalidated = false;
15407
- for (const bucket of checkpoint.buckets) {
15408
- const savedProgress = localProgress[bucket.bucket];
15409
- const atLast = savedProgress?.atLast ?? 0;
15410
- const sinceLast = savedProgress?.sinceLast ?? 0;
15411
- progress[bucket.bucket] = {
15412
- // The fallback priority doesn't matter here, but 3 is the one newer versions of the sync service
15413
- // will use by default.
15414
- priority: bucket.priority ?? 3,
15415
- at_last: atLast,
15416
- since_last: sinceLast,
15417
- target_count: bucket.count ?? 0
15418
- };
15419
- if (bucket.count != null && bucket.count < atLast + sinceLast) {
15420
- // Either due to a defrag / sync rule deploy or a compaction operation, the size
15421
- // of the bucket shrank so much that the local ops exceed the ops in the updated
15422
- // bucket. We can't prossibly report progress in this case (it would overshoot 100%).
15423
- invalidated = true;
15424
- }
15425
- }
15426
- if (invalidated) {
15427
- for (const bucket in progress) {
15428
- const bucketProgress = progress[bucket];
15429
- bucketProgress.at_last = 0;
15430
- bucketProgress.since_last = 0;
15431
- }
15432
- }
15433
- this.updateSyncStatus({
15434
- dataFlow: {
15435
- downloading: true,
15436
- downloadProgress: progress
15437
- }
15438
- });
15439
- }
15440
- async applyCheckpoint(checkpoint) {
15441
- let result = await this.options.adapter.syncLocalDatabase(checkpoint);
15442
- if (!result.checkpointValid) {
15443
- this.logger.debug(`Checksum mismatch in checkpoint ${checkpoint.last_op_id}, will reconnect`);
15444
- // This means checksums failed. Start again with a new checkpoint.
15445
- // TODO: better back-off
15446
- await new Promise((resolve) => setTimeout(resolve, 50));
15447
- return { applied: false, endIteration: true };
15448
- }
15449
- else if (!result.ready) {
15450
- this.logger.debug(`Could not apply checkpoint ${checkpoint.last_op_id} due to local data. We will retry applying the checkpoint after that upload is completed.`);
15451
- return { applied: false, endIteration: false };
15452
- }
15453
- this.logger.debug(`Applied checkpoint ${checkpoint.last_op_id}`, checkpoint);
15454
- this.updateSyncStatus({
15455
- connected: true,
15456
- lastSyncedAt: new Date(),
15457
- dataFlow: {
15458
- downloading: false,
15459
- downloadProgress: null,
15460
- downloadError: undefined
15461
- }
15462
- });
15463
- return { applied: true, endIteration: false };
15464
- }
15465
15029
  updateSyncStatus(options) {
15466
15030
  const updatedStatus = new SyncStatus({
15467
15031
  connected: options.connected ?? this.syncStatus.connected,
@@ -17000,14 +16564,12 @@ class SqliteBucketStorage extends BaseObserver {
17000
16564
  db;
17001
16565
  logger;
17002
16566
  tableNames;
17003
- _hasCompletedSync;
17004
16567
  updateListener;
17005
16568
  _clientId;
17006
16569
  constructor(db, logger = Logger.get('SqliteBucketStorage')) {
17007
16570
  super();
17008
16571
  this.db = db;
17009
16572
  this.logger = logger;
17010
- this._hasCompletedSync = false;
17011
16573
  this.tableNames = new Set();
17012
16574
  this.updateListener = db.registerListener({
17013
16575
  tablesUpdated: (update) => {
@@ -17019,7 +16581,6 @@ class SqliteBucketStorage extends BaseObserver {
17019
16581
  });
17020
16582
  }
17021
16583
  async init() {
17022
- this._hasCompletedSync = false;
17023
16584
  const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
17024
16585
  for (const row of existingTableRows ?? []) {
17025
16586
  this.tableNames.add(row.name);
@@ -17041,156 +16602,6 @@ class SqliteBucketStorage extends BaseObserver {
17041
16602
  getMaxOpId() {
17042
16603
  return MAX_OP_ID;
17043
16604
  }
17044
- /**
17045
- * Reset any caches.
17046
- */
17047
- startSession() { }
17048
- async getBucketStates() {
17049
- const result = await this.db.getAll("SELECT name as bucket, cast(last_op as TEXT) as op_id FROM ps_buckets WHERE pending_delete = 0 AND name != '$local'");
17050
- return result;
17051
- }
17052
- async getBucketOperationProgress() {
17053
- const rows = await this.db.getAll('SELECT name, count_at_last, count_since_last FROM ps_buckets');
17054
- return Object.fromEntries(rows.map((r) => [r.name, { atLast: r.count_at_last, sinceLast: r.count_since_last }]));
17055
- }
17056
- async saveSyncData(batch, fixedKeyFormat = false) {
17057
- await this.writeTransaction(async (tx) => {
17058
- for (const b of batch.buckets) {
17059
- await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
17060
- 'save',
17061
- JSON.stringify({ buckets: [b.toJSON(fixedKeyFormat)] })
17062
- ]);
17063
- this.logger.debug(`Saved batch of data for bucket: ${b.bucket}, operations: ${b.data.length}`);
17064
- }
17065
- });
17066
- }
17067
- async removeBuckets(buckets) {
17068
- for (const bucket of buckets) {
17069
- await this.deleteBucket(bucket);
17070
- }
17071
- }
17072
- /**
17073
- * Mark a bucket for deletion.
17074
- */
17075
- async deleteBucket(bucket) {
17076
- await this.writeTransaction(async (tx) => {
17077
- await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', ['delete_bucket', bucket]);
17078
- });
17079
- this.logger.debug(`Done deleting bucket ${bucket}`);
17080
- }
17081
- async hasCompletedSync() {
17082
- if (this._hasCompletedSync) {
17083
- return true;
17084
- }
17085
- const r = await this.db.get(`SELECT powersync_last_synced_at() as synced_at`);
17086
- const completed = r.synced_at != null;
17087
- if (completed) {
17088
- this._hasCompletedSync = true;
17089
- }
17090
- return completed;
17091
- }
17092
- async syncLocalDatabase(checkpoint, priority) {
17093
- const r = await this.validateChecksums(checkpoint, priority);
17094
- if (!r.checkpointValid) {
17095
- this.logger.error('Checksums failed for', r.checkpointFailures);
17096
- for (const b of r.checkpointFailures ?? []) {
17097
- await this.deleteBucket(b);
17098
- }
17099
- return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
17100
- }
17101
- if (priority == null) {
17102
- this.logger.debug(`Validated checksums checkpoint ${checkpoint.last_op_id}`);
17103
- }
17104
- else {
17105
- this.logger.debug(`Validated checksums for partial checkpoint ${checkpoint.last_op_id}, priority ${priority}`);
17106
- }
17107
- let buckets = checkpoint.buckets;
17108
- if (priority !== undefined) {
17109
- buckets = buckets.filter((b) => hasMatchingPriority(priority, b));
17110
- }
17111
- const bucketNames = buckets.map((b) => b.bucket);
17112
- await this.writeTransaction(async (tx) => {
17113
- await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
17114
- checkpoint.last_op_id,
17115
- JSON.stringify(bucketNames)
17116
- ]);
17117
- if (priority == null && checkpoint.write_checkpoint) {
17118
- await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
17119
- }
17120
- });
17121
- const valid = await this.updateObjectsFromBuckets(checkpoint, priority);
17122
- if (!valid) {
17123
- return { ready: false, checkpointValid: true };
17124
- }
17125
- return {
17126
- ready: true,
17127
- checkpointValid: true
17128
- };
17129
- }
17130
- /**
17131
- * Atomically update the local state to the current checkpoint.
17132
- *
17133
- * This includes creating new tables, dropping old tables, and copying data over from the oplog.
17134
- */
17135
- async updateObjectsFromBuckets(checkpoint, priority) {
17136
- let arg = '';
17137
- if (priority !== undefined) {
17138
- const affectedBuckets = [];
17139
- for (const desc of checkpoint.buckets) {
17140
- if (hasMatchingPriority(priority, desc)) {
17141
- affectedBuckets.push(desc.bucket);
17142
- }
17143
- }
17144
- arg = JSON.stringify({ priority, buckets: affectedBuckets });
17145
- }
17146
- return this.writeTransaction(async (tx) => {
17147
- const { insertId: result } = await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
17148
- 'sync_local',
17149
- arg
17150
- ]);
17151
- if (result == 1) {
17152
- if (priority == null) {
17153
- const bucketToCount = Object.fromEntries(checkpoint.buckets.map((b) => [b.bucket, b.count]));
17154
- // The two parameters could be replaced with one, but: https://github.com/powersync-ja/better-sqlite3/pull/6
17155
- const jsonBucketCount = JSON.stringify(bucketToCount);
17156
- await tx.execute("UPDATE ps_buckets SET count_since_last = 0, count_at_last = ?->name WHERE name != '$local' AND ?->name IS NOT NULL", [jsonBucketCount, jsonBucketCount]);
17157
- }
17158
- return true;
17159
- }
17160
- else {
17161
- return false;
17162
- }
17163
- });
17164
- }
17165
- async validateChecksums(checkpoint, priority) {
17166
- if (priority !== undefined) {
17167
- // Only validate the buckets within the priority we care about
17168
- const newBuckets = checkpoint.buckets.filter((cs) => hasMatchingPriority(priority, cs));
17169
- checkpoint = { ...checkpoint, buckets: newBuckets };
17170
- }
17171
- const rs = await this.db.execute('SELECT powersync_validate_checkpoint(?) as result', [
17172
- JSON.stringify({ ...checkpoint })
17173
- ]);
17174
- const resultItem = rs.rows?.item(0);
17175
- if (!resultItem) {
17176
- return {
17177
- checkpointValid: false,
17178
- ready: false,
17179
- checkpointFailures: []
17180
- };
17181
- }
17182
- const result = JSON.parse(resultItem['result']);
17183
- if (result['valid']) {
17184
- return { ready: true, checkpointValid: true };
17185
- }
17186
- else {
17187
- return {
17188
- checkpointValid: false,
17189
- ready: false,
17190
- checkpointFailures: result['failed_buckets']
17191
- };
17192
- }
17193
- }
17194
16605
  async updateLocalTarget(cb) {
17195
16606
  const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)", [MAX_OP_ID]);
17196
16607
  if (!rs1.length) {
@@ -17281,12 +16692,6 @@ class SqliteBucketStorage extends BaseObserver {
17281
16692
  async writeTransaction(callback, options) {
17282
16693
  return this.db.writeTransaction(callback, options);
17283
16694
  }
17284
- /**
17285
- * Set a target checkpoint.
17286
- */
17287
- async setTargetCheckpoint(checkpoint) {
17288
- // No-op for now
17289
- }
17290
16695
  async control(op, payload) {
17291
16696
  return await this.writeTransaction(async (tx) => {
17292
16697
  const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
@@ -17310,20 +16715,6 @@ class SqliteBucketStorage extends BaseObserver {
17310
16715
  }
17311
16716
  static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
17312
16717
  }
17313
- function hasMatchingPriority(priority, bucket) {
17314
- return bucket.priority != null && bucket.priority <= priority;
17315
- }
17316
-
17317
- // TODO JSON
17318
- class SyncDataBatch {
17319
- buckets;
17320
- static fromJSON(json) {
17321
- return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
17322
- }
17323
- constructor(buckets) {
17324
- this.buckets = buckets;
17325
- }
17326
- }
17327
16718
 
17328
16719
  /**
17329
16720
  * Thrown when an underlying database connection is closed.
@@ -17383,10 +16774,8 @@ class Schema {
17383
16774
  * developer instead of automatically by PowerSync.
17384
16775
  * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
17385
16776
  * using client-side table and column constraints.
17386
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
17387
16777
  *
17388
16778
  * @param tables An object of (table name, raw table definition) entries.
17389
- * @experimental Note that the raw tables API is still experimental and may change in the future.
17390
16779
  */
17391
16780
  withRawTables(tables) {
17392
16781
  for (const [name, rawTableDefinition] of Object.entries(tables)) {
@@ -17759,7 +17148,7 @@ var __webpack_exports__ = {};
17759
17148
  !*** ./lib/src/worker/db/WASQLiteDB.worker.js ***!
17760
17149
  \************************************************/
17761
17150
  __webpack_require__.r(__webpack_exports__);
17762
- /* harmony import */ var _journeyapps_wa_sqlite__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @journeyapps/wa-sqlite */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.5.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-api.js");
17151
+ /* harmony import */ var _journeyapps_wa_sqlite__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @journeyapps/wa-sqlite */ "../../node_modules/.pnpm/@journeyapps+wa-sqlite@1.7.0/node_modules/@journeyapps/wa-sqlite/src/sqlite-api.js");
17763
17152
  /* harmony import */ var _powersync_common__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @powersync/common */ "../common/dist/bundle.mjs");
17764
17153
  /* harmony import */ var comlink__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! comlink */ "../../node_modules/.pnpm/comlink@4.4.2/node_modules/comlink/dist/esm/comlink.mjs");
17765
17154
  /* harmony import */ var _MultiDatabaseServer_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./MultiDatabaseServer.js */ "./lib/src/worker/db/MultiDatabaseServer.js");