@powersync/web 1.37.1 → 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.
- package/dist/2075a31bb151adbb9767.wasm +0 -0
- package/dist/3322bc84de986b63c2cd.wasm +0 -0
- package/dist/8e97452e297be23b5e50.wasm +0 -0
- package/dist/fbc178b70d530e8ce02b.wasm +0 -0
- package/dist/index.umd.js +4289 -4786
- package/dist/index.umd.js.map +1 -1
- package/dist/worker/SharedSyncImplementation.umd.js +517 -1265
- package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
- package/dist/worker/WASQLiteDB.umd.js +576 -1201
- package/dist/worker/WASQLiteDB.umd.js.map +1 -1
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_7_0_node_modules_journeyapps_wa-sqlite_src_examples-2fb422.umd.js +3562 -0
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- package/lib/package.json +5 -8
- package/lib/src/db/PowerSyncDatabase.d.ts +1 -1
- package/lib/src/db/PowerSyncDatabase.js +0 -8
- package/lib/src/db/adapters/AsyncWebAdapter.d.ts +13 -3
- package/lib/src/db/adapters/AsyncWebAdapter.js +115 -21
- package/lib/src/db/adapters/wa-sqlite/DatabaseServer.js +1 -2
- package/lib/src/db/adapters/wa-sqlite/RawSqliteConnection.js +1 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +16 -2
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +60 -38
- package/lib/src/db/adapters/wa-sqlite/vfs.d.ts +7 -18
- package/lib/src/db/adapters/wa-sqlite/vfs.js +34 -49
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.d.ts +4 -0
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js +4 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +0 -1
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +0 -3
- package/lib/src/db/sync/WebRemote.d.ts +1 -3
- package/lib/src/db/sync/WebRemote.js +0 -12
- package/lib/src/worker/db/MultiDatabaseServer.js +4 -1
- package/lib/src/worker/db/open-worker-database.js +2 -2
- package/lib/src/worker/sync/SharedSyncImplementation.d.ts +0 -2
- package/lib/src/worker/sync/SharedSyncImplementation.js +4 -16
- package/lib/src/worker/sync/WorkerClient.d.ts +0 -1
- package/lib/src/worker/sync/WorkerClient.js +0 -3
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -9
- package/src/db/PowerSyncDatabase.ts +1 -9
- package/src/db/adapters/AsyncWebAdapter.ts +138 -22
- package/src/db/adapters/wa-sqlite/DatabaseServer.ts +4 -2
- package/src/db/adapters/wa-sqlite/RawSqliteConnection.ts +4 -1
- package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +89 -44
- package/src/db/adapters/wa-sqlite/vfs.ts +33 -49
- package/src/db/sync/SSRWebStreamingSyncImplementation.ts +5 -0
- package/src/db/sync/SharedWebStreamingSyncImplementation.ts +0 -4
- package/src/db/sync/WebRemote.ts +0 -16
- package/src/worker/db/MultiDatabaseServer.ts +4 -1
- package/src/worker/db/open-worker-database.ts +2 -2
- package/src/worker/sync/SharedSyncImplementation.ts +4 -18
- package/src/worker/sync/WorkerClient.ts +0 -4
- package/dist/26d61ca9f5694d064635.wasm +0 -0
- package/dist/b4c6283dc473b6b3fd24.wasm +0 -0
- package/dist/c78985091a0b22aaef03.wasm +0 -0
- package/dist/ca59e199e1138b553fad.wasm +0 -0
- package/dist/worker/node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js +0 -4646
- package/dist/worker/node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js.map +0 -1
- 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
- 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
- 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
- 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
- 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
- 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
|
@@ -652,7 +652,6 @@ class WebFetchProvider extends _powersync_common__WEBPACK_IMPORTED_MODULE_0__.Fe
|
|
|
652
652
|
class WebRemote extends _powersync_common__WEBPACK_IMPORTED_MODULE_0__.AbstractRemote {
|
|
653
653
|
connector;
|
|
654
654
|
logger;
|
|
655
|
-
_bson;
|
|
656
655
|
constructor(connector, logger = _powersync_common__WEBPACK_IMPORTED_MODULE_0__.DEFAULT_REMOTE_LOGGER, options) {
|
|
657
656
|
super(connector, logger, {
|
|
658
657
|
...(options ?? {}),
|
|
@@ -671,17 +670,6 @@ class WebRemote extends _powersync_common__WEBPACK_IMPORTED_MODULE_0__.AbstractR
|
|
|
671
670
|
}
|
|
672
671
|
return ua.join(' ');
|
|
673
672
|
}
|
|
674
|
-
async getBSON() {
|
|
675
|
-
if (this._bson) {
|
|
676
|
-
return this._bson;
|
|
677
|
-
}
|
|
678
|
-
/**
|
|
679
|
-
* Dynamic import to be used only when needed.
|
|
680
|
-
*/
|
|
681
|
-
const { BSON } = await __webpack_require__.e(/*! import() */ "node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs").then(__webpack_require__.bind(__webpack_require__, /*! bson */ "../../node_modules/.pnpm/bson@6.10.4/node_modules/bson/lib/bson.mjs"));
|
|
682
|
-
this._bson = BSON;
|
|
683
|
-
return this._bson;
|
|
684
|
-
}
|
|
685
673
|
}
|
|
686
674
|
|
|
687
675
|
|
|
@@ -1100,9 +1088,6 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
|
|
|
1100
1088
|
logger: this.logger
|
|
1101
1089
|
});
|
|
1102
1090
|
}
|
|
1103
|
-
get lastSyncedAt() {
|
|
1104
|
-
return this.connectionManager.syncStreamImplementation?.lastSyncedAt;
|
|
1105
|
-
}
|
|
1106
1091
|
get isConnected() {
|
|
1107
1092
|
return this.connectionManager.syncStreamImplementation?.isConnected ?? false;
|
|
1108
1093
|
}
|
|
@@ -1268,11 +1253,6 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
|
|
|
1268
1253
|
sync.triggerCrudUpload();
|
|
1269
1254
|
});
|
|
1270
1255
|
}
|
|
1271
|
-
async hasCompletedSync() {
|
|
1272
|
-
return this.withSyncImplementation(async (sync) => {
|
|
1273
|
-
return sync.hasCompletedSync();
|
|
1274
|
-
});
|
|
1275
|
-
}
|
|
1276
1256
|
async getWriteCheckpoint() {
|
|
1277
1257
|
return this.withSyncImplementation(async (sync) => {
|
|
1278
1258
|
return sync.getWriteCheckpoint();
|
|
@@ -1490,14 +1470,10 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
|
|
|
1490
1470
|
// We can poke the crud in case we missed any updates.
|
|
1491
1471
|
const impl = sharedSync.connectionManager.syncStreamImplementation;
|
|
1492
1472
|
impl?.triggerCrudUpload();
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
* StreamingSyncImplementation to reconnect. It would be nicer if we could trigger
|
|
1498
|
-
* this reconnect earlier.
|
|
1499
|
-
* This reconnect is not required for IndexedDB.
|
|
1500
|
-
*/
|
|
1473
|
+
// The Rust client implementation stores sync state on the connection level. Reopening the database causes a
|
|
1474
|
+
// disruption of the connection state and forces us to reconnect. We want to do that as soon as possible to
|
|
1475
|
+
// minimize downtime.
|
|
1476
|
+
impl?.markConnectionMayHaveChanged();
|
|
1501
1477
|
}
|
|
1502
1478
|
}
|
|
1503
1479
|
async readLock(fn, options) {
|
|
@@ -1640,9 +1616,6 @@ class WorkerClient {
|
|
|
1640
1616
|
getWriteCheckpoint() {
|
|
1641
1617
|
return this.sync.getWriteCheckpoint();
|
|
1642
1618
|
}
|
|
1643
|
-
hasCompletedSync() {
|
|
1644
|
-
return this.sync.hasCompletedSync();
|
|
1645
|
-
}
|
|
1646
1619
|
connect(options) {
|
|
1647
1620
|
return this.sync.connect(options);
|
|
1648
1621
|
}
|
|
@@ -1698,7 +1671,6 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1698
1671
|
/* harmony export */ DEFAULT_LOCK_TIMEOUT_MS: () => (/* binding */ DEFAULT_LOCK_TIMEOUT_MS),
|
|
1699
1672
|
/* harmony export */ DEFAULT_POWERSYNC_CLOSE_OPTIONS: () => (/* binding */ DEFAULT_POWERSYNC_CLOSE_OPTIONS),
|
|
1700
1673
|
/* harmony export */ DEFAULT_POWERSYNC_DB_OPTIONS: () => (/* binding */ DEFAULT_POWERSYNC_DB_OPTIONS),
|
|
1701
|
-
/* harmony export */ DEFAULT_PRESSURE_LIMITS: () => (/* binding */ DEFAULT_PRESSURE_LIMITS),
|
|
1702
1674
|
/* harmony export */ DEFAULT_REMOTE_LOGGER: () => (/* binding */ DEFAULT_REMOTE_LOGGER),
|
|
1703
1675
|
/* harmony export */ DEFAULT_REMOTE_OPTIONS: () => (/* binding */ DEFAULT_REMOTE_OPTIONS),
|
|
1704
1676
|
/* harmony export */ DEFAULT_RETRY_DELAY_MS: () => (/* binding */ DEFAULT_RETRY_DELAY_MS),
|
|
@@ -1709,7 +1681,6 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1709
1681
|
/* harmony export */ DEFAULT_TABLE_OPTIONS: () => (/* binding */ DEFAULT_TABLE_OPTIONS),
|
|
1710
1682
|
/* harmony export */ DEFAULT_WATCH_QUERY_OPTIONS: () => (/* binding */ DEFAULT_WATCH_QUERY_OPTIONS),
|
|
1711
1683
|
/* harmony export */ DEFAULT_WATCH_THROTTLE_MS: () => (/* binding */ DEFAULT_WATCH_THROTTLE_MS),
|
|
1712
|
-
/* harmony export */ DataStream: () => (/* binding */ DataStream),
|
|
1713
1684
|
/* harmony export */ DiffTriggerOperation: () => (/* binding */ DiffTriggerOperation),
|
|
1714
1685
|
/* harmony export */ DifferentialQueryProcessor: () => (/* binding */ DifferentialQueryProcessor),
|
|
1715
1686
|
/* harmony export */ EMPTY_DIFFERENTIAL: () => (/* binding */ EMPTY_DIFFERENTIAL),
|
|
@@ -1728,9 +1699,6 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1728
1699
|
/* harmony export */ MEMORY_TRIGGER_CLAIM_MANAGER: () => (/* binding */ MEMORY_TRIGGER_CLAIM_MANAGER),
|
|
1729
1700
|
/* harmony export */ Mutex: () => (/* binding */ Mutex),
|
|
1730
1701
|
/* harmony export */ OnChangeQueryProcessor: () => (/* binding */ OnChangeQueryProcessor),
|
|
1731
|
-
/* harmony export */ OpType: () => (/* binding */ OpType),
|
|
1732
|
-
/* harmony export */ OpTypeEnum: () => (/* binding */ OpTypeEnum),
|
|
1733
|
-
/* harmony export */ OplogEntry: () => (/* binding */ OplogEntry),
|
|
1734
1702
|
/* harmony export */ PSInternalTable: () => (/* binding */ PSInternalTable),
|
|
1735
1703
|
/* harmony export */ PowerSyncControlCommand: () => (/* binding */ PowerSyncControlCommand),
|
|
1736
1704
|
/* harmony export */ RowUpdateType: () => (/* binding */ RowUpdateType),
|
|
@@ -1738,8 +1706,6 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1738
1706
|
/* harmony export */ Semaphore: () => (/* binding */ Semaphore),
|
|
1739
1707
|
/* harmony export */ SqliteBucketStorage: () => (/* binding */ SqliteBucketStorage),
|
|
1740
1708
|
/* harmony export */ SyncClientImplementation: () => (/* binding */ SyncClientImplementation),
|
|
1741
|
-
/* harmony export */ SyncDataBatch: () => (/* binding */ SyncDataBatch),
|
|
1742
|
-
/* harmony export */ SyncDataBucket: () => (/* binding */ SyncDataBucket),
|
|
1743
1709
|
/* harmony export */ SyncProgress: () => (/* binding */ SyncProgress),
|
|
1744
1710
|
/* harmony export */ SyncStatus: () => (/* binding */ SyncStatus),
|
|
1745
1711
|
/* harmony export */ SyncStreamConnectionMethod: () => (/* binding */ SyncStreamConnectionMethod),
|
|
@@ -1757,18 +1723,10 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1757
1723
|
/* harmony export */ createLogger: () => (/* binding */ createLogger),
|
|
1758
1724
|
/* harmony export */ extractTableUpdates: () => (/* binding */ extractTableUpdates),
|
|
1759
1725
|
/* harmony export */ isBatchedUpdateNotification: () => (/* binding */ isBatchedUpdateNotification),
|
|
1760
|
-
/* harmony export */ isContinueCheckpointRequest: () => (/* binding */ isContinueCheckpointRequest),
|
|
1761
1726
|
/* harmony export */ isDBAdapter: () => (/* binding */ isDBAdapter),
|
|
1762
1727
|
/* harmony export */ isPowerSyncDatabaseOptionsWithSettings: () => (/* binding */ isPowerSyncDatabaseOptionsWithSettings),
|
|
1763
1728
|
/* harmony export */ isSQLOpenFactory: () => (/* binding */ isSQLOpenFactory),
|
|
1764
1729
|
/* harmony export */ isSQLOpenOptions: () => (/* binding */ isSQLOpenOptions),
|
|
1765
|
-
/* harmony export */ isStreamingKeepalive: () => (/* binding */ isStreamingKeepalive),
|
|
1766
|
-
/* harmony export */ isStreamingSyncCheckpoint: () => (/* binding */ isStreamingSyncCheckpoint),
|
|
1767
|
-
/* harmony export */ isStreamingSyncCheckpointComplete: () => (/* binding */ isStreamingSyncCheckpointComplete),
|
|
1768
|
-
/* harmony export */ isStreamingSyncCheckpointDiff: () => (/* binding */ isStreamingSyncCheckpointDiff),
|
|
1769
|
-
/* harmony export */ isStreamingSyncCheckpointPartiallyComplete: () => (/* binding */ isStreamingSyncCheckpointPartiallyComplete),
|
|
1770
|
-
/* harmony export */ isStreamingSyncData: () => (/* binding */ isStreamingSyncData),
|
|
1771
|
-
/* harmony export */ isSyncNewCheckpointRequest: () => (/* binding */ isSyncNewCheckpointRequest),
|
|
1772
1730
|
/* harmony export */ parseQuery: () => (/* binding */ parseQuery),
|
|
1773
1731
|
/* harmony export */ runOnSchemaChange: () => (/* binding */ runOnSchemaChange),
|
|
1774
1732
|
/* harmony export */ sanitizeSQL: () => (/* binding */ sanitizeSQL),
|
|
@@ -3173,6 +3131,8 @@ var EncodingType;
|
|
|
3173
3131
|
EncodingType["Base64"] = "base64";
|
|
3174
3132
|
})(EncodingType || (EncodingType = {}));
|
|
3175
3133
|
|
|
3134
|
+
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
3135
|
+
|
|
3176
3136
|
function getDefaultExportFromCjs (x) {
|
|
3177
3137
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
3178
3138
|
}
|
|
@@ -3253,7 +3213,7 @@ function requireEventIterator () {
|
|
|
3253
3213
|
this.removeCallback();
|
|
3254
3214
|
});
|
|
3255
3215
|
}
|
|
3256
|
-
[
|
|
3216
|
+
[symbolAsyncIterator]() {
|
|
3257
3217
|
return {
|
|
3258
3218
|
next: (value) => {
|
|
3259
3219
|
const result = this.pushQueue.shift();
|
|
@@ -3300,7 +3260,7 @@ function requireEventIterator () {
|
|
|
3300
3260
|
queue.eventHandlers[event] = fn;
|
|
3301
3261
|
},
|
|
3302
3262
|
}) || (() => { });
|
|
3303
|
-
this[
|
|
3263
|
+
this[symbolAsyncIterator] = () => queue[symbolAsyncIterator]();
|
|
3304
3264
|
Object.freeze(this);
|
|
3305
3265
|
}
|
|
3306
3266
|
}
|
|
@@ -3744,8 +3704,15 @@ class BaseTransaction {
|
|
|
3744
3704
|
class TransactionImplementation extends DBGetUtilsDefaultMixin(BaseTransaction) {
|
|
3745
3705
|
static async runWith(ctx, fn) {
|
|
3746
3706
|
let tx = new TransactionImplementation(ctx);
|
|
3707
|
+
// For write transactions, use BEGIN IMMEDIATE to immediately obtain a write lock on the database (instead of doing
|
|
3708
|
+
// that on the first statement). If we have a genuine read-only connection, we also use BEGIN IMMEDIATE there: In
|
|
3709
|
+
// WAL mode, that ensures we pin the current state of the database (instead of the state at the first statement in
|
|
3710
|
+
// the transaction). But if we have a "fake" read-only connection implemented through `pragma query_only = true`, we
|
|
3711
|
+
// can't use this trick because it would attempt to lock the connection. So there, we use a regular `BEGIN`
|
|
3712
|
+
// statement.
|
|
3713
|
+
const useBeginImmediate = ctx.connectionType != 'queryOnly';
|
|
3747
3714
|
try {
|
|
3748
|
-
await ctx.execute('BEGIN IMMEDIATE');
|
|
3715
|
+
await ctx.execute(useBeginImmediate ? 'BEGIN IMMEDIATE' : 'BEGIN');
|
|
3749
3716
|
const result = await fn(tx);
|
|
3750
3717
|
await tx.commit();
|
|
3751
3718
|
return result;
|
|
@@ -3904,16 +3871,12 @@ class SyncStatus {
|
|
|
3904
3871
|
*
|
|
3905
3872
|
* This returns null when the database is currently being opened and we don't have reliable information about all
|
|
3906
3873
|
* included streams yet.
|
|
3907
|
-
*
|
|
3908
|
-
* @experimental Sync streams are currently in alpha.
|
|
3909
3874
|
*/
|
|
3910
3875
|
get syncStreams() {
|
|
3911
3876
|
return this.options.dataFlow?.internalStreamSubscriptions?.map((core) => new SyncStreamStatusView(this, core));
|
|
3912
3877
|
}
|
|
3913
3878
|
/**
|
|
3914
3879
|
* If the `stream` appears in {@link syncStreams}, returns the current status for that stream.
|
|
3915
|
-
*
|
|
3916
|
-
* @experimental Sync streams are currently in alpha.
|
|
3917
3880
|
*/
|
|
3918
3881
|
forStream(stream) {
|
|
3919
3882
|
const asJson = JSON.stringify(stream.parameters);
|
|
@@ -4182,15 +4145,6 @@ class ControlledExecutor {
|
|
|
4182
4145
|
}
|
|
4183
4146
|
}
|
|
4184
4147
|
|
|
4185
|
-
/**
|
|
4186
|
-
* A ponyfill for `Symbol.asyncIterator` that is compatible with the
|
|
4187
|
-
* [recommended polyfill](https://github.com/Azure/azure-sdk-for-js/blob/%40azure/core-asynciterator-polyfill_1.0.2/sdk/core/core-asynciterator-polyfill/src/index.ts#L4-L6)
|
|
4188
|
-
* we recommend for React Native.
|
|
4189
|
-
*
|
|
4190
|
-
* As long as we use this symbol (instead of `for await` and `async *`) in this package, we can be compatible with async
|
|
4191
|
-
* iterators without requiring them.
|
|
4192
|
-
*/
|
|
4193
|
-
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
4194
4148
|
/**
|
|
4195
4149
|
* Throttle a function to be called at most once every "wait" milliseconds,
|
|
4196
4150
|
* on the trailing edge.
|
|
@@ -4505,7 +4459,7 @@ class SyncStreamSubscriptionHandle {
|
|
|
4505
4459
|
constructor(subscription) {
|
|
4506
4460
|
this.subscription = subscription;
|
|
4507
4461
|
subscription.refcount++;
|
|
4508
|
-
_finalizer?.register(this, subscription);
|
|
4462
|
+
_finalizer?.register(this, subscription, this);
|
|
4509
4463
|
}
|
|
4510
4464
|
get name() {
|
|
4511
4465
|
return this.subscription.name;
|
|
@@ -5094,6 +5048,10 @@ var PowerSyncControlCommand;
|
|
|
5094
5048
|
PowerSyncControlCommand["NOTIFY_TOKEN_REFRESHED"] = "refreshed_token";
|
|
5095
5049
|
PowerSyncControlCommand["NOTIFY_CRUD_UPLOAD_COMPLETED"] = "completed_upload";
|
|
5096
5050
|
PowerSyncControlCommand["UPDATE_SUBSCRIPTIONS"] = "update_subscriptions";
|
|
5051
|
+
/**
|
|
5052
|
+
* An `established` or `end` event for response streams.
|
|
5053
|
+
*/
|
|
5054
|
+
PowerSyncControlCommand["CONNECTION_STATE"] = "connection";
|
|
5097
5055
|
})(PowerSyncControlCommand || (PowerSyncControlCommand = {}));
|
|
5098
5056
|
|
|
5099
5057
|
/**
|
|
@@ -5275,103 +5233,6 @@ class AbortOperation extends Error {
|
|
|
5275
5233
|
}
|
|
5276
5234
|
}
|
|
5277
5235
|
|
|
5278
|
-
var OpTypeEnum;
|
|
5279
|
-
(function (OpTypeEnum) {
|
|
5280
|
-
OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
|
|
5281
|
-
OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
|
|
5282
|
-
OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
|
|
5283
|
-
OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
|
|
5284
|
-
})(OpTypeEnum || (OpTypeEnum = {}));
|
|
5285
|
-
/**
|
|
5286
|
-
* Used internally for sync buckets.
|
|
5287
|
-
*/
|
|
5288
|
-
class OpType {
|
|
5289
|
-
value;
|
|
5290
|
-
static fromJSON(jsonValue) {
|
|
5291
|
-
return new OpType(OpTypeEnum[jsonValue]);
|
|
5292
|
-
}
|
|
5293
|
-
constructor(value) {
|
|
5294
|
-
this.value = value;
|
|
5295
|
-
}
|
|
5296
|
-
toJSON() {
|
|
5297
|
-
return Object.entries(OpTypeEnum).find(([, value]) => value === this.value)[0];
|
|
5298
|
-
}
|
|
5299
|
-
}
|
|
5300
|
-
|
|
5301
|
-
class OplogEntry {
|
|
5302
|
-
op_id;
|
|
5303
|
-
op;
|
|
5304
|
-
checksum;
|
|
5305
|
-
subkey;
|
|
5306
|
-
object_type;
|
|
5307
|
-
object_id;
|
|
5308
|
-
data;
|
|
5309
|
-
static fromRow(row) {
|
|
5310
|
-
return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
|
|
5311
|
-
}
|
|
5312
|
-
constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
|
|
5313
|
-
this.op_id = op_id;
|
|
5314
|
-
this.op = op;
|
|
5315
|
-
this.checksum = checksum;
|
|
5316
|
-
this.subkey = subkey;
|
|
5317
|
-
this.object_type = object_type;
|
|
5318
|
-
this.object_id = object_id;
|
|
5319
|
-
this.data = data;
|
|
5320
|
-
}
|
|
5321
|
-
toJSON(fixedKeyEncoding = false) {
|
|
5322
|
-
return {
|
|
5323
|
-
op_id: this.op_id,
|
|
5324
|
-
op: this.op.toJSON(),
|
|
5325
|
-
object_type: this.object_type,
|
|
5326
|
-
object_id: this.object_id,
|
|
5327
|
-
checksum: this.checksum,
|
|
5328
|
-
data: this.data,
|
|
5329
|
-
// Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
|
|
5330
|
-
// but we need to migrate gradually to not break existing databases.
|
|
5331
|
-
subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
|
|
5332
|
-
};
|
|
5333
|
-
}
|
|
5334
|
-
}
|
|
5335
|
-
|
|
5336
|
-
class SyncDataBucket {
|
|
5337
|
-
bucket;
|
|
5338
|
-
data;
|
|
5339
|
-
has_more;
|
|
5340
|
-
after;
|
|
5341
|
-
next_after;
|
|
5342
|
-
static fromRow(row) {
|
|
5343
|
-
return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
|
|
5344
|
-
}
|
|
5345
|
-
constructor(bucket, data,
|
|
5346
|
-
/**
|
|
5347
|
-
* True if the response does not contain all the data for this bucket, and another request must be made.
|
|
5348
|
-
*/
|
|
5349
|
-
has_more,
|
|
5350
|
-
/**
|
|
5351
|
-
* The `after` specified in the request.
|
|
5352
|
-
*/
|
|
5353
|
-
after,
|
|
5354
|
-
/**
|
|
5355
|
-
* Use this for the next request.
|
|
5356
|
-
*/
|
|
5357
|
-
next_after) {
|
|
5358
|
-
this.bucket = bucket;
|
|
5359
|
-
this.data = data;
|
|
5360
|
-
this.has_more = has_more;
|
|
5361
|
-
this.after = after;
|
|
5362
|
-
this.next_after = next_after;
|
|
5363
|
-
}
|
|
5364
|
-
toJSON(fixedKeyEncoding = false) {
|
|
5365
|
-
return {
|
|
5366
|
-
bucket: this.bucket,
|
|
5367
|
-
has_more: this.has_more,
|
|
5368
|
-
after: this.after,
|
|
5369
|
-
next_after: this.next_after,
|
|
5370
|
-
data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
|
|
5371
|
-
};
|
|
5372
|
-
}
|
|
5373
|
-
}
|
|
5374
|
-
|
|
5375
5236
|
var buffer = {};
|
|
5376
5237
|
|
|
5377
5238
|
var base64Js = {};
|
|
@@ -12527,177 +12388,10 @@ function requireDist () {
|
|
|
12527
12388
|
|
|
12528
12389
|
var distExports = requireDist();
|
|
12529
12390
|
|
|
12530
|
-
var version = "1.
|
|
12391
|
+
var version = "1.53.0";
|
|
12531
12392
|
var PACKAGE = {
|
|
12532
12393
|
version: version};
|
|
12533
12394
|
|
|
12534
|
-
const DEFAULT_PRESSURE_LIMITS = {
|
|
12535
|
-
highWater: 10,
|
|
12536
|
-
lowWater: 0
|
|
12537
|
-
};
|
|
12538
|
-
/**
|
|
12539
|
-
* A very basic implementation of a data stream with backpressure support which does not use
|
|
12540
|
-
* native JS streams or async iterators.
|
|
12541
|
-
* This is handy for environments such as React Native which need polyfills for the above.
|
|
12542
|
-
*/
|
|
12543
|
-
class DataStream extends BaseObserver {
|
|
12544
|
-
options;
|
|
12545
|
-
dataQueue;
|
|
12546
|
-
isClosed;
|
|
12547
|
-
processingPromise;
|
|
12548
|
-
notifyDataAdded;
|
|
12549
|
-
logger;
|
|
12550
|
-
mapLine;
|
|
12551
|
-
constructor(options) {
|
|
12552
|
-
super();
|
|
12553
|
-
this.options = options;
|
|
12554
|
-
this.processingPromise = null;
|
|
12555
|
-
this.isClosed = false;
|
|
12556
|
-
this.dataQueue = [];
|
|
12557
|
-
this.mapLine = options?.mapLine ?? ((line) => line);
|
|
12558
|
-
this.logger = options?.logger ?? Logger.get('DataStream');
|
|
12559
|
-
if (options?.closeOnError) {
|
|
12560
|
-
const l = this.registerListener({
|
|
12561
|
-
error: (ex) => {
|
|
12562
|
-
l?.();
|
|
12563
|
-
this.close();
|
|
12564
|
-
}
|
|
12565
|
-
});
|
|
12566
|
-
}
|
|
12567
|
-
}
|
|
12568
|
-
get highWatermark() {
|
|
12569
|
-
return this.options?.pressure?.highWaterMark ?? DEFAULT_PRESSURE_LIMITS.highWater;
|
|
12570
|
-
}
|
|
12571
|
-
get lowWatermark() {
|
|
12572
|
-
return this.options?.pressure?.lowWaterMark ?? DEFAULT_PRESSURE_LIMITS.lowWater;
|
|
12573
|
-
}
|
|
12574
|
-
get closed() {
|
|
12575
|
-
return this.isClosed;
|
|
12576
|
-
}
|
|
12577
|
-
async close() {
|
|
12578
|
-
this.isClosed = true;
|
|
12579
|
-
await this.processingPromise;
|
|
12580
|
-
this.iterateListeners((l) => l.closed?.());
|
|
12581
|
-
// Discard any data in the queue
|
|
12582
|
-
this.dataQueue = [];
|
|
12583
|
-
this.listeners.clear();
|
|
12584
|
-
}
|
|
12585
|
-
/**
|
|
12586
|
-
* Enqueues data for the consumers to read
|
|
12587
|
-
*/
|
|
12588
|
-
enqueueData(data) {
|
|
12589
|
-
if (this.isClosed) {
|
|
12590
|
-
throw new Error('Cannot enqueue data into closed stream.');
|
|
12591
|
-
}
|
|
12592
|
-
this.dataQueue.push(data);
|
|
12593
|
-
this.notifyDataAdded?.();
|
|
12594
|
-
this.processQueue();
|
|
12595
|
-
}
|
|
12596
|
-
/**
|
|
12597
|
-
* Reads data once from the data stream
|
|
12598
|
-
* @returns a Data payload or Null if the stream closed.
|
|
12599
|
-
*/
|
|
12600
|
-
async read() {
|
|
12601
|
-
if (this.closed) {
|
|
12602
|
-
return null;
|
|
12603
|
-
}
|
|
12604
|
-
// Wait for any pending processing to complete first.
|
|
12605
|
-
// This ensures we register our listener before calling processQueue(),
|
|
12606
|
-
// avoiding a race where processQueue() sees no reader and returns early.
|
|
12607
|
-
if (this.processingPromise) {
|
|
12608
|
-
await this.processingPromise;
|
|
12609
|
-
}
|
|
12610
|
-
// Re-check after await - stream may have closed while we were waiting
|
|
12611
|
-
if (this.closed) {
|
|
12612
|
-
return null;
|
|
12613
|
-
}
|
|
12614
|
-
return new Promise((resolve, reject) => {
|
|
12615
|
-
const l = this.registerListener({
|
|
12616
|
-
data: async (data) => {
|
|
12617
|
-
resolve(data);
|
|
12618
|
-
// Remove the listener
|
|
12619
|
-
l?.();
|
|
12620
|
-
},
|
|
12621
|
-
closed: () => {
|
|
12622
|
-
resolve(null);
|
|
12623
|
-
l?.();
|
|
12624
|
-
},
|
|
12625
|
-
error: (ex) => {
|
|
12626
|
-
reject(ex);
|
|
12627
|
-
l?.();
|
|
12628
|
-
}
|
|
12629
|
-
});
|
|
12630
|
-
this.processQueue();
|
|
12631
|
-
});
|
|
12632
|
-
}
|
|
12633
|
-
/**
|
|
12634
|
-
* Executes a callback for each data item in the stream
|
|
12635
|
-
*/
|
|
12636
|
-
forEach(callback) {
|
|
12637
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
12638
|
-
this.iterateAsyncErrored(async (l) => l.lowWater?.());
|
|
12639
|
-
}
|
|
12640
|
-
return this.registerListener({
|
|
12641
|
-
data: callback
|
|
12642
|
-
});
|
|
12643
|
-
}
|
|
12644
|
-
processQueue() {
|
|
12645
|
-
if (this.processingPromise) {
|
|
12646
|
-
return;
|
|
12647
|
-
}
|
|
12648
|
-
const promise = (this.processingPromise = this._processQueue());
|
|
12649
|
-
promise.finally(() => {
|
|
12650
|
-
this.processingPromise = null;
|
|
12651
|
-
});
|
|
12652
|
-
return promise;
|
|
12653
|
-
}
|
|
12654
|
-
hasDataReader() {
|
|
12655
|
-
return Array.from(this.listeners.values()).some((l) => !!l.data);
|
|
12656
|
-
}
|
|
12657
|
-
async _processQueue() {
|
|
12658
|
-
/**
|
|
12659
|
-
* Allow listeners to mutate the queue before processing.
|
|
12660
|
-
* This allows for operations such as dropping or compressing data
|
|
12661
|
-
* on high water or requesting more data on low water.
|
|
12662
|
-
*/
|
|
12663
|
-
if (this.dataQueue.length >= this.highWatermark) {
|
|
12664
|
-
await this.iterateAsyncErrored(async (l) => l.highWater?.());
|
|
12665
|
-
}
|
|
12666
|
-
if (this.isClosed || !this.hasDataReader()) {
|
|
12667
|
-
return;
|
|
12668
|
-
}
|
|
12669
|
-
if (this.dataQueue.length) {
|
|
12670
|
-
const data = this.dataQueue.shift();
|
|
12671
|
-
const mapped = this.mapLine(data);
|
|
12672
|
-
await this.iterateAsyncErrored(async (l) => l.data?.(mapped));
|
|
12673
|
-
}
|
|
12674
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
12675
|
-
const dataAdded = new Promise((resolve) => {
|
|
12676
|
-
this.notifyDataAdded = resolve;
|
|
12677
|
-
});
|
|
12678
|
-
await Promise.race([this.iterateAsyncErrored(async (l) => l.lowWater?.()), dataAdded]);
|
|
12679
|
-
this.notifyDataAdded = null;
|
|
12680
|
-
}
|
|
12681
|
-
if (this.dataQueue.length > 0) {
|
|
12682
|
-
setTimeout(() => this.processQueue());
|
|
12683
|
-
}
|
|
12684
|
-
}
|
|
12685
|
-
async iterateAsyncErrored(cb) {
|
|
12686
|
-
// Important: We need to copy the listeners, as calling a listener could result in adding another
|
|
12687
|
-
// listener, resulting in infinite loops.
|
|
12688
|
-
const listeners = Array.from(this.listeners.values());
|
|
12689
|
-
for (let i of listeners) {
|
|
12690
|
-
try {
|
|
12691
|
-
await cb(i);
|
|
12692
|
-
}
|
|
12693
|
-
catch (ex) {
|
|
12694
|
-
this.logger.error(ex);
|
|
12695
|
-
this.iterateListeners((l) => l.error?.(ex));
|
|
12696
|
-
}
|
|
12697
|
-
}
|
|
12698
|
-
}
|
|
12699
|
-
}
|
|
12700
|
-
|
|
12701
12395
|
var WebsocketDuplexConnection = {};
|
|
12702
12396
|
|
|
12703
12397
|
var hasRequiredWebsocketDuplexConnection;
|
|
@@ -12860,8 +12554,199 @@ class WebsocketClientTransport {
|
|
|
12860
12554
|
}
|
|
12861
12555
|
}
|
|
12862
12556
|
|
|
12557
|
+
const doneResult = { done: true, value: undefined };
|
|
12558
|
+
function valueResult(value) {
|
|
12559
|
+
return { done: false, value };
|
|
12560
|
+
}
|
|
12561
|
+
/**
|
|
12562
|
+
* Expands a source async iterator by allowing to inject events asynchronously.
|
|
12563
|
+
*
|
|
12564
|
+
* The resulting iterator will emit all events from its source. Additionally though, events can be injected. These
|
|
12565
|
+
* events are dropped once the main iterator completes, but are otherwise forwarded.
|
|
12566
|
+
*
|
|
12567
|
+
* The iterator completes when its source completes, and it supports backpressure by only calling `next()` on the source
|
|
12568
|
+
* in response to a `next()` call from downstream if no pending injected events can be dispatched.
|
|
12569
|
+
*/
|
|
12570
|
+
function injectable(source) {
|
|
12571
|
+
let sourceIsDone = false;
|
|
12572
|
+
let waiter = undefined; // An active, waiting next() call.
|
|
12573
|
+
// A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
|
|
12574
|
+
let pendingSourceEvent = null;
|
|
12575
|
+
let pendingInjectedEvents = [];
|
|
12576
|
+
const consumeWaiter = () => {
|
|
12577
|
+
const pending = waiter;
|
|
12578
|
+
waiter = undefined;
|
|
12579
|
+
return pending;
|
|
12580
|
+
};
|
|
12581
|
+
const fetchFromSource = () => {
|
|
12582
|
+
const resolveWaiter = (propagate) => {
|
|
12583
|
+
const active = consumeWaiter();
|
|
12584
|
+
if (active) {
|
|
12585
|
+
propagate(active);
|
|
12586
|
+
}
|
|
12587
|
+
else {
|
|
12588
|
+
pendingSourceEvent = propagate;
|
|
12589
|
+
}
|
|
12590
|
+
};
|
|
12591
|
+
const nextFromSource = source.next();
|
|
12592
|
+
nextFromSource.then((value) => {
|
|
12593
|
+
sourceIsDone = value.done == true;
|
|
12594
|
+
resolveWaiter((w) => w.resolve(value));
|
|
12595
|
+
}, (error) => {
|
|
12596
|
+
resolveWaiter((w) => w.reject(error));
|
|
12597
|
+
});
|
|
12598
|
+
};
|
|
12599
|
+
return {
|
|
12600
|
+
next: () => {
|
|
12601
|
+
return new Promise((resolve, reject) => {
|
|
12602
|
+
// First priority: Dispatch ready upstream events.
|
|
12603
|
+
if (sourceIsDone) {
|
|
12604
|
+
return resolve(doneResult);
|
|
12605
|
+
}
|
|
12606
|
+
if (pendingSourceEvent) {
|
|
12607
|
+
pendingSourceEvent({ resolve, reject });
|
|
12608
|
+
pendingSourceEvent = null;
|
|
12609
|
+
return;
|
|
12610
|
+
}
|
|
12611
|
+
// Second priority: Dispatch injected events
|
|
12612
|
+
if (pendingInjectedEvents.length) {
|
|
12613
|
+
return resolve(valueResult(pendingInjectedEvents.shift()));
|
|
12614
|
+
}
|
|
12615
|
+
// Nothing pending? Fetch from source
|
|
12616
|
+
waiter = { resolve, reject };
|
|
12617
|
+
return fetchFromSource();
|
|
12618
|
+
});
|
|
12619
|
+
},
|
|
12620
|
+
inject: (event) => {
|
|
12621
|
+
const pending = consumeWaiter();
|
|
12622
|
+
if (pending != null) {
|
|
12623
|
+
pending.resolve(valueResult(event));
|
|
12624
|
+
}
|
|
12625
|
+
else {
|
|
12626
|
+
pendingInjectedEvents.push(event);
|
|
12627
|
+
}
|
|
12628
|
+
}
|
|
12629
|
+
};
|
|
12630
|
+
}
|
|
12631
|
+
/**
|
|
12632
|
+
* Splits a byte stream at line endings, emitting each line as a string.
|
|
12633
|
+
*/
|
|
12634
|
+
function extractJsonLines(source, decoder) {
|
|
12635
|
+
let buffer = '';
|
|
12636
|
+
const pendingLines = [];
|
|
12637
|
+
let isFinalEvent = false;
|
|
12638
|
+
return {
|
|
12639
|
+
next: async () => {
|
|
12640
|
+
while (true) {
|
|
12641
|
+
if (isFinalEvent) {
|
|
12642
|
+
return doneResult;
|
|
12643
|
+
}
|
|
12644
|
+
{
|
|
12645
|
+
const first = pendingLines.shift();
|
|
12646
|
+
if (first) {
|
|
12647
|
+
return { done: false, value: first };
|
|
12648
|
+
}
|
|
12649
|
+
}
|
|
12650
|
+
const { done, value } = await source.next();
|
|
12651
|
+
if (done) {
|
|
12652
|
+
const remaining = buffer.trim();
|
|
12653
|
+
if (remaining.length != 0) {
|
|
12654
|
+
isFinalEvent = true;
|
|
12655
|
+
return { done: false, value: remaining };
|
|
12656
|
+
}
|
|
12657
|
+
return doneResult;
|
|
12658
|
+
}
|
|
12659
|
+
const data = decoder.decode(value, { stream: true });
|
|
12660
|
+
buffer += data;
|
|
12661
|
+
const lines = buffer.split('\n');
|
|
12662
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
12663
|
+
const l = lines[i].trim();
|
|
12664
|
+
if (l.length > 0) {
|
|
12665
|
+
pendingLines.push(l);
|
|
12666
|
+
}
|
|
12667
|
+
}
|
|
12668
|
+
buffer = lines[lines.length - 1];
|
|
12669
|
+
}
|
|
12670
|
+
}
|
|
12671
|
+
};
|
|
12672
|
+
}
|
|
12673
|
+
/**
|
|
12674
|
+
* Splits a concatenated stream of BSON objects by emitting individual objects.
|
|
12675
|
+
*/
|
|
12676
|
+
function extractBsonObjects(source) {
|
|
12677
|
+
// Fully read but not emitted yet.
|
|
12678
|
+
const completedObjects = [];
|
|
12679
|
+
// Whether source has returned { done: true }. We do the same once completed objects have been emitted.
|
|
12680
|
+
let isDone = false;
|
|
12681
|
+
const lengthBuffer = new DataView(new ArrayBuffer(4));
|
|
12682
|
+
let objectBody = null;
|
|
12683
|
+
// If we're parsing the length field, a number between 1 and 4 (inclusive) describing remaining bytes in the header.
|
|
12684
|
+
// If we're consuming a document, the bytes remaining.
|
|
12685
|
+
let remainingLength = 4;
|
|
12686
|
+
return {
|
|
12687
|
+
async next() {
|
|
12688
|
+
while (true) {
|
|
12689
|
+
// Before fetching new data from upstream, return completed objects.
|
|
12690
|
+
if (completedObjects.length) {
|
|
12691
|
+
return valueResult(completedObjects.shift());
|
|
12692
|
+
}
|
|
12693
|
+
if (isDone) {
|
|
12694
|
+
return doneResult;
|
|
12695
|
+
}
|
|
12696
|
+
const upstreamEvent = await source.next();
|
|
12697
|
+
if (upstreamEvent.done) {
|
|
12698
|
+
isDone = true;
|
|
12699
|
+
if (objectBody || remainingLength != 4) {
|
|
12700
|
+
throw new Error('illegal end of stream in BSON object');
|
|
12701
|
+
}
|
|
12702
|
+
return doneResult;
|
|
12703
|
+
}
|
|
12704
|
+
const chunk = upstreamEvent.value;
|
|
12705
|
+
for (let i = 0; i < chunk.length;) {
|
|
12706
|
+
const availableInData = chunk.length - i;
|
|
12707
|
+
if (objectBody) {
|
|
12708
|
+
// We're in the middle of reading a BSON document.
|
|
12709
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
12710
|
+
const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
|
|
12711
|
+
objectBody.set(copySource, objectBody.length - remainingLength);
|
|
12712
|
+
i += bytesToRead;
|
|
12713
|
+
remainingLength -= bytesToRead;
|
|
12714
|
+
if (remainingLength == 0) {
|
|
12715
|
+
completedObjects.push(objectBody);
|
|
12716
|
+
// Prepare to read another document, starting with its length
|
|
12717
|
+
objectBody = null;
|
|
12718
|
+
remainingLength = 4;
|
|
12719
|
+
}
|
|
12720
|
+
}
|
|
12721
|
+
else {
|
|
12722
|
+
// Copy up to 4 bytes into lengthBuffer, depending on how many we still need.
|
|
12723
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
12724
|
+
for (let j = 0; j < bytesToRead; j++) {
|
|
12725
|
+
lengthBuffer.setUint8(4 - remainingLength + j, chunk[i + j]);
|
|
12726
|
+
}
|
|
12727
|
+
i += bytesToRead;
|
|
12728
|
+
remainingLength -= bytesToRead;
|
|
12729
|
+
if (remainingLength == 0) {
|
|
12730
|
+
// Transition from reading length header to reading document. Subtracting 4 because the length of the
|
|
12731
|
+
// header is included in length.
|
|
12732
|
+
const length = lengthBuffer.getInt32(0, true /* little endian */);
|
|
12733
|
+
remainingLength = length - 4;
|
|
12734
|
+
if (remainingLength < 1) {
|
|
12735
|
+
throw new Error(`invalid length for bson: ${length}`);
|
|
12736
|
+
}
|
|
12737
|
+
objectBody = new Uint8Array(length);
|
|
12738
|
+
new DataView(objectBody.buffer).setInt32(0, length, true);
|
|
12739
|
+
}
|
|
12740
|
+
}
|
|
12741
|
+
}
|
|
12742
|
+
}
|
|
12743
|
+
}
|
|
12744
|
+
};
|
|
12745
|
+
}
|
|
12746
|
+
|
|
12863
12747
|
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
|
|
12864
12748
|
const POWERSYNC_JS_VERSION = PACKAGE.version;
|
|
12749
|
+
const SYNC_QUEUE_REQUEST_HIGH_WATER = 10;
|
|
12865
12750
|
const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
|
|
12866
12751
|
// Keep alive message is sent every period
|
|
12867
12752
|
const KEEP_ALIVE_MS = 20_000;
|
|
@@ -13041,73 +12926,67 @@ class AbstractRemote {
|
|
|
13041
12926
|
return new WebSocket(url);
|
|
13042
12927
|
}
|
|
13043
12928
|
/**
|
|
13044
|
-
* Returns a data stream of sync line data.
|
|
12929
|
+
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
13045
12930
|
*
|
|
13046
|
-
*
|
|
13047
|
-
* @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
|
|
13048
|
-
* (required for compatibility with older sync services).
|
|
12931
|
+
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
13049
12932
|
*/
|
|
13050
|
-
async socketStreamRaw(options
|
|
12933
|
+
async socketStreamRaw(options) {
|
|
13051
12934
|
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
|
|
13052
|
-
const mimeType =
|
|
12935
|
+
const mimeType = 'application/json';
|
|
13053
12936
|
function toBuffer(js) {
|
|
13054
|
-
|
|
13055
|
-
if (bson != null) {
|
|
13056
|
-
contents = bson.serialize(js);
|
|
13057
|
-
}
|
|
13058
|
-
else {
|
|
13059
|
-
contents = JSON.stringify(js);
|
|
13060
|
-
}
|
|
13061
|
-
return bufferExports.Buffer.from(contents);
|
|
12937
|
+
return bufferExports.Buffer.from(JSON.stringify(js));
|
|
13062
12938
|
}
|
|
13063
12939
|
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
|
|
13064
12940
|
const request = await this.buildRequest(path);
|
|
12941
|
+
const url = this.options.socketUrlTransformer(request.url);
|
|
13065
12942
|
// Add the user agent in the setup payload - we can't set custom
|
|
13066
12943
|
// headers with websockets on web. The browser userAgent is however added
|
|
13067
12944
|
// automatically as a header.
|
|
13068
12945
|
const userAgent = this.getUserAgent();
|
|
13069
|
-
|
|
13070
|
-
|
|
13071
|
-
|
|
13072
|
-
|
|
13073
|
-
|
|
13074
|
-
|
|
13075
|
-
|
|
12946
|
+
// While we're connecting (a process that can't be aborted in RSocket), the WebSocket instance to close if we wanted
|
|
12947
|
+
// to abort the connection.
|
|
12948
|
+
let pendingSocket = null;
|
|
12949
|
+
let keepAliveTimeout;
|
|
12950
|
+
let rsocket = null;
|
|
12951
|
+
let queue = null;
|
|
12952
|
+
let didClose = false;
|
|
12953
|
+
const abortRequest = () => {
|
|
12954
|
+
if (didClose) {
|
|
12955
|
+
return;
|
|
12956
|
+
}
|
|
12957
|
+
didClose = true;
|
|
12958
|
+
clearTimeout(keepAliveTimeout);
|
|
12959
|
+
if (pendingSocket) {
|
|
12960
|
+
pendingSocket.close();
|
|
12961
|
+
}
|
|
12962
|
+
if (rsocket) {
|
|
12963
|
+
rsocket.close();
|
|
12964
|
+
}
|
|
12965
|
+
if (queue) {
|
|
12966
|
+
queue.stop();
|
|
12967
|
+
}
|
|
12968
|
+
};
|
|
13076
12969
|
// Handle upstream abort
|
|
13077
|
-
if (options.abortSignal
|
|
12970
|
+
if (options.abortSignal.aborted) {
|
|
13078
12971
|
throw new AbortOperation('Connection request aborted');
|
|
13079
12972
|
}
|
|
13080
12973
|
else {
|
|
13081
|
-
options.abortSignal
|
|
13082
|
-
stream.close();
|
|
13083
|
-
}, { once: true });
|
|
12974
|
+
options.abortSignal.addEventListener('abort', abortRequest);
|
|
13084
12975
|
}
|
|
13085
|
-
let keepAliveTimeout;
|
|
13086
12976
|
const resetTimeout = () => {
|
|
13087
12977
|
clearTimeout(keepAliveTimeout);
|
|
13088
12978
|
keepAliveTimeout = setTimeout(() => {
|
|
13089
12979
|
this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
|
|
13090
|
-
|
|
12980
|
+
abortRequest();
|
|
13091
12981
|
}, SOCKET_TIMEOUT_MS);
|
|
13092
12982
|
};
|
|
13093
12983
|
resetTimeout();
|
|
13094
|
-
// Typescript complains about this being `never` if it's not assigned here.
|
|
13095
|
-
// This is assigned in `wsCreator`.
|
|
13096
|
-
let disposeSocketConnectionTimeout = () => { };
|
|
13097
|
-
const url = this.options.socketUrlTransformer(request.url);
|
|
13098
12984
|
const connector = new distExports.RSocketConnector({
|
|
13099
12985
|
transport: new WebsocketClientTransport({
|
|
13100
12986
|
url,
|
|
13101
12987
|
wsCreator: (url) => {
|
|
13102
|
-
const socket = this.createSocket(url);
|
|
13103
|
-
|
|
13104
|
-
closed: () => {
|
|
13105
|
-
// Allow closing the underlying WebSocket if the stream was closed before the
|
|
13106
|
-
// RSocket connect completed. This should effectively abort the request.
|
|
13107
|
-
socket.close();
|
|
13108
|
-
}
|
|
13109
|
-
});
|
|
13110
|
-
socket.addEventListener('message', (event) => {
|
|
12988
|
+
const socket = (pendingSocket = this.createSocket(url));
|
|
12989
|
+
socket.addEventListener('message', () => {
|
|
13111
12990
|
resetTimeout();
|
|
13112
12991
|
});
|
|
13113
12992
|
return socket;
|
|
@@ -13127,43 +13006,40 @@ class AbstractRemote {
|
|
|
13127
13006
|
}
|
|
13128
13007
|
}
|
|
13129
13008
|
});
|
|
13130
|
-
let rsocket;
|
|
13131
13009
|
try {
|
|
13132
13010
|
rsocket = await connector.connect();
|
|
13133
13011
|
// The connection is established, we no longer need to monitor the initial timeout
|
|
13134
|
-
|
|
13012
|
+
pendingSocket = null;
|
|
13135
13013
|
}
|
|
13136
13014
|
catch (ex) {
|
|
13137
13015
|
this.logger.error(`Failed to connect WebSocket`, ex);
|
|
13138
|
-
|
|
13139
|
-
if (!stream.closed) {
|
|
13140
|
-
await stream.close();
|
|
13141
|
-
}
|
|
13016
|
+
abortRequest();
|
|
13142
13017
|
throw ex;
|
|
13143
13018
|
}
|
|
13144
13019
|
resetTimeout();
|
|
13145
|
-
let socketIsClosed = false;
|
|
13146
|
-
const closeSocket = () => {
|
|
13147
|
-
clearTimeout(keepAliveTimeout);
|
|
13148
|
-
if (socketIsClosed) {
|
|
13149
|
-
return;
|
|
13150
|
-
}
|
|
13151
|
-
socketIsClosed = true;
|
|
13152
|
-
rsocket.close();
|
|
13153
|
-
};
|
|
13154
13020
|
// Helps to prevent double close scenarios
|
|
13155
|
-
rsocket.onClose(() => (
|
|
13156
|
-
|
|
13157
|
-
let pendingEventsCount = syncQueueRequestSize;
|
|
13158
|
-
const disposeClosedListener = stream.registerListener({
|
|
13159
|
-
closed: () => {
|
|
13160
|
-
closeSocket();
|
|
13161
|
-
disposeClosedListener();
|
|
13162
|
-
}
|
|
13163
|
-
});
|
|
13164
|
-
const socket = await new Promise((resolve, reject) => {
|
|
13021
|
+
rsocket.onClose(() => (rsocket = null));
|
|
13022
|
+
return await new Promise((resolve, reject) => {
|
|
13165
13023
|
let connectionEstablished = false;
|
|
13166
|
-
|
|
13024
|
+
let pendingEventsCount = syncQueueRequestSize;
|
|
13025
|
+
let paused = false;
|
|
13026
|
+
let res = null;
|
|
13027
|
+
function requestMore() {
|
|
13028
|
+
const delta = syncQueueRequestSize - pendingEventsCount;
|
|
13029
|
+
if (!paused && delta > 0) {
|
|
13030
|
+
res?.request(delta);
|
|
13031
|
+
pendingEventsCount = syncQueueRequestSize;
|
|
13032
|
+
}
|
|
13033
|
+
}
|
|
13034
|
+
const events = new domExports.EventIterator((q) => {
|
|
13035
|
+
queue = q;
|
|
13036
|
+
q.on('highWater', () => (paused = true));
|
|
13037
|
+
q.on('lowWater', () => {
|
|
13038
|
+
paused = false;
|
|
13039
|
+
requestMore();
|
|
13040
|
+
});
|
|
13041
|
+
}, { highWaterMark: SYNC_QUEUE_REQUEST_HIGH_WATER, lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER })[symbolAsyncIterator]();
|
|
13042
|
+
res = rsocket.requestStream({
|
|
13167
13043
|
data: toBuffer(options.data),
|
|
13168
13044
|
metadata: toBuffer({
|
|
13169
13045
|
path
|
|
@@ -13188,7 +13064,7 @@ class AbstractRemote {
|
|
|
13188
13064
|
}
|
|
13189
13065
|
// RSocket will close the RSocket stream automatically
|
|
13190
13066
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
13191
|
-
|
|
13067
|
+
abortRequest();
|
|
13192
13068
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
13193
13069
|
if (!connectionEstablished) {
|
|
13194
13070
|
reject(e);
|
|
@@ -13198,41 +13074,40 @@ class AbstractRemote {
|
|
|
13198
13074
|
// The connection is active
|
|
13199
13075
|
if (!connectionEstablished) {
|
|
13200
13076
|
connectionEstablished = true;
|
|
13201
|
-
resolve(
|
|
13077
|
+
resolve(events);
|
|
13202
13078
|
}
|
|
13203
13079
|
const { data } = payload;
|
|
13080
|
+
if (data) {
|
|
13081
|
+
queue.push(data);
|
|
13082
|
+
}
|
|
13204
13083
|
// Less events are now pending
|
|
13205
13084
|
pendingEventsCount--;
|
|
13206
|
-
|
|
13207
|
-
|
|
13208
|
-
}
|
|
13209
|
-
stream.enqueueData(data);
|
|
13085
|
+
// Request another event (unless the downstream consumer is paused).
|
|
13086
|
+
requestMore();
|
|
13210
13087
|
},
|
|
13211
13088
|
onComplete: () => {
|
|
13212
|
-
|
|
13089
|
+
abortRequest(); // this will also emit a done event
|
|
13213
13090
|
},
|
|
13214
13091
|
onExtension: () => { }
|
|
13215
13092
|
});
|
|
13216
13093
|
});
|
|
13217
|
-
const l = stream.registerListener({
|
|
13218
|
-
lowWater: async () => {
|
|
13219
|
-
// Request to fill up the queue
|
|
13220
|
-
const required = syncQueueRequestSize - pendingEventsCount;
|
|
13221
|
-
if (required > 0) {
|
|
13222
|
-
socket.request(syncQueueRequestSize - pendingEventsCount);
|
|
13223
|
-
pendingEventsCount = syncQueueRequestSize;
|
|
13224
|
-
}
|
|
13225
|
-
},
|
|
13226
|
-
closed: () => {
|
|
13227
|
-
l();
|
|
13228
|
-
}
|
|
13229
|
-
});
|
|
13230
|
-
return stream;
|
|
13231
13094
|
}
|
|
13232
13095
|
/**
|
|
13233
|
-
*
|
|
13096
|
+
* @returns Whether the HTTP implementation on this platform can receive streamed binary responses. This is true on
|
|
13097
|
+
* all platforms except React Native (who would have guessed...), where we must not request BSON responses.
|
|
13098
|
+
*
|
|
13099
|
+
* @see https://github.com/react-native-community/fetch?tab=readme-ov-file#motivation
|
|
13100
|
+
*/
|
|
13101
|
+
get supportsStreamingBinaryResponses() {
|
|
13102
|
+
return true;
|
|
13103
|
+
}
|
|
13104
|
+
/**
|
|
13105
|
+
* Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
|
|
13106
|
+
* async iterator of byte blobs.
|
|
13107
|
+
*
|
|
13108
|
+
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
13234
13109
|
*/
|
|
13235
|
-
async
|
|
13110
|
+
async fetchStreamRaw(options) {
|
|
13236
13111
|
const { data, path, headers, abortSignal } = options;
|
|
13237
13112
|
const request = await this.buildRequest(path);
|
|
13238
13113
|
/**
|
|
@@ -13244,119 +13119,94 @@ class AbstractRemote {
|
|
|
13244
13119
|
* Aborting the active fetch request while it is being consumed seems to throw
|
|
13245
13120
|
* an unhandled exception on the window level.
|
|
13246
13121
|
*/
|
|
13247
|
-
if (abortSignal
|
|
13248
|
-
throw new AbortOperation('Abort request received before making
|
|
13122
|
+
if (abortSignal.aborted) {
|
|
13123
|
+
throw new AbortOperation('Abort request received before making fetchStreamRaw request');
|
|
13249
13124
|
}
|
|
13250
13125
|
const controller = new AbortController();
|
|
13251
|
-
let
|
|
13252
|
-
abortSignal
|
|
13253
|
-
|
|
13126
|
+
let reader = null;
|
|
13127
|
+
abortSignal.addEventListener('abort', () => {
|
|
13128
|
+
const reason = abortSignal.reason ??
|
|
13129
|
+
new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.');
|
|
13130
|
+
if (reader == null) {
|
|
13254
13131
|
// Only abort via the abort controller if the request has not resolved yet
|
|
13255
|
-
controller.abort(
|
|
13256
|
-
|
|
13132
|
+
controller.abort(reason);
|
|
13133
|
+
}
|
|
13134
|
+
else {
|
|
13135
|
+
reader.cancel(reason).catch(() => {
|
|
13136
|
+
// Cancelling the reader might rethrow an exception we would have handled by throwing in next(). So we can
|
|
13137
|
+
// ignore it here.
|
|
13138
|
+
});
|
|
13257
13139
|
}
|
|
13258
13140
|
});
|
|
13259
|
-
|
|
13260
|
-
|
|
13261
|
-
|
|
13262
|
-
|
|
13263
|
-
|
|
13264
|
-
|
|
13265
|
-
|
|
13266
|
-
|
|
13267
|
-
|
|
13141
|
+
let res;
|
|
13142
|
+
let responseIsBson = false;
|
|
13143
|
+
try {
|
|
13144
|
+
const ndJson = 'application/x-ndjson';
|
|
13145
|
+
const bson = 'application/vnd.powersync.bson-stream';
|
|
13146
|
+
res = await this.fetch(request.url, {
|
|
13147
|
+
method: 'POST',
|
|
13148
|
+
headers: {
|
|
13149
|
+
...headers,
|
|
13150
|
+
...request.headers,
|
|
13151
|
+
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
13152
|
+
},
|
|
13153
|
+
body: JSON.stringify(data),
|
|
13154
|
+
signal: controller.signal,
|
|
13155
|
+
cache: 'no-store',
|
|
13156
|
+
...(this.options.fetchOptions ?? {}),
|
|
13157
|
+
...options.fetchOptions
|
|
13158
|
+
});
|
|
13159
|
+
if (!res.ok || !res.body) {
|
|
13160
|
+
const text = await res.text();
|
|
13161
|
+
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
13162
|
+
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
13163
|
+
error.status = res.status;
|
|
13164
|
+
throw error;
|
|
13165
|
+
}
|
|
13166
|
+
const contentType = res.headers.get('content-type');
|
|
13167
|
+
responseIsBson = contentType == bson;
|
|
13168
|
+
}
|
|
13169
|
+
catch (ex) {
|
|
13268
13170
|
if (ex.name == 'AbortError') {
|
|
13269
13171
|
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
|
|
13270
13172
|
}
|
|
13271
13173
|
throw ex;
|
|
13272
|
-
});
|
|
13273
|
-
if (!res) {
|
|
13274
|
-
throw new Error('Fetch request was aborted');
|
|
13275
|
-
}
|
|
13276
|
-
requestResolved = true;
|
|
13277
|
-
if (!res.ok || !res.body) {
|
|
13278
|
-
const text = await res.text();
|
|
13279
|
-
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
13280
|
-
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
13281
|
-
error.status = res.status;
|
|
13282
|
-
throw error;
|
|
13283
13174
|
}
|
|
13284
|
-
|
|
13285
|
-
|
|
13286
|
-
|
|
13287
|
-
|
|
13288
|
-
|
|
13289
|
-
|
|
13290
|
-
|
|
13291
|
-
|
|
13292
|
-
|
|
13293
|
-
|
|
13294
|
-
|
|
13295
|
-
|
|
13175
|
+
reader = res.body.getReader();
|
|
13176
|
+
const stream = {
|
|
13177
|
+
next: async () => {
|
|
13178
|
+
if (controller.signal.aborted) {
|
|
13179
|
+
return doneResult;
|
|
13180
|
+
}
|
|
13181
|
+
try {
|
|
13182
|
+
return await reader.read();
|
|
13183
|
+
}
|
|
13184
|
+
catch (ex) {
|
|
13185
|
+
if (controller.signal.aborted) {
|
|
13186
|
+
// .read() completes with an error if we cancel the reader, which we do to disconnect. So this is just
|
|
13187
|
+
// things working as intended, we can return a done event and consider the exception handled.
|
|
13188
|
+
return doneResult;
|
|
13189
|
+
}
|
|
13190
|
+
throw ex;
|
|
13191
|
+
}
|
|
13296
13192
|
}
|
|
13297
|
-
reader.releaseLock();
|
|
13298
13193
|
};
|
|
13299
|
-
|
|
13300
|
-
|
|
13301
|
-
|
|
13302
|
-
|
|
13303
|
-
|
|
13304
|
-
|
|
13305
|
-
|
|
13306
|
-
|
|
13307
|
-
|
|
13308
|
-
|
|
13309
|
-
|
|
13310
|
-
|
|
13311
|
-
|
|
13312
|
-
|
|
13313
|
-
|
|
13314
|
-
|
|
13315
|
-
const { done, value } = await reader.read();
|
|
13316
|
-
if (done) {
|
|
13317
|
-
const remaining = buffer.trim();
|
|
13318
|
-
if (remaining.length != 0) {
|
|
13319
|
-
stream.enqueueData(remaining);
|
|
13320
|
-
}
|
|
13321
|
-
stream.close();
|
|
13322
|
-
await closeReader();
|
|
13323
|
-
return;
|
|
13324
|
-
}
|
|
13325
|
-
const data = decoder.decode(value, { stream: true });
|
|
13326
|
-
buffer += data;
|
|
13327
|
-
const lines = buffer.split('\n');
|
|
13328
|
-
for (var i = 0; i < lines.length - 1; i++) {
|
|
13329
|
-
var l = lines[i].trim();
|
|
13330
|
-
if (l.length > 0) {
|
|
13331
|
-
stream.enqueueData(l);
|
|
13332
|
-
}
|
|
13333
|
-
}
|
|
13334
|
-
buffer = lines[lines.length - 1];
|
|
13335
|
-
// Implement backpressure by waiting for the low water mark to be reached
|
|
13336
|
-
if (stream.dataQueue.length > stream.highWatermark) {
|
|
13337
|
-
await new Promise((resolve) => {
|
|
13338
|
-
const dispose = stream.registerListener({
|
|
13339
|
-
lowWater: async () => {
|
|
13340
|
-
resolve();
|
|
13341
|
-
dispose();
|
|
13342
|
-
},
|
|
13343
|
-
closed: () => {
|
|
13344
|
-
resolve();
|
|
13345
|
-
dispose();
|
|
13346
|
-
}
|
|
13347
|
-
});
|
|
13348
|
-
});
|
|
13349
|
-
}
|
|
13350
|
-
}
|
|
13351
|
-
};
|
|
13352
|
-
consumeStream().catch(ex => this.logger.error('Error consuming stream', ex));
|
|
13353
|
-
const l = stream.registerListener({
|
|
13354
|
-
closed: () => {
|
|
13355
|
-
closeReader();
|
|
13356
|
-
l?.();
|
|
13357
|
-
}
|
|
13358
|
-
});
|
|
13359
|
-
return stream;
|
|
13194
|
+
return { isBson: responseIsBson, stream };
|
|
13195
|
+
}
|
|
13196
|
+
/**
|
|
13197
|
+
* Posts a `/sync/stream` request.
|
|
13198
|
+
*
|
|
13199
|
+
* Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
|
|
13200
|
+
* {@link Uint8Array}s.
|
|
13201
|
+
*/
|
|
13202
|
+
async fetchStream(options) {
|
|
13203
|
+
const { isBson, stream } = await this.fetchStreamRaw(options);
|
|
13204
|
+
if (isBson) {
|
|
13205
|
+
return extractBsonObjects(stream);
|
|
13206
|
+
}
|
|
13207
|
+
else {
|
|
13208
|
+
return extractJsonLines(stream, this.createTextDecoder());
|
|
13209
|
+
}
|
|
13360
13210
|
}
|
|
13361
13211
|
}
|
|
13362
13212
|
|
|
@@ -13385,31 +13235,8 @@ function coreStatusToJs(status) {
|
|
|
13385
13235
|
priorityStatusEntries: status.priority_status.map(priorityToJs)
|
|
13386
13236
|
};
|
|
13387
13237
|
}
|
|
13388
|
-
|
|
13389
|
-
|
|
13390
|
-
return line.data != null;
|
|
13391
|
-
}
|
|
13392
|
-
function isStreamingKeepalive(line) {
|
|
13393
|
-
return line.token_expires_in != null;
|
|
13394
|
-
}
|
|
13395
|
-
function isStreamingSyncCheckpoint(line) {
|
|
13396
|
-
return line.checkpoint != null;
|
|
13397
|
-
}
|
|
13398
|
-
function isStreamingSyncCheckpointComplete(line) {
|
|
13399
|
-
return line.checkpoint_complete != null;
|
|
13400
|
-
}
|
|
13401
|
-
function isStreamingSyncCheckpointPartiallyComplete(line) {
|
|
13402
|
-
return line.partial_checkpoint_complete != null;
|
|
13403
|
-
}
|
|
13404
|
-
function isStreamingSyncCheckpointDiff(line) {
|
|
13405
|
-
return line.checkpoint_diff != null;
|
|
13406
|
-
}
|
|
13407
|
-
function isContinueCheckpointRequest(request) {
|
|
13408
|
-
return (Array.isArray(request.buckets) &&
|
|
13409
|
-
typeof request.checkpoint_token == 'string');
|
|
13410
|
-
}
|
|
13411
|
-
function isSyncNewCheckpointRequest(request) {
|
|
13412
|
-
return typeof request.request_checkpoint == 'object';
|
|
13238
|
+
function isInterruptingInstruction(instruction) {
|
|
13239
|
+
return 'EstablishSyncStream' in instruction || 'CloseSyncStream' in instruction;
|
|
13413
13240
|
}
|
|
13414
13241
|
|
|
13415
13242
|
var LockType;
|
|
@@ -13424,35 +13251,21 @@ var SyncStreamConnectionMethod;
|
|
|
13424
13251
|
})(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
|
|
13425
13252
|
var SyncClientImplementation;
|
|
13426
13253
|
(function (SyncClientImplementation) {
|
|
13427
|
-
/**
|
|
13428
|
-
* Decodes and handles sync lines received from the sync service in JavaScript.
|
|
13429
|
-
*
|
|
13430
|
-
* This is the default option.
|
|
13431
|
-
*
|
|
13432
|
-
* @deprecated We recommend the {@link RUST} client implementation for all apps. If you have issues with
|
|
13433
|
-
* the Rust client, please file an issue or reach out to us. The JavaScript client will be removed in a future
|
|
13434
|
-
* version of the PowerSync SDK.
|
|
13435
|
-
*/
|
|
13436
|
-
SyncClientImplementation["JAVASCRIPT"] = "js";
|
|
13437
13254
|
/**
|
|
13438
13255
|
* This implementation offloads the sync line decoding and handling into the PowerSync
|
|
13439
13256
|
* core extension.
|
|
13440
13257
|
*
|
|
13441
|
-
* This
|
|
13442
|
-
* recommended client implementation for all apps.
|
|
13258
|
+
* This is the only option, as an older JavaScript client implementation has been removed from the SDK.
|
|
13443
13259
|
*
|
|
13444
13260
|
* ## Compatibility warning
|
|
13445
13261
|
*
|
|
13446
13262
|
* The Rust sync client stores sync data in a format that is slightly different than the one used
|
|
13447
|
-
* by the old
|
|
13448
|
-
*
|
|
13449
|
-
* Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
|
|
13450
|
-
* the version introducing {@link RUST} as an option) also supports the new format, so you can switch
|
|
13451
|
-
* back to {@link JAVASCRIPT} later.
|
|
13263
|
+
* by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
|
|
13264
|
+
* migrate the format automatically.
|
|
13452
13265
|
*
|
|
13453
|
-
*
|
|
13454
|
-
*
|
|
13455
|
-
*
|
|
13266
|
+
* SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
|
|
13267
|
+
* implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
|
|
13268
|
+
* possible anymore. Problematic SDK versions have been released before 2025-06-09.
|
|
13456
13269
|
*/
|
|
13457
13270
|
SyncClientImplementation["RUST"] = "rust";
|
|
13458
13271
|
})(SyncClientImplementation || (SyncClientImplementation = {}));
|
|
@@ -13475,13 +13288,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
|
13475
13288
|
serializedSchema: undefined,
|
|
13476
13289
|
includeDefaultStreams: true
|
|
13477
13290
|
};
|
|
13478
|
-
// The priority we assume when we receive checkpoint lines where no priority is set.
|
|
13479
|
-
// This is the default priority used by the sync service, but can be set to an arbitrary
|
|
13480
|
-
// value since sync services without priorities also won't send partial sync completion
|
|
13481
|
-
// messages.
|
|
13482
|
-
const FALLBACK_PRIORITY = 3;
|
|
13483
13291
|
class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
13484
|
-
_lastSyncedAt;
|
|
13485
13292
|
options;
|
|
13486
13293
|
abortController;
|
|
13487
13294
|
// In rare cases, mostly for tests, uploads can be triggered without being properly connected.
|
|
@@ -13491,6 +13298,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
13491
13298
|
streamingSyncPromise;
|
|
13492
13299
|
logger;
|
|
13493
13300
|
activeStreams;
|
|
13301
|
+
connectionMayHaveChanged = false;
|
|
13494
13302
|
isUploadingCrud = false;
|
|
13495
13303
|
notifyCompletedUploads;
|
|
13496
13304
|
handleActiveStreamsChange;
|
|
@@ -13570,9 +13378,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
13570
13378
|
this.crudUpdateListener = undefined;
|
|
13571
13379
|
this.uploadAbortController?.abort();
|
|
13572
13380
|
}
|
|
13573
|
-
async hasCompletedSync() {
|
|
13574
|
-
return this.options.adapter.hasCompletedSync();
|
|
13575
|
-
}
|
|
13576
13381
|
async getWriteCheckpoint() {
|
|
13577
13382
|
const clientId = await this.options.adapter.getClientId();
|
|
13578
13383
|
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
@@ -13654,7 +13459,7 @@ The next upload iteration will be delayed.`);
|
|
|
13654
13459
|
});
|
|
13655
13460
|
}
|
|
13656
13461
|
}
|
|
13657
|
-
this.uploadAbortController =
|
|
13462
|
+
this.uploadAbortController = undefined;
|
|
13658
13463
|
}
|
|
13659
13464
|
});
|
|
13660
13465
|
}
|
|
@@ -13770,6 +13575,11 @@ The next upload iteration will be delayed.`);
|
|
|
13770
13575
|
shouldDelayRetry = false;
|
|
13771
13576
|
// A disconnect was requested, we should not delay since there is no explicit retry
|
|
13772
13577
|
}
|
|
13578
|
+
else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
|
|
13579
|
+
this.connectionMayHaveChanged = false;
|
|
13580
|
+
this.logger.info('Sync error after changed connection, retrying immediately');
|
|
13581
|
+
shouldDelayRetry = false;
|
|
13582
|
+
}
|
|
13773
13583
|
else {
|
|
13774
13584
|
this.logger.error(ex);
|
|
13775
13585
|
}
|
|
@@ -13800,17 +13610,14 @@ The next upload iteration will be delayed.`);
|
|
|
13800
13610
|
// Mark as disconnected if here
|
|
13801
13611
|
this.updateSyncStatus({ connected: false, connecting: false });
|
|
13802
13612
|
}
|
|
13803
|
-
|
|
13804
|
-
|
|
13805
|
-
|
|
13806
|
-
|
|
13807
|
-
|
|
13808
|
-
|
|
13809
|
-
|
|
13810
|
-
|
|
13811
|
-
localDescriptions.set(entry.bucket, null);
|
|
13812
|
-
}
|
|
13813
|
-
return [req, localDescriptions];
|
|
13613
|
+
markConnectionMayHaveChanged() {
|
|
13614
|
+
// By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
|
|
13615
|
+
// an active sync iteration on the connection in use.
|
|
13616
|
+
this.connectionMayHaveChanged = true;
|
|
13617
|
+
// This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
|
|
13618
|
+
// make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
|
|
13619
|
+
// iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
|
|
13620
|
+
this.handleActiveStreamsChange?.();
|
|
13814
13621
|
}
|
|
13815
13622
|
/**
|
|
13816
13623
|
* Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
|
|
@@ -13851,344 +13658,98 @@ The next upload iteration will be delayed.`);
|
|
|
13851
13658
|
if (invalidMetadata.length > 0) {
|
|
13852
13659
|
throw new Error(`Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`);
|
|
13853
13660
|
}
|
|
13854
|
-
|
|
13855
|
-
this.
|
|
13856
|
-
if (clientImplementation == SyncClientImplementation.JAVASCRIPT) {
|
|
13857
|
-
await this.legacyStreamingSyncIteration(signal, resolvedOptions);
|
|
13858
|
-
return null;
|
|
13859
|
-
}
|
|
13860
|
-
else {
|
|
13861
|
-
await this.requireKeyFormat(true);
|
|
13862
|
-
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
13863
|
-
}
|
|
13661
|
+
await this.requireKeyFormat(true);
|
|
13662
|
+
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
13864
13663
|
}
|
|
13865
13664
|
});
|
|
13866
13665
|
}
|
|
13867
|
-
|
|
13868
|
-
const
|
|
13869
|
-
|
|
13870
|
-
|
|
13871
|
-
|
|
13872
|
-
|
|
13873
|
-
this.logger.error('Sync streams require `clientImplementation: SyncClientImplementation.RUST` when connecting.');
|
|
13874
|
-
}
|
|
13875
|
-
this.logger.debug('Streaming sync iteration started');
|
|
13876
|
-
this.options.adapter.startSession();
|
|
13877
|
-
let [req, bucketMap] = await this.collectLocalBucketState();
|
|
13878
|
-
let targetCheckpoint = null;
|
|
13879
|
-
// A checkpoint that has been validated but not applied (e.g. due to pending local writes)
|
|
13880
|
-
let pendingValidatedCheckpoint = null;
|
|
13881
|
-
const clientId = await this.options.adapter.getClientId();
|
|
13882
|
-
const usingFixedKeyFormat = await this.requireKeyFormat(false);
|
|
13883
|
-
this.logger.debug('Requesting stream from server');
|
|
13884
|
-
const syncOptions = {
|
|
13885
|
-
path: '/sync/stream',
|
|
13886
|
-
abortSignal: signal,
|
|
13887
|
-
data: {
|
|
13888
|
-
buckets: req,
|
|
13889
|
-
include_checksum: true,
|
|
13890
|
-
raw_data: true,
|
|
13891
|
-
parameters: resolvedOptions.params,
|
|
13892
|
-
app_metadata: resolvedOptions.appMetadata,
|
|
13893
|
-
client_id: clientId
|
|
13666
|
+
receiveSyncLines(data) {
|
|
13667
|
+
const { options, connection } = data;
|
|
13668
|
+
const remote = this.options.remote;
|
|
13669
|
+
const openInner = async () => {
|
|
13670
|
+
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
13671
|
+
return await remote.fetchStream(options);
|
|
13894
13672
|
}
|
|
13895
|
-
|
|
13896
|
-
|
|
13897
|
-
|
|
13898
|
-
|
|
13899
|
-
|
|
13900
|
-
return JSON.parse(line);
|
|
13901
|
-
}
|
|
13902
|
-
else {
|
|
13903
|
-
// Directly enqueued by us
|
|
13904
|
-
return line;
|
|
13905
|
-
}
|
|
13906
|
-
});
|
|
13907
|
-
}
|
|
13908
|
-
else {
|
|
13909
|
-
const bson = await this.options.remote.getBSON();
|
|
13910
|
-
stream = await this.options.remote.socketStreamRaw({
|
|
13911
|
-
...syncOptions,
|
|
13912
|
-
...{ fetchStrategy: resolvedOptions.fetchStrategy }
|
|
13913
|
-
}, (payload) => {
|
|
13914
|
-
if (payload instanceof Uint8Array) {
|
|
13915
|
-
return bson.deserialize(payload);
|
|
13916
|
-
}
|
|
13917
|
-
else {
|
|
13918
|
-
// Directly enqueued by us
|
|
13919
|
-
return payload;
|
|
13920
|
-
}
|
|
13921
|
-
}, bson);
|
|
13922
|
-
}
|
|
13923
|
-
this.logger.debug('Stream established. Processing events');
|
|
13924
|
-
this.notifyCompletedUploads = () => {
|
|
13925
|
-
if (!stream.closed) {
|
|
13926
|
-
stream.enqueueData({ crud_upload_completed: null });
|
|
13673
|
+
else {
|
|
13674
|
+
return await this.options.remote.socketStreamRaw({
|
|
13675
|
+
...options,
|
|
13676
|
+
...{ fetchStrategy: connection.fetchStrategy }
|
|
13677
|
+
});
|
|
13927
13678
|
}
|
|
13928
13679
|
};
|
|
13929
|
-
|
|
13930
|
-
|
|
13931
|
-
|
|
13932
|
-
|
|
13933
|
-
|
|
13934
|
-
|
|
13935
|
-
if ('crud_upload_completed' in line) {
|
|
13936
|
-
if (pendingValidatedCheckpoint != null) {
|
|
13937
|
-
const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
|
|
13938
|
-
if (applied) {
|
|
13939
|
-
pendingValidatedCheckpoint = null;
|
|
13940
|
-
}
|
|
13941
|
-
else if (endIteration) {
|
|
13942
|
-
break;
|
|
13943
|
-
}
|
|
13680
|
+
let inner;
|
|
13681
|
+
let done = false;
|
|
13682
|
+
return {
|
|
13683
|
+
async next() {
|
|
13684
|
+
if (done) {
|
|
13685
|
+
return doneResult;
|
|
13944
13686
|
}
|
|
13945
|
-
|
|
13946
|
-
|
|
13947
|
-
|
|
13948
|
-
|
|
13949
|
-
|
|
13950
|
-
|
|
13951
|
-
this.updateSyncStatus({
|
|
13952
|
-
connected: true
|
|
13953
|
-
});
|
|
13954
|
-
}
|
|
13955
|
-
if (isStreamingSyncCheckpoint(line)) {
|
|
13956
|
-
targetCheckpoint = line.checkpoint;
|
|
13957
|
-
// New checkpoint - existing validated checkpoint is no longer valid
|
|
13958
|
-
pendingValidatedCheckpoint = null;
|
|
13959
|
-
const bucketsToDelete = new Set(bucketMap.keys());
|
|
13960
|
-
const newBuckets = new Map();
|
|
13961
|
-
for (const checksum of line.checkpoint.buckets) {
|
|
13962
|
-
newBuckets.set(checksum.bucket, {
|
|
13963
|
-
name: checksum.bucket,
|
|
13964
|
-
priority: checksum.priority ?? FALLBACK_PRIORITY
|
|
13687
|
+
else if (inner == null) {
|
|
13688
|
+
inner = await openInner();
|
|
13689
|
+
// We're connected here, so we can tell the core extension about it.
|
|
13690
|
+
return valueResult({
|
|
13691
|
+
command: PowerSyncControlCommand.CONNECTION_STATE,
|
|
13692
|
+
payload: 'established'
|
|
13965
13693
|
});
|
|
13966
|
-
bucketsToDelete.delete(checksum.bucket);
|
|
13967
|
-
}
|
|
13968
|
-
if (bucketsToDelete.size > 0) {
|
|
13969
|
-
this.logger.debug('Removing buckets', [...bucketsToDelete]);
|
|
13970
|
-
}
|
|
13971
|
-
bucketMap = newBuckets;
|
|
13972
|
-
await this.options.adapter.removeBuckets([...bucketsToDelete]);
|
|
13973
|
-
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
|
|
13974
|
-
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
|
|
13975
|
-
}
|
|
13976
|
-
else if (isStreamingSyncCheckpointComplete(line)) {
|
|
13977
|
-
const result = await this.applyCheckpoint(targetCheckpoint);
|
|
13978
|
-
if (result.endIteration) {
|
|
13979
|
-
return;
|
|
13980
|
-
}
|
|
13981
|
-
else if (!result.applied) {
|
|
13982
|
-
// "Could not apply checkpoint due to local data". We need to retry after
|
|
13983
|
-
// finishing uploads.
|
|
13984
|
-
pendingValidatedCheckpoint = targetCheckpoint;
|
|
13985
13694
|
}
|
|
13986
13695
|
else {
|
|
13987
|
-
|
|
13988
|
-
|
|
13989
|
-
|
|
13990
|
-
|
|
13991
|
-
}
|
|
13992
|
-
else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
|
|
13993
|
-
const priority = line.partial_checkpoint_complete.priority;
|
|
13994
|
-
this.logger.debug('Partial checkpoint complete', priority);
|
|
13995
|
-
const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint, priority);
|
|
13996
|
-
if (!result.checkpointValid) {
|
|
13997
|
-
// This means checksums failed. Start again with a new checkpoint.
|
|
13998
|
-
// TODO: better back-off
|
|
13999
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
14000
|
-
return;
|
|
14001
|
-
}
|
|
14002
|
-
else if (!result.ready) ;
|
|
14003
|
-
else {
|
|
14004
|
-
// We'll keep on downloading, but can report that this priority is synced now.
|
|
14005
|
-
this.logger.debug('partial checkpoint validation succeeded');
|
|
14006
|
-
// All states with a higher priority can be deleted since this partial sync includes them.
|
|
14007
|
-
const priorityStates = this.syncStatus.priorityStatusEntries.filter((s) => s.priority <= priority);
|
|
14008
|
-
priorityStates.push({
|
|
14009
|
-
priority,
|
|
14010
|
-
lastSyncedAt: new Date(),
|
|
14011
|
-
hasSynced: true
|
|
14012
|
-
});
|
|
14013
|
-
this.updateSyncStatus({
|
|
14014
|
-
connected: true,
|
|
14015
|
-
priorityStatusEntries: priorityStates
|
|
14016
|
-
});
|
|
14017
|
-
}
|
|
14018
|
-
}
|
|
14019
|
-
else if (isStreamingSyncCheckpointDiff(line)) {
|
|
14020
|
-
// TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
|
|
14021
|
-
if (targetCheckpoint == null) {
|
|
14022
|
-
throw new Error('Checkpoint diff without previous checkpoint');
|
|
14023
|
-
}
|
|
14024
|
-
// New checkpoint - existing validated checkpoint is no longer valid
|
|
14025
|
-
pendingValidatedCheckpoint = null;
|
|
14026
|
-
const diff = line.checkpoint_diff;
|
|
14027
|
-
const newBuckets = new Map();
|
|
14028
|
-
for (const checksum of targetCheckpoint.buckets) {
|
|
14029
|
-
newBuckets.set(checksum.bucket, checksum);
|
|
14030
|
-
}
|
|
14031
|
-
for (const checksum of diff.updated_buckets) {
|
|
14032
|
-
newBuckets.set(checksum.bucket, checksum);
|
|
14033
|
-
}
|
|
14034
|
-
for (const bucket of diff.removed_buckets) {
|
|
14035
|
-
newBuckets.delete(bucket);
|
|
14036
|
-
}
|
|
14037
|
-
const newCheckpoint = {
|
|
14038
|
-
last_op_id: diff.last_op_id,
|
|
14039
|
-
buckets: [...newBuckets.values()],
|
|
14040
|
-
write_checkpoint: diff.write_checkpoint
|
|
14041
|
-
};
|
|
14042
|
-
targetCheckpoint = newCheckpoint;
|
|
14043
|
-
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
|
|
14044
|
-
bucketMap = new Map();
|
|
14045
|
-
newBuckets.forEach((checksum, name) => bucketMap.set(name, {
|
|
14046
|
-
name: checksum.bucket,
|
|
14047
|
-
priority: checksum.priority ?? FALLBACK_PRIORITY
|
|
14048
|
-
}));
|
|
14049
|
-
const bucketsToDelete = diff.removed_buckets;
|
|
14050
|
-
if (bucketsToDelete.length > 0) {
|
|
14051
|
-
this.logger.debug('Remove buckets', bucketsToDelete);
|
|
14052
|
-
}
|
|
14053
|
-
await this.options.adapter.removeBuckets(bucketsToDelete);
|
|
14054
|
-
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
|
|
14055
|
-
}
|
|
14056
|
-
else if (isStreamingSyncData(line)) {
|
|
14057
|
-
const { data } = line;
|
|
14058
|
-
const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
|
|
14059
|
-
let updatedProgress = null;
|
|
14060
|
-
if (previousProgress) {
|
|
14061
|
-
updatedProgress = { ...previousProgress };
|
|
14062
|
-
const progressForBucket = updatedProgress[data.bucket];
|
|
14063
|
-
if (progressForBucket) {
|
|
14064
|
-
updatedProgress[data.bucket] = {
|
|
14065
|
-
...progressForBucket,
|
|
14066
|
-
since_last: progressForBucket.since_last + data.data.length
|
|
14067
|
-
};
|
|
13696
|
+
const event = await inner.next();
|
|
13697
|
+
if (event.done) {
|
|
13698
|
+
done = true;
|
|
13699
|
+
return valueResult({ command: PowerSyncControlCommand.CONNECTION_STATE, payload: 'end' });
|
|
14068
13700
|
}
|
|
14069
|
-
|
|
14070
|
-
|
|
14071
|
-
|
|
14072
|
-
|
|
14073
|
-
|
|
13701
|
+
else {
|
|
13702
|
+
return valueResult({
|
|
13703
|
+
command: typeof event.value == 'string'
|
|
13704
|
+
? PowerSyncControlCommand.PROCESS_TEXT_LINE
|
|
13705
|
+
: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
13706
|
+
payload: event.value
|
|
13707
|
+
});
|
|
14074
13708
|
}
|
|
14075
|
-
});
|
|
14076
|
-
await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
|
|
14077
|
-
}
|
|
14078
|
-
else if (isStreamingKeepalive(line)) {
|
|
14079
|
-
const remaining_seconds = line.token_expires_in;
|
|
14080
|
-
if (remaining_seconds == 0) {
|
|
14081
|
-
// Connection would be closed automatically right after this
|
|
14082
|
-
this.logger.debug('Token expiring; reconnect');
|
|
14083
|
-
/**
|
|
14084
|
-
* For a rare case where the backend connector does not update the token
|
|
14085
|
-
* (uses the same one), this should have some delay.
|
|
14086
|
-
*/
|
|
14087
|
-
await this.delayRetry();
|
|
14088
|
-
return;
|
|
14089
|
-
}
|
|
14090
|
-
else if (remaining_seconds < 30) {
|
|
14091
|
-
this.logger.debug('Token will expire soon; reconnect');
|
|
14092
|
-
// Pre-emptively refresh the token
|
|
14093
|
-
this.options.remote.invalidateCredentials();
|
|
14094
|
-
return;
|
|
14095
13709
|
}
|
|
14096
|
-
this.triggerCrudUpload();
|
|
14097
|
-
}
|
|
14098
|
-
else {
|
|
14099
|
-
this.logger.debug('Received unknown sync line', line);
|
|
14100
13710
|
}
|
|
14101
|
-
}
|
|
14102
|
-
this.logger.debug('Stream input empty');
|
|
14103
|
-
// Connection closed. Likely due to auth issue.
|
|
14104
|
-
return;
|
|
13711
|
+
};
|
|
14105
13712
|
}
|
|
14106
13713
|
async rustSyncIteration(signal, resolvedOptions) {
|
|
14107
13714
|
const syncImplementation = this;
|
|
14108
13715
|
const adapter = this.options.adapter;
|
|
14109
13716
|
const remote = this.options.remote;
|
|
14110
|
-
let receivingLines = null;
|
|
14111
|
-
let hadSyncLine = false;
|
|
14112
13717
|
let hideDisconnectOnRestart = false;
|
|
13718
|
+
let notifyTokenRefreshed;
|
|
14113
13719
|
if (signal.aborted) {
|
|
14114
13720
|
throw new AbortOperation('Connection request has been aborted');
|
|
14115
13721
|
}
|
|
14116
|
-
|
|
14117
|
-
|
|
14118
|
-
|
|
14119
|
-
|
|
14120
|
-
|
|
14121
|
-
|
|
14122
|
-
async function connect(instr) {
|
|
14123
|
-
const syncOptions = {
|
|
14124
|
-
path: '/sync/stream',
|
|
14125
|
-
abortSignal: abortController.signal,
|
|
14126
|
-
data: instr.request
|
|
13722
|
+
function startCommand() {
|
|
13723
|
+
const options = {
|
|
13724
|
+
parameters: resolvedOptions.params,
|
|
13725
|
+
app_metadata: resolvedOptions.appMetadata,
|
|
13726
|
+
active_streams: syncImplementation.activeStreams,
|
|
13727
|
+
include_defaults: resolvedOptions.includeDefaultStreams
|
|
14127
13728
|
};
|
|
14128
|
-
if (resolvedOptions.
|
|
14129
|
-
|
|
14130
|
-
if (typeof line == 'string') {
|
|
14131
|
-
return {
|
|
14132
|
-
command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
|
|
14133
|
-
payload: line
|
|
14134
|
-
};
|
|
14135
|
-
}
|
|
14136
|
-
else {
|
|
14137
|
-
// Directly enqueued by us
|
|
14138
|
-
return line;
|
|
14139
|
-
}
|
|
14140
|
-
});
|
|
14141
|
-
}
|
|
14142
|
-
else {
|
|
14143
|
-
controlInvocations = await remote.socketStreamRaw({
|
|
14144
|
-
...syncOptions,
|
|
14145
|
-
fetchStrategy: resolvedOptions.fetchStrategy
|
|
14146
|
-
}, (payload) => {
|
|
14147
|
-
if (payload instanceof Uint8Array) {
|
|
14148
|
-
return {
|
|
14149
|
-
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
14150
|
-
payload: payload
|
|
14151
|
-
};
|
|
14152
|
-
}
|
|
14153
|
-
else {
|
|
14154
|
-
// Directly enqueued by us
|
|
14155
|
-
return payload;
|
|
14156
|
-
}
|
|
14157
|
-
});
|
|
14158
|
-
}
|
|
14159
|
-
// The rust client will set connected: true after the first sync line because that's when it gets invoked, but
|
|
14160
|
-
// we're already connected here and can report that.
|
|
14161
|
-
syncImplementation.updateSyncStatus({ connected: true });
|
|
14162
|
-
try {
|
|
14163
|
-
while (!controlInvocations.closed) {
|
|
14164
|
-
const line = await controlInvocations.read();
|
|
14165
|
-
if (line == null) {
|
|
14166
|
-
return;
|
|
14167
|
-
}
|
|
14168
|
-
await control(line.command, line.payload);
|
|
14169
|
-
if (!hadSyncLine) {
|
|
14170
|
-
syncImplementation.triggerCrudUpload();
|
|
14171
|
-
hadSyncLine = true;
|
|
14172
|
-
}
|
|
14173
|
-
}
|
|
14174
|
-
}
|
|
14175
|
-
finally {
|
|
14176
|
-
const activeInstructions = controlInvocations;
|
|
14177
|
-
// We concurrently add events to the active data stream when e.g. a CRUD upload is completed or a token is
|
|
14178
|
-
// refreshed. That would throw after closing (and we can't handle those events either way), so set this back
|
|
14179
|
-
// to null.
|
|
14180
|
-
controlInvocations = null;
|
|
14181
|
-
await activeInstructions.close();
|
|
13729
|
+
if (resolvedOptions.serializedSchema) {
|
|
13730
|
+
options.schema = resolvedOptions.serializedSchema;
|
|
14182
13731
|
}
|
|
13732
|
+
return invokePowerSyncControl(PowerSyncControlCommand.START, JSON.stringify(options));
|
|
14183
13733
|
}
|
|
14184
13734
|
async function stop() {
|
|
14185
|
-
await
|
|
13735
|
+
const instructions = await invokePowerSyncControl(PowerSyncControlCommand.STOP);
|
|
13736
|
+
for (const instruction of instructions) {
|
|
13737
|
+
// We don't need to handle interrupting instructions since we're unconditionally ending the sync iteration at
|
|
13738
|
+
// this point.
|
|
13739
|
+
if (isInterruptingInstruction(instruction))
|
|
13740
|
+
continue;
|
|
13741
|
+
await handleInstruction(instruction);
|
|
13742
|
+
}
|
|
14186
13743
|
}
|
|
14187
|
-
async function
|
|
13744
|
+
async function invokePowerSyncControl(op, payload) {
|
|
14188
13745
|
const rawResponse = await adapter.control(op, payload ?? null);
|
|
14189
13746
|
const logger = syncImplementation.logger;
|
|
14190
13747
|
logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
|
|
14191
|
-
|
|
13748
|
+
if (op != PowerSyncControlCommand.STOP) {
|
|
13749
|
+
// Evidently we have a working connection here, otherwise powersync_control would have failed.
|
|
13750
|
+
syncImplementation.connectionMayHaveChanged = false;
|
|
13751
|
+
}
|
|
13752
|
+
return JSON.parse(rawResponse);
|
|
14192
13753
|
}
|
|
14193
13754
|
async function handleInstruction(instruction) {
|
|
14194
13755
|
if ('LogLine' in instruction) {
|
|
@@ -14207,13 +13768,6 @@ The next upload iteration will be delayed.`);
|
|
|
14207
13768
|
else if ('UpdateSyncStatus' in instruction) {
|
|
14208
13769
|
syncImplementation.updateSyncStatus(coreStatusToJs(instruction.UpdateSyncStatus.status));
|
|
14209
13770
|
}
|
|
14210
|
-
else if ('EstablishSyncStream' in instruction) {
|
|
14211
|
-
if (receivingLines != null) {
|
|
14212
|
-
// Already connected, this shouldn't happen during a single iteration.
|
|
14213
|
-
throw 'Unexpected request to establish sync stream, already connected';
|
|
14214
|
-
}
|
|
14215
|
-
receivingLines = connect(instruction.EstablishSyncStream);
|
|
14216
|
-
}
|
|
14217
13771
|
else if ('FetchCredentials' in instruction) {
|
|
14218
13772
|
if (instruction.FetchCredentials.did_expire) {
|
|
14219
13773
|
remote.invalidateCredentials();
|
|
@@ -14222,16 +13776,12 @@ The next upload iteration will be delayed.`);
|
|
|
14222
13776
|
remote.invalidateCredentials();
|
|
14223
13777
|
// Restart iteration after the credentials have been refreshed.
|
|
14224
13778
|
remote.fetchCredentials().then((_) => {
|
|
14225
|
-
|
|
13779
|
+
notifyTokenRefreshed?.();
|
|
14226
13780
|
}, (err) => {
|
|
14227
13781
|
syncImplementation.logger.warn('Could not prefetch credentials', err);
|
|
14228
13782
|
});
|
|
14229
13783
|
}
|
|
14230
13784
|
}
|
|
14231
|
-
else if ('CloseSyncStream' in instruction) {
|
|
14232
|
-
abortController.abort();
|
|
14233
|
-
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
14234
|
-
}
|
|
14235
13785
|
else if ('FlushFileSystem' in instruction) ;
|
|
14236
13786
|
else if ('DidCompleteSync' in instruction) {
|
|
14237
13787
|
syncImplementation.updateSyncStatus({
|
|
@@ -14241,105 +13791,83 @@ The next upload iteration will be delayed.`);
|
|
|
14241
13791
|
});
|
|
14242
13792
|
}
|
|
14243
13793
|
}
|
|
14244
|
-
async function handleInstructions(instructions) {
|
|
14245
|
-
for (const instr of instructions) {
|
|
14246
|
-
await handleInstruction(instr);
|
|
14247
|
-
}
|
|
14248
|
-
}
|
|
14249
13794
|
try {
|
|
14250
|
-
const
|
|
14251
|
-
|
|
14252
|
-
|
|
14253
|
-
|
|
14254
|
-
|
|
14255
|
-
|
|
14256
|
-
|
|
14257
|
-
|
|
13795
|
+
const defaultResult = { immediateRestart: false };
|
|
13796
|
+
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
|
|
13797
|
+
// invocation (local events include refreshed tokens and completed uploads).
|
|
13798
|
+
// This is a single data stream so that we can handle all control calls from a single place.
|
|
13799
|
+
let controlInvocations = null;
|
|
13800
|
+
for (const startInstruction of await startCommand()) {
|
|
13801
|
+
if ('EstablishSyncStream' in startInstruction) {
|
|
13802
|
+
const syncOptions = {
|
|
13803
|
+
path: '/sync/stream',
|
|
13804
|
+
abortSignal: signal,
|
|
13805
|
+
data: startInstruction.EstablishSyncStream.request
|
|
13806
|
+
};
|
|
13807
|
+
controlInvocations = injectable(syncImplementation.receiveSyncLines({
|
|
13808
|
+
options: syncOptions,
|
|
13809
|
+
connection: resolvedOptions
|
|
13810
|
+
}));
|
|
13811
|
+
}
|
|
13812
|
+
else if ('CloseSyncStream' in startInstruction) {
|
|
13813
|
+
return defaultResult;
|
|
13814
|
+
}
|
|
13815
|
+
else {
|
|
13816
|
+
await handleInstruction(startInstruction);
|
|
13817
|
+
}
|
|
14258
13818
|
}
|
|
14259
|
-
|
|
13819
|
+
if (controlInvocations == null)
|
|
13820
|
+
return defaultResult;
|
|
14260
13821
|
this.notifyCompletedUploads = () => {
|
|
14261
|
-
|
|
14262
|
-
controlInvocations.enqueueData({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
14263
|
-
}
|
|
13822
|
+
controlInvocations.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
14264
13823
|
};
|
|
14265
13824
|
this.handleActiveStreamsChange = () => {
|
|
14266
|
-
|
|
14267
|
-
|
|
14268
|
-
|
|
14269
|
-
|
|
14270
|
-
|
|
14271
|
-
|
|
13825
|
+
controlInvocations.inject({
|
|
13826
|
+
command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
|
|
13827
|
+
payload: JSON.stringify(this.activeStreams)
|
|
13828
|
+
});
|
|
13829
|
+
};
|
|
13830
|
+
notifyTokenRefreshed = () => {
|
|
13831
|
+
controlInvocations.inject({
|
|
13832
|
+
command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED
|
|
13833
|
+
});
|
|
14272
13834
|
};
|
|
14273
|
-
|
|
13835
|
+
let hadSyncLine = false;
|
|
13836
|
+
loop: while (true) {
|
|
13837
|
+
const { done, value } = await controlInvocations.next();
|
|
13838
|
+
if (done)
|
|
13839
|
+
break;
|
|
13840
|
+
if (!hadSyncLine) {
|
|
13841
|
+
// Trigger a local CRUD upload when the first sync line has been received, this allows uploading local changes
|
|
13842
|
+
// that have been made while offline or disconnected.
|
|
13843
|
+
if (value.command == PowerSyncControlCommand.PROCESS_TEXT_LINE ||
|
|
13844
|
+
value.command == PowerSyncControlCommand.PROCESS_BSON_LINE) {
|
|
13845
|
+
hadSyncLine = true;
|
|
13846
|
+
this.triggerCrudUpload?.();
|
|
13847
|
+
}
|
|
13848
|
+
}
|
|
13849
|
+
const instructions = await invokePowerSyncControl(value.command, value.payload);
|
|
13850
|
+
for (const instruction of instructions) {
|
|
13851
|
+
if ('EstablishSyncStream' in instruction) {
|
|
13852
|
+
throw new Error('Received EstablishSyncStream while already connected.');
|
|
13853
|
+
}
|
|
13854
|
+
else if ('CloseSyncStream' in instruction) {
|
|
13855
|
+
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
13856
|
+
break loop;
|
|
13857
|
+
}
|
|
13858
|
+
else {
|
|
13859
|
+
await handleInstruction(instruction);
|
|
13860
|
+
}
|
|
13861
|
+
}
|
|
13862
|
+
}
|
|
14274
13863
|
}
|
|
14275
13864
|
finally {
|
|
14276
13865
|
this.notifyCompletedUploads = this.handleActiveStreamsChange = undefined;
|
|
13866
|
+
notifyTokenRefreshed = undefined;
|
|
14277
13867
|
await stop();
|
|
14278
13868
|
}
|
|
14279
13869
|
return { immediateRestart: hideDisconnectOnRestart };
|
|
14280
13870
|
}
|
|
14281
|
-
async updateSyncStatusForStartingCheckpoint(checkpoint) {
|
|
14282
|
-
const localProgress = await this.options.adapter.getBucketOperationProgress();
|
|
14283
|
-
const progress = {};
|
|
14284
|
-
let invalidated = false;
|
|
14285
|
-
for (const bucket of checkpoint.buckets) {
|
|
14286
|
-
const savedProgress = localProgress[bucket.bucket];
|
|
14287
|
-
const atLast = savedProgress?.atLast ?? 0;
|
|
14288
|
-
const sinceLast = savedProgress?.sinceLast ?? 0;
|
|
14289
|
-
progress[bucket.bucket] = {
|
|
14290
|
-
// The fallback priority doesn't matter here, but 3 is the one newer versions of the sync service
|
|
14291
|
-
// will use by default.
|
|
14292
|
-
priority: bucket.priority ?? 3,
|
|
14293
|
-
at_last: atLast,
|
|
14294
|
-
since_last: sinceLast,
|
|
14295
|
-
target_count: bucket.count ?? 0
|
|
14296
|
-
};
|
|
14297
|
-
if (bucket.count != null && bucket.count < atLast + sinceLast) {
|
|
14298
|
-
// Either due to a defrag / sync rule deploy or a compaction operation, the size
|
|
14299
|
-
// of the bucket shrank so much that the local ops exceed the ops in the updated
|
|
14300
|
-
// bucket. We can't prossibly report progress in this case (it would overshoot 100%).
|
|
14301
|
-
invalidated = true;
|
|
14302
|
-
}
|
|
14303
|
-
}
|
|
14304
|
-
if (invalidated) {
|
|
14305
|
-
for (const bucket in progress) {
|
|
14306
|
-
const bucketProgress = progress[bucket];
|
|
14307
|
-
bucketProgress.at_last = 0;
|
|
14308
|
-
bucketProgress.since_last = 0;
|
|
14309
|
-
}
|
|
14310
|
-
}
|
|
14311
|
-
this.updateSyncStatus({
|
|
14312
|
-
dataFlow: {
|
|
14313
|
-
downloading: true,
|
|
14314
|
-
downloadProgress: progress
|
|
14315
|
-
}
|
|
14316
|
-
});
|
|
14317
|
-
}
|
|
14318
|
-
async applyCheckpoint(checkpoint) {
|
|
14319
|
-
let result = await this.options.adapter.syncLocalDatabase(checkpoint);
|
|
14320
|
-
if (!result.checkpointValid) {
|
|
14321
|
-
this.logger.debug(`Checksum mismatch in checkpoint ${checkpoint.last_op_id}, will reconnect`);
|
|
14322
|
-
// This means checksums failed. Start again with a new checkpoint.
|
|
14323
|
-
// TODO: better back-off
|
|
14324
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
14325
|
-
return { applied: false, endIteration: true };
|
|
14326
|
-
}
|
|
14327
|
-
else if (!result.ready) {
|
|
14328
|
-
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.`);
|
|
14329
|
-
return { applied: false, endIteration: false };
|
|
14330
|
-
}
|
|
14331
|
-
this.logger.debug(`Applied checkpoint ${checkpoint.last_op_id}`, checkpoint);
|
|
14332
|
-
this.updateSyncStatus({
|
|
14333
|
-
connected: true,
|
|
14334
|
-
lastSyncedAt: new Date(),
|
|
14335
|
-
dataFlow: {
|
|
14336
|
-
downloading: false,
|
|
14337
|
-
downloadProgress: null,
|
|
14338
|
-
downloadError: undefined
|
|
14339
|
-
}
|
|
14340
|
-
});
|
|
14341
|
-
return { applied: true, endIteration: false };
|
|
14342
|
-
}
|
|
14343
13871
|
updateSyncStatus(options) {
|
|
14344
13872
|
const updatedStatus = new SyncStatus({
|
|
14345
13873
|
connected: options.connected ?? this.syncStatus.connected,
|
|
@@ -15878,14 +15406,12 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
15878
15406
|
db;
|
|
15879
15407
|
logger;
|
|
15880
15408
|
tableNames;
|
|
15881
|
-
_hasCompletedSync;
|
|
15882
15409
|
updateListener;
|
|
15883
15410
|
_clientId;
|
|
15884
15411
|
constructor(db, logger = Logger.get('SqliteBucketStorage')) {
|
|
15885
15412
|
super();
|
|
15886
15413
|
this.db = db;
|
|
15887
15414
|
this.logger = logger;
|
|
15888
|
-
this._hasCompletedSync = false;
|
|
15889
15415
|
this.tableNames = new Set();
|
|
15890
15416
|
this.updateListener = db.registerListener({
|
|
15891
15417
|
tablesUpdated: (update) => {
|
|
@@ -15897,7 +15423,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
15897
15423
|
});
|
|
15898
15424
|
}
|
|
15899
15425
|
async init() {
|
|
15900
|
-
this._hasCompletedSync = false;
|
|
15901
15426
|
const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
|
|
15902
15427
|
for (const row of existingTableRows ?? []) {
|
|
15903
15428
|
this.tableNames.add(row.name);
|
|
@@ -15919,156 +15444,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
15919
15444
|
getMaxOpId() {
|
|
15920
15445
|
return MAX_OP_ID;
|
|
15921
15446
|
}
|
|
15922
|
-
/**
|
|
15923
|
-
* Reset any caches.
|
|
15924
|
-
*/
|
|
15925
|
-
startSession() { }
|
|
15926
|
-
async getBucketStates() {
|
|
15927
|
-
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'");
|
|
15928
|
-
return result;
|
|
15929
|
-
}
|
|
15930
|
-
async getBucketOperationProgress() {
|
|
15931
|
-
const rows = await this.db.getAll('SELECT name, count_at_last, count_since_last FROM ps_buckets');
|
|
15932
|
-
return Object.fromEntries(rows.map((r) => [r.name, { atLast: r.count_at_last, sinceLast: r.count_since_last }]));
|
|
15933
|
-
}
|
|
15934
|
-
async saveSyncData(batch, fixedKeyFormat = false) {
|
|
15935
|
-
await this.writeTransaction(async (tx) => {
|
|
15936
|
-
for (const b of batch.buckets) {
|
|
15937
|
-
await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
|
|
15938
|
-
'save',
|
|
15939
|
-
JSON.stringify({ buckets: [b.toJSON(fixedKeyFormat)] })
|
|
15940
|
-
]);
|
|
15941
|
-
this.logger.debug(`Saved batch of data for bucket: ${b.bucket}, operations: ${b.data.length}`);
|
|
15942
|
-
}
|
|
15943
|
-
});
|
|
15944
|
-
}
|
|
15945
|
-
async removeBuckets(buckets) {
|
|
15946
|
-
for (const bucket of buckets) {
|
|
15947
|
-
await this.deleteBucket(bucket);
|
|
15948
|
-
}
|
|
15949
|
-
}
|
|
15950
|
-
/**
|
|
15951
|
-
* Mark a bucket for deletion.
|
|
15952
|
-
*/
|
|
15953
|
-
async deleteBucket(bucket) {
|
|
15954
|
-
await this.writeTransaction(async (tx) => {
|
|
15955
|
-
await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', ['delete_bucket', bucket]);
|
|
15956
|
-
});
|
|
15957
|
-
this.logger.debug(`Done deleting bucket ${bucket}`);
|
|
15958
|
-
}
|
|
15959
|
-
async hasCompletedSync() {
|
|
15960
|
-
if (this._hasCompletedSync) {
|
|
15961
|
-
return true;
|
|
15962
|
-
}
|
|
15963
|
-
const r = await this.db.get(`SELECT powersync_last_synced_at() as synced_at`);
|
|
15964
|
-
const completed = r.synced_at != null;
|
|
15965
|
-
if (completed) {
|
|
15966
|
-
this._hasCompletedSync = true;
|
|
15967
|
-
}
|
|
15968
|
-
return completed;
|
|
15969
|
-
}
|
|
15970
|
-
async syncLocalDatabase(checkpoint, priority) {
|
|
15971
|
-
const r = await this.validateChecksums(checkpoint, priority);
|
|
15972
|
-
if (!r.checkpointValid) {
|
|
15973
|
-
this.logger.error('Checksums failed for', r.checkpointFailures);
|
|
15974
|
-
for (const b of r.checkpointFailures ?? []) {
|
|
15975
|
-
await this.deleteBucket(b);
|
|
15976
|
-
}
|
|
15977
|
-
return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
|
|
15978
|
-
}
|
|
15979
|
-
if (priority == null) {
|
|
15980
|
-
this.logger.debug(`Validated checksums checkpoint ${checkpoint.last_op_id}`);
|
|
15981
|
-
}
|
|
15982
|
-
else {
|
|
15983
|
-
this.logger.debug(`Validated checksums for partial checkpoint ${checkpoint.last_op_id}, priority ${priority}`);
|
|
15984
|
-
}
|
|
15985
|
-
let buckets = checkpoint.buckets;
|
|
15986
|
-
if (priority !== undefined) {
|
|
15987
|
-
buckets = buckets.filter((b) => hasMatchingPriority(priority, b));
|
|
15988
|
-
}
|
|
15989
|
-
const bucketNames = buckets.map((b) => b.bucket);
|
|
15990
|
-
await this.writeTransaction(async (tx) => {
|
|
15991
|
-
await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
|
|
15992
|
-
checkpoint.last_op_id,
|
|
15993
|
-
JSON.stringify(bucketNames)
|
|
15994
|
-
]);
|
|
15995
|
-
if (priority == null && checkpoint.write_checkpoint) {
|
|
15996
|
-
await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
|
|
15997
|
-
}
|
|
15998
|
-
});
|
|
15999
|
-
const valid = await this.updateObjectsFromBuckets(checkpoint, priority);
|
|
16000
|
-
if (!valid) {
|
|
16001
|
-
return { ready: false, checkpointValid: true };
|
|
16002
|
-
}
|
|
16003
|
-
return {
|
|
16004
|
-
ready: true,
|
|
16005
|
-
checkpointValid: true
|
|
16006
|
-
};
|
|
16007
|
-
}
|
|
16008
|
-
/**
|
|
16009
|
-
* Atomically update the local state to the current checkpoint.
|
|
16010
|
-
*
|
|
16011
|
-
* This includes creating new tables, dropping old tables, and copying data over from the oplog.
|
|
16012
|
-
*/
|
|
16013
|
-
async updateObjectsFromBuckets(checkpoint, priority) {
|
|
16014
|
-
let arg = '';
|
|
16015
|
-
if (priority !== undefined) {
|
|
16016
|
-
const affectedBuckets = [];
|
|
16017
|
-
for (const desc of checkpoint.buckets) {
|
|
16018
|
-
if (hasMatchingPriority(priority, desc)) {
|
|
16019
|
-
affectedBuckets.push(desc.bucket);
|
|
16020
|
-
}
|
|
16021
|
-
}
|
|
16022
|
-
arg = JSON.stringify({ priority, buckets: affectedBuckets });
|
|
16023
|
-
}
|
|
16024
|
-
return this.writeTransaction(async (tx) => {
|
|
16025
|
-
const { insertId: result } = await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
|
|
16026
|
-
'sync_local',
|
|
16027
|
-
arg
|
|
16028
|
-
]);
|
|
16029
|
-
if (result == 1) {
|
|
16030
|
-
if (priority == null) {
|
|
16031
|
-
const bucketToCount = Object.fromEntries(checkpoint.buckets.map((b) => [b.bucket, b.count]));
|
|
16032
|
-
// The two parameters could be replaced with one, but: https://github.com/powersync-ja/better-sqlite3/pull/6
|
|
16033
|
-
const jsonBucketCount = JSON.stringify(bucketToCount);
|
|
16034
|
-
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]);
|
|
16035
|
-
}
|
|
16036
|
-
return true;
|
|
16037
|
-
}
|
|
16038
|
-
else {
|
|
16039
|
-
return false;
|
|
16040
|
-
}
|
|
16041
|
-
});
|
|
16042
|
-
}
|
|
16043
|
-
async validateChecksums(checkpoint, priority) {
|
|
16044
|
-
if (priority !== undefined) {
|
|
16045
|
-
// Only validate the buckets within the priority we care about
|
|
16046
|
-
const newBuckets = checkpoint.buckets.filter((cs) => hasMatchingPriority(priority, cs));
|
|
16047
|
-
checkpoint = { ...checkpoint, buckets: newBuckets };
|
|
16048
|
-
}
|
|
16049
|
-
const rs = await this.db.execute('SELECT powersync_validate_checkpoint(?) as result', [
|
|
16050
|
-
JSON.stringify({ ...checkpoint })
|
|
16051
|
-
]);
|
|
16052
|
-
const resultItem = rs.rows?.item(0);
|
|
16053
|
-
if (!resultItem) {
|
|
16054
|
-
return {
|
|
16055
|
-
checkpointValid: false,
|
|
16056
|
-
ready: false,
|
|
16057
|
-
checkpointFailures: []
|
|
16058
|
-
};
|
|
16059
|
-
}
|
|
16060
|
-
const result = JSON.parse(resultItem['result']);
|
|
16061
|
-
if (result['valid']) {
|
|
16062
|
-
return { ready: true, checkpointValid: true };
|
|
16063
|
-
}
|
|
16064
|
-
else {
|
|
16065
|
-
return {
|
|
16066
|
-
checkpointValid: false,
|
|
16067
|
-
ready: false,
|
|
16068
|
-
checkpointFailures: result['failed_buckets']
|
|
16069
|
-
};
|
|
16070
|
-
}
|
|
16071
|
-
}
|
|
16072
15447
|
async updateLocalTarget(cb) {
|
|
16073
15448
|
const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)", [MAX_OP_ID]);
|
|
16074
15449
|
if (!rs1.length) {
|
|
@@ -16159,12 +15534,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
16159
15534
|
async writeTransaction(callback, options) {
|
|
16160
15535
|
return this.db.writeTransaction(callback, options);
|
|
16161
15536
|
}
|
|
16162
|
-
/**
|
|
16163
|
-
* Set a target checkpoint.
|
|
16164
|
-
*/
|
|
16165
|
-
async setTargetCheckpoint(checkpoint) {
|
|
16166
|
-
// No-op for now
|
|
16167
|
-
}
|
|
16168
15537
|
async control(op, payload) {
|
|
16169
15538
|
return await this.writeTransaction(async (tx) => {
|
|
16170
15539
|
const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
|
|
@@ -16188,20 +15557,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
16188
15557
|
}
|
|
16189
15558
|
static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
|
|
16190
15559
|
}
|
|
16191
|
-
function hasMatchingPriority(priority, bucket) {
|
|
16192
|
-
return bucket.priority != null && bucket.priority <= priority;
|
|
16193
|
-
}
|
|
16194
|
-
|
|
16195
|
-
// TODO JSON
|
|
16196
|
-
class SyncDataBatch {
|
|
16197
|
-
buckets;
|
|
16198
|
-
static fromJSON(json) {
|
|
16199
|
-
return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
|
|
16200
|
-
}
|
|
16201
|
-
constructor(buckets) {
|
|
16202
|
-
this.buckets = buckets;
|
|
16203
|
-
}
|
|
16204
|
-
}
|
|
16205
15560
|
|
|
16206
15561
|
/**
|
|
16207
15562
|
* Thrown when an underlying database connection is closed.
|
|
@@ -16261,10 +15616,8 @@ class Schema {
|
|
|
16261
15616
|
* developer instead of automatically by PowerSync.
|
|
16262
15617
|
* Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
|
|
16263
15618
|
* using client-side table and column constraints.
|
|
16264
|
-
* Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
|
|
16265
15619
|
*
|
|
16266
15620
|
* @param tables An object of (table name, raw table definition) entries.
|
|
16267
|
-
* @experimental Note that the raw tables API is still experimental and may change in the future.
|
|
16268
15621
|
*/
|
|
16269
15622
|
withRawTables(tables) {
|
|
16270
15623
|
for (const [name, rawTableDefinition] of Object.entries(tables)) {
|
|
@@ -16499,9 +15852,6 @@ const parseQuery = (query, parameters) => {
|
|
|
16499
15852
|
/******/ return module.exports;
|
|
16500
15853
|
/******/ }
|
|
16501
15854
|
/******/
|
|
16502
|
-
/******/ // expose the modules object (__webpack_modules__)
|
|
16503
|
-
/******/ __webpack_require__.m = __webpack_modules__;
|
|
16504
|
-
/******/
|
|
16505
15855
|
/************************************************************************/
|
|
16506
15856
|
/******/ /* webpack/runtime/define property getters */
|
|
16507
15857
|
/******/ (() => {
|
|
@@ -16515,40 +15865,6 @@ const parseQuery = (query, parameters) => {
|
|
|
16515
15865
|
/******/ };
|
|
16516
15866
|
/******/ })();
|
|
16517
15867
|
/******/
|
|
16518
|
-
/******/ /* webpack/runtime/ensure chunk */
|
|
16519
|
-
/******/ (() => {
|
|
16520
|
-
/******/ __webpack_require__.f = {};
|
|
16521
|
-
/******/ // This file contains only the entry chunk.
|
|
16522
|
-
/******/ // The chunk loading function for additional chunks
|
|
16523
|
-
/******/ __webpack_require__.e = (chunkId) => {
|
|
16524
|
-
/******/ return Promise.all(Object.keys(__webpack_require__.f).reduce((promises, key) => {
|
|
16525
|
-
/******/ __webpack_require__.f[key](chunkId, promises);
|
|
16526
|
-
/******/ return promises;
|
|
16527
|
-
/******/ }, []));
|
|
16528
|
-
/******/ };
|
|
16529
|
-
/******/ })();
|
|
16530
|
-
/******/
|
|
16531
|
-
/******/ /* webpack/runtime/get javascript chunk filename */
|
|
16532
|
-
/******/ (() => {
|
|
16533
|
-
/******/ // This function allow to reference async chunks
|
|
16534
|
-
/******/ __webpack_require__.u = (chunkId) => {
|
|
16535
|
-
/******/ // return url for filenames based on template
|
|
16536
|
-
/******/ return "worker/" + chunkId + ".umd.js";
|
|
16537
|
-
/******/ };
|
|
16538
|
-
/******/ })();
|
|
16539
|
-
/******/
|
|
16540
|
-
/******/ /* webpack/runtime/global */
|
|
16541
|
-
/******/ (() => {
|
|
16542
|
-
/******/ __webpack_require__.g = (function() {
|
|
16543
|
-
/******/ if (typeof globalThis === 'object') return globalThis;
|
|
16544
|
-
/******/ try {
|
|
16545
|
-
/******/ return this || new Function('return this')();
|
|
16546
|
-
/******/ } catch (e) {
|
|
16547
|
-
/******/ if (typeof window === 'object') return window;
|
|
16548
|
-
/******/ }
|
|
16549
|
-
/******/ })();
|
|
16550
|
-
/******/ })();
|
|
16551
|
-
/******/
|
|
16552
15868
|
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
16553
15869
|
/******/ (() => {
|
|
16554
15870
|
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
@@ -16565,70 +15881,6 @@ const parseQuery = (query, parameters) => {
|
|
|
16565
15881
|
/******/ };
|
|
16566
15882
|
/******/ })();
|
|
16567
15883
|
/******/
|
|
16568
|
-
/******/ /* webpack/runtime/publicPath */
|
|
16569
|
-
/******/ (() => {
|
|
16570
|
-
/******/ var scriptUrl;
|
|
16571
|
-
/******/ if (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + "";
|
|
16572
|
-
/******/ var document = __webpack_require__.g.document;
|
|
16573
|
-
/******/ if (!scriptUrl && document) {
|
|
16574
|
-
/******/ if (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT')
|
|
16575
|
-
/******/ scriptUrl = document.currentScript.src;
|
|
16576
|
-
/******/ if (!scriptUrl) {
|
|
16577
|
-
/******/ var scripts = document.getElementsByTagName("script");
|
|
16578
|
-
/******/ if(scripts.length) {
|
|
16579
|
-
/******/ var i = scripts.length - 1;
|
|
16580
|
-
/******/ while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src;
|
|
16581
|
-
/******/ }
|
|
16582
|
-
/******/ }
|
|
16583
|
-
/******/ }
|
|
16584
|
-
/******/ // When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration
|
|
16585
|
-
/******/ // or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.
|
|
16586
|
-
/******/ if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
|
|
16587
|
-
/******/ scriptUrl = scriptUrl.replace(/^blob:/, "").replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
|
|
16588
|
-
/******/ __webpack_require__.p = scriptUrl + "../";
|
|
16589
|
-
/******/ })();
|
|
16590
|
-
/******/
|
|
16591
|
-
/******/ /* webpack/runtime/importScripts chunk loading */
|
|
16592
|
-
/******/ (() => {
|
|
16593
|
-
/******/ // no baseURI
|
|
16594
|
-
/******/
|
|
16595
|
-
/******/ // object to store loaded chunks
|
|
16596
|
-
/******/ // "1" means "already loaded"
|
|
16597
|
-
/******/ var installedChunks = {
|
|
16598
|
-
/******/ "SharedSyncImplementation": 1
|
|
16599
|
-
/******/ };
|
|
16600
|
-
/******/
|
|
16601
|
-
/******/ // importScripts chunk loading
|
|
16602
|
-
/******/ var installChunk = (data) => {
|
|
16603
|
-
/******/ var [chunkIds, moreModules, runtime] = data;
|
|
16604
|
-
/******/ for(var moduleId in moreModules) {
|
|
16605
|
-
/******/ if(__webpack_require__.o(moreModules, moduleId)) {
|
|
16606
|
-
/******/ __webpack_require__.m[moduleId] = moreModules[moduleId];
|
|
16607
|
-
/******/ }
|
|
16608
|
-
/******/ }
|
|
16609
|
-
/******/ if(runtime) runtime(__webpack_require__);
|
|
16610
|
-
/******/ while(chunkIds.length)
|
|
16611
|
-
/******/ installedChunks[chunkIds.pop()] = 1;
|
|
16612
|
-
/******/ parentChunkLoadingFunction(data);
|
|
16613
|
-
/******/ };
|
|
16614
|
-
/******/ __webpack_require__.f.i = (chunkId, promises) => {
|
|
16615
|
-
/******/ // "1" is the signal for "already loaded"
|
|
16616
|
-
/******/ if(!installedChunks[chunkId]) {
|
|
16617
|
-
/******/ if(true) { // all chunks have JS
|
|
16618
|
-
/******/ importScripts(__webpack_require__.p + __webpack_require__.u(chunkId));
|
|
16619
|
-
/******/ }
|
|
16620
|
-
/******/ }
|
|
16621
|
-
/******/ };
|
|
16622
|
-
/******/
|
|
16623
|
-
/******/ var chunkLoadingGlobal = self["webpackChunksdk_web"] = self["webpackChunksdk_web"] || [];
|
|
16624
|
-
/******/ var parentChunkLoadingFunction = chunkLoadingGlobal.push.bind(chunkLoadingGlobal);
|
|
16625
|
-
/******/ chunkLoadingGlobal.push = installChunk;
|
|
16626
|
-
/******/
|
|
16627
|
-
/******/ // no HMR
|
|
16628
|
-
/******/
|
|
16629
|
-
/******/ // no HMR manifest
|
|
16630
|
-
/******/ })();
|
|
16631
|
-
/******/
|
|
16632
15884
|
/************************************************************************/
|
|
16633
15885
|
var __webpack_exports__ = {};
|
|
16634
15886
|
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
|