@powersync/common 1.50.0 → 1.52.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 +558 -481
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +558 -480
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +556 -481
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +556 -480
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +73 -73
- package/lib/client/AbstractPowerSyncDatabase.js +3 -3
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +29 -8
- package/lib/client/sync/stream/AbstractRemote.js +154 -177
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +69 -88
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +0 -1
- 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/mutex.d.ts +32 -3
- package/lib/utils/mutex.js +85 -36
- package/lib/utils/mutex.js.map +1 -1
- package/lib/utils/queue.d.ts +16 -0
- package/lib/utils/queue.js +42 -0
- package/lib/utils/queue.js.map +1 -0
- 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 +9 -7
- package/src/client/AbstractPowerSyncDatabase.ts +3 -3
- package/src/client/sync/stream/AbstractRemote.ts +182 -206
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +82 -83
- package/src/index.ts +1 -1
- package/src/utils/async.ts +0 -11
- package/src/utils/mutex.ts +111 -48
- package/src/utils/queue.ts +48 -0
- package/src/utils/stream_transform.ts +252 -0
- 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/utils/DataStream.ts +0 -222
package/dist/bundle.mjs
CHANGED
|
@@ -780,19 +780,69 @@ class SyncingService {
|
|
|
780
780
|
}
|
|
781
781
|
|
|
782
782
|
/**
|
|
783
|
-
*
|
|
783
|
+
* A simple fixed-capacity queue implementation.
|
|
784
|
+
*
|
|
785
|
+
* Unlike a naive queue implemented by `array.push()` and `array.shift()`, this avoids moving array elements around
|
|
786
|
+
* and is `O(1)` for {@link addLast} and {@link removeFirst}.
|
|
787
|
+
*/
|
|
788
|
+
class Queue {
|
|
789
|
+
table;
|
|
790
|
+
// Index of the first element in the table.
|
|
791
|
+
head;
|
|
792
|
+
// Amount of items currently in the queue.
|
|
793
|
+
_length;
|
|
794
|
+
constructor(initialItems) {
|
|
795
|
+
this.table = [...initialItems];
|
|
796
|
+
this.head = 0;
|
|
797
|
+
this._length = this.table.length;
|
|
798
|
+
}
|
|
799
|
+
get isEmpty() {
|
|
800
|
+
return this.length == 0;
|
|
801
|
+
}
|
|
802
|
+
get length() {
|
|
803
|
+
return this._length;
|
|
804
|
+
}
|
|
805
|
+
removeFirst() {
|
|
806
|
+
if (this.isEmpty) {
|
|
807
|
+
throw new Error('Queue is empty');
|
|
808
|
+
}
|
|
809
|
+
const result = this.table[this.head];
|
|
810
|
+
this._length--;
|
|
811
|
+
this.table[this.head] = undefined;
|
|
812
|
+
this.head = (this.head + 1) % this.table.length;
|
|
813
|
+
return result;
|
|
814
|
+
}
|
|
815
|
+
addLast(element) {
|
|
816
|
+
if (this.length == this.table.length) {
|
|
817
|
+
throw new Error('Queue is full');
|
|
818
|
+
}
|
|
819
|
+
this.table[(this.head + this._length) % this.table.length] = element;
|
|
820
|
+
this._length++;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* An asynchronous semaphore implementation with associated items per lease.
|
|
784
826
|
*
|
|
785
827
|
* @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
|
|
786
828
|
*/
|
|
787
|
-
class
|
|
788
|
-
|
|
829
|
+
class Semaphore {
|
|
830
|
+
// Available items that are not currently assigned to a waiter.
|
|
831
|
+
available;
|
|
832
|
+
size;
|
|
789
833
|
// Linked list of waiters. We don't expect the wait list to become particularly large, and this allows removing
|
|
790
834
|
// aborted waiters from the middle of the list efficiently.
|
|
791
835
|
firstWaiter;
|
|
792
836
|
lastWaiter;
|
|
793
|
-
|
|
837
|
+
constructor(elements) {
|
|
838
|
+
this.available = new Queue(elements);
|
|
839
|
+
this.size = this.available.length;
|
|
840
|
+
}
|
|
841
|
+
addWaiter(requestedItems, onAcquire) {
|
|
794
842
|
const node = {
|
|
795
843
|
isActive: true,
|
|
844
|
+
acquiredItems: [],
|
|
845
|
+
remainingItems: requestedItems,
|
|
796
846
|
onAcquire,
|
|
797
847
|
prev: this.lastWaiter
|
|
798
848
|
};
|
|
@@ -818,52 +868,92 @@ class Mutex {
|
|
|
818
868
|
if (waiter == this.lastWaiter)
|
|
819
869
|
this.lastWaiter = prev;
|
|
820
870
|
}
|
|
821
|
-
|
|
871
|
+
requestPermits(amount, abort) {
|
|
872
|
+
if (amount <= 0 || amount > this.size) {
|
|
873
|
+
throw new Error(`Invalid amount of items requested (${amount}), must be between 1 and ${this.size}`);
|
|
874
|
+
}
|
|
822
875
|
return new Promise((resolve, reject) => {
|
|
823
876
|
function rejectAborted() {
|
|
824
|
-
reject(abort?.reason ?? new Error('
|
|
877
|
+
reject(abort?.reason ?? new Error('Semaphore acquire aborted'));
|
|
825
878
|
}
|
|
826
879
|
if (abort?.aborted) {
|
|
827
880
|
return rejectAborted();
|
|
828
881
|
}
|
|
829
|
-
let
|
|
882
|
+
let waiter;
|
|
830
883
|
const markCompleted = () => {
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
884
|
+
const items = waiter.acquiredItems;
|
|
885
|
+
waiter.acquiredItems = []; // Avoid releasing items twice.
|
|
886
|
+
for (const element of items) {
|
|
887
|
+
// Give to next waiter, if possible.
|
|
888
|
+
const nextWaiter = this.firstWaiter;
|
|
889
|
+
if (nextWaiter) {
|
|
890
|
+
nextWaiter.acquiredItems.push(element);
|
|
891
|
+
nextWaiter.remainingItems--;
|
|
892
|
+
if (nextWaiter.remainingItems == 0) {
|
|
893
|
+
nextWaiter.onAcquire();
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
else {
|
|
897
|
+
// No pending waiter, return lease into pool.
|
|
898
|
+
this.available.addLast(element);
|
|
899
|
+
}
|
|
839
900
|
}
|
|
840
|
-
|
|
841
|
-
|
|
901
|
+
};
|
|
902
|
+
const onAbort = () => {
|
|
903
|
+
abort?.removeEventListener('abort', onAbort);
|
|
904
|
+
if (waiter.isActive) {
|
|
905
|
+
this.deactivateWaiter(waiter);
|
|
906
|
+
rejectAborted();
|
|
842
907
|
}
|
|
843
908
|
};
|
|
844
|
-
|
|
845
|
-
this.
|
|
846
|
-
|
|
847
|
-
|
|
909
|
+
const resolvePromise = () => {
|
|
910
|
+
this.deactivateWaiter(waiter);
|
|
911
|
+
abort?.removeEventListener('abort', onAbort);
|
|
912
|
+
const items = waiter.acquiredItems;
|
|
913
|
+
resolve({ items, release: markCompleted });
|
|
914
|
+
};
|
|
915
|
+
waiter = this.addWaiter(amount, resolvePromise);
|
|
916
|
+
// If there are items in the pool that haven't been assigned, we can pull them into this waiter. Note that this is
|
|
917
|
+
// only the case if we're the first waiter (otherwise, items would have been assigned to an earlier waiter).
|
|
918
|
+
while (!this.available.isEmpty && waiter.remainingItems > 0) {
|
|
919
|
+
waiter.acquiredItems.push(this.available.removeFirst());
|
|
920
|
+
waiter.remainingItems--;
|
|
848
921
|
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
const onAbort = () => {
|
|
852
|
-
abort?.removeEventListener('abort', onAbort);
|
|
853
|
-
if (node.isActive) {
|
|
854
|
-
this.deactivateWaiter(node);
|
|
855
|
-
rejectAborted();
|
|
856
|
-
}
|
|
857
|
-
};
|
|
858
|
-
node = this.addWaiter(() => {
|
|
859
|
-
abort?.removeEventListener('abort', onAbort);
|
|
860
|
-
holdsMutex = true;
|
|
861
|
-
resolve(markCompleted);
|
|
862
|
-
});
|
|
863
|
-
abort?.addEventListener('abort', onAbort);
|
|
922
|
+
if (waiter.remainingItems == 0) {
|
|
923
|
+
return resolvePromise();
|
|
864
924
|
}
|
|
925
|
+
abort?.addEventListener('abort', onAbort);
|
|
865
926
|
});
|
|
866
927
|
}
|
|
928
|
+
/**
|
|
929
|
+
* Requests a single item from the pool.
|
|
930
|
+
*
|
|
931
|
+
* The returned `release` callback must be invoked to return the item into the pool.
|
|
932
|
+
*/
|
|
933
|
+
async requestOne(abort) {
|
|
934
|
+
const { items, release } = await this.requestPermits(1, abort);
|
|
935
|
+
return { release, item: items[0] };
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* Requests access to all items from the pool.
|
|
939
|
+
*
|
|
940
|
+
* The returned `release` callback must be invoked to return items into the pool.
|
|
941
|
+
*/
|
|
942
|
+
requestAll(abort) {
|
|
943
|
+
return this.requestPermits(this.size, abort);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* An asynchronous mutex implementation.
|
|
948
|
+
*
|
|
949
|
+
* @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
|
|
950
|
+
*/
|
|
951
|
+
class Mutex {
|
|
952
|
+
inner = new Semaphore([null]);
|
|
953
|
+
async acquire(abort) {
|
|
954
|
+
const { release } = await this.inner.requestOne(abort);
|
|
955
|
+
return release;
|
|
956
|
+
}
|
|
867
957
|
async runExclusive(fn, abort) {
|
|
868
958
|
const returnMutex = await this.acquire(abort);
|
|
869
959
|
try {
|
|
@@ -1306,6 +1396,8 @@ var EncodingType;
|
|
|
1306
1396
|
EncodingType["Base64"] = "base64";
|
|
1307
1397
|
})(EncodingType || (EncodingType = {}));
|
|
1308
1398
|
|
|
1399
|
+
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
1400
|
+
|
|
1309
1401
|
function getDefaultExportFromCjs (x) {
|
|
1310
1402
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
1311
1403
|
}
|
|
@@ -1386,7 +1478,7 @@ function requireEventIterator () {
|
|
|
1386
1478
|
this.removeCallback();
|
|
1387
1479
|
});
|
|
1388
1480
|
}
|
|
1389
|
-
[
|
|
1481
|
+
[symbolAsyncIterator]() {
|
|
1390
1482
|
return {
|
|
1391
1483
|
next: (value) => {
|
|
1392
1484
|
const result = this.pushQueue.shift();
|
|
@@ -1433,7 +1525,7 @@ function requireEventIterator () {
|
|
|
1433
1525
|
queue.eventHandlers[event] = fn;
|
|
1434
1526
|
},
|
|
1435
1527
|
}) || (() => { });
|
|
1436
|
-
this[
|
|
1528
|
+
this[symbolAsyncIterator] = () => queue[symbolAsyncIterator]();
|
|
1437
1529
|
Object.freeze(this);
|
|
1438
1530
|
}
|
|
1439
1531
|
}
|
|
@@ -2315,15 +2407,6 @@ class ControlledExecutor {
|
|
|
2315
2407
|
}
|
|
2316
2408
|
}
|
|
2317
2409
|
|
|
2318
|
-
/**
|
|
2319
|
-
* A ponyfill for `Symbol.asyncIterator` that is compatible with the
|
|
2320
|
-
* [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)
|
|
2321
|
-
* we recommend for React Native.
|
|
2322
|
-
*
|
|
2323
|
-
* As long as we use this symbol (instead of `for await` and `async *`) in this package, we can be compatible with async
|
|
2324
|
-
* iterators without requiring them.
|
|
2325
|
-
*/
|
|
2326
|
-
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
2327
2410
|
/**
|
|
2328
2411
|
* Throttle a function to be called at most once every "wait" milliseconds,
|
|
2329
2412
|
* on the trailing edge.
|
|
@@ -10660,177 +10743,10 @@ function requireDist () {
|
|
|
10660
10743
|
|
|
10661
10744
|
var distExports = requireDist();
|
|
10662
10745
|
|
|
10663
|
-
var version = "1.
|
|
10746
|
+
var version = "1.52.0";
|
|
10664
10747
|
var PACKAGE = {
|
|
10665
10748
|
version: version};
|
|
10666
10749
|
|
|
10667
|
-
const DEFAULT_PRESSURE_LIMITS = {
|
|
10668
|
-
highWater: 10,
|
|
10669
|
-
lowWater: 0
|
|
10670
|
-
};
|
|
10671
|
-
/**
|
|
10672
|
-
* A very basic implementation of a data stream with backpressure support which does not use
|
|
10673
|
-
* native JS streams or async iterators.
|
|
10674
|
-
* This is handy for environments such as React Native which need polyfills for the above.
|
|
10675
|
-
*/
|
|
10676
|
-
class DataStream extends BaseObserver {
|
|
10677
|
-
options;
|
|
10678
|
-
dataQueue;
|
|
10679
|
-
isClosed;
|
|
10680
|
-
processingPromise;
|
|
10681
|
-
notifyDataAdded;
|
|
10682
|
-
logger;
|
|
10683
|
-
mapLine;
|
|
10684
|
-
constructor(options) {
|
|
10685
|
-
super();
|
|
10686
|
-
this.options = options;
|
|
10687
|
-
this.processingPromise = null;
|
|
10688
|
-
this.isClosed = false;
|
|
10689
|
-
this.dataQueue = [];
|
|
10690
|
-
this.mapLine = options?.mapLine ?? ((line) => line);
|
|
10691
|
-
this.logger = options?.logger ?? Logger.get('DataStream');
|
|
10692
|
-
if (options?.closeOnError) {
|
|
10693
|
-
const l = this.registerListener({
|
|
10694
|
-
error: (ex) => {
|
|
10695
|
-
l?.();
|
|
10696
|
-
this.close();
|
|
10697
|
-
}
|
|
10698
|
-
});
|
|
10699
|
-
}
|
|
10700
|
-
}
|
|
10701
|
-
get highWatermark() {
|
|
10702
|
-
return this.options?.pressure?.highWaterMark ?? DEFAULT_PRESSURE_LIMITS.highWater;
|
|
10703
|
-
}
|
|
10704
|
-
get lowWatermark() {
|
|
10705
|
-
return this.options?.pressure?.lowWaterMark ?? DEFAULT_PRESSURE_LIMITS.lowWater;
|
|
10706
|
-
}
|
|
10707
|
-
get closed() {
|
|
10708
|
-
return this.isClosed;
|
|
10709
|
-
}
|
|
10710
|
-
async close() {
|
|
10711
|
-
this.isClosed = true;
|
|
10712
|
-
await this.processingPromise;
|
|
10713
|
-
this.iterateListeners((l) => l.closed?.());
|
|
10714
|
-
// Discard any data in the queue
|
|
10715
|
-
this.dataQueue = [];
|
|
10716
|
-
this.listeners.clear();
|
|
10717
|
-
}
|
|
10718
|
-
/**
|
|
10719
|
-
* Enqueues data for the consumers to read
|
|
10720
|
-
*/
|
|
10721
|
-
enqueueData(data) {
|
|
10722
|
-
if (this.isClosed) {
|
|
10723
|
-
throw new Error('Cannot enqueue data into closed stream.');
|
|
10724
|
-
}
|
|
10725
|
-
this.dataQueue.push(data);
|
|
10726
|
-
this.notifyDataAdded?.();
|
|
10727
|
-
this.processQueue();
|
|
10728
|
-
}
|
|
10729
|
-
/**
|
|
10730
|
-
* Reads data once from the data stream
|
|
10731
|
-
* @returns a Data payload or Null if the stream closed.
|
|
10732
|
-
*/
|
|
10733
|
-
async read() {
|
|
10734
|
-
if (this.closed) {
|
|
10735
|
-
return null;
|
|
10736
|
-
}
|
|
10737
|
-
// Wait for any pending processing to complete first.
|
|
10738
|
-
// This ensures we register our listener before calling processQueue(),
|
|
10739
|
-
// avoiding a race where processQueue() sees no reader and returns early.
|
|
10740
|
-
if (this.processingPromise) {
|
|
10741
|
-
await this.processingPromise;
|
|
10742
|
-
}
|
|
10743
|
-
// Re-check after await - stream may have closed while we were waiting
|
|
10744
|
-
if (this.closed) {
|
|
10745
|
-
return null;
|
|
10746
|
-
}
|
|
10747
|
-
return new Promise((resolve, reject) => {
|
|
10748
|
-
const l = this.registerListener({
|
|
10749
|
-
data: async (data) => {
|
|
10750
|
-
resolve(data);
|
|
10751
|
-
// Remove the listener
|
|
10752
|
-
l?.();
|
|
10753
|
-
},
|
|
10754
|
-
closed: () => {
|
|
10755
|
-
resolve(null);
|
|
10756
|
-
l?.();
|
|
10757
|
-
},
|
|
10758
|
-
error: (ex) => {
|
|
10759
|
-
reject(ex);
|
|
10760
|
-
l?.();
|
|
10761
|
-
}
|
|
10762
|
-
});
|
|
10763
|
-
this.processQueue();
|
|
10764
|
-
});
|
|
10765
|
-
}
|
|
10766
|
-
/**
|
|
10767
|
-
* Executes a callback for each data item in the stream
|
|
10768
|
-
*/
|
|
10769
|
-
forEach(callback) {
|
|
10770
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
10771
|
-
this.iterateAsyncErrored(async (l) => l.lowWater?.());
|
|
10772
|
-
}
|
|
10773
|
-
return this.registerListener({
|
|
10774
|
-
data: callback
|
|
10775
|
-
});
|
|
10776
|
-
}
|
|
10777
|
-
processQueue() {
|
|
10778
|
-
if (this.processingPromise) {
|
|
10779
|
-
return;
|
|
10780
|
-
}
|
|
10781
|
-
const promise = (this.processingPromise = this._processQueue());
|
|
10782
|
-
promise.finally(() => {
|
|
10783
|
-
this.processingPromise = null;
|
|
10784
|
-
});
|
|
10785
|
-
return promise;
|
|
10786
|
-
}
|
|
10787
|
-
hasDataReader() {
|
|
10788
|
-
return Array.from(this.listeners.values()).some((l) => !!l.data);
|
|
10789
|
-
}
|
|
10790
|
-
async _processQueue() {
|
|
10791
|
-
/**
|
|
10792
|
-
* Allow listeners to mutate the queue before processing.
|
|
10793
|
-
* This allows for operations such as dropping or compressing data
|
|
10794
|
-
* on high water or requesting more data on low water.
|
|
10795
|
-
*/
|
|
10796
|
-
if (this.dataQueue.length >= this.highWatermark) {
|
|
10797
|
-
await this.iterateAsyncErrored(async (l) => l.highWater?.());
|
|
10798
|
-
}
|
|
10799
|
-
if (this.isClosed || !this.hasDataReader()) {
|
|
10800
|
-
return;
|
|
10801
|
-
}
|
|
10802
|
-
if (this.dataQueue.length) {
|
|
10803
|
-
const data = this.dataQueue.shift();
|
|
10804
|
-
const mapped = this.mapLine(data);
|
|
10805
|
-
await this.iterateAsyncErrored(async (l) => l.data?.(mapped));
|
|
10806
|
-
}
|
|
10807
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
10808
|
-
const dataAdded = new Promise((resolve) => {
|
|
10809
|
-
this.notifyDataAdded = resolve;
|
|
10810
|
-
});
|
|
10811
|
-
await Promise.race([this.iterateAsyncErrored(async (l) => l.lowWater?.()), dataAdded]);
|
|
10812
|
-
this.notifyDataAdded = null;
|
|
10813
|
-
}
|
|
10814
|
-
if (this.dataQueue.length > 0) {
|
|
10815
|
-
setTimeout(() => this.processQueue());
|
|
10816
|
-
}
|
|
10817
|
-
}
|
|
10818
|
-
async iterateAsyncErrored(cb) {
|
|
10819
|
-
// Important: We need to copy the listeners, as calling a listener could result in adding another
|
|
10820
|
-
// listener, resulting in infinite loops.
|
|
10821
|
-
const listeners = Array.from(this.listeners.values());
|
|
10822
|
-
for (let i of listeners) {
|
|
10823
|
-
try {
|
|
10824
|
-
await cb(i);
|
|
10825
|
-
}
|
|
10826
|
-
catch (ex) {
|
|
10827
|
-
this.logger.error(ex);
|
|
10828
|
-
this.iterateListeners((l) => l.error?.(ex));
|
|
10829
|
-
}
|
|
10830
|
-
}
|
|
10831
|
-
}
|
|
10832
|
-
}
|
|
10833
|
-
|
|
10834
10750
|
var WebsocketDuplexConnection = {};
|
|
10835
10751
|
|
|
10836
10752
|
var hasRequiredWebsocketDuplexConnection;
|
|
@@ -10993,8 +10909,215 @@ class WebsocketClientTransport {
|
|
|
10993
10909
|
}
|
|
10994
10910
|
}
|
|
10995
10911
|
|
|
10912
|
+
const doneResult = { done: true, value: undefined };
|
|
10913
|
+
function valueResult(value) {
|
|
10914
|
+
return { done: false, value };
|
|
10915
|
+
}
|
|
10916
|
+
/**
|
|
10917
|
+
* A variant of {@link Array.map} for async iterators.
|
|
10918
|
+
*/
|
|
10919
|
+
function map(source, map) {
|
|
10920
|
+
return {
|
|
10921
|
+
next: async () => {
|
|
10922
|
+
const value = await source.next();
|
|
10923
|
+
if (value.done) {
|
|
10924
|
+
return value;
|
|
10925
|
+
}
|
|
10926
|
+
else {
|
|
10927
|
+
return { value: map(value.value) };
|
|
10928
|
+
}
|
|
10929
|
+
}
|
|
10930
|
+
};
|
|
10931
|
+
}
|
|
10932
|
+
/**
|
|
10933
|
+
* Expands a source async iterator by allowing to inject events asynchronously.
|
|
10934
|
+
*
|
|
10935
|
+
* The resulting iterator will emit all events from its source. Additionally though, events can be injected. These
|
|
10936
|
+
* events are dropped once the main iterator completes, but are otherwise forwarded.
|
|
10937
|
+
*
|
|
10938
|
+
* The iterator completes when its source completes, and it supports backpressure by only calling `next()` on the source
|
|
10939
|
+
* in response to a `next()` call from downstream if no pending injected events can be dispatched.
|
|
10940
|
+
*/
|
|
10941
|
+
function injectable(source) {
|
|
10942
|
+
let sourceIsDone = false;
|
|
10943
|
+
let waiter = undefined; // An active, waiting next() call.
|
|
10944
|
+
// A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
|
|
10945
|
+
let pendingSourceEvent = null;
|
|
10946
|
+
let pendingInjectedEvents = [];
|
|
10947
|
+
const consumeWaiter = () => {
|
|
10948
|
+
const pending = waiter;
|
|
10949
|
+
waiter = undefined;
|
|
10950
|
+
return pending;
|
|
10951
|
+
};
|
|
10952
|
+
const fetchFromSource = () => {
|
|
10953
|
+
const resolveWaiter = (propagate) => {
|
|
10954
|
+
const active = consumeWaiter();
|
|
10955
|
+
if (active) {
|
|
10956
|
+
propagate(active);
|
|
10957
|
+
}
|
|
10958
|
+
else {
|
|
10959
|
+
pendingSourceEvent = propagate;
|
|
10960
|
+
}
|
|
10961
|
+
};
|
|
10962
|
+
const nextFromSource = source.next();
|
|
10963
|
+
nextFromSource.then((value) => {
|
|
10964
|
+
sourceIsDone = value.done == true;
|
|
10965
|
+
resolveWaiter((w) => w.resolve(value));
|
|
10966
|
+
}, (error) => {
|
|
10967
|
+
resolveWaiter((w) => w.reject(error));
|
|
10968
|
+
});
|
|
10969
|
+
};
|
|
10970
|
+
return {
|
|
10971
|
+
next: () => {
|
|
10972
|
+
return new Promise((resolve, reject) => {
|
|
10973
|
+
// First priority: Dispatch ready upstream events.
|
|
10974
|
+
if (sourceIsDone) {
|
|
10975
|
+
return resolve(doneResult);
|
|
10976
|
+
}
|
|
10977
|
+
if (pendingSourceEvent) {
|
|
10978
|
+
pendingSourceEvent({ resolve, reject });
|
|
10979
|
+
pendingSourceEvent = null;
|
|
10980
|
+
return;
|
|
10981
|
+
}
|
|
10982
|
+
// Second priority: Dispatch injected events
|
|
10983
|
+
if (pendingInjectedEvents.length) {
|
|
10984
|
+
return resolve(valueResult(pendingInjectedEvents.shift()));
|
|
10985
|
+
}
|
|
10986
|
+
// Nothing pending? Fetch from source
|
|
10987
|
+
waiter = { resolve, reject };
|
|
10988
|
+
return fetchFromSource();
|
|
10989
|
+
});
|
|
10990
|
+
},
|
|
10991
|
+
inject: (event) => {
|
|
10992
|
+
const pending = consumeWaiter();
|
|
10993
|
+
if (pending != null) {
|
|
10994
|
+
pending.resolve(valueResult(event));
|
|
10995
|
+
}
|
|
10996
|
+
else {
|
|
10997
|
+
pendingInjectedEvents.push(event);
|
|
10998
|
+
}
|
|
10999
|
+
}
|
|
11000
|
+
};
|
|
11001
|
+
}
|
|
11002
|
+
/**
|
|
11003
|
+
* Splits a byte stream at line endings, emitting each line as a string.
|
|
11004
|
+
*/
|
|
11005
|
+
function extractJsonLines(source, decoder) {
|
|
11006
|
+
let buffer = '';
|
|
11007
|
+
const pendingLines = [];
|
|
11008
|
+
let isFinalEvent = false;
|
|
11009
|
+
return {
|
|
11010
|
+
next: async () => {
|
|
11011
|
+
while (true) {
|
|
11012
|
+
if (isFinalEvent) {
|
|
11013
|
+
return doneResult;
|
|
11014
|
+
}
|
|
11015
|
+
{
|
|
11016
|
+
const first = pendingLines.shift();
|
|
11017
|
+
if (first) {
|
|
11018
|
+
return { done: false, value: first };
|
|
11019
|
+
}
|
|
11020
|
+
}
|
|
11021
|
+
const { done, value } = await source.next();
|
|
11022
|
+
if (done) {
|
|
11023
|
+
const remaining = buffer.trim();
|
|
11024
|
+
if (remaining.length != 0) {
|
|
11025
|
+
isFinalEvent = true;
|
|
11026
|
+
return { done: false, value: remaining };
|
|
11027
|
+
}
|
|
11028
|
+
return doneResult;
|
|
11029
|
+
}
|
|
11030
|
+
const data = decoder.decode(value, { stream: true });
|
|
11031
|
+
buffer += data;
|
|
11032
|
+
const lines = buffer.split('\n');
|
|
11033
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
11034
|
+
const l = lines[i].trim();
|
|
11035
|
+
if (l.length > 0) {
|
|
11036
|
+
pendingLines.push(l);
|
|
11037
|
+
}
|
|
11038
|
+
}
|
|
11039
|
+
buffer = lines[lines.length - 1];
|
|
11040
|
+
}
|
|
11041
|
+
}
|
|
11042
|
+
};
|
|
11043
|
+
}
|
|
11044
|
+
/**
|
|
11045
|
+
* Splits a concatenated stream of BSON objects by emitting individual objects.
|
|
11046
|
+
*/
|
|
11047
|
+
function extractBsonObjects(source) {
|
|
11048
|
+
// Fully read but not emitted yet.
|
|
11049
|
+
const completedObjects = [];
|
|
11050
|
+
// Whether source has returned { done: true }. We do the same once completed objects have been emitted.
|
|
11051
|
+
let isDone = false;
|
|
11052
|
+
const lengthBuffer = new DataView(new ArrayBuffer(4));
|
|
11053
|
+
let objectBody = null;
|
|
11054
|
+
// If we're parsing the length field, a number between 1 and 4 (inclusive) describing remaining bytes in the header.
|
|
11055
|
+
// If we're consuming a document, the bytes remaining.
|
|
11056
|
+
let remainingLength = 4;
|
|
11057
|
+
return {
|
|
11058
|
+
async next() {
|
|
11059
|
+
while (true) {
|
|
11060
|
+
// Before fetching new data from upstream, return completed objects.
|
|
11061
|
+
if (completedObjects.length) {
|
|
11062
|
+
return valueResult(completedObjects.shift());
|
|
11063
|
+
}
|
|
11064
|
+
if (isDone) {
|
|
11065
|
+
return doneResult;
|
|
11066
|
+
}
|
|
11067
|
+
const upstreamEvent = await source.next();
|
|
11068
|
+
if (upstreamEvent.done) {
|
|
11069
|
+
isDone = true;
|
|
11070
|
+
if (objectBody || remainingLength != 4) {
|
|
11071
|
+
throw new Error('illegal end of stream in BSON object');
|
|
11072
|
+
}
|
|
11073
|
+
return doneResult;
|
|
11074
|
+
}
|
|
11075
|
+
const chunk = upstreamEvent.value;
|
|
11076
|
+
for (let i = 0; i < chunk.length;) {
|
|
11077
|
+
const availableInData = chunk.length - i;
|
|
11078
|
+
if (objectBody) {
|
|
11079
|
+
// We're in the middle of reading a BSON document.
|
|
11080
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
11081
|
+
const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
|
|
11082
|
+
objectBody.set(copySource, objectBody.length - remainingLength);
|
|
11083
|
+
i += bytesToRead;
|
|
11084
|
+
remainingLength -= bytesToRead;
|
|
11085
|
+
if (remainingLength == 0) {
|
|
11086
|
+
completedObjects.push(objectBody);
|
|
11087
|
+
// Prepare to read another document, starting with its length
|
|
11088
|
+
objectBody = null;
|
|
11089
|
+
remainingLength = 4;
|
|
11090
|
+
}
|
|
11091
|
+
}
|
|
11092
|
+
else {
|
|
11093
|
+
// Copy up to 4 bytes into lengthBuffer, depending on how many we still need.
|
|
11094
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
11095
|
+
for (let j = 0; j < bytesToRead; j++) {
|
|
11096
|
+
lengthBuffer.setUint8(4 - remainingLength + j, chunk[i + j]);
|
|
11097
|
+
}
|
|
11098
|
+
i += bytesToRead;
|
|
11099
|
+
remainingLength -= bytesToRead;
|
|
11100
|
+
if (remainingLength == 0) {
|
|
11101
|
+
// Transition from reading length header to reading document. Subtracting 4 because the length of the
|
|
11102
|
+
// header is included in length.
|
|
11103
|
+
const length = lengthBuffer.getInt32(0, true /* little endian */);
|
|
11104
|
+
remainingLength = length - 4;
|
|
11105
|
+
if (remainingLength < 1) {
|
|
11106
|
+
throw new Error(`invalid length for bson: ${length}`);
|
|
11107
|
+
}
|
|
11108
|
+
objectBody = new Uint8Array(length);
|
|
11109
|
+
new DataView(objectBody.buffer).setInt32(0, length, true);
|
|
11110
|
+
}
|
|
11111
|
+
}
|
|
11112
|
+
}
|
|
11113
|
+
}
|
|
11114
|
+
}
|
|
11115
|
+
};
|
|
11116
|
+
}
|
|
11117
|
+
|
|
10996
11118
|
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
|
|
10997
11119
|
const POWERSYNC_JS_VERSION = PACKAGE.version;
|
|
11120
|
+
const SYNC_QUEUE_REQUEST_HIGH_WATER = 10;
|
|
10998
11121
|
const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
|
|
10999
11122
|
// Keep alive message is sent every period
|
|
11000
11123
|
const KEEP_ALIVE_MS = 20_000;
|
|
@@ -11174,13 +11297,14 @@ class AbstractRemote {
|
|
|
11174
11297
|
return new WebSocket(url);
|
|
11175
11298
|
}
|
|
11176
11299
|
/**
|
|
11177
|
-
* Returns a data stream of sync line data.
|
|
11300
|
+
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
11301
|
+
*
|
|
11302
|
+
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
11178
11303
|
*
|
|
11179
|
-
* @param map Maps received payload frames to the typed event value.
|
|
11180
11304
|
* @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
|
|
11181
11305
|
* (required for compatibility with older sync services).
|
|
11182
11306
|
*/
|
|
11183
|
-
async socketStreamRaw(options,
|
|
11307
|
+
async socketStreamRaw(options, bson) {
|
|
11184
11308
|
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
|
|
11185
11309
|
const mimeType = bson == null ? 'application/json' : 'application/bson';
|
|
11186
11310
|
function toBuffer(js) {
|
|
@@ -11195,52 +11319,55 @@ class AbstractRemote {
|
|
|
11195
11319
|
}
|
|
11196
11320
|
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
|
|
11197
11321
|
const request = await this.buildRequest(path);
|
|
11322
|
+
const url = this.options.socketUrlTransformer(request.url);
|
|
11198
11323
|
// Add the user agent in the setup payload - we can't set custom
|
|
11199
11324
|
// headers with websockets on web. The browser userAgent is however added
|
|
11200
11325
|
// automatically as a header.
|
|
11201
11326
|
const userAgent = this.getUserAgent();
|
|
11202
|
-
|
|
11203
|
-
|
|
11204
|
-
|
|
11205
|
-
|
|
11206
|
-
|
|
11207
|
-
|
|
11208
|
-
|
|
11327
|
+
// While we're connecting (a process that can't be aborted in RSocket), the WebSocket instance to close if we wanted
|
|
11328
|
+
// to abort the connection.
|
|
11329
|
+
let pendingSocket = null;
|
|
11330
|
+
let keepAliveTimeout;
|
|
11331
|
+
let rsocket = null;
|
|
11332
|
+
let queue = null;
|
|
11333
|
+
let didClose = false;
|
|
11334
|
+
const abortRequest = () => {
|
|
11335
|
+
if (didClose) {
|
|
11336
|
+
return;
|
|
11337
|
+
}
|
|
11338
|
+
didClose = true;
|
|
11339
|
+
clearTimeout(keepAliveTimeout);
|
|
11340
|
+
if (pendingSocket) {
|
|
11341
|
+
pendingSocket.close();
|
|
11342
|
+
}
|
|
11343
|
+
if (rsocket) {
|
|
11344
|
+
rsocket.close();
|
|
11345
|
+
}
|
|
11346
|
+
if (queue) {
|
|
11347
|
+
queue.stop();
|
|
11348
|
+
}
|
|
11349
|
+
};
|
|
11209
11350
|
// Handle upstream abort
|
|
11210
|
-
if (options.abortSignal
|
|
11351
|
+
if (options.abortSignal.aborted) {
|
|
11211
11352
|
throw new AbortOperation('Connection request aborted');
|
|
11212
11353
|
}
|
|
11213
11354
|
else {
|
|
11214
|
-
options.abortSignal
|
|
11215
|
-
stream.close();
|
|
11216
|
-
}, { once: true });
|
|
11355
|
+
options.abortSignal.addEventListener('abort', abortRequest);
|
|
11217
11356
|
}
|
|
11218
|
-
let keepAliveTimeout;
|
|
11219
11357
|
const resetTimeout = () => {
|
|
11220
11358
|
clearTimeout(keepAliveTimeout);
|
|
11221
11359
|
keepAliveTimeout = setTimeout(() => {
|
|
11222
11360
|
this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
|
|
11223
|
-
|
|
11361
|
+
abortRequest();
|
|
11224
11362
|
}, SOCKET_TIMEOUT_MS);
|
|
11225
11363
|
};
|
|
11226
11364
|
resetTimeout();
|
|
11227
|
-
// Typescript complains about this being `never` if it's not assigned here.
|
|
11228
|
-
// This is assigned in `wsCreator`.
|
|
11229
|
-
let disposeSocketConnectionTimeout = () => { };
|
|
11230
|
-
const url = this.options.socketUrlTransformer(request.url);
|
|
11231
11365
|
const connector = new distExports.RSocketConnector({
|
|
11232
11366
|
transport: new WebsocketClientTransport({
|
|
11233
11367
|
url,
|
|
11234
11368
|
wsCreator: (url) => {
|
|
11235
|
-
const socket = this.createSocket(url);
|
|
11236
|
-
|
|
11237
|
-
closed: () => {
|
|
11238
|
-
// Allow closing the underlying WebSocket if the stream was closed before the
|
|
11239
|
-
// RSocket connect completed. This should effectively abort the request.
|
|
11240
|
-
socket.close();
|
|
11241
|
-
}
|
|
11242
|
-
});
|
|
11243
|
-
socket.addEventListener('message', (event) => {
|
|
11369
|
+
const socket = (pendingSocket = this.createSocket(url));
|
|
11370
|
+
socket.addEventListener('message', () => {
|
|
11244
11371
|
resetTimeout();
|
|
11245
11372
|
});
|
|
11246
11373
|
return socket;
|
|
@@ -11260,43 +11387,40 @@ class AbstractRemote {
|
|
|
11260
11387
|
}
|
|
11261
11388
|
}
|
|
11262
11389
|
});
|
|
11263
|
-
let rsocket;
|
|
11264
11390
|
try {
|
|
11265
11391
|
rsocket = await connector.connect();
|
|
11266
11392
|
// The connection is established, we no longer need to monitor the initial timeout
|
|
11267
|
-
|
|
11393
|
+
pendingSocket = null;
|
|
11268
11394
|
}
|
|
11269
11395
|
catch (ex) {
|
|
11270
11396
|
this.logger.error(`Failed to connect WebSocket`, ex);
|
|
11271
|
-
|
|
11272
|
-
if (!stream.closed) {
|
|
11273
|
-
await stream.close();
|
|
11274
|
-
}
|
|
11397
|
+
abortRequest();
|
|
11275
11398
|
throw ex;
|
|
11276
11399
|
}
|
|
11277
11400
|
resetTimeout();
|
|
11278
|
-
let socketIsClosed = false;
|
|
11279
|
-
const closeSocket = () => {
|
|
11280
|
-
clearTimeout(keepAliveTimeout);
|
|
11281
|
-
if (socketIsClosed) {
|
|
11282
|
-
return;
|
|
11283
|
-
}
|
|
11284
|
-
socketIsClosed = true;
|
|
11285
|
-
rsocket.close();
|
|
11286
|
-
};
|
|
11287
11401
|
// Helps to prevent double close scenarios
|
|
11288
|
-
rsocket.onClose(() => (
|
|
11289
|
-
|
|
11290
|
-
let pendingEventsCount = syncQueueRequestSize;
|
|
11291
|
-
const disposeClosedListener = stream.registerListener({
|
|
11292
|
-
closed: () => {
|
|
11293
|
-
closeSocket();
|
|
11294
|
-
disposeClosedListener();
|
|
11295
|
-
}
|
|
11296
|
-
});
|
|
11297
|
-
const socket = await new Promise((resolve, reject) => {
|
|
11402
|
+
rsocket.onClose(() => (rsocket = null));
|
|
11403
|
+
return await new Promise((resolve, reject) => {
|
|
11298
11404
|
let connectionEstablished = false;
|
|
11299
|
-
|
|
11405
|
+
let pendingEventsCount = syncQueueRequestSize;
|
|
11406
|
+
let paused = false;
|
|
11407
|
+
let res = null;
|
|
11408
|
+
function requestMore() {
|
|
11409
|
+
const delta = syncQueueRequestSize - pendingEventsCount;
|
|
11410
|
+
if (!paused && delta > 0) {
|
|
11411
|
+
res?.request(delta);
|
|
11412
|
+
pendingEventsCount = syncQueueRequestSize;
|
|
11413
|
+
}
|
|
11414
|
+
}
|
|
11415
|
+
const events = new domExports.EventIterator((q) => {
|
|
11416
|
+
queue = q;
|
|
11417
|
+
q.on('highWater', () => (paused = true));
|
|
11418
|
+
q.on('lowWater', () => {
|
|
11419
|
+
paused = false;
|
|
11420
|
+
requestMore();
|
|
11421
|
+
});
|
|
11422
|
+
}, { highWaterMark: SYNC_QUEUE_REQUEST_HIGH_WATER, lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER })[symbolAsyncIterator]();
|
|
11423
|
+
res = rsocket.requestStream({
|
|
11300
11424
|
data: toBuffer(options.data),
|
|
11301
11425
|
metadata: toBuffer({
|
|
11302
11426
|
path
|
|
@@ -11321,7 +11445,7 @@ class AbstractRemote {
|
|
|
11321
11445
|
}
|
|
11322
11446
|
// RSocket will close the RSocket stream automatically
|
|
11323
11447
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
11324
|
-
|
|
11448
|
+
abortRequest();
|
|
11325
11449
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
11326
11450
|
if (!connectionEstablished) {
|
|
11327
11451
|
reject(e);
|
|
@@ -11331,41 +11455,40 @@ class AbstractRemote {
|
|
|
11331
11455
|
// The connection is active
|
|
11332
11456
|
if (!connectionEstablished) {
|
|
11333
11457
|
connectionEstablished = true;
|
|
11334
|
-
resolve(
|
|
11458
|
+
resolve(events);
|
|
11335
11459
|
}
|
|
11336
11460
|
const { data } = payload;
|
|
11461
|
+
if (data) {
|
|
11462
|
+
queue.push(data);
|
|
11463
|
+
}
|
|
11337
11464
|
// Less events are now pending
|
|
11338
11465
|
pendingEventsCount--;
|
|
11339
|
-
|
|
11340
|
-
|
|
11341
|
-
}
|
|
11342
|
-
stream.enqueueData(data);
|
|
11466
|
+
// Request another event (unless the downstream consumer is paused).
|
|
11467
|
+
requestMore();
|
|
11343
11468
|
},
|
|
11344
11469
|
onComplete: () => {
|
|
11345
|
-
|
|
11470
|
+
abortRequest(); // this will also emit a done event
|
|
11346
11471
|
},
|
|
11347
11472
|
onExtension: () => { }
|
|
11348
11473
|
});
|
|
11349
11474
|
});
|
|
11350
|
-
const l = stream.registerListener({
|
|
11351
|
-
lowWater: async () => {
|
|
11352
|
-
// Request to fill up the queue
|
|
11353
|
-
const required = syncQueueRequestSize - pendingEventsCount;
|
|
11354
|
-
if (required > 0) {
|
|
11355
|
-
socket.request(syncQueueRequestSize - pendingEventsCount);
|
|
11356
|
-
pendingEventsCount = syncQueueRequestSize;
|
|
11357
|
-
}
|
|
11358
|
-
},
|
|
11359
|
-
closed: () => {
|
|
11360
|
-
l();
|
|
11361
|
-
}
|
|
11362
|
-
});
|
|
11363
|
-
return stream;
|
|
11364
11475
|
}
|
|
11365
11476
|
/**
|
|
11366
|
-
*
|
|
11477
|
+
* @returns Whether the HTTP implementation on this platform can receive streamed binary responses. This is true on
|
|
11478
|
+
* all platforms except React Native (who would have guessed...), where we must not request BSON responses.
|
|
11479
|
+
*
|
|
11480
|
+
* @see https://github.com/react-native-community/fetch?tab=readme-ov-file#motivation
|
|
11367
11481
|
*/
|
|
11368
|
-
|
|
11482
|
+
get supportsStreamingBinaryResponses() {
|
|
11483
|
+
return true;
|
|
11484
|
+
}
|
|
11485
|
+
/**
|
|
11486
|
+
* Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
|
|
11487
|
+
* async iterator of byte blobs.
|
|
11488
|
+
*
|
|
11489
|
+
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
11490
|
+
*/
|
|
11491
|
+
async fetchStreamRaw(options) {
|
|
11369
11492
|
const { data, path, headers, abortSignal } = options;
|
|
11370
11493
|
const request = await this.buildRequest(path);
|
|
11371
11494
|
/**
|
|
@@ -11377,119 +11500,94 @@ class AbstractRemote {
|
|
|
11377
11500
|
* Aborting the active fetch request while it is being consumed seems to throw
|
|
11378
11501
|
* an unhandled exception on the window level.
|
|
11379
11502
|
*/
|
|
11380
|
-
if (abortSignal
|
|
11381
|
-
throw new AbortOperation('Abort request received before making
|
|
11503
|
+
if (abortSignal.aborted) {
|
|
11504
|
+
throw new AbortOperation('Abort request received before making fetchStreamRaw request');
|
|
11382
11505
|
}
|
|
11383
11506
|
const controller = new AbortController();
|
|
11384
|
-
let
|
|
11385
|
-
abortSignal
|
|
11386
|
-
|
|
11507
|
+
let reader = null;
|
|
11508
|
+
abortSignal.addEventListener('abort', () => {
|
|
11509
|
+
const reason = abortSignal.reason ??
|
|
11510
|
+
new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.');
|
|
11511
|
+
if (reader == null) {
|
|
11387
11512
|
// Only abort via the abort controller if the request has not resolved yet
|
|
11388
|
-
controller.abort(
|
|
11389
|
-
|
|
11513
|
+
controller.abort(reason);
|
|
11514
|
+
}
|
|
11515
|
+
else {
|
|
11516
|
+
reader.cancel(reason).catch(() => {
|
|
11517
|
+
// Cancelling the reader might rethrow an exception we would have handled by throwing in next(). So we can
|
|
11518
|
+
// ignore it here.
|
|
11519
|
+
});
|
|
11390
11520
|
}
|
|
11391
11521
|
});
|
|
11392
|
-
|
|
11393
|
-
|
|
11394
|
-
|
|
11395
|
-
|
|
11396
|
-
|
|
11397
|
-
|
|
11398
|
-
|
|
11399
|
-
|
|
11400
|
-
|
|
11522
|
+
let res;
|
|
11523
|
+
let responseIsBson = false;
|
|
11524
|
+
try {
|
|
11525
|
+
const ndJson = 'application/x-ndjson';
|
|
11526
|
+
const bson = 'application/vnd.powersync.bson-stream';
|
|
11527
|
+
res = await this.fetch(request.url, {
|
|
11528
|
+
method: 'POST',
|
|
11529
|
+
headers: {
|
|
11530
|
+
...headers,
|
|
11531
|
+
...request.headers,
|
|
11532
|
+
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
11533
|
+
},
|
|
11534
|
+
body: JSON.stringify(data),
|
|
11535
|
+
signal: controller.signal,
|
|
11536
|
+
cache: 'no-store',
|
|
11537
|
+
...(this.options.fetchOptions ?? {}),
|
|
11538
|
+
...options.fetchOptions
|
|
11539
|
+
});
|
|
11540
|
+
if (!res.ok || !res.body) {
|
|
11541
|
+
const text = await res.text();
|
|
11542
|
+
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
11543
|
+
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
11544
|
+
error.status = res.status;
|
|
11545
|
+
throw error;
|
|
11546
|
+
}
|
|
11547
|
+
const contentType = res.headers.get('content-type');
|
|
11548
|
+
responseIsBson = contentType == bson;
|
|
11549
|
+
}
|
|
11550
|
+
catch (ex) {
|
|
11401
11551
|
if (ex.name == 'AbortError') {
|
|
11402
11552
|
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
|
|
11403
11553
|
}
|
|
11404
11554
|
throw ex;
|
|
11405
|
-
});
|
|
11406
|
-
if (!res) {
|
|
11407
|
-
throw new Error('Fetch request was aborted');
|
|
11408
|
-
}
|
|
11409
|
-
requestResolved = true;
|
|
11410
|
-
if (!res.ok || !res.body) {
|
|
11411
|
-
const text = await res.text();
|
|
11412
|
-
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
11413
|
-
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
11414
|
-
error.status = res.status;
|
|
11415
|
-
throw error;
|
|
11416
11555
|
}
|
|
11417
|
-
|
|
11418
|
-
|
|
11419
|
-
|
|
11420
|
-
|
|
11421
|
-
|
|
11422
|
-
const closeReader = async () => {
|
|
11423
|
-
try {
|
|
11424
|
-
readerReleased = true;
|
|
11425
|
-
await reader.cancel();
|
|
11426
|
-
}
|
|
11427
|
-
catch (ex) {
|
|
11428
|
-
// an error will throw if the reader hasn't been used yet
|
|
11429
|
-
}
|
|
11430
|
-
reader.releaseLock();
|
|
11431
|
-
};
|
|
11432
|
-
const stream = new DataStream({
|
|
11433
|
-
logger: this.logger,
|
|
11434
|
-
mapLine: mapLine,
|
|
11435
|
-
pressure: {
|
|
11436
|
-
highWaterMark: 20,
|
|
11437
|
-
lowWaterMark: 10
|
|
11438
|
-
}
|
|
11439
|
-
});
|
|
11440
|
-
abortSignal?.addEventListener('abort', () => {
|
|
11441
|
-
closeReader();
|
|
11442
|
-
stream.close();
|
|
11443
|
-
});
|
|
11444
|
-
const decoder = this.createTextDecoder();
|
|
11445
|
-
let buffer = '';
|
|
11446
|
-
const consumeStream = async () => {
|
|
11447
|
-
while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
|
|
11448
|
-
const { done, value } = await reader.read();
|
|
11449
|
-
if (done) {
|
|
11450
|
-
const remaining = buffer.trim();
|
|
11451
|
-
if (remaining.length != 0) {
|
|
11452
|
-
stream.enqueueData(remaining);
|
|
11453
|
-
}
|
|
11454
|
-
stream.close();
|
|
11455
|
-
await closeReader();
|
|
11456
|
-
return;
|
|
11556
|
+
reader = res.body.getReader();
|
|
11557
|
+
const stream = {
|
|
11558
|
+
next: async () => {
|
|
11559
|
+
if (controller.signal.aborted) {
|
|
11560
|
+
return doneResult;
|
|
11457
11561
|
}
|
|
11458
|
-
|
|
11459
|
-
|
|
11460
|
-
const lines = buffer.split('\n');
|
|
11461
|
-
for (var i = 0; i < lines.length - 1; i++) {
|
|
11462
|
-
var l = lines[i].trim();
|
|
11463
|
-
if (l.length > 0) {
|
|
11464
|
-
stream.enqueueData(l);
|
|
11465
|
-
}
|
|
11562
|
+
try {
|
|
11563
|
+
return await reader.read();
|
|
11466
11564
|
}
|
|
11467
|
-
|
|
11468
|
-
|
|
11469
|
-
|
|
11470
|
-
|
|
11471
|
-
|
|
11472
|
-
|
|
11473
|
-
|
|
11474
|
-
dispose();
|
|
11475
|
-
},
|
|
11476
|
-
closed: () => {
|
|
11477
|
-
resolve();
|
|
11478
|
-
dispose();
|
|
11479
|
-
}
|
|
11480
|
-
});
|
|
11481
|
-
});
|
|
11565
|
+
catch (ex) {
|
|
11566
|
+
if (controller.signal.aborted) {
|
|
11567
|
+
// .read() completes with an error if we cancel the reader, which we do to disconnect. So this is just
|
|
11568
|
+
// things working as intended, we can return a done event and consider the exception handled.
|
|
11569
|
+
return doneResult;
|
|
11570
|
+
}
|
|
11571
|
+
throw ex;
|
|
11482
11572
|
}
|
|
11483
11573
|
}
|
|
11484
11574
|
};
|
|
11485
|
-
|
|
11486
|
-
|
|
11487
|
-
|
|
11488
|
-
|
|
11489
|
-
|
|
11490
|
-
|
|
11491
|
-
|
|
11492
|
-
|
|
11575
|
+
return { isBson: responseIsBson, stream };
|
|
11576
|
+
}
|
|
11577
|
+
/**
|
|
11578
|
+
* Posts a `/sync/stream` request.
|
|
11579
|
+
*
|
|
11580
|
+
* Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
|
|
11581
|
+
* {@link Uint8Array}s.
|
|
11582
|
+
*/
|
|
11583
|
+
async fetchStream(options) {
|
|
11584
|
+
const { isBson, stream } = await this.fetchStreamRaw(options);
|
|
11585
|
+
if (isBson) {
|
|
11586
|
+
return extractBsonObjects(stream);
|
|
11587
|
+
}
|
|
11588
|
+
else {
|
|
11589
|
+
return extractJsonLines(stream, this.createTextDecoder());
|
|
11590
|
+
}
|
|
11493
11591
|
}
|
|
11494
11592
|
}
|
|
11495
11593
|
|
|
@@ -11997,6 +12095,19 @@ The next upload iteration will be delayed.`);
|
|
|
11997
12095
|
}
|
|
11998
12096
|
});
|
|
11999
12097
|
}
|
|
12098
|
+
async receiveSyncLines(data) {
|
|
12099
|
+
const { options, connection, bson } = data;
|
|
12100
|
+
const remote = this.options.remote;
|
|
12101
|
+
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
12102
|
+
return await remote.fetchStream(options);
|
|
12103
|
+
}
|
|
12104
|
+
else {
|
|
12105
|
+
return await this.options.remote.socketStreamRaw({
|
|
12106
|
+
...options,
|
|
12107
|
+
...{ fetchStrategy: connection.fetchStrategy }
|
|
12108
|
+
}, bson);
|
|
12109
|
+
}
|
|
12110
|
+
}
|
|
12000
12111
|
async legacyStreamingSyncIteration(signal, resolvedOptions) {
|
|
12001
12112
|
const rawTables = resolvedOptions.serializedSchema?.raw_tables;
|
|
12002
12113
|
if (rawTables != null && rawTables.length) {
|
|
@@ -12026,42 +12137,27 @@ The next upload iteration will be delayed.`);
|
|
|
12026
12137
|
client_id: clientId
|
|
12027
12138
|
}
|
|
12028
12139
|
};
|
|
12029
|
-
|
|
12030
|
-
|
|
12031
|
-
|
|
12032
|
-
|
|
12033
|
-
|
|
12034
|
-
|
|
12035
|
-
|
|
12036
|
-
|
|
12037
|
-
|
|
12038
|
-
|
|
12039
|
-
|
|
12040
|
-
|
|
12041
|
-
|
|
12042
|
-
|
|
12043
|
-
stream = await this.options.remote.socketStreamRaw({
|
|
12044
|
-
...syncOptions,
|
|
12045
|
-
...{ fetchStrategy: resolvedOptions.fetchStrategy }
|
|
12046
|
-
}, (payload) => {
|
|
12047
|
-
if (payload instanceof Uint8Array) {
|
|
12048
|
-
return bson.deserialize(payload);
|
|
12049
|
-
}
|
|
12050
|
-
else {
|
|
12051
|
-
// Directly enqueued by us
|
|
12052
|
-
return payload;
|
|
12053
|
-
}
|
|
12054
|
-
}, bson);
|
|
12055
|
-
}
|
|
12140
|
+
const bson = await this.options.remote.getBSON();
|
|
12141
|
+
const source = await this.receiveSyncLines({
|
|
12142
|
+
options: syncOptions,
|
|
12143
|
+
connection: resolvedOptions,
|
|
12144
|
+
bson
|
|
12145
|
+
});
|
|
12146
|
+
const stream = injectable(map(source, (line) => {
|
|
12147
|
+
if (typeof line == 'string') {
|
|
12148
|
+
return JSON.parse(line);
|
|
12149
|
+
}
|
|
12150
|
+
else {
|
|
12151
|
+
return bson.deserialize(line);
|
|
12152
|
+
}
|
|
12153
|
+
}));
|
|
12056
12154
|
this.logger.debug('Stream established. Processing events');
|
|
12057
12155
|
this.notifyCompletedUploads = () => {
|
|
12058
|
-
|
|
12059
|
-
stream.enqueueData({ crud_upload_completed: null });
|
|
12060
|
-
}
|
|
12156
|
+
stream.inject({ crud_upload_completed: null });
|
|
12061
12157
|
};
|
|
12062
|
-
while (
|
|
12063
|
-
const line = await stream.
|
|
12064
|
-
if (
|
|
12158
|
+
while (true) {
|
|
12159
|
+
const { value: line, done } = await stream.next();
|
|
12160
|
+
if (done) {
|
|
12065
12161
|
// The stream has closed while waiting
|
|
12066
12162
|
return;
|
|
12067
12163
|
}
|
|
@@ -12240,14 +12336,17 @@ The next upload iteration will be delayed.`);
|
|
|
12240
12336
|
const syncImplementation = this;
|
|
12241
12337
|
const adapter = this.options.adapter;
|
|
12242
12338
|
const remote = this.options.remote;
|
|
12339
|
+
const controller = new AbortController();
|
|
12340
|
+
const abort = () => {
|
|
12341
|
+
return controller.abort(signal.reason);
|
|
12342
|
+
};
|
|
12343
|
+
signal.addEventListener('abort', abort);
|
|
12243
12344
|
let receivingLines = null;
|
|
12244
12345
|
let hadSyncLine = false;
|
|
12245
12346
|
let hideDisconnectOnRestart = false;
|
|
12246
12347
|
if (signal.aborted) {
|
|
12247
12348
|
throw new AbortOperation('Connection request has been aborted');
|
|
12248
12349
|
}
|
|
12249
|
-
const abortController = new AbortController();
|
|
12250
|
-
signal.addEventListener('abort', () => abortController.abort());
|
|
12251
12350
|
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
|
|
12252
12351
|
// invocation (local events include refreshed tokens and completed uploads).
|
|
12253
12352
|
// This is a single data stream so that we can handle all control calls from a single place.
|
|
@@ -12255,49 +12354,36 @@ The next upload iteration will be delayed.`);
|
|
|
12255
12354
|
async function connect(instr) {
|
|
12256
12355
|
const syncOptions = {
|
|
12257
12356
|
path: '/sync/stream',
|
|
12258
|
-
abortSignal:
|
|
12357
|
+
abortSignal: controller.signal,
|
|
12259
12358
|
data: instr.request
|
|
12260
12359
|
};
|
|
12261
|
-
|
|
12262
|
-
|
|
12263
|
-
|
|
12264
|
-
|
|
12265
|
-
|
|
12266
|
-
|
|
12267
|
-
|
|
12268
|
-
|
|
12269
|
-
|
|
12270
|
-
|
|
12271
|
-
|
|
12272
|
-
|
|
12273
|
-
|
|
12274
|
-
|
|
12275
|
-
|
|
12276
|
-
|
|
12277
|
-
|
|
12278
|
-
fetchStrategy: resolvedOptions.fetchStrategy
|
|
12279
|
-
}, (payload) => {
|
|
12280
|
-
if (payload instanceof Uint8Array) {
|
|
12281
|
-
return {
|
|
12282
|
-
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
12283
|
-
payload: payload
|
|
12284
|
-
};
|
|
12285
|
-
}
|
|
12286
|
-
else {
|
|
12287
|
-
// Directly enqueued by us
|
|
12288
|
-
return payload;
|
|
12289
|
-
}
|
|
12290
|
-
});
|
|
12291
|
-
}
|
|
12360
|
+
controlInvocations = injectable(map(await syncImplementation.receiveSyncLines({
|
|
12361
|
+
options: syncOptions,
|
|
12362
|
+
connection: resolvedOptions
|
|
12363
|
+
}), (line) => {
|
|
12364
|
+
if (typeof line == 'string') {
|
|
12365
|
+
return {
|
|
12366
|
+
command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
|
|
12367
|
+
payload: line
|
|
12368
|
+
};
|
|
12369
|
+
}
|
|
12370
|
+
else {
|
|
12371
|
+
return {
|
|
12372
|
+
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
12373
|
+
payload: line
|
|
12374
|
+
};
|
|
12375
|
+
}
|
|
12376
|
+
}));
|
|
12292
12377
|
// The rust client will set connected: true after the first sync line because that's when it gets invoked, but
|
|
12293
12378
|
// we're already connected here and can report that.
|
|
12294
12379
|
syncImplementation.updateSyncStatus({ connected: true });
|
|
12295
12380
|
try {
|
|
12296
|
-
while (
|
|
12297
|
-
|
|
12298
|
-
if (
|
|
12299
|
-
|
|
12381
|
+
while (true) {
|
|
12382
|
+
let event = await controlInvocations.next();
|
|
12383
|
+
if (event.done) {
|
|
12384
|
+
break;
|
|
12300
12385
|
}
|
|
12386
|
+
const line = event.value;
|
|
12301
12387
|
await control(line.command, line.payload);
|
|
12302
12388
|
if (!hadSyncLine) {
|
|
12303
12389
|
syncImplementation.triggerCrudUpload();
|
|
@@ -12306,12 +12392,8 @@ The next upload iteration will be delayed.`);
|
|
|
12306
12392
|
}
|
|
12307
12393
|
}
|
|
12308
12394
|
finally {
|
|
12309
|
-
|
|
12310
|
-
|
|
12311
|
-
// refreshed. That would throw after closing (and we can't handle those events either way), so set this back
|
|
12312
|
-
// to null.
|
|
12313
|
-
controlInvocations = null;
|
|
12314
|
-
await activeInstructions.close();
|
|
12395
|
+
abort();
|
|
12396
|
+
signal.removeEventListener('abort', abort);
|
|
12315
12397
|
}
|
|
12316
12398
|
}
|
|
12317
12399
|
async function stop() {
|
|
@@ -12355,14 +12437,14 @@ The next upload iteration will be delayed.`);
|
|
|
12355
12437
|
remote.invalidateCredentials();
|
|
12356
12438
|
// Restart iteration after the credentials have been refreshed.
|
|
12357
12439
|
remote.fetchCredentials().then((_) => {
|
|
12358
|
-
controlInvocations?.
|
|
12440
|
+
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
|
|
12359
12441
|
}, (err) => {
|
|
12360
12442
|
syncImplementation.logger.warn('Could not prefetch credentials', err);
|
|
12361
12443
|
});
|
|
12362
12444
|
}
|
|
12363
12445
|
}
|
|
12364
12446
|
else if ('CloseSyncStream' in instruction) {
|
|
12365
|
-
|
|
12447
|
+
controller.abort();
|
|
12366
12448
|
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
12367
12449
|
}
|
|
12368
12450
|
else if ('FlushFileSystem' in instruction) ;
|
|
@@ -12391,17 +12473,13 @@ The next upload iteration will be delayed.`);
|
|
|
12391
12473
|
}
|
|
12392
12474
|
await control(PowerSyncControlCommand.START, JSON.stringify(options));
|
|
12393
12475
|
this.notifyCompletedUploads = () => {
|
|
12394
|
-
|
|
12395
|
-
controlInvocations.enqueueData({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
12396
|
-
}
|
|
12476
|
+
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
12397
12477
|
};
|
|
12398
12478
|
this.handleActiveStreamsChange = () => {
|
|
12399
|
-
|
|
12400
|
-
|
|
12401
|
-
|
|
12402
|
-
|
|
12403
|
-
});
|
|
12404
|
-
}
|
|
12479
|
+
controlInvocations?.inject({
|
|
12480
|
+
command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
|
|
12481
|
+
payload: JSON.stringify(this.activeStreams)
|
|
12482
|
+
});
|
|
12405
12483
|
};
|
|
12406
12484
|
await receivingLines;
|
|
12407
12485
|
}
|
|
@@ -14594,5 +14672,5 @@ const parseQuery = (query, parameters) => {
|
|
|
14594
14672
|
return { sqlStatement, parameters: parameters };
|
|
14595
14673
|
};
|
|
14596
14674
|
|
|
14597
|
-
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,
|
|
14675
|
+
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, OpType, OpTypeEnum, OplogEntry, PSInternalTable, PowerSyncControlCommand, RowUpdateType, Schema, Semaphore, SqliteBucketStorage, SyncClientImplementation, SyncDataBatch, SyncDataBucket, SyncProgress, SyncStatus, SyncStreamConnectionMethod, SyncingService, Table, TableV2, TriggerManagerImpl, UpdateType, UploadQueueStats, WatchedQueryListenerEvent, attachmentFromSql, column, compilableQueryWatch, createBaseLogger, createLogger, extractTableUpdates, isBatchedUpdateNotification, isContinueCheckpointRequest, isDBAdapter, isPowerSyncDatabaseOptionsWithSettings, isSQLOpenFactory, isSQLOpenOptions, isStreamingKeepalive, isStreamingSyncCheckpoint, isStreamingSyncCheckpointComplete, isStreamingSyncCheckpointDiff, isStreamingSyncCheckpointPartiallyComplete, isStreamingSyncData, isSyncNewCheckpointRequest, parseQuery, runOnSchemaChange, sanitizeSQL, sanitizeUUID, timeoutSignal };
|
|
14598
14676
|
//# sourceMappingURL=bundle.mjs.map
|