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