@powersync/common 0.0.0-dev-20260414110516 → 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/bundle.cjs +33 -665
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +34 -654
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +33 -665
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +34 -654
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +22 -369
- package/legacy/sync_protocol.d.ts +103 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +1 -63
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -1
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +1 -28
- package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -162
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +2 -12
- package/lib/client/sync/stream/AbstractRemote.js +3 -13
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +12 -35
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +29 -337
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/client/sync/stream/JsonValue.d.ts +7 -0
- package/lib/client/sync/stream/JsonValue.js +2 -0
- package/lib/client/sync/stream/JsonValue.js.map +1 -0
- package/lib/client/sync/stream/core-instruction.d.ts +1 -2
- package/lib/client/sync/stream/core-instruction.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +0 -4
- package/lib/db/crud/SyncStatus.js +0 -4
- package/lib/db/crud/SyncStatus.js.map +1 -1
- package/lib/db/schema/RawTable.d.ts +0 -5
- package/lib/db/schema/Schema.d.ts +0 -2
- package/lib/db/schema/Schema.js +0 -2
- package/lib/db/schema/Schema.js.map +1 -1
- package/lib/index.d.ts +1 -5
- package/lib/index.js +1 -5
- package/lib/index.js.map +1 -1
- package/package.json +7 -4
- package/src/client/sync/bucket/BucketStorageAdapter.ts +1 -70
- package/src/client/sync/bucket/SqliteBucketStorage.ts +1 -197
- package/src/client/sync/stream/AbstractRemote.ts +5 -27
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +41 -407
- package/src/client/sync/stream/JsonValue.ts +8 -0
- package/src/client/sync/stream/core-instruction.ts +1 -2
- package/src/db/crud/SyncStatus.ts +0 -4
- package/src/db/schema/RawTable.ts +0 -5
- package/src/db/schema/Schema.ts +0 -2
- package/src/index.ts +1 -5
- package/lib/client/sync/bucket/OpType.d.ts +0 -16
- package/lib/client/sync/bucket/OpType.js +0 -23
- package/lib/client/sync/bucket/OpType.js.map +0 -1
- package/lib/client/sync/bucket/OplogEntry.d.ts +0 -23
- package/lib/client/sync/bucket/OplogEntry.js +0 -36
- package/lib/client/sync/bucket/OplogEntry.js.map +0 -1
- package/lib/client/sync/bucket/SyncDataBatch.d.ts +0 -6
- package/lib/client/sync/bucket/SyncDataBatch.js +0 -12
- package/lib/client/sync/bucket/SyncDataBatch.js.map +0 -1
- package/lib/client/sync/bucket/SyncDataBucket.d.ts +0 -40
- package/lib/client/sync/bucket/SyncDataBucket.js +0 -40
- package/lib/client/sync/bucket/SyncDataBucket.js.map +0 -1
- package/lib/client/sync/stream/streaming-sync-types.d.ts +0 -143
- package/lib/client/sync/stream/streaming-sync-types.js +0 -26
- package/lib/client/sync/stream/streaming-sync-types.js.map +0 -1
- package/src/client/sync/bucket/OpType.ts +0 -23
- package/src/client/sync/bucket/OplogEntry.ts +0 -50
- package/src/client/sync/bucket/SyncDataBatch.ts +0 -11
- package/src/client/sync/bucket/SyncDataBucket.ts +0 -49
- package/src/client/sync/stream/streaming-sync-types.ts +0 -210
package/dist/bundle.node.mjs
CHANGED
|
@@ -1977,16 +1977,12 @@ class SyncStatus {
|
|
|
1977
1977
|
*
|
|
1978
1978
|
* This returns null when the database is currently being opened and we don't have reliable information about all
|
|
1979
1979
|
* included streams yet.
|
|
1980
|
-
*
|
|
1981
|
-
* @experimental Sync streams are currently in alpha.
|
|
1982
1980
|
*/
|
|
1983
1981
|
get syncStreams() {
|
|
1984
1982
|
return this.options.dataFlow?.internalStreamSubscriptions?.map((core) => new SyncStreamStatusView(this, core));
|
|
1985
1983
|
}
|
|
1986
1984
|
/**
|
|
1987
1985
|
* If the `stream` appears in {@link syncStreams}, returns the current status for that stream.
|
|
1988
|
-
*
|
|
1989
|
-
* @experimental Sync streams are currently in alpha.
|
|
1990
1986
|
*/
|
|
1991
1987
|
forStream(stream) {
|
|
1992
1988
|
const asJson = JSON.stringify(stream.parameters);
|
|
@@ -3339,103 +3335,6 @@ class AbortOperation extends Error {
|
|
|
3339
3335
|
}
|
|
3340
3336
|
}
|
|
3341
3337
|
|
|
3342
|
-
var OpTypeEnum;
|
|
3343
|
-
(function (OpTypeEnum) {
|
|
3344
|
-
OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
|
|
3345
|
-
OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
|
|
3346
|
-
OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
|
|
3347
|
-
OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
|
|
3348
|
-
})(OpTypeEnum || (OpTypeEnum = {}));
|
|
3349
|
-
/**
|
|
3350
|
-
* Used internally for sync buckets.
|
|
3351
|
-
*/
|
|
3352
|
-
class OpType {
|
|
3353
|
-
value;
|
|
3354
|
-
static fromJSON(jsonValue) {
|
|
3355
|
-
return new OpType(OpTypeEnum[jsonValue]);
|
|
3356
|
-
}
|
|
3357
|
-
constructor(value) {
|
|
3358
|
-
this.value = value;
|
|
3359
|
-
}
|
|
3360
|
-
toJSON() {
|
|
3361
|
-
return Object.entries(OpTypeEnum).find(([, value]) => value === this.value)[0];
|
|
3362
|
-
}
|
|
3363
|
-
}
|
|
3364
|
-
|
|
3365
|
-
class OplogEntry {
|
|
3366
|
-
op_id;
|
|
3367
|
-
op;
|
|
3368
|
-
checksum;
|
|
3369
|
-
subkey;
|
|
3370
|
-
object_type;
|
|
3371
|
-
object_id;
|
|
3372
|
-
data;
|
|
3373
|
-
static fromRow(row) {
|
|
3374
|
-
return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
|
|
3375
|
-
}
|
|
3376
|
-
constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
|
|
3377
|
-
this.op_id = op_id;
|
|
3378
|
-
this.op = op;
|
|
3379
|
-
this.checksum = checksum;
|
|
3380
|
-
this.subkey = subkey;
|
|
3381
|
-
this.object_type = object_type;
|
|
3382
|
-
this.object_id = object_id;
|
|
3383
|
-
this.data = data;
|
|
3384
|
-
}
|
|
3385
|
-
toJSON(fixedKeyEncoding = false) {
|
|
3386
|
-
return {
|
|
3387
|
-
op_id: this.op_id,
|
|
3388
|
-
op: this.op.toJSON(),
|
|
3389
|
-
object_type: this.object_type,
|
|
3390
|
-
object_id: this.object_id,
|
|
3391
|
-
checksum: this.checksum,
|
|
3392
|
-
data: this.data,
|
|
3393
|
-
// Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
|
|
3394
|
-
// but we need to migrate gradually to not break existing databases.
|
|
3395
|
-
subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
|
|
3396
|
-
};
|
|
3397
|
-
}
|
|
3398
|
-
}
|
|
3399
|
-
|
|
3400
|
-
class SyncDataBucket {
|
|
3401
|
-
bucket;
|
|
3402
|
-
data;
|
|
3403
|
-
has_more;
|
|
3404
|
-
after;
|
|
3405
|
-
next_after;
|
|
3406
|
-
static fromRow(row) {
|
|
3407
|
-
return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
|
|
3408
|
-
}
|
|
3409
|
-
constructor(bucket, data,
|
|
3410
|
-
/**
|
|
3411
|
-
* True if the response does not contain all the data for this bucket, and another request must be made.
|
|
3412
|
-
*/
|
|
3413
|
-
has_more,
|
|
3414
|
-
/**
|
|
3415
|
-
* The `after` specified in the request.
|
|
3416
|
-
*/
|
|
3417
|
-
after,
|
|
3418
|
-
/**
|
|
3419
|
-
* Use this for the next request.
|
|
3420
|
-
*/
|
|
3421
|
-
next_after) {
|
|
3422
|
-
this.bucket = bucket;
|
|
3423
|
-
this.data = data;
|
|
3424
|
-
this.has_more = has_more;
|
|
3425
|
-
this.after = after;
|
|
3426
|
-
this.next_after = next_after;
|
|
3427
|
-
}
|
|
3428
|
-
toJSON(fixedKeyEncoding = false) {
|
|
3429
|
-
return {
|
|
3430
|
-
bucket: this.bucket,
|
|
3431
|
-
has_more: this.has_more,
|
|
3432
|
-
after: this.after,
|
|
3433
|
-
next_after: this.next_after,
|
|
3434
|
-
data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
|
|
3435
|
-
};
|
|
3436
|
-
}
|
|
3437
|
-
}
|
|
3438
|
-
|
|
3439
3338
|
var dist = {};
|
|
3440
3339
|
|
|
3441
3340
|
var Codecs = {};
|
|
@@ -8220,7 +8119,7 @@ function requireDist () {
|
|
|
8220
8119
|
|
|
8221
8120
|
var distExports = requireDist();
|
|
8222
8121
|
|
|
8223
|
-
var version = "1.
|
|
8122
|
+
var version = "1.52.0";
|
|
8224
8123
|
var PACKAGE = {
|
|
8225
8124
|
version: version};
|
|
8226
8125
|
|
|
@@ -8777,22 +8676,12 @@ class AbstractRemote {
|
|
|
8777
8676
|
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
8778
8677
|
*
|
|
8779
8678
|
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
8780
|
-
*
|
|
8781
|
-
* @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
|
|
8782
|
-
* (required for compatibility with older sync services).
|
|
8783
8679
|
*/
|
|
8784
|
-
async socketStreamRaw(options
|
|
8680
|
+
async socketStreamRaw(options) {
|
|
8785
8681
|
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
|
|
8786
|
-
const mimeType =
|
|
8682
|
+
const mimeType = 'application/json';
|
|
8787
8683
|
function toBuffer(js) {
|
|
8788
|
-
|
|
8789
|
-
if (bson != null) {
|
|
8790
|
-
contents = bson.serialize(js);
|
|
8791
|
-
}
|
|
8792
|
-
else {
|
|
8793
|
-
contents = JSON.stringify(js);
|
|
8794
|
-
}
|
|
8795
|
-
return Buffer.from(contents);
|
|
8684
|
+
return Buffer.from(JSON.stringify(js));
|
|
8796
8685
|
}
|
|
8797
8686
|
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
|
|
8798
8687
|
const request = await this.buildRequest(path);
|
|
@@ -9094,32 +8983,6 @@ function coreStatusToJs(status) {
|
|
|
9094
8983
|
};
|
|
9095
8984
|
}
|
|
9096
8985
|
|
|
9097
|
-
function isStreamingSyncData(line) {
|
|
9098
|
-
return line.data != null;
|
|
9099
|
-
}
|
|
9100
|
-
function isStreamingKeepalive(line) {
|
|
9101
|
-
return line.token_expires_in != null;
|
|
9102
|
-
}
|
|
9103
|
-
function isStreamingSyncCheckpoint(line) {
|
|
9104
|
-
return line.checkpoint != null;
|
|
9105
|
-
}
|
|
9106
|
-
function isStreamingSyncCheckpointComplete(line) {
|
|
9107
|
-
return line.checkpoint_complete != null;
|
|
9108
|
-
}
|
|
9109
|
-
function isStreamingSyncCheckpointPartiallyComplete(line) {
|
|
9110
|
-
return line.partial_checkpoint_complete != null;
|
|
9111
|
-
}
|
|
9112
|
-
function isStreamingSyncCheckpointDiff(line) {
|
|
9113
|
-
return line.checkpoint_diff != null;
|
|
9114
|
-
}
|
|
9115
|
-
function isContinueCheckpointRequest(request) {
|
|
9116
|
-
return (Array.isArray(request.buckets) &&
|
|
9117
|
-
typeof request.checkpoint_token == 'string');
|
|
9118
|
-
}
|
|
9119
|
-
function isSyncNewCheckpointRequest(request) {
|
|
9120
|
-
return typeof request.request_checkpoint == 'object';
|
|
9121
|
-
}
|
|
9122
|
-
|
|
9123
8986
|
var LockType;
|
|
9124
8987
|
(function (LockType) {
|
|
9125
8988
|
LockType["CRUD"] = "crud";
|
|
@@ -9132,35 +8995,21 @@ var SyncStreamConnectionMethod;
|
|
|
9132
8995
|
})(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
|
|
9133
8996
|
var SyncClientImplementation;
|
|
9134
8997
|
(function (SyncClientImplementation) {
|
|
9135
|
-
/**
|
|
9136
|
-
* Decodes and handles sync lines received from the sync service in JavaScript.
|
|
9137
|
-
*
|
|
9138
|
-
* This is the default option.
|
|
9139
|
-
*
|
|
9140
|
-
* @deprecated We recommend the {@link RUST} client implementation for all apps. If you have issues with
|
|
9141
|
-
* the Rust client, please file an issue or reach out to us. The JavaScript client will be removed in a future
|
|
9142
|
-
* version of the PowerSync SDK.
|
|
9143
|
-
*/
|
|
9144
|
-
SyncClientImplementation["JAVASCRIPT"] = "js";
|
|
9145
8998
|
/**
|
|
9146
8999
|
* This implementation offloads the sync line decoding and handling into the PowerSync
|
|
9147
9000
|
* core extension.
|
|
9148
9001
|
*
|
|
9149
|
-
* This
|
|
9150
|
-
* recommended client implementation for all apps.
|
|
9002
|
+
* This is the only option, as an older JavaScript client implementation has been removed from the SDK.
|
|
9151
9003
|
*
|
|
9152
9004
|
* ## Compatibility warning
|
|
9153
9005
|
*
|
|
9154
9006
|
* The Rust sync client stores sync data in a format that is slightly different than the one used
|
|
9155
|
-
* by the old
|
|
9156
|
-
*
|
|
9157
|
-
* Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
|
|
9158
|
-
* the version introducing {@link RUST} as an option) also supports the new format, so you can switch
|
|
9159
|
-
* back to {@link JAVASCRIPT} later.
|
|
9007
|
+
* by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
|
|
9008
|
+
* migrate the format automatically.
|
|
9160
9009
|
*
|
|
9161
|
-
*
|
|
9162
|
-
*
|
|
9163
|
-
*
|
|
9010
|
+
* SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
|
|
9011
|
+
* implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
|
|
9012
|
+
* possible anymore. Problematic SDK versions have been released before 2025-06-09.
|
|
9164
9013
|
*/
|
|
9165
9014
|
SyncClientImplementation["RUST"] = "rust";
|
|
9166
9015
|
})(SyncClientImplementation || (SyncClientImplementation = {}));
|
|
@@ -9183,13 +9032,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
|
9183
9032
|
serializedSchema: undefined,
|
|
9184
9033
|
includeDefaultStreams: true
|
|
9185
9034
|
};
|
|
9186
|
-
// The priority we assume when we receive checkpoint lines where no priority is set.
|
|
9187
|
-
// This is the default priority used by the sync service, but can be set to an arbitrary
|
|
9188
|
-
// value since sync services without priorities also won't send partial sync completion
|
|
9189
|
-
// messages.
|
|
9190
|
-
const FALLBACK_PRIORITY = 3;
|
|
9191
9035
|
class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
9192
|
-
_lastSyncedAt;
|
|
9193
9036
|
options;
|
|
9194
9037
|
abortController;
|
|
9195
9038
|
// In rare cases, mostly for tests, uploads can be triggered without being properly connected.
|
|
@@ -9199,6 +9042,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
9199
9042
|
streamingSyncPromise;
|
|
9200
9043
|
logger;
|
|
9201
9044
|
activeStreams;
|
|
9045
|
+
connectionMayHaveChanged = false;
|
|
9202
9046
|
isUploadingCrud = false;
|
|
9203
9047
|
notifyCompletedUploads;
|
|
9204
9048
|
handleActiveStreamsChange;
|
|
@@ -9278,9 +9122,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
9278
9122
|
this.crudUpdateListener = undefined;
|
|
9279
9123
|
this.uploadAbortController?.abort();
|
|
9280
9124
|
}
|
|
9281
|
-
async hasCompletedSync() {
|
|
9282
|
-
return this.options.adapter.hasCompletedSync();
|
|
9283
|
-
}
|
|
9284
9125
|
async getWriteCheckpoint() {
|
|
9285
9126
|
const clientId = await this.options.adapter.getClientId();
|
|
9286
9127
|
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
@@ -9362,7 +9203,7 @@ The next upload iteration will be delayed.`);
|
|
|
9362
9203
|
});
|
|
9363
9204
|
}
|
|
9364
9205
|
}
|
|
9365
|
-
this.uploadAbortController =
|
|
9206
|
+
this.uploadAbortController = undefined;
|
|
9366
9207
|
}
|
|
9367
9208
|
});
|
|
9368
9209
|
}
|
|
@@ -9478,6 +9319,11 @@ The next upload iteration will be delayed.`);
|
|
|
9478
9319
|
shouldDelayRetry = false;
|
|
9479
9320
|
// A disconnect was requested, we should not delay since there is no explicit retry
|
|
9480
9321
|
}
|
|
9322
|
+
else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
|
|
9323
|
+
this.connectionMayHaveChanged = false;
|
|
9324
|
+
this.logger.info('Sync error after changed connection, retrying immediately');
|
|
9325
|
+
shouldDelayRetry = false;
|
|
9326
|
+
}
|
|
9481
9327
|
else {
|
|
9482
9328
|
this.logger.error(ex);
|
|
9483
9329
|
}
|
|
@@ -9508,17 +9354,14 @@ The next upload iteration will be delayed.`);
|
|
|
9508
9354
|
// Mark as disconnected if here
|
|
9509
9355
|
this.updateSyncStatus({ connected: false, connecting: false });
|
|
9510
9356
|
}
|
|
9511
|
-
|
|
9512
|
-
|
|
9513
|
-
|
|
9514
|
-
|
|
9515
|
-
|
|
9516
|
-
|
|
9517
|
-
|
|
9518
|
-
|
|
9519
|
-
localDescriptions.set(entry.bucket, null);
|
|
9520
|
-
}
|
|
9521
|
-
return [req, localDescriptions];
|
|
9357
|
+
markConnectionMayHaveChanged() {
|
|
9358
|
+
// By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
|
|
9359
|
+
// an active sync iteration on the connection in use.
|
|
9360
|
+
this.connectionMayHaveChanged = true;
|
|
9361
|
+
// This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
|
|
9362
|
+
// make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
|
|
9363
|
+
// iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
|
|
9364
|
+
this.handleActiveStreamsChange?.();
|
|
9522
9365
|
}
|
|
9523
9366
|
/**
|
|
9524
9367
|
* Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
|
|
@@ -9561,19 +9404,13 @@ The next upload iteration will be delayed.`);
|
|
|
9561
9404
|
}
|
|
9562
9405
|
const clientImplementation = resolvedOptions.clientImplementation;
|
|
9563
9406
|
this.updateSyncStatus({ clientImplementation });
|
|
9564
|
-
|
|
9565
|
-
|
|
9566
|
-
return null;
|
|
9567
|
-
}
|
|
9568
|
-
else {
|
|
9569
|
-
await this.requireKeyFormat(true);
|
|
9570
|
-
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
9571
|
-
}
|
|
9407
|
+
await this.requireKeyFormat(true);
|
|
9408
|
+
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
9572
9409
|
}
|
|
9573
9410
|
});
|
|
9574
9411
|
}
|
|
9575
9412
|
async receiveSyncLines(data) {
|
|
9576
|
-
const { options, connection
|
|
9413
|
+
const { options, connection } = data;
|
|
9577
9414
|
const remote = this.options.remote;
|
|
9578
9415
|
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
9579
9416
|
return await remote.fetchStream(options);
|
|
@@ -9582,232 +9419,8 @@ The next upload iteration will be delayed.`);
|
|
|
9582
9419
|
return await this.options.remote.socketStreamRaw({
|
|
9583
9420
|
...options,
|
|
9584
9421
|
...{ fetchStrategy: connection.fetchStrategy }
|
|
9585
|
-
}
|
|
9586
|
-
}
|
|
9587
|
-
}
|
|
9588
|
-
async legacyStreamingSyncIteration(signal, resolvedOptions) {
|
|
9589
|
-
const rawTables = resolvedOptions.serializedSchema?.raw_tables;
|
|
9590
|
-
if (rawTables != null && rawTables.length) {
|
|
9591
|
-
this.logger.warn('Raw tables require the Rust-based sync client. The JS client will ignore them.');
|
|
9592
|
-
}
|
|
9593
|
-
if (this.activeStreams.length) {
|
|
9594
|
-
this.logger.error('Sync streams require `clientImplementation: SyncClientImplementation.RUST` when connecting.');
|
|
9595
|
-
}
|
|
9596
|
-
this.logger.debug('Streaming sync iteration started');
|
|
9597
|
-
this.options.adapter.startSession();
|
|
9598
|
-
let [req, bucketMap] = await this.collectLocalBucketState();
|
|
9599
|
-
let targetCheckpoint = null;
|
|
9600
|
-
// A checkpoint that has been validated but not applied (e.g. due to pending local writes)
|
|
9601
|
-
let pendingValidatedCheckpoint = null;
|
|
9602
|
-
const clientId = await this.options.adapter.getClientId();
|
|
9603
|
-
const usingFixedKeyFormat = await this.requireKeyFormat(false);
|
|
9604
|
-
this.logger.debug('Requesting stream from server');
|
|
9605
|
-
const syncOptions = {
|
|
9606
|
-
path: '/sync/stream',
|
|
9607
|
-
abortSignal: signal,
|
|
9608
|
-
data: {
|
|
9609
|
-
buckets: req,
|
|
9610
|
-
include_checksum: true,
|
|
9611
|
-
raw_data: true,
|
|
9612
|
-
parameters: resolvedOptions.params,
|
|
9613
|
-
app_metadata: resolvedOptions.appMetadata,
|
|
9614
|
-
client_id: clientId
|
|
9615
|
-
}
|
|
9616
|
-
};
|
|
9617
|
-
const bson = await this.options.remote.getBSON();
|
|
9618
|
-
const source = await this.receiveSyncLines({
|
|
9619
|
-
options: syncOptions,
|
|
9620
|
-
connection: resolvedOptions,
|
|
9621
|
-
bson
|
|
9622
|
-
});
|
|
9623
|
-
const stream = injectable(map(source, (line) => {
|
|
9624
|
-
if (typeof line == 'string') {
|
|
9625
|
-
return JSON.parse(line);
|
|
9626
|
-
}
|
|
9627
|
-
else {
|
|
9628
|
-
return bson.deserialize(line);
|
|
9629
|
-
}
|
|
9630
|
-
}));
|
|
9631
|
-
this.logger.debug('Stream established. Processing events');
|
|
9632
|
-
this.notifyCompletedUploads = () => {
|
|
9633
|
-
stream.inject({ crud_upload_completed: null });
|
|
9634
|
-
};
|
|
9635
|
-
while (true) {
|
|
9636
|
-
const { value: line, done } = await stream.next();
|
|
9637
|
-
if (done) {
|
|
9638
|
-
// The stream has closed while waiting
|
|
9639
|
-
return;
|
|
9640
|
-
}
|
|
9641
|
-
if ('crud_upload_completed' in line) {
|
|
9642
|
-
if (pendingValidatedCheckpoint != null) {
|
|
9643
|
-
const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
|
|
9644
|
-
if (applied) {
|
|
9645
|
-
pendingValidatedCheckpoint = null;
|
|
9646
|
-
}
|
|
9647
|
-
else if (endIteration) {
|
|
9648
|
-
break;
|
|
9649
|
-
}
|
|
9650
|
-
}
|
|
9651
|
-
continue;
|
|
9652
|
-
}
|
|
9653
|
-
// A connection is active and messages are being received
|
|
9654
|
-
if (!this.syncStatus.connected) {
|
|
9655
|
-
// There is a connection now
|
|
9656
|
-
Promise.resolve().then(() => this.triggerCrudUpload());
|
|
9657
|
-
this.updateSyncStatus({
|
|
9658
|
-
connected: true
|
|
9659
|
-
});
|
|
9660
|
-
}
|
|
9661
|
-
if (isStreamingSyncCheckpoint(line)) {
|
|
9662
|
-
targetCheckpoint = line.checkpoint;
|
|
9663
|
-
// New checkpoint - existing validated checkpoint is no longer valid
|
|
9664
|
-
pendingValidatedCheckpoint = null;
|
|
9665
|
-
const bucketsToDelete = new Set(bucketMap.keys());
|
|
9666
|
-
const newBuckets = new Map();
|
|
9667
|
-
for (const checksum of line.checkpoint.buckets) {
|
|
9668
|
-
newBuckets.set(checksum.bucket, {
|
|
9669
|
-
name: checksum.bucket,
|
|
9670
|
-
priority: checksum.priority ?? FALLBACK_PRIORITY
|
|
9671
|
-
});
|
|
9672
|
-
bucketsToDelete.delete(checksum.bucket);
|
|
9673
|
-
}
|
|
9674
|
-
if (bucketsToDelete.size > 0) {
|
|
9675
|
-
this.logger.debug('Removing buckets', [...bucketsToDelete]);
|
|
9676
|
-
}
|
|
9677
|
-
bucketMap = newBuckets;
|
|
9678
|
-
await this.options.adapter.removeBuckets([...bucketsToDelete]);
|
|
9679
|
-
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
|
|
9680
|
-
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
|
|
9681
|
-
}
|
|
9682
|
-
else if (isStreamingSyncCheckpointComplete(line)) {
|
|
9683
|
-
const result = await this.applyCheckpoint(targetCheckpoint);
|
|
9684
|
-
if (result.endIteration) {
|
|
9685
|
-
return;
|
|
9686
|
-
}
|
|
9687
|
-
else if (!result.applied) {
|
|
9688
|
-
// "Could not apply checkpoint due to local data". We need to retry after
|
|
9689
|
-
// finishing uploads.
|
|
9690
|
-
pendingValidatedCheckpoint = targetCheckpoint;
|
|
9691
|
-
}
|
|
9692
|
-
else {
|
|
9693
|
-
// Nothing to retry later. This would likely already be null from the last
|
|
9694
|
-
// checksum or checksum_diff operation, but we make sure.
|
|
9695
|
-
pendingValidatedCheckpoint = null;
|
|
9696
|
-
}
|
|
9697
|
-
}
|
|
9698
|
-
else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
|
|
9699
|
-
const priority = line.partial_checkpoint_complete.priority;
|
|
9700
|
-
this.logger.debug('Partial checkpoint complete', priority);
|
|
9701
|
-
const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint, priority);
|
|
9702
|
-
if (!result.checkpointValid) {
|
|
9703
|
-
// This means checksums failed. Start again with a new checkpoint.
|
|
9704
|
-
// TODO: better back-off
|
|
9705
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
9706
|
-
return;
|
|
9707
|
-
}
|
|
9708
|
-
else if (!result.ready) ;
|
|
9709
|
-
else {
|
|
9710
|
-
// We'll keep on downloading, but can report that this priority is synced now.
|
|
9711
|
-
this.logger.debug('partial checkpoint validation succeeded');
|
|
9712
|
-
// All states with a higher priority can be deleted since this partial sync includes them.
|
|
9713
|
-
const priorityStates = this.syncStatus.priorityStatusEntries.filter((s) => s.priority <= priority);
|
|
9714
|
-
priorityStates.push({
|
|
9715
|
-
priority,
|
|
9716
|
-
lastSyncedAt: new Date(),
|
|
9717
|
-
hasSynced: true
|
|
9718
|
-
});
|
|
9719
|
-
this.updateSyncStatus({
|
|
9720
|
-
connected: true,
|
|
9721
|
-
priorityStatusEntries: priorityStates
|
|
9722
|
-
});
|
|
9723
|
-
}
|
|
9724
|
-
}
|
|
9725
|
-
else if (isStreamingSyncCheckpointDiff(line)) {
|
|
9726
|
-
// TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
|
|
9727
|
-
if (targetCheckpoint == null) {
|
|
9728
|
-
throw new Error('Checkpoint diff without previous checkpoint');
|
|
9729
|
-
}
|
|
9730
|
-
// New checkpoint - existing validated checkpoint is no longer valid
|
|
9731
|
-
pendingValidatedCheckpoint = null;
|
|
9732
|
-
const diff = line.checkpoint_diff;
|
|
9733
|
-
const newBuckets = new Map();
|
|
9734
|
-
for (const checksum of targetCheckpoint.buckets) {
|
|
9735
|
-
newBuckets.set(checksum.bucket, checksum);
|
|
9736
|
-
}
|
|
9737
|
-
for (const checksum of diff.updated_buckets) {
|
|
9738
|
-
newBuckets.set(checksum.bucket, checksum);
|
|
9739
|
-
}
|
|
9740
|
-
for (const bucket of diff.removed_buckets) {
|
|
9741
|
-
newBuckets.delete(bucket);
|
|
9742
|
-
}
|
|
9743
|
-
const newCheckpoint = {
|
|
9744
|
-
last_op_id: diff.last_op_id,
|
|
9745
|
-
buckets: [...newBuckets.values()],
|
|
9746
|
-
write_checkpoint: diff.write_checkpoint
|
|
9747
|
-
};
|
|
9748
|
-
targetCheckpoint = newCheckpoint;
|
|
9749
|
-
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
|
|
9750
|
-
bucketMap = new Map();
|
|
9751
|
-
newBuckets.forEach((checksum, name) => bucketMap.set(name, {
|
|
9752
|
-
name: checksum.bucket,
|
|
9753
|
-
priority: checksum.priority ?? FALLBACK_PRIORITY
|
|
9754
|
-
}));
|
|
9755
|
-
const bucketsToDelete = diff.removed_buckets;
|
|
9756
|
-
if (bucketsToDelete.length > 0) {
|
|
9757
|
-
this.logger.debug('Remove buckets', bucketsToDelete);
|
|
9758
|
-
}
|
|
9759
|
-
await this.options.adapter.removeBuckets(bucketsToDelete);
|
|
9760
|
-
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
|
|
9761
|
-
}
|
|
9762
|
-
else if (isStreamingSyncData(line)) {
|
|
9763
|
-
const { data } = line;
|
|
9764
|
-
const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
|
|
9765
|
-
let updatedProgress = null;
|
|
9766
|
-
if (previousProgress) {
|
|
9767
|
-
updatedProgress = { ...previousProgress };
|
|
9768
|
-
const progressForBucket = updatedProgress[data.bucket];
|
|
9769
|
-
if (progressForBucket) {
|
|
9770
|
-
updatedProgress[data.bucket] = {
|
|
9771
|
-
...progressForBucket,
|
|
9772
|
-
since_last: progressForBucket.since_last + data.data.length
|
|
9773
|
-
};
|
|
9774
|
-
}
|
|
9775
|
-
}
|
|
9776
|
-
this.updateSyncStatus({
|
|
9777
|
-
dataFlow: {
|
|
9778
|
-
downloading: true,
|
|
9779
|
-
downloadProgress: updatedProgress
|
|
9780
|
-
}
|
|
9781
|
-
});
|
|
9782
|
-
await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
|
|
9783
|
-
}
|
|
9784
|
-
else if (isStreamingKeepalive(line)) {
|
|
9785
|
-
const remaining_seconds = line.token_expires_in;
|
|
9786
|
-
if (remaining_seconds == 0) {
|
|
9787
|
-
// Connection would be closed automatically right after this
|
|
9788
|
-
this.logger.debug('Token expiring; reconnect');
|
|
9789
|
-
/**
|
|
9790
|
-
* For a rare case where the backend connector does not update the token
|
|
9791
|
-
* (uses the same one), this should have some delay.
|
|
9792
|
-
*/
|
|
9793
|
-
await this.delayRetry();
|
|
9794
|
-
return;
|
|
9795
|
-
}
|
|
9796
|
-
else if (remaining_seconds < 30) {
|
|
9797
|
-
this.logger.debug('Token will expire soon; reconnect');
|
|
9798
|
-
// Pre-emptively refresh the token
|
|
9799
|
-
this.options.remote.invalidateCredentials();
|
|
9800
|
-
return;
|
|
9801
|
-
}
|
|
9802
|
-
this.triggerCrudUpload();
|
|
9803
|
-
}
|
|
9804
|
-
else {
|
|
9805
|
-
this.logger.debug('Received unknown sync line', line);
|
|
9806
|
-
}
|
|
9422
|
+
});
|
|
9807
9423
|
}
|
|
9808
|
-
this.logger.debug('Stream input empty');
|
|
9809
|
-
// Connection closed. Likely due to auth issue.
|
|
9810
|
-
return;
|
|
9811
9424
|
}
|
|
9812
9425
|
async rustSyncIteration(signal, resolvedOptions) {
|
|
9813
9426
|
const syncImplementation = this;
|
|
@@ -9880,6 +9493,10 @@ The next upload iteration will be delayed.`);
|
|
|
9880
9493
|
const rawResponse = await adapter.control(op, payload ?? null);
|
|
9881
9494
|
const logger = syncImplementation.logger;
|
|
9882
9495
|
logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
|
|
9496
|
+
if (op != PowerSyncControlCommand.STOP) {
|
|
9497
|
+
// Evidently we have a working connection here, otherwise powersync_control would have failed.
|
|
9498
|
+
syncImplementation.connectionMayHaveChanged = false;
|
|
9499
|
+
}
|
|
9883
9500
|
await handleInstructions(JSON.parse(rawResponse));
|
|
9884
9501
|
}
|
|
9885
9502
|
async function handleInstruction(instruction) {
|
|
@@ -9966,68 +9583,6 @@ The next upload iteration will be delayed.`);
|
|
|
9966
9583
|
}
|
|
9967
9584
|
return { immediateRestart: hideDisconnectOnRestart };
|
|
9968
9585
|
}
|
|
9969
|
-
async updateSyncStatusForStartingCheckpoint(checkpoint) {
|
|
9970
|
-
const localProgress = await this.options.adapter.getBucketOperationProgress();
|
|
9971
|
-
const progress = {};
|
|
9972
|
-
let invalidated = false;
|
|
9973
|
-
for (const bucket of checkpoint.buckets) {
|
|
9974
|
-
const savedProgress = localProgress[bucket.bucket];
|
|
9975
|
-
const atLast = savedProgress?.atLast ?? 0;
|
|
9976
|
-
const sinceLast = savedProgress?.sinceLast ?? 0;
|
|
9977
|
-
progress[bucket.bucket] = {
|
|
9978
|
-
// The fallback priority doesn't matter here, but 3 is the one newer versions of the sync service
|
|
9979
|
-
// will use by default.
|
|
9980
|
-
priority: bucket.priority ?? 3,
|
|
9981
|
-
at_last: atLast,
|
|
9982
|
-
since_last: sinceLast,
|
|
9983
|
-
target_count: bucket.count ?? 0
|
|
9984
|
-
};
|
|
9985
|
-
if (bucket.count != null && bucket.count < atLast + sinceLast) {
|
|
9986
|
-
// Either due to a defrag / sync rule deploy or a compaction operation, the size
|
|
9987
|
-
// of the bucket shrank so much that the local ops exceed the ops in the updated
|
|
9988
|
-
// bucket. We can't prossibly report progress in this case (it would overshoot 100%).
|
|
9989
|
-
invalidated = true;
|
|
9990
|
-
}
|
|
9991
|
-
}
|
|
9992
|
-
if (invalidated) {
|
|
9993
|
-
for (const bucket in progress) {
|
|
9994
|
-
const bucketProgress = progress[bucket];
|
|
9995
|
-
bucketProgress.at_last = 0;
|
|
9996
|
-
bucketProgress.since_last = 0;
|
|
9997
|
-
}
|
|
9998
|
-
}
|
|
9999
|
-
this.updateSyncStatus({
|
|
10000
|
-
dataFlow: {
|
|
10001
|
-
downloading: true,
|
|
10002
|
-
downloadProgress: progress
|
|
10003
|
-
}
|
|
10004
|
-
});
|
|
10005
|
-
}
|
|
10006
|
-
async applyCheckpoint(checkpoint) {
|
|
10007
|
-
let result = await this.options.adapter.syncLocalDatabase(checkpoint);
|
|
10008
|
-
if (!result.checkpointValid) {
|
|
10009
|
-
this.logger.debug(`Checksum mismatch in checkpoint ${checkpoint.last_op_id}, will reconnect`);
|
|
10010
|
-
// This means checksums failed. Start again with a new checkpoint.
|
|
10011
|
-
// TODO: better back-off
|
|
10012
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
10013
|
-
return { applied: false, endIteration: true };
|
|
10014
|
-
}
|
|
10015
|
-
else if (!result.ready) {
|
|
10016
|
-
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.`);
|
|
10017
|
-
return { applied: false, endIteration: false };
|
|
10018
|
-
}
|
|
10019
|
-
this.logger.debug(`Applied checkpoint ${checkpoint.last_op_id}`, checkpoint);
|
|
10020
|
-
this.updateSyncStatus({
|
|
10021
|
-
connected: true,
|
|
10022
|
-
lastSyncedAt: new Date(),
|
|
10023
|
-
dataFlow: {
|
|
10024
|
-
downloading: false,
|
|
10025
|
-
downloadProgress: null,
|
|
10026
|
-
downloadError: undefined
|
|
10027
|
-
}
|
|
10028
|
-
});
|
|
10029
|
-
return { applied: true, endIteration: false };
|
|
10030
|
-
}
|
|
10031
9586
|
updateSyncStatus(options) {
|
|
10032
9587
|
const updatedStatus = new SyncStatus({
|
|
10033
9588
|
connected: options.connected ?? this.syncStatus.connected,
|
|
@@ -11566,14 +11121,12 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
11566
11121
|
db;
|
|
11567
11122
|
logger;
|
|
11568
11123
|
tableNames;
|
|
11569
|
-
_hasCompletedSync;
|
|
11570
11124
|
updateListener;
|
|
11571
11125
|
_clientId;
|
|
11572
11126
|
constructor(db, logger = Logger.get('SqliteBucketStorage')) {
|
|
11573
11127
|
super();
|
|
11574
11128
|
this.db = db;
|
|
11575
11129
|
this.logger = logger;
|
|
11576
|
-
this._hasCompletedSync = false;
|
|
11577
11130
|
this.tableNames = new Set();
|
|
11578
11131
|
this.updateListener = db.registerListener({
|
|
11579
11132
|
tablesUpdated: (update) => {
|
|
@@ -11585,7 +11138,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
11585
11138
|
});
|
|
11586
11139
|
}
|
|
11587
11140
|
async init() {
|
|
11588
|
-
this._hasCompletedSync = false;
|
|
11589
11141
|
const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
|
|
11590
11142
|
for (const row of existingTableRows ?? []) {
|
|
11591
11143
|
this.tableNames.add(row.name);
|
|
@@ -11607,156 +11159,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
11607
11159
|
getMaxOpId() {
|
|
11608
11160
|
return MAX_OP_ID;
|
|
11609
11161
|
}
|
|
11610
|
-
/**
|
|
11611
|
-
* Reset any caches.
|
|
11612
|
-
*/
|
|
11613
|
-
startSession() { }
|
|
11614
|
-
async getBucketStates() {
|
|
11615
|
-
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'");
|
|
11616
|
-
return result;
|
|
11617
|
-
}
|
|
11618
|
-
async getBucketOperationProgress() {
|
|
11619
|
-
const rows = await this.db.getAll('SELECT name, count_at_last, count_since_last FROM ps_buckets');
|
|
11620
|
-
return Object.fromEntries(rows.map((r) => [r.name, { atLast: r.count_at_last, sinceLast: r.count_since_last }]));
|
|
11621
|
-
}
|
|
11622
|
-
async saveSyncData(batch, fixedKeyFormat = false) {
|
|
11623
|
-
await this.writeTransaction(async (tx) => {
|
|
11624
|
-
for (const b of batch.buckets) {
|
|
11625
|
-
await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
|
|
11626
|
-
'save',
|
|
11627
|
-
JSON.stringify({ buckets: [b.toJSON(fixedKeyFormat)] })
|
|
11628
|
-
]);
|
|
11629
|
-
this.logger.debug(`Saved batch of data for bucket: ${b.bucket}, operations: ${b.data.length}`);
|
|
11630
|
-
}
|
|
11631
|
-
});
|
|
11632
|
-
}
|
|
11633
|
-
async removeBuckets(buckets) {
|
|
11634
|
-
for (const bucket of buckets) {
|
|
11635
|
-
await this.deleteBucket(bucket);
|
|
11636
|
-
}
|
|
11637
|
-
}
|
|
11638
|
-
/**
|
|
11639
|
-
* Mark a bucket for deletion.
|
|
11640
|
-
*/
|
|
11641
|
-
async deleteBucket(bucket) {
|
|
11642
|
-
await this.writeTransaction(async (tx) => {
|
|
11643
|
-
await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', ['delete_bucket', bucket]);
|
|
11644
|
-
});
|
|
11645
|
-
this.logger.debug(`Done deleting bucket ${bucket}`);
|
|
11646
|
-
}
|
|
11647
|
-
async hasCompletedSync() {
|
|
11648
|
-
if (this._hasCompletedSync) {
|
|
11649
|
-
return true;
|
|
11650
|
-
}
|
|
11651
|
-
const r = await this.db.get(`SELECT powersync_last_synced_at() as synced_at`);
|
|
11652
|
-
const completed = r.synced_at != null;
|
|
11653
|
-
if (completed) {
|
|
11654
|
-
this._hasCompletedSync = true;
|
|
11655
|
-
}
|
|
11656
|
-
return completed;
|
|
11657
|
-
}
|
|
11658
|
-
async syncLocalDatabase(checkpoint, priority) {
|
|
11659
|
-
const r = await this.validateChecksums(checkpoint, priority);
|
|
11660
|
-
if (!r.checkpointValid) {
|
|
11661
|
-
this.logger.error('Checksums failed for', r.checkpointFailures);
|
|
11662
|
-
for (const b of r.checkpointFailures ?? []) {
|
|
11663
|
-
await this.deleteBucket(b);
|
|
11664
|
-
}
|
|
11665
|
-
return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
|
|
11666
|
-
}
|
|
11667
|
-
if (priority == null) {
|
|
11668
|
-
this.logger.debug(`Validated checksums checkpoint ${checkpoint.last_op_id}`);
|
|
11669
|
-
}
|
|
11670
|
-
else {
|
|
11671
|
-
this.logger.debug(`Validated checksums for partial checkpoint ${checkpoint.last_op_id}, priority ${priority}`);
|
|
11672
|
-
}
|
|
11673
|
-
let buckets = checkpoint.buckets;
|
|
11674
|
-
if (priority !== undefined) {
|
|
11675
|
-
buckets = buckets.filter((b) => hasMatchingPriority(priority, b));
|
|
11676
|
-
}
|
|
11677
|
-
const bucketNames = buckets.map((b) => b.bucket);
|
|
11678
|
-
await this.writeTransaction(async (tx) => {
|
|
11679
|
-
await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
|
|
11680
|
-
checkpoint.last_op_id,
|
|
11681
|
-
JSON.stringify(bucketNames)
|
|
11682
|
-
]);
|
|
11683
|
-
if (priority == null && checkpoint.write_checkpoint) {
|
|
11684
|
-
await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
|
|
11685
|
-
}
|
|
11686
|
-
});
|
|
11687
|
-
const valid = await this.updateObjectsFromBuckets(checkpoint, priority);
|
|
11688
|
-
if (!valid) {
|
|
11689
|
-
return { ready: false, checkpointValid: true };
|
|
11690
|
-
}
|
|
11691
|
-
return {
|
|
11692
|
-
ready: true,
|
|
11693
|
-
checkpointValid: true
|
|
11694
|
-
};
|
|
11695
|
-
}
|
|
11696
|
-
/**
|
|
11697
|
-
* Atomically update the local state to the current checkpoint.
|
|
11698
|
-
*
|
|
11699
|
-
* This includes creating new tables, dropping old tables, and copying data over from the oplog.
|
|
11700
|
-
*/
|
|
11701
|
-
async updateObjectsFromBuckets(checkpoint, priority) {
|
|
11702
|
-
let arg = '';
|
|
11703
|
-
if (priority !== undefined) {
|
|
11704
|
-
const affectedBuckets = [];
|
|
11705
|
-
for (const desc of checkpoint.buckets) {
|
|
11706
|
-
if (hasMatchingPriority(priority, desc)) {
|
|
11707
|
-
affectedBuckets.push(desc.bucket);
|
|
11708
|
-
}
|
|
11709
|
-
}
|
|
11710
|
-
arg = JSON.stringify({ priority, buckets: affectedBuckets });
|
|
11711
|
-
}
|
|
11712
|
-
return this.writeTransaction(async (tx) => {
|
|
11713
|
-
const { insertId: result } = await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
|
|
11714
|
-
'sync_local',
|
|
11715
|
-
arg
|
|
11716
|
-
]);
|
|
11717
|
-
if (result == 1) {
|
|
11718
|
-
if (priority == null) {
|
|
11719
|
-
const bucketToCount = Object.fromEntries(checkpoint.buckets.map((b) => [b.bucket, b.count]));
|
|
11720
|
-
// The two parameters could be replaced with one, but: https://github.com/powersync-ja/better-sqlite3/pull/6
|
|
11721
|
-
const jsonBucketCount = JSON.stringify(bucketToCount);
|
|
11722
|
-
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]);
|
|
11723
|
-
}
|
|
11724
|
-
return true;
|
|
11725
|
-
}
|
|
11726
|
-
else {
|
|
11727
|
-
return false;
|
|
11728
|
-
}
|
|
11729
|
-
});
|
|
11730
|
-
}
|
|
11731
|
-
async validateChecksums(checkpoint, priority) {
|
|
11732
|
-
if (priority !== undefined) {
|
|
11733
|
-
// Only validate the buckets within the priority we care about
|
|
11734
|
-
const newBuckets = checkpoint.buckets.filter((cs) => hasMatchingPriority(priority, cs));
|
|
11735
|
-
checkpoint = { ...checkpoint, buckets: newBuckets };
|
|
11736
|
-
}
|
|
11737
|
-
const rs = await this.db.execute('SELECT powersync_validate_checkpoint(?) as result', [
|
|
11738
|
-
JSON.stringify({ ...checkpoint })
|
|
11739
|
-
]);
|
|
11740
|
-
const resultItem = rs.rows?.item(0);
|
|
11741
|
-
if (!resultItem) {
|
|
11742
|
-
return {
|
|
11743
|
-
checkpointValid: false,
|
|
11744
|
-
ready: false,
|
|
11745
|
-
checkpointFailures: []
|
|
11746
|
-
};
|
|
11747
|
-
}
|
|
11748
|
-
const result = JSON.parse(resultItem['result']);
|
|
11749
|
-
if (result['valid']) {
|
|
11750
|
-
return { ready: true, checkpointValid: true };
|
|
11751
|
-
}
|
|
11752
|
-
else {
|
|
11753
|
-
return {
|
|
11754
|
-
checkpointValid: false,
|
|
11755
|
-
ready: false,
|
|
11756
|
-
checkpointFailures: result['failed_buckets']
|
|
11757
|
-
};
|
|
11758
|
-
}
|
|
11759
|
-
}
|
|
11760
11162
|
async updateLocalTarget(cb) {
|
|
11761
11163
|
const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)", [MAX_OP_ID]);
|
|
11762
11164
|
if (!rs1.length) {
|
|
@@ -11847,12 +11249,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
11847
11249
|
async writeTransaction(callback, options) {
|
|
11848
11250
|
return this.db.writeTransaction(callback, options);
|
|
11849
11251
|
}
|
|
11850
|
-
/**
|
|
11851
|
-
* Set a target checkpoint.
|
|
11852
|
-
*/
|
|
11853
|
-
async setTargetCheckpoint(checkpoint) {
|
|
11854
|
-
// No-op for now
|
|
11855
|
-
}
|
|
11856
11252
|
async control(op, payload) {
|
|
11857
11253
|
return await this.writeTransaction(async (tx) => {
|
|
11858
11254
|
const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
|
|
@@ -11876,20 +11272,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
11876
11272
|
}
|
|
11877
11273
|
static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
|
|
11878
11274
|
}
|
|
11879
|
-
function hasMatchingPriority(priority, bucket) {
|
|
11880
|
-
return bucket.priority != null && bucket.priority <= priority;
|
|
11881
|
-
}
|
|
11882
|
-
|
|
11883
|
-
// TODO JSON
|
|
11884
|
-
class SyncDataBatch {
|
|
11885
|
-
buckets;
|
|
11886
|
-
static fromJSON(json) {
|
|
11887
|
-
return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
|
|
11888
|
-
}
|
|
11889
|
-
constructor(buckets) {
|
|
11890
|
-
this.buckets = buckets;
|
|
11891
|
-
}
|
|
11892
|
-
}
|
|
11893
11275
|
|
|
11894
11276
|
/**
|
|
11895
11277
|
* Thrown when an underlying database connection is closed.
|
|
@@ -11949,10 +11331,8 @@ class Schema {
|
|
|
11949
11331
|
* developer instead of automatically by PowerSync.
|
|
11950
11332
|
* Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
|
|
11951
11333
|
* using client-side table and column constraints.
|
|
11952
|
-
* Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
|
|
11953
11334
|
*
|
|
11954
11335
|
* @param tables An object of (table name, raw table definition) entries.
|
|
11955
|
-
* @experimental Note that the raw tables API is still experimental and may change in the future.
|
|
11956
11336
|
*/
|
|
11957
11337
|
withRawTables(tables) {
|
|
11958
11338
|
for (const [name, rawTableDefinition] of Object.entries(tables)) {
|
|
@@ -12149,5 +11529,5 @@ const parseQuery = (query, parameters) => {
|
|
|
12149
11529
|
return { sqlStatement, parameters: parameters };
|
|
12150
11530
|
};
|
|
12151
11531
|
|
|
12152
|
-
export { ATTACHMENT_TABLE, AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, AttachmentContext, AttachmentQueue, AttachmentService, AttachmentState, AttachmentTable, BaseObserver, Column, ColumnType, ConnectionClosedError, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, DBAdapterDefaultMixin, DBGetUtilsDefaultMixin, DEFAULT_CRUD_BATCH_LIMIT, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_INDEX_COLUMN_OPTIONS, DEFAULT_INDEX_OPTIONS, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_POWERSYNC_CLOSE_OPTIONS, DEFAULT_POWERSYNC_DB_OPTIONS, DEFAULT_REMOTE_LOGGER, DEFAULT_REMOTE_OPTIONS, DEFAULT_RETRY_DELAY_MS, DEFAULT_ROW_COMPARATOR, DEFAULT_STREAMING_SYNC_OPTIONS, DEFAULT_STREAM_CONNECTION_OPTIONS, DEFAULT_SYNC_CLIENT_IMPLEMENTATION, DEFAULT_TABLE_OPTIONS, DEFAULT_WATCH_QUERY_OPTIONS, DEFAULT_WATCH_THROTTLE_MS, DiffTriggerOperation, DifferentialQueryProcessor, EMPTY_DIFFERENTIAL, EncodingType, FalsyComparator, FetchImplementationProvider, FetchStrategy, GetAllQuery, Index, IndexedColumn, InvalidSQLCharacters, LockType, LogLevel, MAX_AMOUNT_OF_COLUMNS, MAX_OP_ID, MEMORY_TRIGGER_CLAIM_MANAGER, Mutex, OnChangeQueryProcessor,
|
|
11532
|
+
export { ATTACHMENT_TABLE, AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, AttachmentContext, AttachmentQueue, AttachmentService, AttachmentState, AttachmentTable, BaseObserver, Column, ColumnType, ConnectionClosedError, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, DBAdapterDefaultMixin, DBGetUtilsDefaultMixin, DEFAULT_CRUD_BATCH_LIMIT, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_INDEX_COLUMN_OPTIONS, DEFAULT_INDEX_OPTIONS, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_POWERSYNC_CLOSE_OPTIONS, DEFAULT_POWERSYNC_DB_OPTIONS, DEFAULT_REMOTE_LOGGER, DEFAULT_REMOTE_OPTIONS, DEFAULT_RETRY_DELAY_MS, DEFAULT_ROW_COMPARATOR, DEFAULT_STREAMING_SYNC_OPTIONS, DEFAULT_STREAM_CONNECTION_OPTIONS, DEFAULT_SYNC_CLIENT_IMPLEMENTATION, DEFAULT_TABLE_OPTIONS, DEFAULT_WATCH_QUERY_OPTIONS, DEFAULT_WATCH_THROTTLE_MS, DiffTriggerOperation, DifferentialQueryProcessor, EMPTY_DIFFERENTIAL, EncodingType, FalsyComparator, FetchImplementationProvider, FetchStrategy, GetAllQuery, Index, IndexedColumn, InvalidSQLCharacters, LockType, LogLevel, MAX_AMOUNT_OF_COLUMNS, MAX_OP_ID, MEMORY_TRIGGER_CLAIM_MANAGER, Mutex, OnChangeQueryProcessor, PSInternalTable, PowerSyncControlCommand, RowUpdateType, Schema, Semaphore, SqliteBucketStorage, SyncClientImplementation, SyncProgress, SyncStatus, SyncStreamConnectionMethod, SyncingService, Table, TableV2, TriggerManagerImpl, UpdateType, UploadQueueStats, WatchedQueryListenerEvent, attachmentFromSql, column, compilableQueryWatch, createBaseLogger, createLogger, extractTableUpdates, isBatchedUpdateNotification, isDBAdapter, isPowerSyncDatabaseOptionsWithSettings, isSQLOpenFactory, isSQLOpenOptions, parseQuery, runOnSchemaChange, sanitizeSQL, sanitizeUUID, timeoutSignal };
|
|
12153
11533
|
//# sourceMappingURL=bundle.node.mjs.map
|