@powersync/web 0.0.0-dev-20260503073249 → 0.0.0-dev-20260504100448
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/index.umd.js +0 -4664
- package/dist/index.umd.js.map +1 -1
- package/dist/worker/SharedSyncImplementation.umd.js +14 -784
- package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
- package/dist/worker/WASQLiteDB.umd.js +14 -660
- package/dist/worker/WASQLiteDB.umd.js.map +1 -1
- package/lib/package.json +1 -4
- 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/sync/SharedSyncImplementation.d.ts +0 -2
- package/lib/src/worker/sync/SharedSyncImplementation.js +0 -8
- 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 +4 -7
- package/src/db/sync/SharedWebStreamingSyncImplementation.ts +0 -4
- package/src/db/sync/WebRemote.ts +0 -16
- package/src/worker/sync/SharedSyncImplementation.ts +0 -10
- package/src/worker/sync/WorkerClient.ts +0 -4
- 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
|
@@ -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();
|
|
@@ -1636,9 +1616,6 @@ class WorkerClient {
|
|
|
1636
1616
|
getWriteCheckpoint() {
|
|
1637
1617
|
return this.sync.getWriteCheckpoint();
|
|
1638
1618
|
}
|
|
1639
|
-
hasCompletedSync() {
|
|
1640
|
-
return this.sync.hasCompletedSync();
|
|
1641
|
-
}
|
|
1642
1619
|
connect(options) {
|
|
1643
1620
|
return this.sync.connect(options);
|
|
1644
1621
|
}
|
|
@@ -1722,9 +1699,6 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1722
1699
|
/* harmony export */ MEMORY_TRIGGER_CLAIM_MANAGER: () => (/* binding */ MEMORY_TRIGGER_CLAIM_MANAGER),
|
|
1723
1700
|
/* harmony export */ Mutex: () => (/* binding */ Mutex),
|
|
1724
1701
|
/* harmony export */ OnChangeQueryProcessor: () => (/* binding */ OnChangeQueryProcessor),
|
|
1725
|
-
/* harmony export */ OpType: () => (/* binding */ OpType),
|
|
1726
|
-
/* harmony export */ OpTypeEnum: () => (/* binding */ OpTypeEnum),
|
|
1727
|
-
/* harmony export */ OplogEntry: () => (/* binding */ OplogEntry),
|
|
1728
1702
|
/* harmony export */ PSInternalTable: () => (/* binding */ PSInternalTable),
|
|
1729
1703
|
/* harmony export */ PowerSyncControlCommand: () => (/* binding */ PowerSyncControlCommand),
|
|
1730
1704
|
/* harmony export */ RowUpdateType: () => (/* binding */ RowUpdateType),
|
|
@@ -1732,8 +1706,6 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1732
1706
|
/* harmony export */ Semaphore: () => (/* binding */ Semaphore),
|
|
1733
1707
|
/* harmony export */ SqliteBucketStorage: () => (/* binding */ SqliteBucketStorage),
|
|
1734
1708
|
/* harmony export */ SyncClientImplementation: () => (/* binding */ SyncClientImplementation),
|
|
1735
|
-
/* harmony export */ SyncDataBatch: () => (/* binding */ SyncDataBatch),
|
|
1736
|
-
/* harmony export */ SyncDataBucket: () => (/* binding */ SyncDataBucket),
|
|
1737
1709
|
/* harmony export */ SyncProgress: () => (/* binding */ SyncProgress),
|
|
1738
1710
|
/* harmony export */ SyncStatus: () => (/* binding */ SyncStatus),
|
|
1739
1711
|
/* harmony export */ SyncStreamConnectionMethod: () => (/* binding */ SyncStreamConnectionMethod),
|
|
@@ -1751,18 +1723,10 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1751
1723
|
/* harmony export */ createLogger: () => (/* binding */ createLogger),
|
|
1752
1724
|
/* harmony export */ extractTableUpdates: () => (/* binding */ extractTableUpdates),
|
|
1753
1725
|
/* harmony export */ isBatchedUpdateNotification: () => (/* binding */ isBatchedUpdateNotification),
|
|
1754
|
-
/* harmony export */ isContinueCheckpointRequest: () => (/* binding */ isContinueCheckpointRequest),
|
|
1755
1726
|
/* harmony export */ isDBAdapter: () => (/* binding */ isDBAdapter),
|
|
1756
1727
|
/* harmony export */ isPowerSyncDatabaseOptionsWithSettings: () => (/* binding */ isPowerSyncDatabaseOptionsWithSettings),
|
|
1757
1728
|
/* harmony export */ isSQLOpenFactory: () => (/* binding */ isSQLOpenFactory),
|
|
1758
1729
|
/* harmony export */ isSQLOpenOptions: () => (/* binding */ isSQLOpenOptions),
|
|
1759
|
-
/* harmony export */ isStreamingKeepalive: () => (/* binding */ isStreamingKeepalive),
|
|
1760
|
-
/* harmony export */ isStreamingSyncCheckpoint: () => (/* binding */ isStreamingSyncCheckpoint),
|
|
1761
|
-
/* harmony export */ isStreamingSyncCheckpointComplete: () => (/* binding */ isStreamingSyncCheckpointComplete),
|
|
1762
|
-
/* harmony export */ isStreamingSyncCheckpointDiff: () => (/* binding */ isStreamingSyncCheckpointDiff),
|
|
1763
|
-
/* harmony export */ isStreamingSyncCheckpointPartiallyComplete: () => (/* binding */ isStreamingSyncCheckpointPartiallyComplete),
|
|
1764
|
-
/* harmony export */ isStreamingSyncData: () => (/* binding */ isStreamingSyncData),
|
|
1765
|
-
/* harmony export */ isSyncNewCheckpointRequest: () => (/* binding */ isSyncNewCheckpointRequest),
|
|
1766
1730
|
/* harmony export */ parseQuery: () => (/* binding */ parseQuery),
|
|
1767
1731
|
/* harmony export */ runOnSchemaChange: () => (/* binding */ runOnSchemaChange),
|
|
1768
1732
|
/* harmony export */ sanitizeSQL: () => (/* binding */ sanitizeSQL),
|
|
@@ -5258,103 +5222,6 @@ class AbortOperation extends Error {
|
|
|
5258
5222
|
}
|
|
5259
5223
|
}
|
|
5260
5224
|
|
|
5261
|
-
var OpTypeEnum;
|
|
5262
|
-
(function (OpTypeEnum) {
|
|
5263
|
-
OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
|
|
5264
|
-
OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
|
|
5265
|
-
OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
|
|
5266
|
-
OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
|
|
5267
|
-
})(OpTypeEnum || (OpTypeEnum = {}));
|
|
5268
|
-
/**
|
|
5269
|
-
* Used internally for sync buckets.
|
|
5270
|
-
*/
|
|
5271
|
-
class OpType {
|
|
5272
|
-
value;
|
|
5273
|
-
static fromJSON(jsonValue) {
|
|
5274
|
-
return new OpType(OpTypeEnum[jsonValue]);
|
|
5275
|
-
}
|
|
5276
|
-
constructor(value) {
|
|
5277
|
-
this.value = value;
|
|
5278
|
-
}
|
|
5279
|
-
toJSON() {
|
|
5280
|
-
return Object.entries(OpTypeEnum).find(([, value]) => value === this.value)[0];
|
|
5281
|
-
}
|
|
5282
|
-
}
|
|
5283
|
-
|
|
5284
|
-
class OplogEntry {
|
|
5285
|
-
op_id;
|
|
5286
|
-
op;
|
|
5287
|
-
checksum;
|
|
5288
|
-
subkey;
|
|
5289
|
-
object_type;
|
|
5290
|
-
object_id;
|
|
5291
|
-
data;
|
|
5292
|
-
static fromRow(row) {
|
|
5293
|
-
return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
|
|
5294
|
-
}
|
|
5295
|
-
constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
|
|
5296
|
-
this.op_id = op_id;
|
|
5297
|
-
this.op = op;
|
|
5298
|
-
this.checksum = checksum;
|
|
5299
|
-
this.subkey = subkey;
|
|
5300
|
-
this.object_type = object_type;
|
|
5301
|
-
this.object_id = object_id;
|
|
5302
|
-
this.data = data;
|
|
5303
|
-
}
|
|
5304
|
-
toJSON(fixedKeyEncoding = false) {
|
|
5305
|
-
return {
|
|
5306
|
-
op_id: this.op_id,
|
|
5307
|
-
op: this.op.toJSON(),
|
|
5308
|
-
object_type: this.object_type,
|
|
5309
|
-
object_id: this.object_id,
|
|
5310
|
-
checksum: this.checksum,
|
|
5311
|
-
data: this.data,
|
|
5312
|
-
// Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
|
|
5313
|
-
// but we need to migrate gradually to not break existing databases.
|
|
5314
|
-
subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
|
|
5315
|
-
};
|
|
5316
|
-
}
|
|
5317
|
-
}
|
|
5318
|
-
|
|
5319
|
-
class SyncDataBucket {
|
|
5320
|
-
bucket;
|
|
5321
|
-
data;
|
|
5322
|
-
has_more;
|
|
5323
|
-
after;
|
|
5324
|
-
next_after;
|
|
5325
|
-
static fromRow(row) {
|
|
5326
|
-
return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
|
|
5327
|
-
}
|
|
5328
|
-
constructor(bucket, data,
|
|
5329
|
-
/**
|
|
5330
|
-
* True if the response does not contain all the data for this bucket, and another request must be made.
|
|
5331
|
-
*/
|
|
5332
|
-
has_more,
|
|
5333
|
-
/**
|
|
5334
|
-
* The `after` specified in the request.
|
|
5335
|
-
*/
|
|
5336
|
-
after,
|
|
5337
|
-
/**
|
|
5338
|
-
* Use this for the next request.
|
|
5339
|
-
*/
|
|
5340
|
-
next_after) {
|
|
5341
|
-
this.bucket = bucket;
|
|
5342
|
-
this.data = data;
|
|
5343
|
-
this.has_more = has_more;
|
|
5344
|
-
this.after = after;
|
|
5345
|
-
this.next_after = next_after;
|
|
5346
|
-
}
|
|
5347
|
-
toJSON(fixedKeyEncoding = false) {
|
|
5348
|
-
return {
|
|
5349
|
-
bucket: this.bucket,
|
|
5350
|
-
has_more: this.has_more,
|
|
5351
|
-
after: this.after,
|
|
5352
|
-
next_after: this.next_after,
|
|
5353
|
-
data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
|
|
5354
|
-
};
|
|
5355
|
-
}
|
|
5356
|
-
}
|
|
5357
|
-
|
|
5358
5225
|
var buffer = {};
|
|
5359
5226
|
|
|
5360
5227
|
var base64Js = {};
|
|
@@ -13067,22 +12934,12 @@ class AbstractRemote {
|
|
|
13067
12934
|
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
13068
12935
|
*
|
|
13069
12936
|
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
13070
|
-
*
|
|
13071
|
-
* @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
|
|
13072
|
-
* (required for compatibility with older sync services).
|
|
13073
12937
|
*/
|
|
13074
|
-
async socketStreamRaw(options
|
|
12938
|
+
async socketStreamRaw(options) {
|
|
13075
12939
|
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
|
|
13076
|
-
const mimeType =
|
|
12940
|
+
const mimeType = 'application/json';
|
|
13077
12941
|
function toBuffer(js) {
|
|
13078
|
-
|
|
13079
|
-
if (bson != null) {
|
|
13080
|
-
contents = bson.serialize(js);
|
|
13081
|
-
}
|
|
13082
|
-
else {
|
|
13083
|
-
contents = JSON.stringify(js);
|
|
13084
|
-
}
|
|
13085
|
-
return bufferExports.Buffer.from(contents);
|
|
12942
|
+
return bufferExports.Buffer.from(JSON.stringify(js));
|
|
13086
12943
|
}
|
|
13087
12944
|
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
|
|
13088
12945
|
const request = await this.buildRequest(path);
|
|
@@ -13384,32 +13241,6 @@ function coreStatusToJs(status) {
|
|
|
13384
13241
|
};
|
|
13385
13242
|
}
|
|
13386
13243
|
|
|
13387
|
-
function isStreamingSyncData(line) {
|
|
13388
|
-
return line.data != null;
|
|
13389
|
-
}
|
|
13390
|
-
function isStreamingKeepalive(line) {
|
|
13391
|
-
return line.token_expires_in != null;
|
|
13392
|
-
}
|
|
13393
|
-
function isStreamingSyncCheckpoint(line) {
|
|
13394
|
-
return line.checkpoint != null;
|
|
13395
|
-
}
|
|
13396
|
-
function isStreamingSyncCheckpointComplete(line) {
|
|
13397
|
-
return line.checkpoint_complete != null;
|
|
13398
|
-
}
|
|
13399
|
-
function isStreamingSyncCheckpointPartiallyComplete(line) {
|
|
13400
|
-
return line.partial_checkpoint_complete != null;
|
|
13401
|
-
}
|
|
13402
|
-
function isStreamingSyncCheckpointDiff(line) {
|
|
13403
|
-
return line.checkpoint_diff != null;
|
|
13404
|
-
}
|
|
13405
|
-
function isContinueCheckpointRequest(request) {
|
|
13406
|
-
return (Array.isArray(request.buckets) &&
|
|
13407
|
-
typeof request.checkpoint_token == 'string');
|
|
13408
|
-
}
|
|
13409
|
-
function isSyncNewCheckpointRequest(request) {
|
|
13410
|
-
return typeof request.request_checkpoint == 'object';
|
|
13411
|
-
}
|
|
13412
|
-
|
|
13413
13244
|
var LockType;
|
|
13414
13245
|
(function (LockType) {
|
|
13415
13246
|
LockType["CRUD"] = "crud";
|
|
@@ -13422,35 +13253,21 @@ var SyncStreamConnectionMethod;
|
|
|
13422
13253
|
})(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
|
|
13423
13254
|
var SyncClientImplementation;
|
|
13424
13255
|
(function (SyncClientImplementation) {
|
|
13425
|
-
/**
|
|
13426
|
-
* Decodes and handles sync lines received from the sync service in JavaScript.
|
|
13427
|
-
*
|
|
13428
|
-
* This is the default option.
|
|
13429
|
-
*
|
|
13430
|
-
* @deprecated We recommend the {@link RUST} client implementation for all apps. If you have issues with
|
|
13431
|
-
* the Rust client, please file an issue or reach out to us. The JavaScript client will be removed in a future
|
|
13432
|
-
* version of the PowerSync SDK.
|
|
13433
|
-
*/
|
|
13434
|
-
SyncClientImplementation["JAVASCRIPT"] = "js";
|
|
13435
13256
|
/**
|
|
13436
13257
|
* This implementation offloads the sync line decoding and handling into the PowerSync
|
|
13437
13258
|
* core extension.
|
|
13438
13259
|
*
|
|
13439
|
-
* This
|
|
13440
|
-
* recommended client implementation for all apps.
|
|
13260
|
+
* This is the only option, as an older JavaScript client implementation has been removed from the SDK.
|
|
13441
13261
|
*
|
|
13442
13262
|
* ## Compatibility warning
|
|
13443
13263
|
*
|
|
13444
13264
|
* The Rust sync client stores sync data in a format that is slightly different than the one used
|
|
13445
|
-
* by the old
|
|
13446
|
-
*
|
|
13447
|
-
* Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
|
|
13448
|
-
* the version introducing {@link RUST} as an option) also supports the new format, so you can switch
|
|
13449
|
-
* back to {@link JAVASCRIPT} later.
|
|
13265
|
+
* by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
|
|
13266
|
+
* migrate the format automatically.
|
|
13450
13267
|
*
|
|
13451
|
-
*
|
|
13452
|
-
*
|
|
13453
|
-
*
|
|
13268
|
+
* SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
|
|
13269
|
+
* implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
|
|
13270
|
+
* possible anymore. Problematic SDK versions have been released before 2025-06-09.
|
|
13454
13271
|
*/
|
|
13455
13272
|
SyncClientImplementation["RUST"] = "rust";
|
|
13456
13273
|
})(SyncClientImplementation || (SyncClientImplementation = {}));
|
|
@@ -13473,13 +13290,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
|
13473
13290
|
serializedSchema: undefined,
|
|
13474
13291
|
includeDefaultStreams: true
|
|
13475
13292
|
};
|
|
13476
|
-
// The priority we assume when we receive checkpoint lines where no priority is set.
|
|
13477
|
-
// This is the default priority used by the sync service, but can be set to an arbitrary
|
|
13478
|
-
// value since sync services without priorities also won't send partial sync completion
|
|
13479
|
-
// messages.
|
|
13480
|
-
const FALLBACK_PRIORITY = 3;
|
|
13481
13293
|
class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
13482
|
-
_lastSyncedAt;
|
|
13483
13294
|
options;
|
|
13484
13295
|
abortController;
|
|
13485
13296
|
// In rare cases, mostly for tests, uploads can be triggered without being properly connected.
|
|
@@ -13569,9 +13380,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
13569
13380
|
this.crudUpdateListener = undefined;
|
|
13570
13381
|
this.uploadAbortController?.abort();
|
|
13571
13382
|
}
|
|
13572
|
-
async hasCompletedSync() {
|
|
13573
|
-
return this.options.adapter.hasCompletedSync();
|
|
13574
|
-
}
|
|
13575
13383
|
async getWriteCheckpoint() {
|
|
13576
13384
|
const clientId = await this.options.adapter.getClientId();
|
|
13577
13385
|
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
@@ -13653,7 +13461,7 @@ The next upload iteration will be delayed.`);
|
|
|
13653
13461
|
});
|
|
13654
13462
|
}
|
|
13655
13463
|
}
|
|
13656
|
-
this.uploadAbortController =
|
|
13464
|
+
this.uploadAbortController = undefined;
|
|
13657
13465
|
}
|
|
13658
13466
|
});
|
|
13659
13467
|
}
|
|
@@ -13813,18 +13621,6 @@ The next upload iteration will be delayed.`);
|
|
|
13813
13621
|
// iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
|
|
13814
13622
|
this.handleActiveStreamsChange?.();
|
|
13815
13623
|
}
|
|
13816
|
-
async collectLocalBucketState() {
|
|
13817
|
-
const bucketEntries = await this.options.adapter.getBucketStates();
|
|
13818
|
-
const req = bucketEntries.map((entry) => ({
|
|
13819
|
-
name: entry.bucket,
|
|
13820
|
-
after: entry.op_id
|
|
13821
|
-
}));
|
|
13822
|
-
const localDescriptions = new Map();
|
|
13823
|
-
for (const entry of bucketEntries) {
|
|
13824
|
-
localDescriptions.set(entry.bucket, null);
|
|
13825
|
-
}
|
|
13826
|
-
return [req, localDescriptions];
|
|
13827
|
-
}
|
|
13828
13624
|
/**
|
|
13829
13625
|
* Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
|
|
13830
13626
|
* Because subkeys are always strings, this leads to quotes being added around them in `ps_oplog`.
|
|
@@ -13866,19 +13662,13 @@ The next upload iteration will be delayed.`);
|
|
|
13866
13662
|
}
|
|
13867
13663
|
const clientImplementation = resolvedOptions.clientImplementation;
|
|
13868
13664
|
this.updateSyncStatus({ clientImplementation });
|
|
13869
|
-
|
|
13870
|
-
|
|
13871
|
-
return null;
|
|
13872
|
-
}
|
|
13873
|
-
else {
|
|
13874
|
-
await this.requireKeyFormat(true);
|
|
13875
|
-
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
13876
|
-
}
|
|
13665
|
+
await this.requireKeyFormat(true);
|
|
13666
|
+
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
13877
13667
|
}
|
|
13878
13668
|
});
|
|
13879
13669
|
}
|
|
13880
13670
|
async receiveSyncLines(data) {
|
|
13881
|
-
const { options, connection
|
|
13671
|
+
const { options, connection } = data;
|
|
13882
13672
|
const remote = this.options.remote;
|
|
13883
13673
|
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
13884
13674
|
return await remote.fetchStream(options);
|
|
@@ -13887,232 +13677,8 @@ The next upload iteration will be delayed.`);
|
|
|
13887
13677
|
return await this.options.remote.socketStreamRaw({
|
|
13888
13678
|
...options,
|
|
13889
13679
|
...{ fetchStrategy: connection.fetchStrategy }
|
|
13890
|
-
}
|
|
13891
|
-
}
|
|
13892
|
-
}
|
|
13893
|
-
async legacyStreamingSyncIteration(signal, resolvedOptions) {
|
|
13894
|
-
const rawTables = resolvedOptions.serializedSchema?.raw_tables;
|
|
13895
|
-
if (rawTables != null && rawTables.length) {
|
|
13896
|
-
this.logger.warn('Raw tables require the Rust-based sync client. The JS client will ignore them.');
|
|
13897
|
-
}
|
|
13898
|
-
if (this.activeStreams.length) {
|
|
13899
|
-
this.logger.error('Sync streams require `clientImplementation: SyncClientImplementation.RUST` when connecting.');
|
|
13900
|
-
}
|
|
13901
|
-
this.logger.debug('Streaming sync iteration started');
|
|
13902
|
-
this.options.adapter.startSession();
|
|
13903
|
-
let [req, bucketMap] = await this.collectLocalBucketState();
|
|
13904
|
-
let targetCheckpoint = null;
|
|
13905
|
-
// A checkpoint that has been validated but not applied (e.g. due to pending local writes)
|
|
13906
|
-
let pendingValidatedCheckpoint = null;
|
|
13907
|
-
const clientId = await this.options.adapter.getClientId();
|
|
13908
|
-
const usingFixedKeyFormat = await this.requireKeyFormat(false);
|
|
13909
|
-
this.logger.debug('Requesting stream from server');
|
|
13910
|
-
const syncOptions = {
|
|
13911
|
-
path: '/sync/stream',
|
|
13912
|
-
abortSignal: signal,
|
|
13913
|
-
data: {
|
|
13914
|
-
buckets: req,
|
|
13915
|
-
include_checksum: true,
|
|
13916
|
-
raw_data: true,
|
|
13917
|
-
parameters: resolvedOptions.params,
|
|
13918
|
-
app_metadata: resolvedOptions.appMetadata,
|
|
13919
|
-
client_id: clientId
|
|
13920
|
-
}
|
|
13921
|
-
};
|
|
13922
|
-
const bson = await this.options.remote.getBSON();
|
|
13923
|
-
const source = await this.receiveSyncLines({
|
|
13924
|
-
options: syncOptions,
|
|
13925
|
-
connection: resolvedOptions,
|
|
13926
|
-
bson
|
|
13927
|
-
});
|
|
13928
|
-
const stream = injectable(map(source, (line) => {
|
|
13929
|
-
if (typeof line == 'string') {
|
|
13930
|
-
return JSON.parse(line);
|
|
13931
|
-
}
|
|
13932
|
-
else {
|
|
13933
|
-
return bson.deserialize(line);
|
|
13934
|
-
}
|
|
13935
|
-
}));
|
|
13936
|
-
this.logger.debug('Stream established. Processing events');
|
|
13937
|
-
this.notifyCompletedUploads = () => {
|
|
13938
|
-
stream.inject({ crud_upload_completed: null });
|
|
13939
|
-
};
|
|
13940
|
-
while (true) {
|
|
13941
|
-
const { value: line, done } = await stream.next();
|
|
13942
|
-
if (done) {
|
|
13943
|
-
// The stream has closed while waiting
|
|
13944
|
-
return;
|
|
13945
|
-
}
|
|
13946
|
-
if ('crud_upload_completed' in line) {
|
|
13947
|
-
if (pendingValidatedCheckpoint != null) {
|
|
13948
|
-
const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
|
|
13949
|
-
if (applied) {
|
|
13950
|
-
pendingValidatedCheckpoint = null;
|
|
13951
|
-
}
|
|
13952
|
-
else if (endIteration) {
|
|
13953
|
-
break;
|
|
13954
|
-
}
|
|
13955
|
-
}
|
|
13956
|
-
continue;
|
|
13957
|
-
}
|
|
13958
|
-
// A connection is active and messages are being received
|
|
13959
|
-
if (!this.syncStatus.connected) {
|
|
13960
|
-
// There is a connection now
|
|
13961
|
-
Promise.resolve().then(() => this.triggerCrudUpload());
|
|
13962
|
-
this.updateSyncStatus({
|
|
13963
|
-
connected: true
|
|
13964
|
-
});
|
|
13965
|
-
}
|
|
13966
|
-
if (isStreamingSyncCheckpoint(line)) {
|
|
13967
|
-
targetCheckpoint = line.checkpoint;
|
|
13968
|
-
// New checkpoint - existing validated checkpoint is no longer valid
|
|
13969
|
-
pendingValidatedCheckpoint = null;
|
|
13970
|
-
const bucketsToDelete = new Set(bucketMap.keys());
|
|
13971
|
-
const newBuckets = new Map();
|
|
13972
|
-
for (const checksum of line.checkpoint.buckets) {
|
|
13973
|
-
newBuckets.set(checksum.bucket, {
|
|
13974
|
-
name: checksum.bucket,
|
|
13975
|
-
priority: checksum.priority ?? FALLBACK_PRIORITY
|
|
13976
|
-
});
|
|
13977
|
-
bucketsToDelete.delete(checksum.bucket);
|
|
13978
|
-
}
|
|
13979
|
-
if (bucketsToDelete.size > 0) {
|
|
13980
|
-
this.logger.debug('Removing buckets', [...bucketsToDelete]);
|
|
13981
|
-
}
|
|
13982
|
-
bucketMap = newBuckets;
|
|
13983
|
-
await this.options.adapter.removeBuckets([...bucketsToDelete]);
|
|
13984
|
-
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
|
|
13985
|
-
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
|
|
13986
|
-
}
|
|
13987
|
-
else if (isStreamingSyncCheckpointComplete(line)) {
|
|
13988
|
-
const result = await this.applyCheckpoint(targetCheckpoint);
|
|
13989
|
-
if (result.endIteration) {
|
|
13990
|
-
return;
|
|
13991
|
-
}
|
|
13992
|
-
else if (!result.applied) {
|
|
13993
|
-
// "Could not apply checkpoint due to local data". We need to retry after
|
|
13994
|
-
// finishing uploads.
|
|
13995
|
-
pendingValidatedCheckpoint = targetCheckpoint;
|
|
13996
|
-
}
|
|
13997
|
-
else {
|
|
13998
|
-
// Nothing to retry later. This would likely already be null from the last
|
|
13999
|
-
// checksum or checksum_diff operation, but we make sure.
|
|
14000
|
-
pendingValidatedCheckpoint = null;
|
|
14001
|
-
}
|
|
14002
|
-
}
|
|
14003
|
-
else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
|
|
14004
|
-
const priority = line.partial_checkpoint_complete.priority;
|
|
14005
|
-
this.logger.debug('Partial checkpoint complete', priority);
|
|
14006
|
-
const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint, priority);
|
|
14007
|
-
if (!result.checkpointValid) {
|
|
14008
|
-
// This means checksums failed. Start again with a new checkpoint.
|
|
14009
|
-
// TODO: better back-off
|
|
14010
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
14011
|
-
return;
|
|
14012
|
-
}
|
|
14013
|
-
else if (!result.ready) ;
|
|
14014
|
-
else {
|
|
14015
|
-
// We'll keep on downloading, but can report that this priority is synced now.
|
|
14016
|
-
this.logger.debug('partial checkpoint validation succeeded');
|
|
14017
|
-
// All states with a higher priority can be deleted since this partial sync includes them.
|
|
14018
|
-
const priorityStates = this.syncStatus.priorityStatusEntries.filter((s) => s.priority <= priority);
|
|
14019
|
-
priorityStates.push({
|
|
14020
|
-
priority,
|
|
14021
|
-
lastSyncedAt: new Date(),
|
|
14022
|
-
hasSynced: true
|
|
14023
|
-
});
|
|
14024
|
-
this.updateSyncStatus({
|
|
14025
|
-
connected: true,
|
|
14026
|
-
priorityStatusEntries: priorityStates
|
|
14027
|
-
});
|
|
14028
|
-
}
|
|
14029
|
-
}
|
|
14030
|
-
else if (isStreamingSyncCheckpointDiff(line)) {
|
|
14031
|
-
// TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
|
|
14032
|
-
if (targetCheckpoint == null) {
|
|
14033
|
-
throw new Error('Checkpoint diff without previous checkpoint');
|
|
14034
|
-
}
|
|
14035
|
-
// New checkpoint - existing validated checkpoint is no longer valid
|
|
14036
|
-
pendingValidatedCheckpoint = null;
|
|
14037
|
-
const diff = line.checkpoint_diff;
|
|
14038
|
-
const newBuckets = new Map();
|
|
14039
|
-
for (const checksum of targetCheckpoint.buckets) {
|
|
14040
|
-
newBuckets.set(checksum.bucket, checksum);
|
|
14041
|
-
}
|
|
14042
|
-
for (const checksum of diff.updated_buckets) {
|
|
14043
|
-
newBuckets.set(checksum.bucket, checksum);
|
|
14044
|
-
}
|
|
14045
|
-
for (const bucket of diff.removed_buckets) {
|
|
14046
|
-
newBuckets.delete(bucket);
|
|
14047
|
-
}
|
|
14048
|
-
const newCheckpoint = {
|
|
14049
|
-
last_op_id: diff.last_op_id,
|
|
14050
|
-
buckets: [...newBuckets.values()],
|
|
14051
|
-
write_checkpoint: diff.write_checkpoint
|
|
14052
|
-
};
|
|
14053
|
-
targetCheckpoint = newCheckpoint;
|
|
14054
|
-
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
|
|
14055
|
-
bucketMap = new Map();
|
|
14056
|
-
newBuckets.forEach((checksum, name) => bucketMap.set(name, {
|
|
14057
|
-
name: checksum.bucket,
|
|
14058
|
-
priority: checksum.priority ?? FALLBACK_PRIORITY
|
|
14059
|
-
}));
|
|
14060
|
-
const bucketsToDelete = diff.removed_buckets;
|
|
14061
|
-
if (bucketsToDelete.length > 0) {
|
|
14062
|
-
this.logger.debug('Remove buckets', bucketsToDelete);
|
|
14063
|
-
}
|
|
14064
|
-
await this.options.adapter.removeBuckets(bucketsToDelete);
|
|
14065
|
-
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
|
|
14066
|
-
}
|
|
14067
|
-
else if (isStreamingSyncData(line)) {
|
|
14068
|
-
const { data } = line;
|
|
14069
|
-
const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
|
|
14070
|
-
let updatedProgress = null;
|
|
14071
|
-
if (previousProgress) {
|
|
14072
|
-
updatedProgress = { ...previousProgress };
|
|
14073
|
-
const progressForBucket = updatedProgress[data.bucket];
|
|
14074
|
-
if (progressForBucket) {
|
|
14075
|
-
updatedProgress[data.bucket] = {
|
|
14076
|
-
...progressForBucket,
|
|
14077
|
-
since_last: progressForBucket.since_last + data.data.length
|
|
14078
|
-
};
|
|
14079
|
-
}
|
|
14080
|
-
}
|
|
14081
|
-
this.updateSyncStatus({
|
|
14082
|
-
dataFlow: {
|
|
14083
|
-
downloading: true,
|
|
14084
|
-
downloadProgress: updatedProgress
|
|
14085
|
-
}
|
|
14086
|
-
});
|
|
14087
|
-
await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
|
|
14088
|
-
}
|
|
14089
|
-
else if (isStreamingKeepalive(line)) {
|
|
14090
|
-
const remaining_seconds = line.token_expires_in;
|
|
14091
|
-
if (remaining_seconds == 0) {
|
|
14092
|
-
// Connection would be closed automatically right after this
|
|
14093
|
-
this.logger.debug('Token expiring; reconnect');
|
|
14094
|
-
/**
|
|
14095
|
-
* For a rare case where the backend connector does not update the token
|
|
14096
|
-
* (uses the same one), this should have some delay.
|
|
14097
|
-
*/
|
|
14098
|
-
await this.delayRetry();
|
|
14099
|
-
return;
|
|
14100
|
-
}
|
|
14101
|
-
else if (remaining_seconds < 30) {
|
|
14102
|
-
this.logger.debug('Token will expire soon; reconnect');
|
|
14103
|
-
// Pre-emptively refresh the token
|
|
14104
|
-
this.options.remote.invalidateCredentials();
|
|
14105
|
-
return;
|
|
14106
|
-
}
|
|
14107
|
-
this.triggerCrudUpload();
|
|
14108
|
-
}
|
|
14109
|
-
else {
|
|
14110
|
-
this.logger.debug('Received unknown sync line', line);
|
|
14111
|
-
}
|
|
13680
|
+
});
|
|
14112
13681
|
}
|
|
14113
|
-
this.logger.debug('Stream input empty');
|
|
14114
|
-
// Connection closed. Likely due to auth issue.
|
|
14115
|
-
return;
|
|
14116
13682
|
}
|
|
14117
13683
|
async rustSyncIteration(signal, resolvedOptions) {
|
|
14118
13684
|
const syncImplementation = this;
|
|
@@ -14275,68 +13841,6 @@ The next upload iteration will be delayed.`);
|
|
|
14275
13841
|
}
|
|
14276
13842
|
return { immediateRestart: hideDisconnectOnRestart };
|
|
14277
13843
|
}
|
|
14278
|
-
async updateSyncStatusForStartingCheckpoint(checkpoint) {
|
|
14279
|
-
const localProgress = await this.options.adapter.getBucketOperationProgress();
|
|
14280
|
-
const progress = {};
|
|
14281
|
-
let invalidated = false;
|
|
14282
|
-
for (const bucket of checkpoint.buckets) {
|
|
14283
|
-
const savedProgress = localProgress[bucket.bucket];
|
|
14284
|
-
const atLast = savedProgress?.atLast ?? 0;
|
|
14285
|
-
const sinceLast = savedProgress?.sinceLast ?? 0;
|
|
14286
|
-
progress[bucket.bucket] = {
|
|
14287
|
-
// The fallback priority doesn't matter here, but 3 is the one newer versions of the sync service
|
|
14288
|
-
// will use by default.
|
|
14289
|
-
priority: bucket.priority ?? 3,
|
|
14290
|
-
at_last: atLast,
|
|
14291
|
-
since_last: sinceLast,
|
|
14292
|
-
target_count: bucket.count ?? 0
|
|
14293
|
-
};
|
|
14294
|
-
if (bucket.count != null && bucket.count < atLast + sinceLast) {
|
|
14295
|
-
// Either due to a defrag / sync rule deploy or a compaction operation, the size
|
|
14296
|
-
// of the bucket shrank so much that the local ops exceed the ops in the updated
|
|
14297
|
-
// bucket. We can't prossibly report progress in this case (it would overshoot 100%).
|
|
14298
|
-
invalidated = true;
|
|
14299
|
-
}
|
|
14300
|
-
}
|
|
14301
|
-
if (invalidated) {
|
|
14302
|
-
for (const bucket in progress) {
|
|
14303
|
-
const bucketProgress = progress[bucket];
|
|
14304
|
-
bucketProgress.at_last = 0;
|
|
14305
|
-
bucketProgress.since_last = 0;
|
|
14306
|
-
}
|
|
14307
|
-
}
|
|
14308
|
-
this.updateSyncStatus({
|
|
14309
|
-
dataFlow: {
|
|
14310
|
-
downloading: true,
|
|
14311
|
-
downloadProgress: progress
|
|
14312
|
-
}
|
|
14313
|
-
});
|
|
14314
|
-
}
|
|
14315
|
-
async applyCheckpoint(checkpoint) {
|
|
14316
|
-
let result = await this.options.adapter.syncLocalDatabase(checkpoint);
|
|
14317
|
-
if (!result.checkpointValid) {
|
|
14318
|
-
this.logger.debug(`Checksum mismatch in checkpoint ${checkpoint.last_op_id}, will reconnect`);
|
|
14319
|
-
// This means checksums failed. Start again with a new checkpoint.
|
|
14320
|
-
// TODO: better back-off
|
|
14321
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
14322
|
-
return { applied: false, endIteration: true };
|
|
14323
|
-
}
|
|
14324
|
-
else if (!result.ready) {
|
|
14325
|
-
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.`);
|
|
14326
|
-
return { applied: false, endIteration: false };
|
|
14327
|
-
}
|
|
14328
|
-
this.logger.debug(`Applied checkpoint ${checkpoint.last_op_id}`, checkpoint);
|
|
14329
|
-
this.updateSyncStatus({
|
|
14330
|
-
connected: true,
|
|
14331
|
-
lastSyncedAt: new Date(),
|
|
14332
|
-
dataFlow: {
|
|
14333
|
-
downloading: false,
|
|
14334
|
-
downloadProgress: null,
|
|
14335
|
-
downloadError: undefined
|
|
14336
|
-
}
|
|
14337
|
-
});
|
|
14338
|
-
return { applied: true, endIteration: false };
|
|
14339
|
-
}
|
|
14340
13844
|
updateSyncStatus(options) {
|
|
14341
13845
|
const updatedStatus = new SyncStatus({
|
|
14342
13846
|
connected: options.connected ?? this.syncStatus.connected,
|
|
@@ -15875,14 +15379,12 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
15875
15379
|
db;
|
|
15876
15380
|
logger;
|
|
15877
15381
|
tableNames;
|
|
15878
|
-
_hasCompletedSync;
|
|
15879
15382
|
updateListener;
|
|
15880
15383
|
_clientId;
|
|
15881
15384
|
constructor(db, logger = Logger.get('SqliteBucketStorage')) {
|
|
15882
15385
|
super();
|
|
15883
15386
|
this.db = db;
|
|
15884
15387
|
this.logger = logger;
|
|
15885
|
-
this._hasCompletedSync = false;
|
|
15886
15388
|
this.tableNames = new Set();
|
|
15887
15389
|
this.updateListener = db.registerListener({
|
|
15888
15390
|
tablesUpdated: (update) => {
|
|
@@ -15894,7 +15396,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
15894
15396
|
});
|
|
15895
15397
|
}
|
|
15896
15398
|
async init() {
|
|
15897
|
-
this._hasCompletedSync = false;
|
|
15898
15399
|
const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
|
|
15899
15400
|
for (const row of existingTableRows ?? []) {
|
|
15900
15401
|
this.tableNames.add(row.name);
|
|
@@ -15916,156 +15417,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
15916
15417
|
getMaxOpId() {
|
|
15917
15418
|
return MAX_OP_ID;
|
|
15918
15419
|
}
|
|
15919
|
-
/**
|
|
15920
|
-
* Reset any caches.
|
|
15921
|
-
*/
|
|
15922
|
-
startSession() { }
|
|
15923
|
-
async getBucketStates() {
|
|
15924
|
-
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'");
|
|
15925
|
-
return result;
|
|
15926
|
-
}
|
|
15927
|
-
async getBucketOperationProgress() {
|
|
15928
|
-
const rows = await this.db.getAll('SELECT name, count_at_last, count_since_last FROM ps_buckets');
|
|
15929
|
-
return Object.fromEntries(rows.map((r) => [r.name, { atLast: r.count_at_last, sinceLast: r.count_since_last }]));
|
|
15930
|
-
}
|
|
15931
|
-
async saveSyncData(batch, fixedKeyFormat = false) {
|
|
15932
|
-
await this.writeTransaction(async (tx) => {
|
|
15933
|
-
for (const b of batch.buckets) {
|
|
15934
|
-
await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
|
|
15935
|
-
'save',
|
|
15936
|
-
JSON.stringify({ buckets: [b.toJSON(fixedKeyFormat)] })
|
|
15937
|
-
]);
|
|
15938
|
-
this.logger.debug(`Saved batch of data for bucket: ${b.bucket}, operations: ${b.data.length}`);
|
|
15939
|
-
}
|
|
15940
|
-
});
|
|
15941
|
-
}
|
|
15942
|
-
async removeBuckets(buckets) {
|
|
15943
|
-
for (const bucket of buckets) {
|
|
15944
|
-
await this.deleteBucket(bucket);
|
|
15945
|
-
}
|
|
15946
|
-
}
|
|
15947
|
-
/**
|
|
15948
|
-
* Mark a bucket for deletion.
|
|
15949
|
-
*/
|
|
15950
|
-
async deleteBucket(bucket) {
|
|
15951
|
-
await this.writeTransaction(async (tx) => {
|
|
15952
|
-
await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', ['delete_bucket', bucket]);
|
|
15953
|
-
});
|
|
15954
|
-
this.logger.debug(`Done deleting bucket ${bucket}`);
|
|
15955
|
-
}
|
|
15956
|
-
async hasCompletedSync() {
|
|
15957
|
-
if (this._hasCompletedSync) {
|
|
15958
|
-
return true;
|
|
15959
|
-
}
|
|
15960
|
-
const r = await this.db.get(`SELECT powersync_last_synced_at() as synced_at`);
|
|
15961
|
-
const completed = r.synced_at != null;
|
|
15962
|
-
if (completed) {
|
|
15963
|
-
this._hasCompletedSync = true;
|
|
15964
|
-
}
|
|
15965
|
-
return completed;
|
|
15966
|
-
}
|
|
15967
|
-
async syncLocalDatabase(checkpoint, priority) {
|
|
15968
|
-
const r = await this.validateChecksums(checkpoint, priority);
|
|
15969
|
-
if (!r.checkpointValid) {
|
|
15970
|
-
this.logger.error('Checksums failed for', r.checkpointFailures);
|
|
15971
|
-
for (const b of r.checkpointFailures ?? []) {
|
|
15972
|
-
await this.deleteBucket(b);
|
|
15973
|
-
}
|
|
15974
|
-
return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
|
|
15975
|
-
}
|
|
15976
|
-
if (priority == null) {
|
|
15977
|
-
this.logger.debug(`Validated checksums checkpoint ${checkpoint.last_op_id}`);
|
|
15978
|
-
}
|
|
15979
|
-
else {
|
|
15980
|
-
this.logger.debug(`Validated checksums for partial checkpoint ${checkpoint.last_op_id}, priority ${priority}`);
|
|
15981
|
-
}
|
|
15982
|
-
let buckets = checkpoint.buckets;
|
|
15983
|
-
if (priority !== undefined) {
|
|
15984
|
-
buckets = buckets.filter((b) => hasMatchingPriority(priority, b));
|
|
15985
|
-
}
|
|
15986
|
-
const bucketNames = buckets.map((b) => b.bucket);
|
|
15987
|
-
await this.writeTransaction(async (tx) => {
|
|
15988
|
-
await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
|
|
15989
|
-
checkpoint.last_op_id,
|
|
15990
|
-
JSON.stringify(bucketNames)
|
|
15991
|
-
]);
|
|
15992
|
-
if (priority == null && checkpoint.write_checkpoint) {
|
|
15993
|
-
await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
|
|
15994
|
-
}
|
|
15995
|
-
});
|
|
15996
|
-
const valid = await this.updateObjectsFromBuckets(checkpoint, priority);
|
|
15997
|
-
if (!valid) {
|
|
15998
|
-
return { ready: false, checkpointValid: true };
|
|
15999
|
-
}
|
|
16000
|
-
return {
|
|
16001
|
-
ready: true,
|
|
16002
|
-
checkpointValid: true
|
|
16003
|
-
};
|
|
16004
|
-
}
|
|
16005
|
-
/**
|
|
16006
|
-
* Atomically update the local state to the current checkpoint.
|
|
16007
|
-
*
|
|
16008
|
-
* This includes creating new tables, dropping old tables, and copying data over from the oplog.
|
|
16009
|
-
*/
|
|
16010
|
-
async updateObjectsFromBuckets(checkpoint, priority) {
|
|
16011
|
-
let arg = '';
|
|
16012
|
-
if (priority !== undefined) {
|
|
16013
|
-
const affectedBuckets = [];
|
|
16014
|
-
for (const desc of checkpoint.buckets) {
|
|
16015
|
-
if (hasMatchingPriority(priority, desc)) {
|
|
16016
|
-
affectedBuckets.push(desc.bucket);
|
|
16017
|
-
}
|
|
16018
|
-
}
|
|
16019
|
-
arg = JSON.stringify({ priority, buckets: affectedBuckets });
|
|
16020
|
-
}
|
|
16021
|
-
return this.writeTransaction(async (tx) => {
|
|
16022
|
-
const { insertId: result } = await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
|
|
16023
|
-
'sync_local',
|
|
16024
|
-
arg
|
|
16025
|
-
]);
|
|
16026
|
-
if (result == 1) {
|
|
16027
|
-
if (priority == null) {
|
|
16028
|
-
const bucketToCount = Object.fromEntries(checkpoint.buckets.map((b) => [b.bucket, b.count]));
|
|
16029
|
-
// The two parameters could be replaced with one, but: https://github.com/powersync-ja/better-sqlite3/pull/6
|
|
16030
|
-
const jsonBucketCount = JSON.stringify(bucketToCount);
|
|
16031
|
-
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]);
|
|
16032
|
-
}
|
|
16033
|
-
return true;
|
|
16034
|
-
}
|
|
16035
|
-
else {
|
|
16036
|
-
return false;
|
|
16037
|
-
}
|
|
16038
|
-
});
|
|
16039
|
-
}
|
|
16040
|
-
async validateChecksums(checkpoint, priority) {
|
|
16041
|
-
if (priority !== undefined) {
|
|
16042
|
-
// Only validate the buckets within the priority we care about
|
|
16043
|
-
const newBuckets = checkpoint.buckets.filter((cs) => hasMatchingPriority(priority, cs));
|
|
16044
|
-
checkpoint = { ...checkpoint, buckets: newBuckets };
|
|
16045
|
-
}
|
|
16046
|
-
const rs = await this.db.execute('SELECT powersync_validate_checkpoint(?) as result', [
|
|
16047
|
-
JSON.stringify({ ...checkpoint })
|
|
16048
|
-
]);
|
|
16049
|
-
const resultItem = rs.rows?.item(0);
|
|
16050
|
-
if (!resultItem) {
|
|
16051
|
-
return {
|
|
16052
|
-
checkpointValid: false,
|
|
16053
|
-
ready: false,
|
|
16054
|
-
checkpointFailures: []
|
|
16055
|
-
};
|
|
16056
|
-
}
|
|
16057
|
-
const result = JSON.parse(resultItem['result']);
|
|
16058
|
-
if (result['valid']) {
|
|
16059
|
-
return { ready: true, checkpointValid: true };
|
|
16060
|
-
}
|
|
16061
|
-
else {
|
|
16062
|
-
return {
|
|
16063
|
-
checkpointValid: false,
|
|
16064
|
-
ready: false,
|
|
16065
|
-
checkpointFailures: result['failed_buckets']
|
|
16066
|
-
};
|
|
16067
|
-
}
|
|
16068
|
-
}
|
|
16069
15420
|
async updateLocalTarget(cb) {
|
|
16070
15421
|
const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)", [MAX_OP_ID]);
|
|
16071
15422
|
if (!rs1.length) {
|
|
@@ -16156,12 +15507,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
16156
15507
|
async writeTransaction(callback, options) {
|
|
16157
15508
|
return this.db.writeTransaction(callback, options);
|
|
16158
15509
|
}
|
|
16159
|
-
/**
|
|
16160
|
-
* Set a target checkpoint.
|
|
16161
|
-
*/
|
|
16162
|
-
async setTargetCheckpoint(checkpoint) {
|
|
16163
|
-
// No-op for now
|
|
16164
|
-
}
|
|
16165
15510
|
async control(op, payload) {
|
|
16166
15511
|
return await this.writeTransaction(async (tx) => {
|
|
16167
15512
|
const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
|
|
@@ -16185,20 +15530,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
16185
15530
|
}
|
|
16186
15531
|
static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
|
|
16187
15532
|
}
|
|
16188
|
-
function hasMatchingPriority(priority, bucket) {
|
|
16189
|
-
return bucket.priority != null && bucket.priority <= priority;
|
|
16190
|
-
}
|
|
16191
|
-
|
|
16192
|
-
// TODO JSON
|
|
16193
|
-
class SyncDataBatch {
|
|
16194
|
-
buckets;
|
|
16195
|
-
static fromJSON(json) {
|
|
16196
|
-
return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
|
|
16197
|
-
}
|
|
16198
|
-
constructor(buckets) {
|
|
16199
|
-
this.buckets = buckets;
|
|
16200
|
-
}
|
|
16201
|
-
}
|
|
16202
15533
|
|
|
16203
15534
|
/**
|
|
16204
15535
|
* Thrown when an underlying database connection is closed.
|
|
@@ -16494,9 +15825,6 @@ const parseQuery = (query, parameters) => {
|
|
|
16494
15825
|
/******/ return module.exports;
|
|
16495
15826
|
/******/ }
|
|
16496
15827
|
/******/
|
|
16497
|
-
/******/ // expose the modules object (__webpack_modules__)
|
|
16498
|
-
/******/ __webpack_require__.m = __webpack_modules__;
|
|
16499
|
-
/******/
|
|
16500
15828
|
/************************************************************************/
|
|
16501
15829
|
/******/ /* webpack/runtime/define property getters */
|
|
16502
15830
|
/******/ (() => {
|
|
@@ -16510,40 +15838,6 @@ const parseQuery = (query, parameters) => {
|
|
|
16510
15838
|
/******/ };
|
|
16511
15839
|
/******/ })();
|
|
16512
15840
|
/******/
|
|
16513
|
-
/******/ /* webpack/runtime/ensure chunk */
|
|
16514
|
-
/******/ (() => {
|
|
16515
|
-
/******/ __webpack_require__.f = {};
|
|
16516
|
-
/******/ // This file contains only the entry chunk.
|
|
16517
|
-
/******/ // The chunk loading function for additional chunks
|
|
16518
|
-
/******/ __webpack_require__.e = (chunkId) => {
|
|
16519
|
-
/******/ return Promise.all(Object.keys(__webpack_require__.f).reduce((promises, key) => {
|
|
16520
|
-
/******/ __webpack_require__.f[key](chunkId, promises);
|
|
16521
|
-
/******/ return promises;
|
|
16522
|
-
/******/ }, []));
|
|
16523
|
-
/******/ };
|
|
16524
|
-
/******/ })();
|
|
16525
|
-
/******/
|
|
16526
|
-
/******/ /* webpack/runtime/get javascript chunk filename */
|
|
16527
|
-
/******/ (() => {
|
|
16528
|
-
/******/ // This function allow to reference async chunks
|
|
16529
|
-
/******/ __webpack_require__.u = (chunkId) => {
|
|
16530
|
-
/******/ // return url for filenames based on template
|
|
16531
|
-
/******/ return "worker/" + chunkId + ".umd.js";
|
|
16532
|
-
/******/ };
|
|
16533
|
-
/******/ })();
|
|
16534
|
-
/******/
|
|
16535
|
-
/******/ /* webpack/runtime/global */
|
|
16536
|
-
/******/ (() => {
|
|
16537
|
-
/******/ __webpack_require__.g = (function() {
|
|
16538
|
-
/******/ if (typeof globalThis === 'object') return globalThis;
|
|
16539
|
-
/******/ try {
|
|
16540
|
-
/******/ return this || new Function('return this')();
|
|
16541
|
-
/******/ } catch (e) {
|
|
16542
|
-
/******/ if (typeof window === 'object') return window;
|
|
16543
|
-
/******/ }
|
|
16544
|
-
/******/ })();
|
|
16545
|
-
/******/ })();
|
|
16546
|
-
/******/
|
|
16547
15841
|
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
16548
15842
|
/******/ (() => {
|
|
16549
15843
|
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
@@ -16560,70 +15854,6 @@ const parseQuery = (query, parameters) => {
|
|
|
16560
15854
|
/******/ };
|
|
16561
15855
|
/******/ })();
|
|
16562
15856
|
/******/
|
|
16563
|
-
/******/ /* webpack/runtime/publicPath */
|
|
16564
|
-
/******/ (() => {
|
|
16565
|
-
/******/ var scriptUrl;
|
|
16566
|
-
/******/ if (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + "";
|
|
16567
|
-
/******/ var document = __webpack_require__.g.document;
|
|
16568
|
-
/******/ if (!scriptUrl && document) {
|
|
16569
|
-
/******/ if (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT')
|
|
16570
|
-
/******/ scriptUrl = document.currentScript.src;
|
|
16571
|
-
/******/ if (!scriptUrl) {
|
|
16572
|
-
/******/ var scripts = document.getElementsByTagName("script");
|
|
16573
|
-
/******/ if(scripts.length) {
|
|
16574
|
-
/******/ var i = scripts.length - 1;
|
|
16575
|
-
/******/ while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src;
|
|
16576
|
-
/******/ }
|
|
16577
|
-
/******/ }
|
|
16578
|
-
/******/ }
|
|
16579
|
-
/******/ // When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration
|
|
16580
|
-
/******/ // or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.
|
|
16581
|
-
/******/ if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
|
|
16582
|
-
/******/ scriptUrl = scriptUrl.replace(/^blob:/, "").replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/");
|
|
16583
|
-
/******/ __webpack_require__.p = scriptUrl + "../";
|
|
16584
|
-
/******/ })();
|
|
16585
|
-
/******/
|
|
16586
|
-
/******/ /* webpack/runtime/importScripts chunk loading */
|
|
16587
|
-
/******/ (() => {
|
|
16588
|
-
/******/ // no baseURI
|
|
16589
|
-
/******/
|
|
16590
|
-
/******/ // object to store loaded chunks
|
|
16591
|
-
/******/ // "1" means "already loaded"
|
|
16592
|
-
/******/ var installedChunks = {
|
|
16593
|
-
/******/ "SharedSyncImplementation": 1
|
|
16594
|
-
/******/ };
|
|
16595
|
-
/******/
|
|
16596
|
-
/******/ // importScripts chunk loading
|
|
16597
|
-
/******/ var installChunk = (data) => {
|
|
16598
|
-
/******/ var [chunkIds, moreModules, runtime] = data;
|
|
16599
|
-
/******/ for(var moduleId in moreModules) {
|
|
16600
|
-
/******/ if(__webpack_require__.o(moreModules, moduleId)) {
|
|
16601
|
-
/******/ __webpack_require__.m[moduleId] = moreModules[moduleId];
|
|
16602
|
-
/******/ }
|
|
16603
|
-
/******/ }
|
|
16604
|
-
/******/ if(runtime) runtime(__webpack_require__);
|
|
16605
|
-
/******/ while(chunkIds.length)
|
|
16606
|
-
/******/ installedChunks[chunkIds.pop()] = 1;
|
|
16607
|
-
/******/ parentChunkLoadingFunction(data);
|
|
16608
|
-
/******/ };
|
|
16609
|
-
/******/ __webpack_require__.f.i = (chunkId, promises) => {
|
|
16610
|
-
/******/ // "1" is the signal for "already loaded"
|
|
16611
|
-
/******/ if(!installedChunks[chunkId]) {
|
|
16612
|
-
/******/ if(true) { // all chunks have JS
|
|
16613
|
-
/******/ importScripts(__webpack_require__.p + __webpack_require__.u(chunkId));
|
|
16614
|
-
/******/ }
|
|
16615
|
-
/******/ }
|
|
16616
|
-
/******/ };
|
|
16617
|
-
/******/
|
|
16618
|
-
/******/ var chunkLoadingGlobal = self["webpackChunksdk_web"] = self["webpackChunksdk_web"] || [];
|
|
16619
|
-
/******/ var parentChunkLoadingFunction = chunkLoadingGlobal.push.bind(chunkLoadingGlobal);
|
|
16620
|
-
/******/ chunkLoadingGlobal.push = installChunk;
|
|
16621
|
-
/******/
|
|
16622
|
-
/******/ // no HMR
|
|
16623
|
-
/******/
|
|
16624
|
-
/******/ // no HMR manifest
|
|
16625
|
-
/******/ })();
|
|
16626
|
-
/******/
|
|
16627
15857
|
/************************************************************************/
|
|
16628
15858
|
var __webpack_exports__ = {};
|
|
16629
15859
|
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
|