@powersync/common 1.51.0 → 1.53.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.cjs +510 -1129
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +511 -1116
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +508 -1129
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +509 -1116
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +73 -433
- package/legacy/sync_protocol.d.ts +103 -0
- package/lib/client/AbstractPowerSyncDatabase.js +3 -3
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
- package/lib/client/ConnectionManager.js +1 -1
- package/lib/client/ConnectionManager.js.map +1 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +6 -64
- package/lib/client/sync/bucket/BucketStorageAdapter.js +4 -0
- 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 +29 -18
- package/lib/client/sync/stream/AbstractRemote.js +155 -188
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +13 -35
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +150 -448
- 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 +14 -9
- package/lib/client/sync/stream/core-instruction.js +3 -0
- package/lib/client/sync/stream/core-instruction.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +9 -0
- package/lib/db/DBAdapter.js +8 -1
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +3 -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 +2 -6
- package/lib/index.js +1 -6
- package/lib/index.js.map +1 -1
- package/lib/utils/async.d.ts +0 -9
- package/lib/utils/async.js +0 -9
- package/lib/utils/async.js.map +1 -1
- package/lib/utils/stream_transform.d.ts +39 -0
- package/lib/utils/stream_transform.js +206 -0
- package/lib/utils/stream_transform.js.map +1 -0
- package/package.json +15 -10
- package/src/client/AbstractPowerSyncDatabase.ts +3 -3
- package/src/client/ConnectionManager.ts +1 -1
- package/src/client/sync/bucket/BucketStorageAdapter.ts +6 -71
- package/src/client/sync/bucket/SqliteBucketStorage.ts +1 -197
- package/src/client/sync/stream/AbstractRemote.ts +183 -229
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +181 -510
- package/src/client/sync/stream/JsonValue.ts +8 -0
- package/src/client/sync/stream/core-instruction.ts +15 -5
- package/src/db/DBAdapter.ts +20 -2
- package/src/db/crud/SyncStatus.ts +4 -5
- package/src/db/schema/RawTable.ts +0 -5
- package/src/db/schema/Schema.ts +0 -2
- package/src/index.ts +2 -6
- package/src/utils/async.ts +0 -11
- package/src/utils/stream_transform.ts +252 -0
- 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/lib/utils/DataStream.d.ts +0 -62
- package/lib/utils/DataStream.js +0 -169
- package/lib/utils/DataStream.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/src/utils/DataStream.ts +0 -222
package/dist/bundle.mjs
CHANGED
|
@@ -1396,6 +1396,8 @@ var EncodingType;
|
|
|
1396
1396
|
EncodingType["Base64"] = "base64";
|
|
1397
1397
|
})(EncodingType || (EncodingType = {}));
|
|
1398
1398
|
|
|
1399
|
+
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
1400
|
+
|
|
1399
1401
|
function getDefaultExportFromCjs (x) {
|
|
1400
1402
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
1401
1403
|
}
|
|
@@ -1476,7 +1478,7 @@ function requireEventIterator () {
|
|
|
1476
1478
|
this.removeCallback();
|
|
1477
1479
|
});
|
|
1478
1480
|
}
|
|
1479
|
-
[
|
|
1481
|
+
[symbolAsyncIterator]() {
|
|
1480
1482
|
return {
|
|
1481
1483
|
next: (value) => {
|
|
1482
1484
|
const result = this.pushQueue.shift();
|
|
@@ -1523,7 +1525,7 @@ function requireEventIterator () {
|
|
|
1523
1525
|
queue.eventHandlers[event] = fn;
|
|
1524
1526
|
},
|
|
1525
1527
|
}) || (() => { });
|
|
1526
|
-
this[
|
|
1528
|
+
this[symbolAsyncIterator] = () => queue[symbolAsyncIterator]();
|
|
1527
1529
|
Object.freeze(this);
|
|
1528
1530
|
}
|
|
1529
1531
|
}
|
|
@@ -1967,8 +1969,15 @@ class BaseTransaction {
|
|
|
1967
1969
|
class TransactionImplementation extends DBGetUtilsDefaultMixin(BaseTransaction) {
|
|
1968
1970
|
static async runWith(ctx, fn) {
|
|
1969
1971
|
let tx = new TransactionImplementation(ctx);
|
|
1972
|
+
// For write transactions, use BEGIN IMMEDIATE to immediately obtain a write lock on the database (instead of doing
|
|
1973
|
+
// that on the first statement). If we have a genuine read-only connection, we also use BEGIN IMMEDIATE there: In
|
|
1974
|
+
// WAL mode, that ensures we pin the current state of the database (instead of the state at the first statement in
|
|
1975
|
+
// the transaction). But if we have a "fake" read-only connection implemented through `pragma query_only = true`, we
|
|
1976
|
+
// can't use this trick because it would attempt to lock the connection. So there, we use a regular `BEGIN`
|
|
1977
|
+
// statement.
|
|
1978
|
+
const useBeginImmediate = ctx.connectionType != 'queryOnly';
|
|
1970
1979
|
try {
|
|
1971
|
-
await ctx.execute('BEGIN IMMEDIATE');
|
|
1980
|
+
await ctx.execute(useBeginImmediate ? 'BEGIN IMMEDIATE' : 'BEGIN');
|
|
1972
1981
|
const result = await fn(tx);
|
|
1973
1982
|
await tx.commit();
|
|
1974
1983
|
return result;
|
|
@@ -2127,16 +2136,12 @@ class SyncStatus {
|
|
|
2127
2136
|
*
|
|
2128
2137
|
* This returns null when the database is currently being opened and we don't have reliable information about all
|
|
2129
2138
|
* included streams yet.
|
|
2130
|
-
*
|
|
2131
|
-
* @experimental Sync streams are currently in alpha.
|
|
2132
2139
|
*/
|
|
2133
2140
|
get syncStreams() {
|
|
2134
2141
|
return this.options.dataFlow?.internalStreamSubscriptions?.map((core) => new SyncStreamStatusView(this, core));
|
|
2135
2142
|
}
|
|
2136
2143
|
/**
|
|
2137
2144
|
* If the `stream` appears in {@link syncStreams}, returns the current status for that stream.
|
|
2138
|
-
*
|
|
2139
|
-
* @experimental Sync streams are currently in alpha.
|
|
2140
2145
|
*/
|
|
2141
2146
|
forStream(stream) {
|
|
2142
2147
|
const asJson = JSON.stringify(stream.parameters);
|
|
@@ -2405,15 +2410,6 @@ class ControlledExecutor {
|
|
|
2405
2410
|
}
|
|
2406
2411
|
}
|
|
2407
2412
|
|
|
2408
|
-
/**
|
|
2409
|
-
* A ponyfill for `Symbol.asyncIterator` that is compatible with the
|
|
2410
|
-
* [recommended polyfill](https://github.com/Azure/azure-sdk-for-js/blob/%40azure/core-asynciterator-polyfill_1.0.2/sdk/core/core-asynciterator-polyfill/src/index.ts#L4-L6)
|
|
2411
|
-
* we recommend for React Native.
|
|
2412
|
-
*
|
|
2413
|
-
* As long as we use this symbol (instead of `for await` and `async *`) in this package, we can be compatible with async
|
|
2414
|
-
* iterators without requiring them.
|
|
2415
|
-
*/
|
|
2416
|
-
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
2417
2413
|
/**
|
|
2418
2414
|
* Throttle a function to be called at most once every "wait" milliseconds,
|
|
2419
2415
|
* on the trailing edge.
|
|
@@ -2728,7 +2724,7 @@ class SyncStreamSubscriptionHandle {
|
|
|
2728
2724
|
constructor(subscription) {
|
|
2729
2725
|
this.subscription = subscription;
|
|
2730
2726
|
subscription.refcount++;
|
|
2731
|
-
_finalizer?.register(this, subscription);
|
|
2727
|
+
_finalizer?.register(this, subscription, this);
|
|
2732
2728
|
}
|
|
2733
2729
|
get name() {
|
|
2734
2730
|
return this.subscription.name;
|
|
@@ -3317,6 +3313,10 @@ var PowerSyncControlCommand;
|
|
|
3317
3313
|
PowerSyncControlCommand["NOTIFY_TOKEN_REFRESHED"] = "refreshed_token";
|
|
3318
3314
|
PowerSyncControlCommand["NOTIFY_CRUD_UPLOAD_COMPLETED"] = "completed_upload";
|
|
3319
3315
|
PowerSyncControlCommand["UPDATE_SUBSCRIPTIONS"] = "update_subscriptions";
|
|
3316
|
+
/**
|
|
3317
|
+
* An `established` or `end` event for response streams.
|
|
3318
|
+
*/
|
|
3319
|
+
PowerSyncControlCommand["CONNECTION_STATE"] = "connection";
|
|
3320
3320
|
})(PowerSyncControlCommand || (PowerSyncControlCommand = {}));
|
|
3321
3321
|
|
|
3322
3322
|
/**
|
|
@@ -3498,103 +3498,6 @@ class AbortOperation extends Error {
|
|
|
3498
3498
|
}
|
|
3499
3499
|
}
|
|
3500
3500
|
|
|
3501
|
-
var OpTypeEnum;
|
|
3502
|
-
(function (OpTypeEnum) {
|
|
3503
|
-
OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
|
|
3504
|
-
OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
|
|
3505
|
-
OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
|
|
3506
|
-
OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
|
|
3507
|
-
})(OpTypeEnum || (OpTypeEnum = {}));
|
|
3508
|
-
/**
|
|
3509
|
-
* Used internally for sync buckets.
|
|
3510
|
-
*/
|
|
3511
|
-
class OpType {
|
|
3512
|
-
value;
|
|
3513
|
-
static fromJSON(jsonValue) {
|
|
3514
|
-
return new OpType(OpTypeEnum[jsonValue]);
|
|
3515
|
-
}
|
|
3516
|
-
constructor(value) {
|
|
3517
|
-
this.value = value;
|
|
3518
|
-
}
|
|
3519
|
-
toJSON() {
|
|
3520
|
-
return Object.entries(OpTypeEnum).find(([, value]) => value === this.value)[0];
|
|
3521
|
-
}
|
|
3522
|
-
}
|
|
3523
|
-
|
|
3524
|
-
class OplogEntry {
|
|
3525
|
-
op_id;
|
|
3526
|
-
op;
|
|
3527
|
-
checksum;
|
|
3528
|
-
subkey;
|
|
3529
|
-
object_type;
|
|
3530
|
-
object_id;
|
|
3531
|
-
data;
|
|
3532
|
-
static fromRow(row) {
|
|
3533
|
-
return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
|
|
3534
|
-
}
|
|
3535
|
-
constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
|
|
3536
|
-
this.op_id = op_id;
|
|
3537
|
-
this.op = op;
|
|
3538
|
-
this.checksum = checksum;
|
|
3539
|
-
this.subkey = subkey;
|
|
3540
|
-
this.object_type = object_type;
|
|
3541
|
-
this.object_id = object_id;
|
|
3542
|
-
this.data = data;
|
|
3543
|
-
}
|
|
3544
|
-
toJSON(fixedKeyEncoding = false) {
|
|
3545
|
-
return {
|
|
3546
|
-
op_id: this.op_id,
|
|
3547
|
-
op: this.op.toJSON(),
|
|
3548
|
-
object_type: this.object_type,
|
|
3549
|
-
object_id: this.object_id,
|
|
3550
|
-
checksum: this.checksum,
|
|
3551
|
-
data: this.data,
|
|
3552
|
-
// Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
|
|
3553
|
-
// but we need to migrate gradually to not break existing databases.
|
|
3554
|
-
subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
|
|
3555
|
-
};
|
|
3556
|
-
}
|
|
3557
|
-
}
|
|
3558
|
-
|
|
3559
|
-
class SyncDataBucket {
|
|
3560
|
-
bucket;
|
|
3561
|
-
data;
|
|
3562
|
-
has_more;
|
|
3563
|
-
after;
|
|
3564
|
-
next_after;
|
|
3565
|
-
static fromRow(row) {
|
|
3566
|
-
return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
|
|
3567
|
-
}
|
|
3568
|
-
constructor(bucket, data,
|
|
3569
|
-
/**
|
|
3570
|
-
* True if the response does not contain all the data for this bucket, and another request must be made.
|
|
3571
|
-
*/
|
|
3572
|
-
has_more,
|
|
3573
|
-
/**
|
|
3574
|
-
* The `after` specified in the request.
|
|
3575
|
-
*/
|
|
3576
|
-
after,
|
|
3577
|
-
/**
|
|
3578
|
-
* Use this for the next request.
|
|
3579
|
-
*/
|
|
3580
|
-
next_after) {
|
|
3581
|
-
this.bucket = bucket;
|
|
3582
|
-
this.data = data;
|
|
3583
|
-
this.has_more = has_more;
|
|
3584
|
-
this.after = after;
|
|
3585
|
-
this.next_after = next_after;
|
|
3586
|
-
}
|
|
3587
|
-
toJSON(fixedKeyEncoding = false) {
|
|
3588
|
-
return {
|
|
3589
|
-
bucket: this.bucket,
|
|
3590
|
-
has_more: this.has_more,
|
|
3591
|
-
after: this.after,
|
|
3592
|
-
next_after: this.next_after,
|
|
3593
|
-
data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
|
|
3594
|
-
};
|
|
3595
|
-
}
|
|
3596
|
-
}
|
|
3597
|
-
|
|
3598
3501
|
var buffer = {};
|
|
3599
3502
|
|
|
3600
3503
|
var base64Js = {};
|
|
@@ -10750,177 +10653,10 @@ function requireDist () {
|
|
|
10750
10653
|
|
|
10751
10654
|
var distExports = requireDist();
|
|
10752
10655
|
|
|
10753
|
-
var version = "1.
|
|
10656
|
+
var version = "1.53.0";
|
|
10754
10657
|
var PACKAGE = {
|
|
10755
10658
|
version: version};
|
|
10756
10659
|
|
|
10757
|
-
const DEFAULT_PRESSURE_LIMITS = {
|
|
10758
|
-
highWater: 10,
|
|
10759
|
-
lowWater: 0
|
|
10760
|
-
};
|
|
10761
|
-
/**
|
|
10762
|
-
* A very basic implementation of a data stream with backpressure support which does not use
|
|
10763
|
-
* native JS streams or async iterators.
|
|
10764
|
-
* This is handy for environments such as React Native which need polyfills for the above.
|
|
10765
|
-
*/
|
|
10766
|
-
class DataStream extends BaseObserver {
|
|
10767
|
-
options;
|
|
10768
|
-
dataQueue;
|
|
10769
|
-
isClosed;
|
|
10770
|
-
processingPromise;
|
|
10771
|
-
notifyDataAdded;
|
|
10772
|
-
logger;
|
|
10773
|
-
mapLine;
|
|
10774
|
-
constructor(options) {
|
|
10775
|
-
super();
|
|
10776
|
-
this.options = options;
|
|
10777
|
-
this.processingPromise = null;
|
|
10778
|
-
this.isClosed = false;
|
|
10779
|
-
this.dataQueue = [];
|
|
10780
|
-
this.mapLine = options?.mapLine ?? ((line) => line);
|
|
10781
|
-
this.logger = options?.logger ?? Logger.get('DataStream');
|
|
10782
|
-
if (options?.closeOnError) {
|
|
10783
|
-
const l = this.registerListener({
|
|
10784
|
-
error: (ex) => {
|
|
10785
|
-
l?.();
|
|
10786
|
-
this.close();
|
|
10787
|
-
}
|
|
10788
|
-
});
|
|
10789
|
-
}
|
|
10790
|
-
}
|
|
10791
|
-
get highWatermark() {
|
|
10792
|
-
return this.options?.pressure?.highWaterMark ?? DEFAULT_PRESSURE_LIMITS.highWater;
|
|
10793
|
-
}
|
|
10794
|
-
get lowWatermark() {
|
|
10795
|
-
return this.options?.pressure?.lowWaterMark ?? DEFAULT_PRESSURE_LIMITS.lowWater;
|
|
10796
|
-
}
|
|
10797
|
-
get closed() {
|
|
10798
|
-
return this.isClosed;
|
|
10799
|
-
}
|
|
10800
|
-
async close() {
|
|
10801
|
-
this.isClosed = true;
|
|
10802
|
-
await this.processingPromise;
|
|
10803
|
-
this.iterateListeners((l) => l.closed?.());
|
|
10804
|
-
// Discard any data in the queue
|
|
10805
|
-
this.dataQueue = [];
|
|
10806
|
-
this.listeners.clear();
|
|
10807
|
-
}
|
|
10808
|
-
/**
|
|
10809
|
-
* Enqueues data for the consumers to read
|
|
10810
|
-
*/
|
|
10811
|
-
enqueueData(data) {
|
|
10812
|
-
if (this.isClosed) {
|
|
10813
|
-
throw new Error('Cannot enqueue data into closed stream.');
|
|
10814
|
-
}
|
|
10815
|
-
this.dataQueue.push(data);
|
|
10816
|
-
this.notifyDataAdded?.();
|
|
10817
|
-
this.processQueue();
|
|
10818
|
-
}
|
|
10819
|
-
/**
|
|
10820
|
-
* Reads data once from the data stream
|
|
10821
|
-
* @returns a Data payload or Null if the stream closed.
|
|
10822
|
-
*/
|
|
10823
|
-
async read() {
|
|
10824
|
-
if (this.closed) {
|
|
10825
|
-
return null;
|
|
10826
|
-
}
|
|
10827
|
-
// Wait for any pending processing to complete first.
|
|
10828
|
-
// This ensures we register our listener before calling processQueue(),
|
|
10829
|
-
// avoiding a race where processQueue() sees no reader and returns early.
|
|
10830
|
-
if (this.processingPromise) {
|
|
10831
|
-
await this.processingPromise;
|
|
10832
|
-
}
|
|
10833
|
-
// Re-check after await - stream may have closed while we were waiting
|
|
10834
|
-
if (this.closed) {
|
|
10835
|
-
return null;
|
|
10836
|
-
}
|
|
10837
|
-
return new Promise((resolve, reject) => {
|
|
10838
|
-
const l = this.registerListener({
|
|
10839
|
-
data: async (data) => {
|
|
10840
|
-
resolve(data);
|
|
10841
|
-
// Remove the listener
|
|
10842
|
-
l?.();
|
|
10843
|
-
},
|
|
10844
|
-
closed: () => {
|
|
10845
|
-
resolve(null);
|
|
10846
|
-
l?.();
|
|
10847
|
-
},
|
|
10848
|
-
error: (ex) => {
|
|
10849
|
-
reject(ex);
|
|
10850
|
-
l?.();
|
|
10851
|
-
}
|
|
10852
|
-
});
|
|
10853
|
-
this.processQueue();
|
|
10854
|
-
});
|
|
10855
|
-
}
|
|
10856
|
-
/**
|
|
10857
|
-
* Executes a callback for each data item in the stream
|
|
10858
|
-
*/
|
|
10859
|
-
forEach(callback) {
|
|
10860
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
10861
|
-
this.iterateAsyncErrored(async (l) => l.lowWater?.());
|
|
10862
|
-
}
|
|
10863
|
-
return this.registerListener({
|
|
10864
|
-
data: callback
|
|
10865
|
-
});
|
|
10866
|
-
}
|
|
10867
|
-
processQueue() {
|
|
10868
|
-
if (this.processingPromise) {
|
|
10869
|
-
return;
|
|
10870
|
-
}
|
|
10871
|
-
const promise = (this.processingPromise = this._processQueue());
|
|
10872
|
-
promise.finally(() => {
|
|
10873
|
-
this.processingPromise = null;
|
|
10874
|
-
});
|
|
10875
|
-
return promise;
|
|
10876
|
-
}
|
|
10877
|
-
hasDataReader() {
|
|
10878
|
-
return Array.from(this.listeners.values()).some((l) => !!l.data);
|
|
10879
|
-
}
|
|
10880
|
-
async _processQueue() {
|
|
10881
|
-
/**
|
|
10882
|
-
* Allow listeners to mutate the queue before processing.
|
|
10883
|
-
* This allows for operations such as dropping or compressing data
|
|
10884
|
-
* on high water or requesting more data on low water.
|
|
10885
|
-
*/
|
|
10886
|
-
if (this.dataQueue.length >= this.highWatermark) {
|
|
10887
|
-
await this.iterateAsyncErrored(async (l) => l.highWater?.());
|
|
10888
|
-
}
|
|
10889
|
-
if (this.isClosed || !this.hasDataReader()) {
|
|
10890
|
-
return;
|
|
10891
|
-
}
|
|
10892
|
-
if (this.dataQueue.length) {
|
|
10893
|
-
const data = this.dataQueue.shift();
|
|
10894
|
-
const mapped = this.mapLine(data);
|
|
10895
|
-
await this.iterateAsyncErrored(async (l) => l.data?.(mapped));
|
|
10896
|
-
}
|
|
10897
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
10898
|
-
const dataAdded = new Promise((resolve) => {
|
|
10899
|
-
this.notifyDataAdded = resolve;
|
|
10900
|
-
});
|
|
10901
|
-
await Promise.race([this.iterateAsyncErrored(async (l) => l.lowWater?.()), dataAdded]);
|
|
10902
|
-
this.notifyDataAdded = null;
|
|
10903
|
-
}
|
|
10904
|
-
if (this.dataQueue.length > 0) {
|
|
10905
|
-
setTimeout(() => this.processQueue());
|
|
10906
|
-
}
|
|
10907
|
-
}
|
|
10908
|
-
async iterateAsyncErrored(cb) {
|
|
10909
|
-
// Important: We need to copy the listeners, as calling a listener could result in adding another
|
|
10910
|
-
// listener, resulting in infinite loops.
|
|
10911
|
-
const listeners = Array.from(this.listeners.values());
|
|
10912
|
-
for (let i of listeners) {
|
|
10913
|
-
try {
|
|
10914
|
-
await cb(i);
|
|
10915
|
-
}
|
|
10916
|
-
catch (ex) {
|
|
10917
|
-
this.logger.error(ex);
|
|
10918
|
-
this.iterateListeners((l) => l.error?.(ex));
|
|
10919
|
-
}
|
|
10920
|
-
}
|
|
10921
|
-
}
|
|
10922
|
-
}
|
|
10923
|
-
|
|
10924
10660
|
var WebsocketDuplexConnection = {};
|
|
10925
10661
|
|
|
10926
10662
|
var hasRequiredWebsocketDuplexConnection;
|
|
@@ -11083,8 +10819,199 @@ class WebsocketClientTransport {
|
|
|
11083
10819
|
}
|
|
11084
10820
|
}
|
|
11085
10821
|
|
|
10822
|
+
const doneResult = { done: true, value: undefined };
|
|
10823
|
+
function valueResult(value) {
|
|
10824
|
+
return { done: false, value };
|
|
10825
|
+
}
|
|
10826
|
+
/**
|
|
10827
|
+
* Expands a source async iterator by allowing to inject events asynchronously.
|
|
10828
|
+
*
|
|
10829
|
+
* The resulting iterator will emit all events from its source. Additionally though, events can be injected. These
|
|
10830
|
+
* events are dropped once the main iterator completes, but are otherwise forwarded.
|
|
10831
|
+
*
|
|
10832
|
+
* The iterator completes when its source completes, and it supports backpressure by only calling `next()` on the source
|
|
10833
|
+
* in response to a `next()` call from downstream if no pending injected events can be dispatched.
|
|
10834
|
+
*/
|
|
10835
|
+
function injectable(source) {
|
|
10836
|
+
let sourceIsDone = false;
|
|
10837
|
+
let waiter = undefined; // An active, waiting next() call.
|
|
10838
|
+
// A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
|
|
10839
|
+
let pendingSourceEvent = null;
|
|
10840
|
+
let pendingInjectedEvents = [];
|
|
10841
|
+
const consumeWaiter = () => {
|
|
10842
|
+
const pending = waiter;
|
|
10843
|
+
waiter = undefined;
|
|
10844
|
+
return pending;
|
|
10845
|
+
};
|
|
10846
|
+
const fetchFromSource = () => {
|
|
10847
|
+
const resolveWaiter = (propagate) => {
|
|
10848
|
+
const active = consumeWaiter();
|
|
10849
|
+
if (active) {
|
|
10850
|
+
propagate(active);
|
|
10851
|
+
}
|
|
10852
|
+
else {
|
|
10853
|
+
pendingSourceEvent = propagate;
|
|
10854
|
+
}
|
|
10855
|
+
};
|
|
10856
|
+
const nextFromSource = source.next();
|
|
10857
|
+
nextFromSource.then((value) => {
|
|
10858
|
+
sourceIsDone = value.done == true;
|
|
10859
|
+
resolveWaiter((w) => w.resolve(value));
|
|
10860
|
+
}, (error) => {
|
|
10861
|
+
resolveWaiter((w) => w.reject(error));
|
|
10862
|
+
});
|
|
10863
|
+
};
|
|
10864
|
+
return {
|
|
10865
|
+
next: () => {
|
|
10866
|
+
return new Promise((resolve, reject) => {
|
|
10867
|
+
// First priority: Dispatch ready upstream events.
|
|
10868
|
+
if (sourceIsDone) {
|
|
10869
|
+
return resolve(doneResult);
|
|
10870
|
+
}
|
|
10871
|
+
if (pendingSourceEvent) {
|
|
10872
|
+
pendingSourceEvent({ resolve, reject });
|
|
10873
|
+
pendingSourceEvent = null;
|
|
10874
|
+
return;
|
|
10875
|
+
}
|
|
10876
|
+
// Second priority: Dispatch injected events
|
|
10877
|
+
if (pendingInjectedEvents.length) {
|
|
10878
|
+
return resolve(valueResult(pendingInjectedEvents.shift()));
|
|
10879
|
+
}
|
|
10880
|
+
// Nothing pending? Fetch from source
|
|
10881
|
+
waiter = { resolve, reject };
|
|
10882
|
+
return fetchFromSource();
|
|
10883
|
+
});
|
|
10884
|
+
},
|
|
10885
|
+
inject: (event) => {
|
|
10886
|
+
const pending = consumeWaiter();
|
|
10887
|
+
if (pending != null) {
|
|
10888
|
+
pending.resolve(valueResult(event));
|
|
10889
|
+
}
|
|
10890
|
+
else {
|
|
10891
|
+
pendingInjectedEvents.push(event);
|
|
10892
|
+
}
|
|
10893
|
+
}
|
|
10894
|
+
};
|
|
10895
|
+
}
|
|
10896
|
+
/**
|
|
10897
|
+
* Splits a byte stream at line endings, emitting each line as a string.
|
|
10898
|
+
*/
|
|
10899
|
+
function extractJsonLines(source, decoder) {
|
|
10900
|
+
let buffer = '';
|
|
10901
|
+
const pendingLines = [];
|
|
10902
|
+
let isFinalEvent = false;
|
|
10903
|
+
return {
|
|
10904
|
+
next: async () => {
|
|
10905
|
+
while (true) {
|
|
10906
|
+
if (isFinalEvent) {
|
|
10907
|
+
return doneResult;
|
|
10908
|
+
}
|
|
10909
|
+
{
|
|
10910
|
+
const first = pendingLines.shift();
|
|
10911
|
+
if (first) {
|
|
10912
|
+
return { done: false, value: first };
|
|
10913
|
+
}
|
|
10914
|
+
}
|
|
10915
|
+
const { done, value } = await source.next();
|
|
10916
|
+
if (done) {
|
|
10917
|
+
const remaining = buffer.trim();
|
|
10918
|
+
if (remaining.length != 0) {
|
|
10919
|
+
isFinalEvent = true;
|
|
10920
|
+
return { done: false, value: remaining };
|
|
10921
|
+
}
|
|
10922
|
+
return doneResult;
|
|
10923
|
+
}
|
|
10924
|
+
const data = decoder.decode(value, { stream: true });
|
|
10925
|
+
buffer += data;
|
|
10926
|
+
const lines = buffer.split('\n');
|
|
10927
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
10928
|
+
const l = lines[i].trim();
|
|
10929
|
+
if (l.length > 0) {
|
|
10930
|
+
pendingLines.push(l);
|
|
10931
|
+
}
|
|
10932
|
+
}
|
|
10933
|
+
buffer = lines[lines.length - 1];
|
|
10934
|
+
}
|
|
10935
|
+
}
|
|
10936
|
+
};
|
|
10937
|
+
}
|
|
10938
|
+
/**
|
|
10939
|
+
* Splits a concatenated stream of BSON objects by emitting individual objects.
|
|
10940
|
+
*/
|
|
10941
|
+
function extractBsonObjects(source) {
|
|
10942
|
+
// Fully read but not emitted yet.
|
|
10943
|
+
const completedObjects = [];
|
|
10944
|
+
// Whether source has returned { done: true }. We do the same once completed objects have been emitted.
|
|
10945
|
+
let isDone = false;
|
|
10946
|
+
const lengthBuffer = new DataView(new ArrayBuffer(4));
|
|
10947
|
+
let objectBody = null;
|
|
10948
|
+
// If we're parsing the length field, a number between 1 and 4 (inclusive) describing remaining bytes in the header.
|
|
10949
|
+
// If we're consuming a document, the bytes remaining.
|
|
10950
|
+
let remainingLength = 4;
|
|
10951
|
+
return {
|
|
10952
|
+
async next() {
|
|
10953
|
+
while (true) {
|
|
10954
|
+
// Before fetching new data from upstream, return completed objects.
|
|
10955
|
+
if (completedObjects.length) {
|
|
10956
|
+
return valueResult(completedObjects.shift());
|
|
10957
|
+
}
|
|
10958
|
+
if (isDone) {
|
|
10959
|
+
return doneResult;
|
|
10960
|
+
}
|
|
10961
|
+
const upstreamEvent = await source.next();
|
|
10962
|
+
if (upstreamEvent.done) {
|
|
10963
|
+
isDone = true;
|
|
10964
|
+
if (objectBody || remainingLength != 4) {
|
|
10965
|
+
throw new Error('illegal end of stream in BSON object');
|
|
10966
|
+
}
|
|
10967
|
+
return doneResult;
|
|
10968
|
+
}
|
|
10969
|
+
const chunk = upstreamEvent.value;
|
|
10970
|
+
for (let i = 0; i < chunk.length;) {
|
|
10971
|
+
const availableInData = chunk.length - i;
|
|
10972
|
+
if (objectBody) {
|
|
10973
|
+
// We're in the middle of reading a BSON document.
|
|
10974
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
10975
|
+
const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
|
|
10976
|
+
objectBody.set(copySource, objectBody.length - remainingLength);
|
|
10977
|
+
i += bytesToRead;
|
|
10978
|
+
remainingLength -= bytesToRead;
|
|
10979
|
+
if (remainingLength == 0) {
|
|
10980
|
+
completedObjects.push(objectBody);
|
|
10981
|
+
// Prepare to read another document, starting with its length
|
|
10982
|
+
objectBody = null;
|
|
10983
|
+
remainingLength = 4;
|
|
10984
|
+
}
|
|
10985
|
+
}
|
|
10986
|
+
else {
|
|
10987
|
+
// Copy up to 4 bytes into lengthBuffer, depending on how many we still need.
|
|
10988
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
10989
|
+
for (let j = 0; j < bytesToRead; j++) {
|
|
10990
|
+
lengthBuffer.setUint8(4 - remainingLength + j, chunk[i + j]);
|
|
10991
|
+
}
|
|
10992
|
+
i += bytesToRead;
|
|
10993
|
+
remainingLength -= bytesToRead;
|
|
10994
|
+
if (remainingLength == 0) {
|
|
10995
|
+
// Transition from reading length header to reading document. Subtracting 4 because the length of the
|
|
10996
|
+
// header is included in length.
|
|
10997
|
+
const length = lengthBuffer.getInt32(0, true /* little endian */);
|
|
10998
|
+
remainingLength = length - 4;
|
|
10999
|
+
if (remainingLength < 1) {
|
|
11000
|
+
throw new Error(`invalid length for bson: ${length}`);
|
|
11001
|
+
}
|
|
11002
|
+
objectBody = new Uint8Array(length);
|
|
11003
|
+
new DataView(objectBody.buffer).setInt32(0, length, true);
|
|
11004
|
+
}
|
|
11005
|
+
}
|
|
11006
|
+
}
|
|
11007
|
+
}
|
|
11008
|
+
}
|
|
11009
|
+
};
|
|
11010
|
+
}
|
|
11011
|
+
|
|
11086
11012
|
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
|
|
11087
11013
|
const POWERSYNC_JS_VERSION = PACKAGE.version;
|
|
11014
|
+
const SYNC_QUEUE_REQUEST_HIGH_WATER = 10;
|
|
11088
11015
|
const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
|
|
11089
11016
|
// Keep alive message is sent every period
|
|
11090
11017
|
const KEEP_ALIVE_MS = 20_000;
|
|
@@ -11264,73 +11191,67 @@ class AbstractRemote {
|
|
|
11264
11191
|
return new WebSocket(url);
|
|
11265
11192
|
}
|
|
11266
11193
|
/**
|
|
11267
|
-
* Returns a data stream of sync line data.
|
|
11194
|
+
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
11268
11195
|
*
|
|
11269
|
-
*
|
|
11270
|
-
* @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
|
|
11271
|
-
* (required for compatibility with older sync services).
|
|
11196
|
+
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
11272
11197
|
*/
|
|
11273
|
-
async socketStreamRaw(options
|
|
11198
|
+
async socketStreamRaw(options) {
|
|
11274
11199
|
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
|
|
11275
|
-
const mimeType =
|
|
11200
|
+
const mimeType = 'application/json';
|
|
11276
11201
|
function toBuffer(js) {
|
|
11277
|
-
|
|
11278
|
-
if (bson != null) {
|
|
11279
|
-
contents = bson.serialize(js);
|
|
11280
|
-
}
|
|
11281
|
-
else {
|
|
11282
|
-
contents = JSON.stringify(js);
|
|
11283
|
-
}
|
|
11284
|
-
return bufferExports.Buffer.from(contents);
|
|
11202
|
+
return bufferExports.Buffer.from(JSON.stringify(js));
|
|
11285
11203
|
}
|
|
11286
11204
|
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
|
|
11287
11205
|
const request = await this.buildRequest(path);
|
|
11206
|
+
const url = this.options.socketUrlTransformer(request.url);
|
|
11288
11207
|
// Add the user agent in the setup payload - we can't set custom
|
|
11289
11208
|
// headers with websockets on web. The browser userAgent is however added
|
|
11290
11209
|
// automatically as a header.
|
|
11291
11210
|
const userAgent = this.getUserAgent();
|
|
11292
|
-
|
|
11293
|
-
|
|
11294
|
-
|
|
11295
|
-
|
|
11296
|
-
|
|
11297
|
-
|
|
11298
|
-
|
|
11211
|
+
// While we're connecting (a process that can't be aborted in RSocket), the WebSocket instance to close if we wanted
|
|
11212
|
+
// to abort the connection.
|
|
11213
|
+
let pendingSocket = null;
|
|
11214
|
+
let keepAliveTimeout;
|
|
11215
|
+
let rsocket = null;
|
|
11216
|
+
let queue = null;
|
|
11217
|
+
let didClose = false;
|
|
11218
|
+
const abortRequest = () => {
|
|
11219
|
+
if (didClose) {
|
|
11220
|
+
return;
|
|
11221
|
+
}
|
|
11222
|
+
didClose = true;
|
|
11223
|
+
clearTimeout(keepAliveTimeout);
|
|
11224
|
+
if (pendingSocket) {
|
|
11225
|
+
pendingSocket.close();
|
|
11226
|
+
}
|
|
11227
|
+
if (rsocket) {
|
|
11228
|
+
rsocket.close();
|
|
11229
|
+
}
|
|
11230
|
+
if (queue) {
|
|
11231
|
+
queue.stop();
|
|
11232
|
+
}
|
|
11233
|
+
};
|
|
11299
11234
|
// Handle upstream abort
|
|
11300
|
-
if (options.abortSignal
|
|
11235
|
+
if (options.abortSignal.aborted) {
|
|
11301
11236
|
throw new AbortOperation('Connection request aborted');
|
|
11302
11237
|
}
|
|
11303
11238
|
else {
|
|
11304
|
-
options.abortSignal
|
|
11305
|
-
stream.close();
|
|
11306
|
-
}, { once: true });
|
|
11239
|
+
options.abortSignal.addEventListener('abort', abortRequest);
|
|
11307
11240
|
}
|
|
11308
|
-
let keepAliveTimeout;
|
|
11309
11241
|
const resetTimeout = () => {
|
|
11310
11242
|
clearTimeout(keepAliveTimeout);
|
|
11311
11243
|
keepAliveTimeout = setTimeout(() => {
|
|
11312
11244
|
this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
|
|
11313
|
-
|
|
11245
|
+
abortRequest();
|
|
11314
11246
|
}, SOCKET_TIMEOUT_MS);
|
|
11315
11247
|
};
|
|
11316
11248
|
resetTimeout();
|
|
11317
|
-
// Typescript complains about this being `never` if it's not assigned here.
|
|
11318
|
-
// This is assigned in `wsCreator`.
|
|
11319
|
-
let disposeSocketConnectionTimeout = () => { };
|
|
11320
|
-
const url = this.options.socketUrlTransformer(request.url);
|
|
11321
11249
|
const connector = new distExports.RSocketConnector({
|
|
11322
11250
|
transport: new WebsocketClientTransport({
|
|
11323
11251
|
url,
|
|
11324
11252
|
wsCreator: (url) => {
|
|
11325
|
-
const socket = this.createSocket(url);
|
|
11326
|
-
|
|
11327
|
-
closed: () => {
|
|
11328
|
-
// Allow closing the underlying WebSocket if the stream was closed before the
|
|
11329
|
-
// RSocket connect completed. This should effectively abort the request.
|
|
11330
|
-
socket.close();
|
|
11331
|
-
}
|
|
11332
|
-
});
|
|
11333
|
-
socket.addEventListener('message', (event) => {
|
|
11253
|
+
const socket = (pendingSocket = this.createSocket(url));
|
|
11254
|
+
socket.addEventListener('message', () => {
|
|
11334
11255
|
resetTimeout();
|
|
11335
11256
|
});
|
|
11336
11257
|
return socket;
|
|
@@ -11350,43 +11271,40 @@ class AbstractRemote {
|
|
|
11350
11271
|
}
|
|
11351
11272
|
}
|
|
11352
11273
|
});
|
|
11353
|
-
let rsocket;
|
|
11354
11274
|
try {
|
|
11355
11275
|
rsocket = await connector.connect();
|
|
11356
11276
|
// The connection is established, we no longer need to monitor the initial timeout
|
|
11357
|
-
|
|
11277
|
+
pendingSocket = null;
|
|
11358
11278
|
}
|
|
11359
11279
|
catch (ex) {
|
|
11360
11280
|
this.logger.error(`Failed to connect WebSocket`, ex);
|
|
11361
|
-
|
|
11362
|
-
if (!stream.closed) {
|
|
11363
|
-
await stream.close();
|
|
11364
|
-
}
|
|
11281
|
+
abortRequest();
|
|
11365
11282
|
throw ex;
|
|
11366
11283
|
}
|
|
11367
11284
|
resetTimeout();
|
|
11368
|
-
let socketIsClosed = false;
|
|
11369
|
-
const closeSocket = () => {
|
|
11370
|
-
clearTimeout(keepAliveTimeout);
|
|
11371
|
-
if (socketIsClosed) {
|
|
11372
|
-
return;
|
|
11373
|
-
}
|
|
11374
|
-
socketIsClosed = true;
|
|
11375
|
-
rsocket.close();
|
|
11376
|
-
};
|
|
11377
11285
|
// Helps to prevent double close scenarios
|
|
11378
|
-
rsocket.onClose(() => (
|
|
11379
|
-
|
|
11380
|
-
let pendingEventsCount = syncQueueRequestSize;
|
|
11381
|
-
const disposeClosedListener = stream.registerListener({
|
|
11382
|
-
closed: () => {
|
|
11383
|
-
closeSocket();
|
|
11384
|
-
disposeClosedListener();
|
|
11385
|
-
}
|
|
11386
|
-
});
|
|
11387
|
-
const socket = await new Promise((resolve, reject) => {
|
|
11286
|
+
rsocket.onClose(() => (rsocket = null));
|
|
11287
|
+
return await new Promise((resolve, reject) => {
|
|
11388
11288
|
let connectionEstablished = false;
|
|
11389
|
-
|
|
11289
|
+
let pendingEventsCount = syncQueueRequestSize;
|
|
11290
|
+
let paused = false;
|
|
11291
|
+
let res = null;
|
|
11292
|
+
function requestMore() {
|
|
11293
|
+
const delta = syncQueueRequestSize - pendingEventsCount;
|
|
11294
|
+
if (!paused && delta > 0) {
|
|
11295
|
+
res?.request(delta);
|
|
11296
|
+
pendingEventsCount = syncQueueRequestSize;
|
|
11297
|
+
}
|
|
11298
|
+
}
|
|
11299
|
+
const events = new domExports.EventIterator((q) => {
|
|
11300
|
+
queue = q;
|
|
11301
|
+
q.on('highWater', () => (paused = true));
|
|
11302
|
+
q.on('lowWater', () => {
|
|
11303
|
+
paused = false;
|
|
11304
|
+
requestMore();
|
|
11305
|
+
});
|
|
11306
|
+
}, { highWaterMark: SYNC_QUEUE_REQUEST_HIGH_WATER, lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER })[symbolAsyncIterator]();
|
|
11307
|
+
res = rsocket.requestStream({
|
|
11390
11308
|
data: toBuffer(options.data),
|
|
11391
11309
|
metadata: toBuffer({
|
|
11392
11310
|
path
|
|
@@ -11411,7 +11329,7 @@ class AbstractRemote {
|
|
|
11411
11329
|
}
|
|
11412
11330
|
// RSocket will close the RSocket stream automatically
|
|
11413
11331
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
11414
|
-
|
|
11332
|
+
abortRequest();
|
|
11415
11333
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
11416
11334
|
if (!connectionEstablished) {
|
|
11417
11335
|
reject(e);
|
|
@@ -11421,41 +11339,40 @@ class AbstractRemote {
|
|
|
11421
11339
|
// The connection is active
|
|
11422
11340
|
if (!connectionEstablished) {
|
|
11423
11341
|
connectionEstablished = true;
|
|
11424
|
-
resolve(
|
|
11342
|
+
resolve(events);
|
|
11425
11343
|
}
|
|
11426
11344
|
const { data } = payload;
|
|
11345
|
+
if (data) {
|
|
11346
|
+
queue.push(data);
|
|
11347
|
+
}
|
|
11427
11348
|
// Less events are now pending
|
|
11428
11349
|
pendingEventsCount--;
|
|
11429
|
-
|
|
11430
|
-
|
|
11431
|
-
}
|
|
11432
|
-
stream.enqueueData(data);
|
|
11350
|
+
// Request another event (unless the downstream consumer is paused).
|
|
11351
|
+
requestMore();
|
|
11433
11352
|
},
|
|
11434
11353
|
onComplete: () => {
|
|
11435
|
-
|
|
11354
|
+
abortRequest(); // this will also emit a done event
|
|
11436
11355
|
},
|
|
11437
11356
|
onExtension: () => { }
|
|
11438
11357
|
});
|
|
11439
11358
|
});
|
|
11440
|
-
const l = stream.registerListener({
|
|
11441
|
-
lowWater: async () => {
|
|
11442
|
-
// Request to fill up the queue
|
|
11443
|
-
const required = syncQueueRequestSize - pendingEventsCount;
|
|
11444
|
-
if (required > 0) {
|
|
11445
|
-
socket.request(syncQueueRequestSize - pendingEventsCount);
|
|
11446
|
-
pendingEventsCount = syncQueueRequestSize;
|
|
11447
|
-
}
|
|
11448
|
-
},
|
|
11449
|
-
closed: () => {
|
|
11450
|
-
l();
|
|
11451
|
-
}
|
|
11452
|
-
});
|
|
11453
|
-
return stream;
|
|
11454
11359
|
}
|
|
11455
11360
|
/**
|
|
11456
|
-
*
|
|
11361
|
+
* @returns Whether the HTTP implementation on this platform can receive streamed binary responses. This is true on
|
|
11362
|
+
* all platforms except React Native (who would have guessed...), where we must not request BSON responses.
|
|
11363
|
+
*
|
|
11364
|
+
* @see https://github.com/react-native-community/fetch?tab=readme-ov-file#motivation
|
|
11365
|
+
*/
|
|
11366
|
+
get supportsStreamingBinaryResponses() {
|
|
11367
|
+
return true;
|
|
11368
|
+
}
|
|
11369
|
+
/**
|
|
11370
|
+
* Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
|
|
11371
|
+
* async iterator of byte blobs.
|
|
11372
|
+
*
|
|
11373
|
+
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
11457
11374
|
*/
|
|
11458
|
-
async
|
|
11375
|
+
async fetchStreamRaw(options) {
|
|
11459
11376
|
const { data, path, headers, abortSignal } = options;
|
|
11460
11377
|
const request = await this.buildRequest(path);
|
|
11461
11378
|
/**
|
|
@@ -11467,119 +11384,94 @@ class AbstractRemote {
|
|
|
11467
11384
|
* Aborting the active fetch request while it is being consumed seems to throw
|
|
11468
11385
|
* an unhandled exception on the window level.
|
|
11469
11386
|
*/
|
|
11470
|
-
if (abortSignal
|
|
11471
|
-
throw new AbortOperation('Abort request received before making
|
|
11387
|
+
if (abortSignal.aborted) {
|
|
11388
|
+
throw new AbortOperation('Abort request received before making fetchStreamRaw request');
|
|
11472
11389
|
}
|
|
11473
11390
|
const controller = new AbortController();
|
|
11474
|
-
let
|
|
11475
|
-
abortSignal
|
|
11476
|
-
|
|
11391
|
+
let reader = null;
|
|
11392
|
+
abortSignal.addEventListener('abort', () => {
|
|
11393
|
+
const reason = abortSignal.reason ??
|
|
11394
|
+
new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.');
|
|
11395
|
+
if (reader == null) {
|
|
11477
11396
|
// Only abort via the abort controller if the request has not resolved yet
|
|
11478
|
-
controller.abort(
|
|
11479
|
-
|
|
11397
|
+
controller.abort(reason);
|
|
11398
|
+
}
|
|
11399
|
+
else {
|
|
11400
|
+
reader.cancel(reason).catch(() => {
|
|
11401
|
+
// Cancelling the reader might rethrow an exception we would have handled by throwing in next(). So we can
|
|
11402
|
+
// ignore it here.
|
|
11403
|
+
});
|
|
11480
11404
|
}
|
|
11481
11405
|
});
|
|
11482
|
-
|
|
11483
|
-
|
|
11484
|
-
|
|
11485
|
-
|
|
11486
|
-
|
|
11487
|
-
|
|
11488
|
-
|
|
11489
|
-
|
|
11490
|
-
|
|
11406
|
+
let res;
|
|
11407
|
+
let responseIsBson = false;
|
|
11408
|
+
try {
|
|
11409
|
+
const ndJson = 'application/x-ndjson';
|
|
11410
|
+
const bson = 'application/vnd.powersync.bson-stream';
|
|
11411
|
+
res = await this.fetch(request.url, {
|
|
11412
|
+
method: 'POST',
|
|
11413
|
+
headers: {
|
|
11414
|
+
...headers,
|
|
11415
|
+
...request.headers,
|
|
11416
|
+
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
11417
|
+
},
|
|
11418
|
+
body: JSON.stringify(data),
|
|
11419
|
+
signal: controller.signal,
|
|
11420
|
+
cache: 'no-store',
|
|
11421
|
+
...(this.options.fetchOptions ?? {}),
|
|
11422
|
+
...options.fetchOptions
|
|
11423
|
+
});
|
|
11424
|
+
if (!res.ok || !res.body) {
|
|
11425
|
+
const text = await res.text();
|
|
11426
|
+
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
11427
|
+
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
11428
|
+
error.status = res.status;
|
|
11429
|
+
throw error;
|
|
11430
|
+
}
|
|
11431
|
+
const contentType = res.headers.get('content-type');
|
|
11432
|
+
responseIsBson = contentType == bson;
|
|
11433
|
+
}
|
|
11434
|
+
catch (ex) {
|
|
11491
11435
|
if (ex.name == 'AbortError') {
|
|
11492
11436
|
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
|
|
11493
11437
|
}
|
|
11494
11438
|
throw ex;
|
|
11495
|
-
});
|
|
11496
|
-
if (!res) {
|
|
11497
|
-
throw new Error('Fetch request was aborted');
|
|
11498
|
-
}
|
|
11499
|
-
requestResolved = true;
|
|
11500
|
-
if (!res.ok || !res.body) {
|
|
11501
|
-
const text = await res.text();
|
|
11502
|
-
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
11503
|
-
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
11504
|
-
error.status = res.status;
|
|
11505
|
-
throw error;
|
|
11506
11439
|
}
|
|
11507
|
-
|
|
11508
|
-
|
|
11509
|
-
|
|
11510
|
-
|
|
11511
|
-
|
|
11512
|
-
const closeReader = async () => {
|
|
11513
|
-
try {
|
|
11514
|
-
readerReleased = true;
|
|
11515
|
-
await reader.cancel();
|
|
11516
|
-
}
|
|
11517
|
-
catch (ex) {
|
|
11518
|
-
// an error will throw if the reader hasn't been used yet
|
|
11519
|
-
}
|
|
11520
|
-
reader.releaseLock();
|
|
11521
|
-
};
|
|
11522
|
-
const stream = new DataStream({
|
|
11523
|
-
logger: this.logger,
|
|
11524
|
-
mapLine: mapLine,
|
|
11525
|
-
pressure: {
|
|
11526
|
-
highWaterMark: 20,
|
|
11527
|
-
lowWaterMark: 10
|
|
11528
|
-
}
|
|
11529
|
-
});
|
|
11530
|
-
abortSignal?.addEventListener('abort', () => {
|
|
11531
|
-
closeReader();
|
|
11532
|
-
stream.close();
|
|
11533
|
-
});
|
|
11534
|
-
const decoder = this.createTextDecoder();
|
|
11535
|
-
let buffer = '';
|
|
11536
|
-
const consumeStream = async () => {
|
|
11537
|
-
while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
|
|
11538
|
-
const { done, value } = await reader.read();
|
|
11539
|
-
if (done) {
|
|
11540
|
-
const remaining = buffer.trim();
|
|
11541
|
-
if (remaining.length != 0) {
|
|
11542
|
-
stream.enqueueData(remaining);
|
|
11543
|
-
}
|
|
11544
|
-
stream.close();
|
|
11545
|
-
await closeReader();
|
|
11546
|
-
return;
|
|
11440
|
+
reader = res.body.getReader();
|
|
11441
|
+
const stream = {
|
|
11442
|
+
next: async () => {
|
|
11443
|
+
if (controller.signal.aborted) {
|
|
11444
|
+
return doneResult;
|
|
11547
11445
|
}
|
|
11548
|
-
|
|
11549
|
-
|
|
11550
|
-
const lines = buffer.split('\n');
|
|
11551
|
-
for (var i = 0; i < lines.length - 1; i++) {
|
|
11552
|
-
var l = lines[i].trim();
|
|
11553
|
-
if (l.length > 0) {
|
|
11554
|
-
stream.enqueueData(l);
|
|
11555
|
-
}
|
|
11446
|
+
try {
|
|
11447
|
+
return await reader.read();
|
|
11556
11448
|
}
|
|
11557
|
-
|
|
11558
|
-
|
|
11559
|
-
|
|
11560
|
-
|
|
11561
|
-
|
|
11562
|
-
|
|
11563
|
-
|
|
11564
|
-
dispose();
|
|
11565
|
-
},
|
|
11566
|
-
closed: () => {
|
|
11567
|
-
resolve();
|
|
11568
|
-
dispose();
|
|
11569
|
-
}
|
|
11570
|
-
});
|
|
11571
|
-
});
|
|
11449
|
+
catch (ex) {
|
|
11450
|
+
if (controller.signal.aborted) {
|
|
11451
|
+
// .read() completes with an error if we cancel the reader, which we do to disconnect. So this is just
|
|
11452
|
+
// things working as intended, we can return a done event and consider the exception handled.
|
|
11453
|
+
return doneResult;
|
|
11454
|
+
}
|
|
11455
|
+
throw ex;
|
|
11572
11456
|
}
|
|
11573
11457
|
}
|
|
11574
11458
|
};
|
|
11575
|
-
|
|
11576
|
-
|
|
11577
|
-
|
|
11578
|
-
|
|
11579
|
-
|
|
11580
|
-
|
|
11581
|
-
|
|
11582
|
-
|
|
11459
|
+
return { isBson: responseIsBson, stream };
|
|
11460
|
+
}
|
|
11461
|
+
/**
|
|
11462
|
+
* Posts a `/sync/stream` request.
|
|
11463
|
+
*
|
|
11464
|
+
* Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
|
|
11465
|
+
* {@link Uint8Array}s.
|
|
11466
|
+
*/
|
|
11467
|
+
async fetchStream(options) {
|
|
11468
|
+
const { isBson, stream } = await this.fetchStreamRaw(options);
|
|
11469
|
+
if (isBson) {
|
|
11470
|
+
return extractBsonObjects(stream);
|
|
11471
|
+
}
|
|
11472
|
+
else {
|
|
11473
|
+
return extractJsonLines(stream, this.createTextDecoder());
|
|
11474
|
+
}
|
|
11583
11475
|
}
|
|
11584
11476
|
}
|
|
11585
11477
|
|
|
@@ -11608,31 +11500,8 @@ function coreStatusToJs(status) {
|
|
|
11608
11500
|
priorityStatusEntries: status.priority_status.map(priorityToJs)
|
|
11609
11501
|
};
|
|
11610
11502
|
}
|
|
11611
|
-
|
|
11612
|
-
|
|
11613
|
-
return line.data != null;
|
|
11614
|
-
}
|
|
11615
|
-
function isStreamingKeepalive(line) {
|
|
11616
|
-
return line.token_expires_in != null;
|
|
11617
|
-
}
|
|
11618
|
-
function isStreamingSyncCheckpoint(line) {
|
|
11619
|
-
return line.checkpoint != null;
|
|
11620
|
-
}
|
|
11621
|
-
function isStreamingSyncCheckpointComplete(line) {
|
|
11622
|
-
return line.checkpoint_complete != null;
|
|
11623
|
-
}
|
|
11624
|
-
function isStreamingSyncCheckpointPartiallyComplete(line) {
|
|
11625
|
-
return line.partial_checkpoint_complete != null;
|
|
11626
|
-
}
|
|
11627
|
-
function isStreamingSyncCheckpointDiff(line) {
|
|
11628
|
-
return line.checkpoint_diff != null;
|
|
11629
|
-
}
|
|
11630
|
-
function isContinueCheckpointRequest(request) {
|
|
11631
|
-
return (Array.isArray(request.buckets) &&
|
|
11632
|
-
typeof request.checkpoint_token == 'string');
|
|
11633
|
-
}
|
|
11634
|
-
function isSyncNewCheckpointRequest(request) {
|
|
11635
|
-
return typeof request.request_checkpoint == 'object';
|
|
11503
|
+
function isInterruptingInstruction(instruction) {
|
|
11504
|
+
return 'EstablishSyncStream' in instruction || 'CloseSyncStream' in instruction;
|
|
11636
11505
|
}
|
|
11637
11506
|
|
|
11638
11507
|
var LockType;
|
|
@@ -11647,35 +11516,21 @@ var SyncStreamConnectionMethod;
|
|
|
11647
11516
|
})(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
|
|
11648
11517
|
var SyncClientImplementation;
|
|
11649
11518
|
(function (SyncClientImplementation) {
|
|
11650
|
-
/**
|
|
11651
|
-
* Decodes and handles sync lines received from the sync service in JavaScript.
|
|
11652
|
-
*
|
|
11653
|
-
* This is the default option.
|
|
11654
|
-
*
|
|
11655
|
-
* @deprecated We recommend the {@link RUST} client implementation for all apps. If you have issues with
|
|
11656
|
-
* the Rust client, please file an issue or reach out to us. The JavaScript client will be removed in a future
|
|
11657
|
-
* version of the PowerSync SDK.
|
|
11658
|
-
*/
|
|
11659
|
-
SyncClientImplementation["JAVASCRIPT"] = "js";
|
|
11660
11519
|
/**
|
|
11661
11520
|
* This implementation offloads the sync line decoding and handling into the PowerSync
|
|
11662
11521
|
* core extension.
|
|
11663
11522
|
*
|
|
11664
|
-
* This
|
|
11665
|
-
* recommended client implementation for all apps.
|
|
11523
|
+
* This is the only option, as an older JavaScript client implementation has been removed from the SDK.
|
|
11666
11524
|
*
|
|
11667
11525
|
* ## Compatibility warning
|
|
11668
11526
|
*
|
|
11669
11527
|
* The Rust sync client stores sync data in a format that is slightly different than the one used
|
|
11670
|
-
* by the old
|
|
11671
|
-
*
|
|
11672
|
-
* Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
|
|
11673
|
-
* the version introducing {@link RUST} as an option) also supports the new format, so you can switch
|
|
11674
|
-
* back to {@link JAVASCRIPT} later.
|
|
11528
|
+
* by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
|
|
11529
|
+
* migrate the format automatically.
|
|
11675
11530
|
*
|
|
11676
|
-
*
|
|
11677
|
-
*
|
|
11678
|
-
*
|
|
11531
|
+
* SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
|
|
11532
|
+
* implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
|
|
11533
|
+
* possible anymore. Problematic SDK versions have been released before 2025-06-09.
|
|
11679
11534
|
*/
|
|
11680
11535
|
SyncClientImplementation["RUST"] = "rust";
|
|
11681
11536
|
})(SyncClientImplementation || (SyncClientImplementation = {}));
|
|
@@ -11698,13 +11553,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
|
11698
11553
|
serializedSchema: undefined,
|
|
11699
11554
|
includeDefaultStreams: true
|
|
11700
11555
|
};
|
|
11701
|
-
// The priority we assume when we receive checkpoint lines where no priority is set.
|
|
11702
|
-
// This is the default priority used by the sync service, but can be set to an arbitrary
|
|
11703
|
-
// value since sync services without priorities also won't send partial sync completion
|
|
11704
|
-
// messages.
|
|
11705
|
-
const FALLBACK_PRIORITY = 3;
|
|
11706
11556
|
class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
11707
|
-
_lastSyncedAt;
|
|
11708
11557
|
options;
|
|
11709
11558
|
abortController;
|
|
11710
11559
|
// In rare cases, mostly for tests, uploads can be triggered without being properly connected.
|
|
@@ -11714,6 +11563,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
11714
11563
|
streamingSyncPromise;
|
|
11715
11564
|
logger;
|
|
11716
11565
|
activeStreams;
|
|
11566
|
+
connectionMayHaveChanged = false;
|
|
11717
11567
|
isUploadingCrud = false;
|
|
11718
11568
|
notifyCompletedUploads;
|
|
11719
11569
|
handleActiveStreamsChange;
|
|
@@ -11793,9 +11643,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
11793
11643
|
this.crudUpdateListener = undefined;
|
|
11794
11644
|
this.uploadAbortController?.abort();
|
|
11795
11645
|
}
|
|
11796
|
-
async hasCompletedSync() {
|
|
11797
|
-
return this.options.adapter.hasCompletedSync();
|
|
11798
|
-
}
|
|
11799
11646
|
async getWriteCheckpoint() {
|
|
11800
11647
|
const clientId = await this.options.adapter.getClientId();
|
|
11801
11648
|
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
@@ -11877,7 +11724,7 @@ The next upload iteration will be delayed.`);
|
|
|
11877
11724
|
});
|
|
11878
11725
|
}
|
|
11879
11726
|
}
|
|
11880
|
-
this.uploadAbortController =
|
|
11727
|
+
this.uploadAbortController = undefined;
|
|
11881
11728
|
}
|
|
11882
11729
|
});
|
|
11883
11730
|
}
|
|
@@ -11993,6 +11840,11 @@ The next upload iteration will be delayed.`);
|
|
|
11993
11840
|
shouldDelayRetry = false;
|
|
11994
11841
|
// A disconnect was requested, we should not delay since there is no explicit retry
|
|
11995
11842
|
}
|
|
11843
|
+
else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
|
|
11844
|
+
this.connectionMayHaveChanged = false;
|
|
11845
|
+
this.logger.info('Sync error after changed connection, retrying immediately');
|
|
11846
|
+
shouldDelayRetry = false;
|
|
11847
|
+
}
|
|
11996
11848
|
else {
|
|
11997
11849
|
this.logger.error(ex);
|
|
11998
11850
|
}
|
|
@@ -12023,17 +11875,14 @@ The next upload iteration will be delayed.`);
|
|
|
12023
11875
|
// Mark as disconnected if here
|
|
12024
11876
|
this.updateSyncStatus({ connected: false, connecting: false });
|
|
12025
11877
|
}
|
|
12026
|
-
|
|
12027
|
-
|
|
12028
|
-
|
|
12029
|
-
|
|
12030
|
-
|
|
12031
|
-
|
|
12032
|
-
|
|
12033
|
-
|
|
12034
|
-
localDescriptions.set(entry.bucket, null);
|
|
12035
|
-
}
|
|
12036
|
-
return [req, localDescriptions];
|
|
11878
|
+
markConnectionMayHaveChanged() {
|
|
11879
|
+
// By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
|
|
11880
|
+
// an active sync iteration on the connection in use.
|
|
11881
|
+
this.connectionMayHaveChanged = true;
|
|
11882
|
+
// This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
|
|
11883
|
+
// make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
|
|
11884
|
+
// iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
|
|
11885
|
+
this.handleActiveStreamsChange?.();
|
|
12037
11886
|
}
|
|
12038
11887
|
/**
|
|
12039
11888
|
* Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
|
|
@@ -12074,344 +11923,98 @@ The next upload iteration will be delayed.`);
|
|
|
12074
11923
|
if (invalidMetadata.length > 0) {
|
|
12075
11924
|
throw new Error(`Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`);
|
|
12076
11925
|
}
|
|
12077
|
-
|
|
12078
|
-
this.
|
|
12079
|
-
if (clientImplementation == SyncClientImplementation.JAVASCRIPT) {
|
|
12080
|
-
await this.legacyStreamingSyncIteration(signal, resolvedOptions);
|
|
12081
|
-
return null;
|
|
12082
|
-
}
|
|
12083
|
-
else {
|
|
12084
|
-
await this.requireKeyFormat(true);
|
|
12085
|
-
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
12086
|
-
}
|
|
11926
|
+
await this.requireKeyFormat(true);
|
|
11927
|
+
return await this.rustSyncIteration(signal, resolvedOptions);
|
|
12087
11928
|
}
|
|
12088
11929
|
});
|
|
12089
11930
|
}
|
|
12090
|
-
|
|
12091
|
-
const
|
|
12092
|
-
|
|
12093
|
-
|
|
12094
|
-
|
|
12095
|
-
|
|
12096
|
-
this.logger.error('Sync streams require `clientImplementation: SyncClientImplementation.RUST` when connecting.');
|
|
12097
|
-
}
|
|
12098
|
-
this.logger.debug('Streaming sync iteration started');
|
|
12099
|
-
this.options.adapter.startSession();
|
|
12100
|
-
let [req, bucketMap] = await this.collectLocalBucketState();
|
|
12101
|
-
let targetCheckpoint = null;
|
|
12102
|
-
// A checkpoint that has been validated but not applied (e.g. due to pending local writes)
|
|
12103
|
-
let pendingValidatedCheckpoint = null;
|
|
12104
|
-
const clientId = await this.options.adapter.getClientId();
|
|
12105
|
-
const usingFixedKeyFormat = await this.requireKeyFormat(false);
|
|
12106
|
-
this.logger.debug('Requesting stream from server');
|
|
12107
|
-
const syncOptions = {
|
|
12108
|
-
path: '/sync/stream',
|
|
12109
|
-
abortSignal: signal,
|
|
12110
|
-
data: {
|
|
12111
|
-
buckets: req,
|
|
12112
|
-
include_checksum: true,
|
|
12113
|
-
raw_data: true,
|
|
12114
|
-
parameters: resolvedOptions.params,
|
|
12115
|
-
app_metadata: resolvedOptions.appMetadata,
|
|
12116
|
-
client_id: clientId
|
|
11931
|
+
receiveSyncLines(data) {
|
|
11932
|
+
const { options, connection } = data;
|
|
11933
|
+
const remote = this.options.remote;
|
|
11934
|
+
const openInner = async () => {
|
|
11935
|
+
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
11936
|
+
return await remote.fetchStream(options);
|
|
12117
11937
|
}
|
|
12118
|
-
|
|
12119
|
-
|
|
12120
|
-
|
|
12121
|
-
|
|
12122
|
-
|
|
12123
|
-
return JSON.parse(line);
|
|
12124
|
-
}
|
|
12125
|
-
else {
|
|
12126
|
-
// Directly enqueued by us
|
|
12127
|
-
return line;
|
|
12128
|
-
}
|
|
12129
|
-
});
|
|
12130
|
-
}
|
|
12131
|
-
else {
|
|
12132
|
-
const bson = await this.options.remote.getBSON();
|
|
12133
|
-
stream = await this.options.remote.socketStreamRaw({
|
|
12134
|
-
...syncOptions,
|
|
12135
|
-
...{ fetchStrategy: resolvedOptions.fetchStrategy }
|
|
12136
|
-
}, (payload) => {
|
|
12137
|
-
if (payload instanceof Uint8Array) {
|
|
12138
|
-
return bson.deserialize(payload);
|
|
12139
|
-
}
|
|
12140
|
-
else {
|
|
12141
|
-
// Directly enqueued by us
|
|
12142
|
-
return payload;
|
|
12143
|
-
}
|
|
12144
|
-
}, bson);
|
|
12145
|
-
}
|
|
12146
|
-
this.logger.debug('Stream established. Processing events');
|
|
12147
|
-
this.notifyCompletedUploads = () => {
|
|
12148
|
-
if (!stream.closed) {
|
|
12149
|
-
stream.enqueueData({ crud_upload_completed: null });
|
|
11938
|
+
else {
|
|
11939
|
+
return await this.options.remote.socketStreamRaw({
|
|
11940
|
+
...options,
|
|
11941
|
+
...{ fetchStrategy: connection.fetchStrategy }
|
|
11942
|
+
});
|
|
12150
11943
|
}
|
|
12151
11944
|
};
|
|
12152
|
-
|
|
12153
|
-
|
|
12154
|
-
|
|
12155
|
-
|
|
12156
|
-
|
|
12157
|
-
|
|
12158
|
-
if ('crud_upload_completed' in line) {
|
|
12159
|
-
if (pendingValidatedCheckpoint != null) {
|
|
12160
|
-
const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
|
|
12161
|
-
if (applied) {
|
|
12162
|
-
pendingValidatedCheckpoint = null;
|
|
12163
|
-
}
|
|
12164
|
-
else if (endIteration) {
|
|
12165
|
-
break;
|
|
12166
|
-
}
|
|
11945
|
+
let inner;
|
|
11946
|
+
let done = false;
|
|
11947
|
+
return {
|
|
11948
|
+
async next() {
|
|
11949
|
+
if (done) {
|
|
11950
|
+
return doneResult;
|
|
12167
11951
|
}
|
|
12168
|
-
|
|
12169
|
-
|
|
12170
|
-
|
|
12171
|
-
|
|
12172
|
-
|
|
12173
|
-
|
|
12174
|
-
this.updateSyncStatus({
|
|
12175
|
-
connected: true
|
|
12176
|
-
});
|
|
12177
|
-
}
|
|
12178
|
-
if (isStreamingSyncCheckpoint(line)) {
|
|
12179
|
-
targetCheckpoint = line.checkpoint;
|
|
12180
|
-
// New checkpoint - existing validated checkpoint is no longer valid
|
|
12181
|
-
pendingValidatedCheckpoint = null;
|
|
12182
|
-
const bucketsToDelete = new Set(bucketMap.keys());
|
|
12183
|
-
const newBuckets = new Map();
|
|
12184
|
-
for (const checksum of line.checkpoint.buckets) {
|
|
12185
|
-
newBuckets.set(checksum.bucket, {
|
|
12186
|
-
name: checksum.bucket,
|
|
12187
|
-
priority: checksum.priority ?? FALLBACK_PRIORITY
|
|
11952
|
+
else if (inner == null) {
|
|
11953
|
+
inner = await openInner();
|
|
11954
|
+
// We're connected here, so we can tell the core extension about it.
|
|
11955
|
+
return valueResult({
|
|
11956
|
+
command: PowerSyncControlCommand.CONNECTION_STATE,
|
|
11957
|
+
payload: 'established'
|
|
12188
11958
|
});
|
|
12189
|
-
bucketsToDelete.delete(checksum.bucket);
|
|
12190
|
-
}
|
|
12191
|
-
if (bucketsToDelete.size > 0) {
|
|
12192
|
-
this.logger.debug('Removing buckets', [...bucketsToDelete]);
|
|
12193
|
-
}
|
|
12194
|
-
bucketMap = newBuckets;
|
|
12195
|
-
await this.options.adapter.removeBuckets([...bucketsToDelete]);
|
|
12196
|
-
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
|
|
12197
|
-
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
|
|
12198
|
-
}
|
|
12199
|
-
else if (isStreamingSyncCheckpointComplete(line)) {
|
|
12200
|
-
const result = await this.applyCheckpoint(targetCheckpoint);
|
|
12201
|
-
if (result.endIteration) {
|
|
12202
|
-
return;
|
|
12203
|
-
}
|
|
12204
|
-
else if (!result.applied) {
|
|
12205
|
-
// "Could not apply checkpoint due to local data". We need to retry after
|
|
12206
|
-
// finishing uploads.
|
|
12207
|
-
pendingValidatedCheckpoint = targetCheckpoint;
|
|
12208
|
-
}
|
|
12209
|
-
else {
|
|
12210
|
-
// Nothing to retry later. This would likely already be null from the last
|
|
12211
|
-
// checksum or checksum_diff operation, but we make sure.
|
|
12212
|
-
pendingValidatedCheckpoint = null;
|
|
12213
11959
|
}
|
|
12214
|
-
}
|
|
12215
|
-
else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
|
|
12216
|
-
const priority = line.partial_checkpoint_complete.priority;
|
|
12217
|
-
this.logger.debug('Partial checkpoint complete', priority);
|
|
12218
|
-
const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint, priority);
|
|
12219
|
-
if (!result.checkpointValid) {
|
|
12220
|
-
// This means checksums failed. Start again with a new checkpoint.
|
|
12221
|
-
// TODO: better back-off
|
|
12222
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
12223
|
-
return;
|
|
12224
|
-
}
|
|
12225
|
-
else if (!result.ready) ;
|
|
12226
11960
|
else {
|
|
12227
|
-
|
|
12228
|
-
|
|
12229
|
-
|
|
12230
|
-
|
|
12231
|
-
priorityStates.push({
|
|
12232
|
-
priority,
|
|
12233
|
-
lastSyncedAt: new Date(),
|
|
12234
|
-
hasSynced: true
|
|
12235
|
-
});
|
|
12236
|
-
this.updateSyncStatus({
|
|
12237
|
-
connected: true,
|
|
12238
|
-
priorityStatusEntries: priorityStates
|
|
12239
|
-
});
|
|
12240
|
-
}
|
|
12241
|
-
}
|
|
12242
|
-
else if (isStreamingSyncCheckpointDiff(line)) {
|
|
12243
|
-
// TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
|
|
12244
|
-
if (targetCheckpoint == null) {
|
|
12245
|
-
throw new Error('Checkpoint diff without previous checkpoint');
|
|
12246
|
-
}
|
|
12247
|
-
// New checkpoint - existing validated checkpoint is no longer valid
|
|
12248
|
-
pendingValidatedCheckpoint = null;
|
|
12249
|
-
const diff = line.checkpoint_diff;
|
|
12250
|
-
const newBuckets = new Map();
|
|
12251
|
-
for (const checksum of targetCheckpoint.buckets) {
|
|
12252
|
-
newBuckets.set(checksum.bucket, checksum);
|
|
12253
|
-
}
|
|
12254
|
-
for (const checksum of diff.updated_buckets) {
|
|
12255
|
-
newBuckets.set(checksum.bucket, checksum);
|
|
12256
|
-
}
|
|
12257
|
-
for (const bucket of diff.removed_buckets) {
|
|
12258
|
-
newBuckets.delete(bucket);
|
|
12259
|
-
}
|
|
12260
|
-
const newCheckpoint = {
|
|
12261
|
-
last_op_id: diff.last_op_id,
|
|
12262
|
-
buckets: [...newBuckets.values()],
|
|
12263
|
-
write_checkpoint: diff.write_checkpoint
|
|
12264
|
-
};
|
|
12265
|
-
targetCheckpoint = newCheckpoint;
|
|
12266
|
-
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
|
|
12267
|
-
bucketMap = new Map();
|
|
12268
|
-
newBuckets.forEach((checksum, name) => bucketMap.set(name, {
|
|
12269
|
-
name: checksum.bucket,
|
|
12270
|
-
priority: checksum.priority ?? FALLBACK_PRIORITY
|
|
12271
|
-
}));
|
|
12272
|
-
const bucketsToDelete = diff.removed_buckets;
|
|
12273
|
-
if (bucketsToDelete.length > 0) {
|
|
12274
|
-
this.logger.debug('Remove buckets', bucketsToDelete);
|
|
12275
|
-
}
|
|
12276
|
-
await this.options.adapter.removeBuckets(bucketsToDelete);
|
|
12277
|
-
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
|
|
12278
|
-
}
|
|
12279
|
-
else if (isStreamingSyncData(line)) {
|
|
12280
|
-
const { data } = line;
|
|
12281
|
-
const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
|
|
12282
|
-
let updatedProgress = null;
|
|
12283
|
-
if (previousProgress) {
|
|
12284
|
-
updatedProgress = { ...previousProgress };
|
|
12285
|
-
const progressForBucket = updatedProgress[data.bucket];
|
|
12286
|
-
if (progressForBucket) {
|
|
12287
|
-
updatedProgress[data.bucket] = {
|
|
12288
|
-
...progressForBucket,
|
|
12289
|
-
since_last: progressForBucket.since_last + data.data.length
|
|
12290
|
-
};
|
|
11961
|
+
const event = await inner.next();
|
|
11962
|
+
if (event.done) {
|
|
11963
|
+
done = true;
|
|
11964
|
+
return valueResult({ command: PowerSyncControlCommand.CONNECTION_STATE, payload: 'end' });
|
|
12291
11965
|
}
|
|
12292
|
-
|
|
12293
|
-
|
|
12294
|
-
|
|
12295
|
-
|
|
12296
|
-
|
|
11966
|
+
else {
|
|
11967
|
+
return valueResult({
|
|
11968
|
+
command: typeof event.value == 'string'
|
|
11969
|
+
? PowerSyncControlCommand.PROCESS_TEXT_LINE
|
|
11970
|
+
: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
11971
|
+
payload: event.value
|
|
11972
|
+
});
|
|
12297
11973
|
}
|
|
12298
|
-
});
|
|
12299
|
-
await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
|
|
12300
|
-
}
|
|
12301
|
-
else if (isStreamingKeepalive(line)) {
|
|
12302
|
-
const remaining_seconds = line.token_expires_in;
|
|
12303
|
-
if (remaining_seconds == 0) {
|
|
12304
|
-
// Connection would be closed automatically right after this
|
|
12305
|
-
this.logger.debug('Token expiring; reconnect');
|
|
12306
|
-
/**
|
|
12307
|
-
* For a rare case where the backend connector does not update the token
|
|
12308
|
-
* (uses the same one), this should have some delay.
|
|
12309
|
-
*/
|
|
12310
|
-
await this.delayRetry();
|
|
12311
|
-
return;
|
|
12312
|
-
}
|
|
12313
|
-
else if (remaining_seconds < 30) {
|
|
12314
|
-
this.logger.debug('Token will expire soon; reconnect');
|
|
12315
|
-
// Pre-emptively refresh the token
|
|
12316
|
-
this.options.remote.invalidateCredentials();
|
|
12317
|
-
return;
|
|
12318
11974
|
}
|
|
12319
|
-
this.triggerCrudUpload();
|
|
12320
11975
|
}
|
|
12321
|
-
|
|
12322
|
-
this.logger.debug('Received unknown sync line', line);
|
|
12323
|
-
}
|
|
12324
|
-
}
|
|
12325
|
-
this.logger.debug('Stream input empty');
|
|
12326
|
-
// Connection closed. Likely due to auth issue.
|
|
12327
|
-
return;
|
|
11976
|
+
};
|
|
12328
11977
|
}
|
|
12329
11978
|
async rustSyncIteration(signal, resolvedOptions) {
|
|
12330
11979
|
const syncImplementation = this;
|
|
12331
11980
|
const adapter = this.options.adapter;
|
|
12332
11981
|
const remote = this.options.remote;
|
|
12333
|
-
let receivingLines = null;
|
|
12334
|
-
let hadSyncLine = false;
|
|
12335
11982
|
let hideDisconnectOnRestart = false;
|
|
11983
|
+
let notifyTokenRefreshed;
|
|
12336
11984
|
if (signal.aborted) {
|
|
12337
11985
|
throw new AbortOperation('Connection request has been aborted');
|
|
12338
11986
|
}
|
|
12339
|
-
|
|
12340
|
-
|
|
12341
|
-
|
|
12342
|
-
|
|
12343
|
-
|
|
12344
|
-
|
|
12345
|
-
async function connect(instr) {
|
|
12346
|
-
const syncOptions = {
|
|
12347
|
-
path: '/sync/stream',
|
|
12348
|
-
abortSignal: abortController.signal,
|
|
12349
|
-
data: instr.request
|
|
11987
|
+
function startCommand() {
|
|
11988
|
+
const options = {
|
|
11989
|
+
parameters: resolvedOptions.params,
|
|
11990
|
+
app_metadata: resolvedOptions.appMetadata,
|
|
11991
|
+
active_streams: syncImplementation.activeStreams,
|
|
11992
|
+
include_defaults: resolvedOptions.includeDefaultStreams
|
|
12350
11993
|
};
|
|
12351
|
-
if (resolvedOptions.
|
|
12352
|
-
|
|
12353
|
-
if (typeof line == 'string') {
|
|
12354
|
-
return {
|
|
12355
|
-
command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
|
|
12356
|
-
payload: line
|
|
12357
|
-
};
|
|
12358
|
-
}
|
|
12359
|
-
else {
|
|
12360
|
-
// Directly enqueued by us
|
|
12361
|
-
return line;
|
|
12362
|
-
}
|
|
12363
|
-
});
|
|
12364
|
-
}
|
|
12365
|
-
else {
|
|
12366
|
-
controlInvocations = await remote.socketStreamRaw({
|
|
12367
|
-
...syncOptions,
|
|
12368
|
-
fetchStrategy: resolvedOptions.fetchStrategy
|
|
12369
|
-
}, (payload) => {
|
|
12370
|
-
if (payload instanceof Uint8Array) {
|
|
12371
|
-
return {
|
|
12372
|
-
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
12373
|
-
payload: payload
|
|
12374
|
-
};
|
|
12375
|
-
}
|
|
12376
|
-
else {
|
|
12377
|
-
// Directly enqueued by us
|
|
12378
|
-
return payload;
|
|
12379
|
-
}
|
|
12380
|
-
});
|
|
12381
|
-
}
|
|
12382
|
-
// The rust client will set connected: true after the first sync line because that's when it gets invoked, but
|
|
12383
|
-
// we're already connected here and can report that.
|
|
12384
|
-
syncImplementation.updateSyncStatus({ connected: true });
|
|
12385
|
-
try {
|
|
12386
|
-
while (!controlInvocations.closed) {
|
|
12387
|
-
const line = await controlInvocations.read();
|
|
12388
|
-
if (line == null) {
|
|
12389
|
-
return;
|
|
12390
|
-
}
|
|
12391
|
-
await control(line.command, line.payload);
|
|
12392
|
-
if (!hadSyncLine) {
|
|
12393
|
-
syncImplementation.triggerCrudUpload();
|
|
12394
|
-
hadSyncLine = true;
|
|
12395
|
-
}
|
|
12396
|
-
}
|
|
12397
|
-
}
|
|
12398
|
-
finally {
|
|
12399
|
-
const activeInstructions = controlInvocations;
|
|
12400
|
-
// We concurrently add events to the active data stream when e.g. a CRUD upload is completed or a token is
|
|
12401
|
-
// refreshed. That would throw after closing (and we can't handle those events either way), so set this back
|
|
12402
|
-
// to null.
|
|
12403
|
-
controlInvocations = null;
|
|
12404
|
-
await activeInstructions.close();
|
|
11994
|
+
if (resolvedOptions.serializedSchema) {
|
|
11995
|
+
options.schema = resolvedOptions.serializedSchema;
|
|
12405
11996
|
}
|
|
11997
|
+
return invokePowerSyncControl(PowerSyncControlCommand.START, JSON.stringify(options));
|
|
12406
11998
|
}
|
|
12407
11999
|
async function stop() {
|
|
12408
|
-
await
|
|
12000
|
+
const instructions = await invokePowerSyncControl(PowerSyncControlCommand.STOP);
|
|
12001
|
+
for (const instruction of instructions) {
|
|
12002
|
+
// We don't need to handle interrupting instructions since we're unconditionally ending the sync iteration at
|
|
12003
|
+
// this point.
|
|
12004
|
+
if (isInterruptingInstruction(instruction))
|
|
12005
|
+
continue;
|
|
12006
|
+
await handleInstruction(instruction);
|
|
12007
|
+
}
|
|
12409
12008
|
}
|
|
12410
|
-
async function
|
|
12009
|
+
async function invokePowerSyncControl(op, payload) {
|
|
12411
12010
|
const rawResponse = await adapter.control(op, payload ?? null);
|
|
12412
12011
|
const logger = syncImplementation.logger;
|
|
12413
12012
|
logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
|
|
12414
|
-
|
|
12013
|
+
if (op != PowerSyncControlCommand.STOP) {
|
|
12014
|
+
// Evidently we have a working connection here, otherwise powersync_control would have failed.
|
|
12015
|
+
syncImplementation.connectionMayHaveChanged = false;
|
|
12016
|
+
}
|
|
12017
|
+
return JSON.parse(rawResponse);
|
|
12415
12018
|
}
|
|
12416
12019
|
async function handleInstruction(instruction) {
|
|
12417
12020
|
if ('LogLine' in instruction) {
|
|
@@ -12430,13 +12033,6 @@ The next upload iteration will be delayed.`);
|
|
|
12430
12033
|
else if ('UpdateSyncStatus' in instruction) {
|
|
12431
12034
|
syncImplementation.updateSyncStatus(coreStatusToJs(instruction.UpdateSyncStatus.status));
|
|
12432
12035
|
}
|
|
12433
|
-
else if ('EstablishSyncStream' in instruction) {
|
|
12434
|
-
if (receivingLines != null) {
|
|
12435
|
-
// Already connected, this shouldn't happen during a single iteration.
|
|
12436
|
-
throw 'Unexpected request to establish sync stream, already connected';
|
|
12437
|
-
}
|
|
12438
|
-
receivingLines = connect(instruction.EstablishSyncStream);
|
|
12439
|
-
}
|
|
12440
12036
|
else if ('FetchCredentials' in instruction) {
|
|
12441
12037
|
if (instruction.FetchCredentials.did_expire) {
|
|
12442
12038
|
remote.invalidateCredentials();
|
|
@@ -12445,16 +12041,12 @@ The next upload iteration will be delayed.`);
|
|
|
12445
12041
|
remote.invalidateCredentials();
|
|
12446
12042
|
// Restart iteration after the credentials have been refreshed.
|
|
12447
12043
|
remote.fetchCredentials().then((_) => {
|
|
12448
|
-
|
|
12044
|
+
notifyTokenRefreshed?.();
|
|
12449
12045
|
}, (err) => {
|
|
12450
12046
|
syncImplementation.logger.warn('Could not prefetch credentials', err);
|
|
12451
12047
|
});
|
|
12452
12048
|
}
|
|
12453
12049
|
}
|
|
12454
|
-
else if ('CloseSyncStream' in instruction) {
|
|
12455
|
-
abortController.abort();
|
|
12456
|
-
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
12457
|
-
}
|
|
12458
12050
|
else if ('FlushFileSystem' in instruction) ;
|
|
12459
12051
|
else if ('DidCompleteSync' in instruction) {
|
|
12460
12052
|
syncImplementation.updateSyncStatus({
|
|
@@ -12464,105 +12056,83 @@ The next upload iteration will be delayed.`);
|
|
|
12464
12056
|
});
|
|
12465
12057
|
}
|
|
12466
12058
|
}
|
|
12467
|
-
async function handleInstructions(instructions) {
|
|
12468
|
-
for (const instr of instructions) {
|
|
12469
|
-
await handleInstruction(instr);
|
|
12470
|
-
}
|
|
12471
|
-
}
|
|
12472
12059
|
try {
|
|
12473
|
-
const
|
|
12474
|
-
|
|
12475
|
-
|
|
12476
|
-
|
|
12477
|
-
|
|
12478
|
-
|
|
12479
|
-
|
|
12480
|
-
|
|
12060
|
+
const defaultResult = { immediateRestart: false };
|
|
12061
|
+
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
|
|
12062
|
+
// invocation (local events include refreshed tokens and completed uploads).
|
|
12063
|
+
// This is a single data stream so that we can handle all control calls from a single place.
|
|
12064
|
+
let controlInvocations = null;
|
|
12065
|
+
for (const startInstruction of await startCommand()) {
|
|
12066
|
+
if ('EstablishSyncStream' in startInstruction) {
|
|
12067
|
+
const syncOptions = {
|
|
12068
|
+
path: '/sync/stream',
|
|
12069
|
+
abortSignal: signal,
|
|
12070
|
+
data: startInstruction.EstablishSyncStream.request
|
|
12071
|
+
};
|
|
12072
|
+
controlInvocations = injectable(syncImplementation.receiveSyncLines({
|
|
12073
|
+
options: syncOptions,
|
|
12074
|
+
connection: resolvedOptions
|
|
12075
|
+
}));
|
|
12076
|
+
}
|
|
12077
|
+
else if ('CloseSyncStream' in startInstruction) {
|
|
12078
|
+
return defaultResult;
|
|
12079
|
+
}
|
|
12080
|
+
else {
|
|
12081
|
+
await handleInstruction(startInstruction);
|
|
12082
|
+
}
|
|
12481
12083
|
}
|
|
12482
|
-
|
|
12084
|
+
if (controlInvocations == null)
|
|
12085
|
+
return defaultResult;
|
|
12483
12086
|
this.notifyCompletedUploads = () => {
|
|
12484
|
-
|
|
12485
|
-
controlInvocations.enqueueData({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
12486
|
-
}
|
|
12087
|
+
controlInvocations.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
12487
12088
|
};
|
|
12488
12089
|
this.handleActiveStreamsChange = () => {
|
|
12489
|
-
|
|
12490
|
-
|
|
12491
|
-
|
|
12492
|
-
|
|
12493
|
-
});
|
|
12494
|
-
}
|
|
12090
|
+
controlInvocations.inject({
|
|
12091
|
+
command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
|
|
12092
|
+
payload: JSON.stringify(this.activeStreams)
|
|
12093
|
+
});
|
|
12495
12094
|
};
|
|
12496
|
-
|
|
12095
|
+
notifyTokenRefreshed = () => {
|
|
12096
|
+
controlInvocations.inject({
|
|
12097
|
+
command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED
|
|
12098
|
+
});
|
|
12099
|
+
};
|
|
12100
|
+
let hadSyncLine = false;
|
|
12101
|
+
loop: while (true) {
|
|
12102
|
+
const { done, value } = await controlInvocations.next();
|
|
12103
|
+
if (done)
|
|
12104
|
+
break;
|
|
12105
|
+
if (!hadSyncLine) {
|
|
12106
|
+
// Trigger a local CRUD upload when the first sync line has been received, this allows uploading local changes
|
|
12107
|
+
// that have been made while offline or disconnected.
|
|
12108
|
+
if (value.command == PowerSyncControlCommand.PROCESS_TEXT_LINE ||
|
|
12109
|
+
value.command == PowerSyncControlCommand.PROCESS_BSON_LINE) {
|
|
12110
|
+
hadSyncLine = true;
|
|
12111
|
+
this.triggerCrudUpload?.();
|
|
12112
|
+
}
|
|
12113
|
+
}
|
|
12114
|
+
const instructions = await invokePowerSyncControl(value.command, value.payload);
|
|
12115
|
+
for (const instruction of instructions) {
|
|
12116
|
+
if ('EstablishSyncStream' in instruction) {
|
|
12117
|
+
throw new Error('Received EstablishSyncStream while already connected.');
|
|
12118
|
+
}
|
|
12119
|
+
else if ('CloseSyncStream' in instruction) {
|
|
12120
|
+
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
12121
|
+
break loop;
|
|
12122
|
+
}
|
|
12123
|
+
else {
|
|
12124
|
+
await handleInstruction(instruction);
|
|
12125
|
+
}
|
|
12126
|
+
}
|
|
12127
|
+
}
|
|
12497
12128
|
}
|
|
12498
12129
|
finally {
|
|
12499
12130
|
this.notifyCompletedUploads = this.handleActiveStreamsChange = undefined;
|
|
12131
|
+
notifyTokenRefreshed = undefined;
|
|
12500
12132
|
await stop();
|
|
12501
12133
|
}
|
|
12502
12134
|
return { immediateRestart: hideDisconnectOnRestart };
|
|
12503
12135
|
}
|
|
12504
|
-
async updateSyncStatusForStartingCheckpoint(checkpoint) {
|
|
12505
|
-
const localProgress = await this.options.adapter.getBucketOperationProgress();
|
|
12506
|
-
const progress = {};
|
|
12507
|
-
let invalidated = false;
|
|
12508
|
-
for (const bucket of checkpoint.buckets) {
|
|
12509
|
-
const savedProgress = localProgress[bucket.bucket];
|
|
12510
|
-
const atLast = savedProgress?.atLast ?? 0;
|
|
12511
|
-
const sinceLast = savedProgress?.sinceLast ?? 0;
|
|
12512
|
-
progress[bucket.bucket] = {
|
|
12513
|
-
// The fallback priority doesn't matter here, but 3 is the one newer versions of the sync service
|
|
12514
|
-
// will use by default.
|
|
12515
|
-
priority: bucket.priority ?? 3,
|
|
12516
|
-
at_last: atLast,
|
|
12517
|
-
since_last: sinceLast,
|
|
12518
|
-
target_count: bucket.count ?? 0
|
|
12519
|
-
};
|
|
12520
|
-
if (bucket.count != null && bucket.count < atLast + sinceLast) {
|
|
12521
|
-
// Either due to a defrag / sync rule deploy or a compaction operation, the size
|
|
12522
|
-
// of the bucket shrank so much that the local ops exceed the ops in the updated
|
|
12523
|
-
// bucket. We can't prossibly report progress in this case (it would overshoot 100%).
|
|
12524
|
-
invalidated = true;
|
|
12525
|
-
}
|
|
12526
|
-
}
|
|
12527
|
-
if (invalidated) {
|
|
12528
|
-
for (const bucket in progress) {
|
|
12529
|
-
const bucketProgress = progress[bucket];
|
|
12530
|
-
bucketProgress.at_last = 0;
|
|
12531
|
-
bucketProgress.since_last = 0;
|
|
12532
|
-
}
|
|
12533
|
-
}
|
|
12534
|
-
this.updateSyncStatus({
|
|
12535
|
-
dataFlow: {
|
|
12536
|
-
downloading: true,
|
|
12537
|
-
downloadProgress: progress
|
|
12538
|
-
}
|
|
12539
|
-
});
|
|
12540
|
-
}
|
|
12541
|
-
async applyCheckpoint(checkpoint) {
|
|
12542
|
-
let result = await this.options.adapter.syncLocalDatabase(checkpoint);
|
|
12543
|
-
if (!result.checkpointValid) {
|
|
12544
|
-
this.logger.debug(`Checksum mismatch in checkpoint ${checkpoint.last_op_id}, will reconnect`);
|
|
12545
|
-
// This means checksums failed. Start again with a new checkpoint.
|
|
12546
|
-
// TODO: better back-off
|
|
12547
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
12548
|
-
return { applied: false, endIteration: true };
|
|
12549
|
-
}
|
|
12550
|
-
else if (!result.ready) {
|
|
12551
|
-
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.`);
|
|
12552
|
-
return { applied: false, endIteration: false };
|
|
12553
|
-
}
|
|
12554
|
-
this.logger.debug(`Applied checkpoint ${checkpoint.last_op_id}`, checkpoint);
|
|
12555
|
-
this.updateSyncStatus({
|
|
12556
|
-
connected: true,
|
|
12557
|
-
lastSyncedAt: new Date(),
|
|
12558
|
-
dataFlow: {
|
|
12559
|
-
downloading: false,
|
|
12560
|
-
downloadProgress: null,
|
|
12561
|
-
downloadError: undefined
|
|
12562
|
-
}
|
|
12563
|
-
});
|
|
12564
|
-
return { applied: true, endIteration: false };
|
|
12565
|
-
}
|
|
12566
12136
|
updateSyncStatus(options) {
|
|
12567
12137
|
const updatedStatus = new SyncStatus({
|
|
12568
12138
|
connected: options.connected ?? this.syncStatus.connected,
|
|
@@ -14101,14 +13671,12 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
14101
13671
|
db;
|
|
14102
13672
|
logger;
|
|
14103
13673
|
tableNames;
|
|
14104
|
-
_hasCompletedSync;
|
|
14105
13674
|
updateListener;
|
|
14106
13675
|
_clientId;
|
|
14107
13676
|
constructor(db, logger = Logger.get('SqliteBucketStorage')) {
|
|
14108
13677
|
super();
|
|
14109
13678
|
this.db = db;
|
|
14110
13679
|
this.logger = logger;
|
|
14111
|
-
this._hasCompletedSync = false;
|
|
14112
13680
|
this.tableNames = new Set();
|
|
14113
13681
|
this.updateListener = db.registerListener({
|
|
14114
13682
|
tablesUpdated: (update) => {
|
|
@@ -14120,7 +13688,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
14120
13688
|
});
|
|
14121
13689
|
}
|
|
14122
13690
|
async init() {
|
|
14123
|
-
this._hasCompletedSync = false;
|
|
14124
13691
|
const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
|
|
14125
13692
|
for (const row of existingTableRows ?? []) {
|
|
14126
13693
|
this.tableNames.add(row.name);
|
|
@@ -14142,156 +13709,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
14142
13709
|
getMaxOpId() {
|
|
14143
13710
|
return MAX_OP_ID;
|
|
14144
13711
|
}
|
|
14145
|
-
/**
|
|
14146
|
-
* Reset any caches.
|
|
14147
|
-
*/
|
|
14148
|
-
startSession() { }
|
|
14149
|
-
async getBucketStates() {
|
|
14150
|
-
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'");
|
|
14151
|
-
return result;
|
|
14152
|
-
}
|
|
14153
|
-
async getBucketOperationProgress() {
|
|
14154
|
-
const rows = await this.db.getAll('SELECT name, count_at_last, count_since_last FROM ps_buckets');
|
|
14155
|
-
return Object.fromEntries(rows.map((r) => [r.name, { atLast: r.count_at_last, sinceLast: r.count_since_last }]));
|
|
14156
|
-
}
|
|
14157
|
-
async saveSyncData(batch, fixedKeyFormat = false) {
|
|
14158
|
-
await this.writeTransaction(async (tx) => {
|
|
14159
|
-
for (const b of batch.buckets) {
|
|
14160
|
-
await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
|
|
14161
|
-
'save',
|
|
14162
|
-
JSON.stringify({ buckets: [b.toJSON(fixedKeyFormat)] })
|
|
14163
|
-
]);
|
|
14164
|
-
this.logger.debug(`Saved batch of data for bucket: ${b.bucket}, operations: ${b.data.length}`);
|
|
14165
|
-
}
|
|
14166
|
-
});
|
|
14167
|
-
}
|
|
14168
|
-
async removeBuckets(buckets) {
|
|
14169
|
-
for (const bucket of buckets) {
|
|
14170
|
-
await this.deleteBucket(bucket);
|
|
14171
|
-
}
|
|
14172
|
-
}
|
|
14173
|
-
/**
|
|
14174
|
-
* Mark a bucket for deletion.
|
|
14175
|
-
*/
|
|
14176
|
-
async deleteBucket(bucket) {
|
|
14177
|
-
await this.writeTransaction(async (tx) => {
|
|
14178
|
-
await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', ['delete_bucket', bucket]);
|
|
14179
|
-
});
|
|
14180
|
-
this.logger.debug(`Done deleting bucket ${bucket}`);
|
|
14181
|
-
}
|
|
14182
|
-
async hasCompletedSync() {
|
|
14183
|
-
if (this._hasCompletedSync) {
|
|
14184
|
-
return true;
|
|
14185
|
-
}
|
|
14186
|
-
const r = await this.db.get(`SELECT powersync_last_synced_at() as synced_at`);
|
|
14187
|
-
const completed = r.synced_at != null;
|
|
14188
|
-
if (completed) {
|
|
14189
|
-
this._hasCompletedSync = true;
|
|
14190
|
-
}
|
|
14191
|
-
return completed;
|
|
14192
|
-
}
|
|
14193
|
-
async syncLocalDatabase(checkpoint, priority) {
|
|
14194
|
-
const r = await this.validateChecksums(checkpoint, priority);
|
|
14195
|
-
if (!r.checkpointValid) {
|
|
14196
|
-
this.logger.error('Checksums failed for', r.checkpointFailures);
|
|
14197
|
-
for (const b of r.checkpointFailures ?? []) {
|
|
14198
|
-
await this.deleteBucket(b);
|
|
14199
|
-
}
|
|
14200
|
-
return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
|
|
14201
|
-
}
|
|
14202
|
-
if (priority == null) {
|
|
14203
|
-
this.logger.debug(`Validated checksums checkpoint ${checkpoint.last_op_id}`);
|
|
14204
|
-
}
|
|
14205
|
-
else {
|
|
14206
|
-
this.logger.debug(`Validated checksums for partial checkpoint ${checkpoint.last_op_id}, priority ${priority}`);
|
|
14207
|
-
}
|
|
14208
|
-
let buckets = checkpoint.buckets;
|
|
14209
|
-
if (priority !== undefined) {
|
|
14210
|
-
buckets = buckets.filter((b) => hasMatchingPriority(priority, b));
|
|
14211
|
-
}
|
|
14212
|
-
const bucketNames = buckets.map((b) => b.bucket);
|
|
14213
|
-
await this.writeTransaction(async (tx) => {
|
|
14214
|
-
await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
|
|
14215
|
-
checkpoint.last_op_id,
|
|
14216
|
-
JSON.stringify(bucketNames)
|
|
14217
|
-
]);
|
|
14218
|
-
if (priority == null && checkpoint.write_checkpoint) {
|
|
14219
|
-
await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
|
|
14220
|
-
}
|
|
14221
|
-
});
|
|
14222
|
-
const valid = await this.updateObjectsFromBuckets(checkpoint, priority);
|
|
14223
|
-
if (!valid) {
|
|
14224
|
-
return { ready: false, checkpointValid: true };
|
|
14225
|
-
}
|
|
14226
|
-
return {
|
|
14227
|
-
ready: true,
|
|
14228
|
-
checkpointValid: true
|
|
14229
|
-
};
|
|
14230
|
-
}
|
|
14231
|
-
/**
|
|
14232
|
-
* Atomically update the local state to the current checkpoint.
|
|
14233
|
-
*
|
|
14234
|
-
* This includes creating new tables, dropping old tables, and copying data over from the oplog.
|
|
14235
|
-
*/
|
|
14236
|
-
async updateObjectsFromBuckets(checkpoint, priority) {
|
|
14237
|
-
let arg = '';
|
|
14238
|
-
if (priority !== undefined) {
|
|
14239
|
-
const affectedBuckets = [];
|
|
14240
|
-
for (const desc of checkpoint.buckets) {
|
|
14241
|
-
if (hasMatchingPriority(priority, desc)) {
|
|
14242
|
-
affectedBuckets.push(desc.bucket);
|
|
14243
|
-
}
|
|
14244
|
-
}
|
|
14245
|
-
arg = JSON.stringify({ priority, buckets: affectedBuckets });
|
|
14246
|
-
}
|
|
14247
|
-
return this.writeTransaction(async (tx) => {
|
|
14248
|
-
const { insertId: result } = await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
|
|
14249
|
-
'sync_local',
|
|
14250
|
-
arg
|
|
14251
|
-
]);
|
|
14252
|
-
if (result == 1) {
|
|
14253
|
-
if (priority == null) {
|
|
14254
|
-
const bucketToCount = Object.fromEntries(checkpoint.buckets.map((b) => [b.bucket, b.count]));
|
|
14255
|
-
// The two parameters could be replaced with one, but: https://github.com/powersync-ja/better-sqlite3/pull/6
|
|
14256
|
-
const jsonBucketCount = JSON.stringify(bucketToCount);
|
|
14257
|
-
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]);
|
|
14258
|
-
}
|
|
14259
|
-
return true;
|
|
14260
|
-
}
|
|
14261
|
-
else {
|
|
14262
|
-
return false;
|
|
14263
|
-
}
|
|
14264
|
-
});
|
|
14265
|
-
}
|
|
14266
|
-
async validateChecksums(checkpoint, priority) {
|
|
14267
|
-
if (priority !== undefined) {
|
|
14268
|
-
// Only validate the buckets within the priority we care about
|
|
14269
|
-
const newBuckets = checkpoint.buckets.filter((cs) => hasMatchingPriority(priority, cs));
|
|
14270
|
-
checkpoint = { ...checkpoint, buckets: newBuckets };
|
|
14271
|
-
}
|
|
14272
|
-
const rs = await this.db.execute('SELECT powersync_validate_checkpoint(?) as result', [
|
|
14273
|
-
JSON.stringify({ ...checkpoint })
|
|
14274
|
-
]);
|
|
14275
|
-
const resultItem = rs.rows?.item(0);
|
|
14276
|
-
if (!resultItem) {
|
|
14277
|
-
return {
|
|
14278
|
-
checkpointValid: false,
|
|
14279
|
-
ready: false,
|
|
14280
|
-
checkpointFailures: []
|
|
14281
|
-
};
|
|
14282
|
-
}
|
|
14283
|
-
const result = JSON.parse(resultItem['result']);
|
|
14284
|
-
if (result['valid']) {
|
|
14285
|
-
return { ready: true, checkpointValid: true };
|
|
14286
|
-
}
|
|
14287
|
-
else {
|
|
14288
|
-
return {
|
|
14289
|
-
checkpointValid: false,
|
|
14290
|
-
ready: false,
|
|
14291
|
-
checkpointFailures: result['failed_buckets']
|
|
14292
|
-
};
|
|
14293
|
-
}
|
|
14294
|
-
}
|
|
14295
13712
|
async updateLocalTarget(cb) {
|
|
14296
13713
|
const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)", [MAX_OP_ID]);
|
|
14297
13714
|
if (!rs1.length) {
|
|
@@ -14382,12 +13799,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
14382
13799
|
async writeTransaction(callback, options) {
|
|
14383
13800
|
return this.db.writeTransaction(callback, options);
|
|
14384
13801
|
}
|
|
14385
|
-
/**
|
|
14386
|
-
* Set a target checkpoint.
|
|
14387
|
-
*/
|
|
14388
|
-
async setTargetCheckpoint(checkpoint) {
|
|
14389
|
-
// No-op for now
|
|
14390
|
-
}
|
|
14391
13802
|
async control(op, payload) {
|
|
14392
13803
|
return await this.writeTransaction(async (tx) => {
|
|
14393
13804
|
const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
|
|
@@ -14411,20 +13822,6 @@ class SqliteBucketStorage extends BaseObserver {
|
|
|
14411
13822
|
}
|
|
14412
13823
|
static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
|
|
14413
13824
|
}
|
|
14414
|
-
function hasMatchingPriority(priority, bucket) {
|
|
14415
|
-
return bucket.priority != null && bucket.priority <= priority;
|
|
14416
|
-
}
|
|
14417
|
-
|
|
14418
|
-
// TODO JSON
|
|
14419
|
-
class SyncDataBatch {
|
|
14420
|
-
buckets;
|
|
14421
|
-
static fromJSON(json) {
|
|
14422
|
-
return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
|
|
14423
|
-
}
|
|
14424
|
-
constructor(buckets) {
|
|
14425
|
-
this.buckets = buckets;
|
|
14426
|
-
}
|
|
14427
|
-
}
|
|
14428
13825
|
|
|
14429
13826
|
/**
|
|
14430
13827
|
* Thrown when an underlying database connection is closed.
|
|
@@ -14484,10 +13881,8 @@ class Schema {
|
|
|
14484
13881
|
* developer instead of automatically by PowerSync.
|
|
14485
13882
|
* Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
|
|
14486
13883
|
* using client-side table and column constraints.
|
|
14487
|
-
* Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
|
|
14488
13884
|
*
|
|
14489
13885
|
* @param tables An object of (table name, raw table definition) entries.
|
|
14490
|
-
* @experimental Note that the raw tables API is still experimental and may change in the future.
|
|
14491
13886
|
*/
|
|
14492
13887
|
withRawTables(tables) {
|
|
14493
13888
|
for (const [name, rawTableDefinition] of Object.entries(tables)) {
|
|
@@ -14684,5 +14079,5 @@ const parseQuery = (query, parameters) => {
|
|
|
14684
14079
|
return { sqlStatement, parameters: parameters };
|
|
14685
14080
|
};
|
|
14686
14081
|
|
|
14687
|
-
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,
|
|
14082
|
+
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 };
|
|
14688
14083
|
//# sourceMappingURL=bundle.mjs.map
|