@powersync/web 1.37.0 → 1.37.2

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.
@@ -2834,7 +2834,6 @@ __webpack_require__.r(__webpack_exports__);
2834
2834
  /* harmony export */ DEFAULT_LOCK_TIMEOUT_MS: () => (/* binding */ DEFAULT_LOCK_TIMEOUT_MS),
2835
2835
  /* harmony export */ DEFAULT_POWERSYNC_CLOSE_OPTIONS: () => (/* binding */ DEFAULT_POWERSYNC_CLOSE_OPTIONS),
2836
2836
  /* harmony export */ DEFAULT_POWERSYNC_DB_OPTIONS: () => (/* binding */ DEFAULT_POWERSYNC_DB_OPTIONS),
2837
- /* harmony export */ DEFAULT_PRESSURE_LIMITS: () => (/* binding */ DEFAULT_PRESSURE_LIMITS),
2838
2837
  /* harmony export */ DEFAULT_REMOTE_LOGGER: () => (/* binding */ DEFAULT_REMOTE_LOGGER),
2839
2838
  /* harmony export */ DEFAULT_REMOTE_OPTIONS: () => (/* binding */ DEFAULT_REMOTE_OPTIONS),
2840
2839
  /* harmony export */ DEFAULT_RETRY_DELAY_MS: () => (/* binding */ DEFAULT_RETRY_DELAY_MS),
@@ -2845,7 +2844,6 @@ __webpack_require__.r(__webpack_exports__);
2845
2844
  /* harmony export */ DEFAULT_TABLE_OPTIONS: () => (/* binding */ DEFAULT_TABLE_OPTIONS),
2846
2845
  /* harmony export */ DEFAULT_WATCH_QUERY_OPTIONS: () => (/* binding */ DEFAULT_WATCH_QUERY_OPTIONS),
2847
2846
  /* harmony export */ DEFAULT_WATCH_THROTTLE_MS: () => (/* binding */ DEFAULT_WATCH_THROTTLE_MS),
2848
- /* harmony export */ DataStream: () => (/* binding */ DataStream),
2849
2847
  /* harmony export */ DiffTriggerOperation: () => (/* binding */ DiffTriggerOperation),
2850
2848
  /* harmony export */ DifferentialQueryProcessor: () => (/* binding */ DifferentialQueryProcessor),
2851
2849
  /* harmony export */ EMPTY_DIFFERENTIAL: () => (/* binding */ EMPTY_DIFFERENTIAL),
@@ -2871,6 +2869,7 @@ __webpack_require__.r(__webpack_exports__);
2871
2869
  /* harmony export */ PowerSyncControlCommand: () => (/* binding */ PowerSyncControlCommand),
2872
2870
  /* harmony export */ RowUpdateType: () => (/* binding */ RowUpdateType),
2873
2871
  /* harmony export */ Schema: () => (/* binding */ Schema),
2872
+ /* harmony export */ Semaphore: () => (/* binding */ Semaphore),
2874
2873
  /* harmony export */ SqliteBucketStorage: () => (/* binding */ SqliteBucketStorage),
2875
2874
  /* harmony export */ SyncClientImplementation: () => (/* binding */ SyncClientImplementation),
2876
2875
  /* harmony export */ SyncDataBatch: () => (/* binding */ SyncDataBatch),
@@ -3692,19 +3691,69 @@ class SyncingService {
3692
3691
  }
3693
3692
 
3694
3693
  /**
3695
- * An asynchronous mutex implementation.
3694
+ * A simple fixed-capacity queue implementation.
3695
+ *
3696
+ * Unlike a naive queue implemented by `array.push()` and `array.shift()`, this avoids moving array elements around
3697
+ * and is `O(1)` for {@link addLast} and {@link removeFirst}.
3698
+ */
3699
+ class Queue {
3700
+ table;
3701
+ // Index of the first element in the table.
3702
+ head;
3703
+ // Amount of items currently in the queue.
3704
+ _length;
3705
+ constructor(initialItems) {
3706
+ this.table = [...initialItems];
3707
+ this.head = 0;
3708
+ this._length = this.table.length;
3709
+ }
3710
+ get isEmpty() {
3711
+ return this.length == 0;
3712
+ }
3713
+ get length() {
3714
+ return this._length;
3715
+ }
3716
+ removeFirst() {
3717
+ if (this.isEmpty) {
3718
+ throw new Error('Queue is empty');
3719
+ }
3720
+ const result = this.table[this.head];
3721
+ this._length--;
3722
+ this.table[this.head] = undefined;
3723
+ this.head = (this.head + 1) % this.table.length;
3724
+ return result;
3725
+ }
3726
+ addLast(element) {
3727
+ if (this.length == this.table.length) {
3728
+ throw new Error('Queue is full');
3729
+ }
3730
+ this.table[(this.head + this._length) % this.table.length] = element;
3731
+ this._length++;
3732
+ }
3733
+ }
3734
+
3735
+ /**
3736
+ * An asynchronous semaphore implementation with associated items per lease.
3696
3737
  *
3697
3738
  * @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
3698
3739
  */
3699
- class Mutex {
3700
- inCriticalSection = false;
3740
+ class Semaphore {
3741
+ // Available items that are not currently assigned to a waiter.
3742
+ available;
3743
+ size;
3701
3744
  // Linked list of waiters. We don't expect the wait list to become particularly large, and this allows removing
3702
3745
  // aborted waiters from the middle of the list efficiently.
3703
3746
  firstWaiter;
3704
3747
  lastWaiter;
3705
- addWaiter(onAcquire) {
3748
+ constructor(elements) {
3749
+ this.available = new Queue(elements);
3750
+ this.size = this.available.length;
3751
+ }
3752
+ addWaiter(requestedItems, onAcquire) {
3706
3753
  const node = {
3707
3754
  isActive: true,
3755
+ acquiredItems: [],
3756
+ remainingItems: requestedItems,
3708
3757
  onAcquire,
3709
3758
  prev: this.lastWaiter
3710
3759
  };
@@ -3730,52 +3779,92 @@ class Mutex {
3730
3779
  if (waiter == this.lastWaiter)
3731
3780
  this.lastWaiter = prev;
3732
3781
  }
3733
- acquire(abort) {
3782
+ requestPermits(amount, abort) {
3783
+ if (amount <= 0 || amount > this.size) {
3784
+ throw new Error(`Invalid amount of items requested (${amount}), must be between 1 and ${this.size}`);
3785
+ }
3734
3786
  return new Promise((resolve, reject) => {
3735
3787
  function rejectAborted() {
3736
- reject(abort?.reason ?? new Error('Mutex acquire aborted'));
3788
+ reject(abort?.reason ?? new Error('Semaphore acquire aborted'));
3737
3789
  }
3738
3790
  if (abort?.aborted) {
3739
3791
  return rejectAborted();
3740
3792
  }
3741
- let holdsMutex = false;
3793
+ let waiter;
3742
3794
  const markCompleted = () => {
3743
- if (!holdsMutex)
3744
- return;
3745
- holdsMutex = false;
3746
- const waiter = this.firstWaiter;
3747
- if (waiter) {
3748
- this.deactivateWaiter(waiter);
3749
- // Still in critical section, but owned by next waiter now.
3750
- waiter.onAcquire();
3795
+ const items = waiter.acquiredItems;
3796
+ waiter.acquiredItems = []; // Avoid releasing items twice.
3797
+ for (const element of items) {
3798
+ // Give to next waiter, if possible.
3799
+ const nextWaiter = this.firstWaiter;
3800
+ if (nextWaiter) {
3801
+ nextWaiter.acquiredItems.push(element);
3802
+ nextWaiter.remainingItems--;
3803
+ if (nextWaiter.remainingItems == 0) {
3804
+ nextWaiter.onAcquire();
3805
+ }
3806
+ }
3807
+ else {
3808
+ // No pending waiter, return lease into pool.
3809
+ this.available.addLast(element);
3810
+ }
3751
3811
  }
3752
- else {
3753
- this.inCriticalSection = false;
3812
+ };
3813
+ const onAbort = () => {
3814
+ abort?.removeEventListener('abort', onAbort);
3815
+ if (waiter.isActive) {
3816
+ this.deactivateWaiter(waiter);
3817
+ rejectAborted();
3754
3818
  }
3755
3819
  };
3756
- if (!this.inCriticalSection) {
3757
- this.inCriticalSection = true;
3758
- holdsMutex = true;
3759
- return resolve(markCompleted);
3820
+ const resolvePromise = () => {
3821
+ this.deactivateWaiter(waiter);
3822
+ abort?.removeEventListener('abort', onAbort);
3823
+ const items = waiter.acquiredItems;
3824
+ resolve({ items, release: markCompleted });
3825
+ };
3826
+ waiter = this.addWaiter(amount, resolvePromise);
3827
+ // If there are items in the pool that haven't been assigned, we can pull them into this waiter. Note that this is
3828
+ // only the case if we're the first waiter (otherwise, items would have been assigned to an earlier waiter).
3829
+ while (!this.available.isEmpty && waiter.remainingItems > 0) {
3830
+ waiter.acquiredItems.push(this.available.removeFirst());
3831
+ waiter.remainingItems--;
3760
3832
  }
3761
- else {
3762
- let node;
3763
- const onAbort = () => {
3764
- abort?.removeEventListener('abort', onAbort);
3765
- if (node.isActive) {
3766
- this.deactivateWaiter(node);
3767
- rejectAborted();
3768
- }
3769
- };
3770
- node = this.addWaiter(() => {
3771
- abort?.removeEventListener('abort', onAbort);
3772
- holdsMutex = true;
3773
- resolve(markCompleted);
3774
- });
3775
- abort?.addEventListener('abort', onAbort);
3833
+ if (waiter.remainingItems == 0) {
3834
+ return resolvePromise();
3776
3835
  }
3836
+ abort?.addEventListener('abort', onAbort);
3777
3837
  });
3778
3838
  }
3839
+ /**
3840
+ * Requests a single item from the pool.
3841
+ *
3842
+ * The returned `release` callback must be invoked to return the item into the pool.
3843
+ */
3844
+ async requestOne(abort) {
3845
+ const { items, release } = await this.requestPermits(1, abort);
3846
+ return { release, item: items[0] };
3847
+ }
3848
+ /**
3849
+ * Requests access to all items from the pool.
3850
+ *
3851
+ * The returned `release` callback must be invoked to return items into the pool.
3852
+ */
3853
+ requestAll(abort) {
3854
+ return this.requestPermits(this.size, abort);
3855
+ }
3856
+ }
3857
+ /**
3858
+ * An asynchronous mutex implementation.
3859
+ *
3860
+ * @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
3861
+ */
3862
+ class Mutex {
3863
+ inner = new Semaphore([null]);
3864
+ async acquire(abort) {
3865
+ const { release } = await this.inner.requestOne(abort);
3866
+ return release;
3867
+ }
3779
3868
  async runExclusive(fn, abort) {
3780
3869
  const returnMutex = await this.acquire(abort);
3781
3870
  try {
@@ -4218,6 +4307,8 @@ var EncodingType;
4218
4307
  EncodingType["Base64"] = "base64";
4219
4308
  })(EncodingType || (EncodingType = {}));
4220
4309
 
4310
+ const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
4311
+
4221
4312
  function getDefaultExportFromCjs (x) {
4222
4313
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
4223
4314
  }
@@ -4298,7 +4389,7 @@ function requireEventIterator () {
4298
4389
  this.removeCallback();
4299
4390
  });
4300
4391
  }
4301
- [Symbol.asyncIterator]() {
4392
+ [symbolAsyncIterator]() {
4302
4393
  return {
4303
4394
  next: (value) => {
4304
4395
  const result = this.pushQueue.shift();
@@ -4345,7 +4436,7 @@ function requireEventIterator () {
4345
4436
  queue.eventHandlers[event] = fn;
4346
4437
  },
4347
4438
  }) || (() => { });
4348
- this[Symbol.asyncIterator] = () => queue[Symbol.asyncIterator]();
4439
+ this[symbolAsyncIterator] = () => queue[symbolAsyncIterator]();
4349
4440
  Object.freeze(this);
4350
4441
  }
4351
4442
  }
@@ -5227,15 +5318,6 @@ class ControlledExecutor {
5227
5318
  }
5228
5319
  }
5229
5320
 
5230
- /**
5231
- * A ponyfill for `Symbol.asyncIterator` that is compatible with the
5232
- * [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)
5233
- * we recommend for React Native.
5234
- *
5235
- * As long as we use this symbol (instead of `for await` and `async *`) in this package, we can be compatible with async
5236
- * iterators without requiring them.
5237
- */
5238
- const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
5239
5321
  /**
5240
5322
  * Throttle a function to be called at most once every "wait" milliseconds,
5241
5323
  * on the trailing edge.
@@ -13572,177 +13654,10 @@ function requireDist () {
13572
13654
 
13573
13655
  var distExports = requireDist();
13574
13656
 
13575
- var version = "1.50.0";
13657
+ var version = "1.52.0";
13576
13658
  var PACKAGE = {
13577
13659
  version: version};
13578
13660
 
13579
- const DEFAULT_PRESSURE_LIMITS = {
13580
- highWater: 10,
13581
- lowWater: 0
13582
- };
13583
- /**
13584
- * A very basic implementation of a data stream with backpressure support which does not use
13585
- * native JS streams or async iterators.
13586
- * This is handy for environments such as React Native which need polyfills for the above.
13587
- */
13588
- class DataStream extends BaseObserver {
13589
- options;
13590
- dataQueue;
13591
- isClosed;
13592
- processingPromise;
13593
- notifyDataAdded;
13594
- logger;
13595
- mapLine;
13596
- constructor(options) {
13597
- super();
13598
- this.options = options;
13599
- this.processingPromise = null;
13600
- this.isClosed = false;
13601
- this.dataQueue = [];
13602
- this.mapLine = options?.mapLine ?? ((line) => line);
13603
- this.logger = options?.logger ?? Logger.get('DataStream');
13604
- if (options?.closeOnError) {
13605
- const l = this.registerListener({
13606
- error: (ex) => {
13607
- l?.();
13608
- this.close();
13609
- }
13610
- });
13611
- }
13612
- }
13613
- get highWatermark() {
13614
- return this.options?.pressure?.highWaterMark ?? DEFAULT_PRESSURE_LIMITS.highWater;
13615
- }
13616
- get lowWatermark() {
13617
- return this.options?.pressure?.lowWaterMark ?? DEFAULT_PRESSURE_LIMITS.lowWater;
13618
- }
13619
- get closed() {
13620
- return this.isClosed;
13621
- }
13622
- async close() {
13623
- this.isClosed = true;
13624
- await this.processingPromise;
13625
- this.iterateListeners((l) => l.closed?.());
13626
- // Discard any data in the queue
13627
- this.dataQueue = [];
13628
- this.listeners.clear();
13629
- }
13630
- /**
13631
- * Enqueues data for the consumers to read
13632
- */
13633
- enqueueData(data) {
13634
- if (this.isClosed) {
13635
- throw new Error('Cannot enqueue data into closed stream.');
13636
- }
13637
- this.dataQueue.push(data);
13638
- this.notifyDataAdded?.();
13639
- this.processQueue();
13640
- }
13641
- /**
13642
- * Reads data once from the data stream
13643
- * @returns a Data payload or Null if the stream closed.
13644
- */
13645
- async read() {
13646
- if (this.closed) {
13647
- return null;
13648
- }
13649
- // Wait for any pending processing to complete first.
13650
- // This ensures we register our listener before calling processQueue(),
13651
- // avoiding a race where processQueue() sees no reader and returns early.
13652
- if (this.processingPromise) {
13653
- await this.processingPromise;
13654
- }
13655
- // Re-check after await - stream may have closed while we were waiting
13656
- if (this.closed) {
13657
- return null;
13658
- }
13659
- return new Promise((resolve, reject) => {
13660
- const l = this.registerListener({
13661
- data: async (data) => {
13662
- resolve(data);
13663
- // Remove the listener
13664
- l?.();
13665
- },
13666
- closed: () => {
13667
- resolve(null);
13668
- l?.();
13669
- },
13670
- error: (ex) => {
13671
- reject(ex);
13672
- l?.();
13673
- }
13674
- });
13675
- this.processQueue();
13676
- });
13677
- }
13678
- /**
13679
- * Executes a callback for each data item in the stream
13680
- */
13681
- forEach(callback) {
13682
- if (this.dataQueue.length <= this.lowWatermark) {
13683
- this.iterateAsyncErrored(async (l) => l.lowWater?.());
13684
- }
13685
- return this.registerListener({
13686
- data: callback
13687
- });
13688
- }
13689
- processQueue() {
13690
- if (this.processingPromise) {
13691
- return;
13692
- }
13693
- const promise = (this.processingPromise = this._processQueue());
13694
- promise.finally(() => {
13695
- this.processingPromise = null;
13696
- });
13697
- return promise;
13698
- }
13699
- hasDataReader() {
13700
- return Array.from(this.listeners.values()).some((l) => !!l.data);
13701
- }
13702
- async _processQueue() {
13703
- /**
13704
- * Allow listeners to mutate the queue before processing.
13705
- * This allows for operations such as dropping or compressing data
13706
- * on high water or requesting more data on low water.
13707
- */
13708
- if (this.dataQueue.length >= this.highWatermark) {
13709
- await this.iterateAsyncErrored(async (l) => l.highWater?.());
13710
- }
13711
- if (this.isClosed || !this.hasDataReader()) {
13712
- return;
13713
- }
13714
- if (this.dataQueue.length) {
13715
- const data = this.dataQueue.shift();
13716
- const mapped = this.mapLine(data);
13717
- await this.iterateAsyncErrored(async (l) => l.data?.(mapped));
13718
- }
13719
- if (this.dataQueue.length <= this.lowWatermark) {
13720
- const dataAdded = new Promise((resolve) => {
13721
- this.notifyDataAdded = resolve;
13722
- });
13723
- await Promise.race([this.iterateAsyncErrored(async (l) => l.lowWater?.()), dataAdded]);
13724
- this.notifyDataAdded = null;
13725
- }
13726
- if (this.dataQueue.length > 0) {
13727
- setTimeout(() => this.processQueue());
13728
- }
13729
- }
13730
- async iterateAsyncErrored(cb) {
13731
- // Important: We need to copy the listeners, as calling a listener could result in adding another
13732
- // listener, resulting in infinite loops.
13733
- const listeners = Array.from(this.listeners.values());
13734
- for (let i of listeners) {
13735
- try {
13736
- await cb(i);
13737
- }
13738
- catch (ex) {
13739
- this.logger.error(ex);
13740
- this.iterateListeners((l) => l.error?.(ex));
13741
- }
13742
- }
13743
- }
13744
- }
13745
-
13746
13661
  var WebsocketDuplexConnection = {};
13747
13662
 
13748
13663
  var hasRequiredWebsocketDuplexConnection;
@@ -13905,8 +13820,215 @@ class WebsocketClientTransport {
13905
13820
  }
13906
13821
  }
13907
13822
 
13823
+ const doneResult = { done: true, value: undefined };
13824
+ function valueResult(value) {
13825
+ return { done: false, value };
13826
+ }
13827
+ /**
13828
+ * A variant of {@link Array.map} for async iterators.
13829
+ */
13830
+ function map(source, map) {
13831
+ return {
13832
+ next: async () => {
13833
+ const value = await source.next();
13834
+ if (value.done) {
13835
+ return value;
13836
+ }
13837
+ else {
13838
+ return { value: map(value.value) };
13839
+ }
13840
+ }
13841
+ };
13842
+ }
13843
+ /**
13844
+ * Expands a source async iterator by allowing to inject events asynchronously.
13845
+ *
13846
+ * The resulting iterator will emit all events from its source. Additionally though, events can be injected. These
13847
+ * events are dropped once the main iterator completes, but are otherwise forwarded.
13848
+ *
13849
+ * The iterator completes when its source completes, and it supports backpressure by only calling `next()` on the source
13850
+ * in response to a `next()` call from downstream if no pending injected events can be dispatched.
13851
+ */
13852
+ function injectable(source) {
13853
+ let sourceIsDone = false;
13854
+ let waiter = undefined; // An active, waiting next() call.
13855
+ // A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
13856
+ let pendingSourceEvent = null;
13857
+ let pendingInjectedEvents = [];
13858
+ const consumeWaiter = () => {
13859
+ const pending = waiter;
13860
+ waiter = undefined;
13861
+ return pending;
13862
+ };
13863
+ const fetchFromSource = () => {
13864
+ const resolveWaiter = (propagate) => {
13865
+ const active = consumeWaiter();
13866
+ if (active) {
13867
+ propagate(active);
13868
+ }
13869
+ else {
13870
+ pendingSourceEvent = propagate;
13871
+ }
13872
+ };
13873
+ const nextFromSource = source.next();
13874
+ nextFromSource.then((value) => {
13875
+ sourceIsDone = value.done == true;
13876
+ resolveWaiter((w) => w.resolve(value));
13877
+ }, (error) => {
13878
+ resolveWaiter((w) => w.reject(error));
13879
+ });
13880
+ };
13881
+ return {
13882
+ next: () => {
13883
+ return new Promise((resolve, reject) => {
13884
+ // First priority: Dispatch ready upstream events.
13885
+ if (sourceIsDone) {
13886
+ return resolve(doneResult);
13887
+ }
13888
+ if (pendingSourceEvent) {
13889
+ pendingSourceEvent({ resolve, reject });
13890
+ pendingSourceEvent = null;
13891
+ return;
13892
+ }
13893
+ // Second priority: Dispatch injected events
13894
+ if (pendingInjectedEvents.length) {
13895
+ return resolve(valueResult(pendingInjectedEvents.shift()));
13896
+ }
13897
+ // Nothing pending? Fetch from source
13898
+ waiter = { resolve, reject };
13899
+ return fetchFromSource();
13900
+ });
13901
+ },
13902
+ inject: (event) => {
13903
+ const pending = consumeWaiter();
13904
+ if (pending != null) {
13905
+ pending.resolve(valueResult(event));
13906
+ }
13907
+ else {
13908
+ pendingInjectedEvents.push(event);
13909
+ }
13910
+ }
13911
+ };
13912
+ }
13913
+ /**
13914
+ * Splits a byte stream at line endings, emitting each line as a string.
13915
+ */
13916
+ function extractJsonLines(source, decoder) {
13917
+ let buffer = '';
13918
+ const pendingLines = [];
13919
+ let isFinalEvent = false;
13920
+ return {
13921
+ next: async () => {
13922
+ while (true) {
13923
+ if (isFinalEvent) {
13924
+ return doneResult;
13925
+ }
13926
+ {
13927
+ const first = pendingLines.shift();
13928
+ if (first) {
13929
+ return { done: false, value: first };
13930
+ }
13931
+ }
13932
+ const { done, value } = await source.next();
13933
+ if (done) {
13934
+ const remaining = buffer.trim();
13935
+ if (remaining.length != 0) {
13936
+ isFinalEvent = true;
13937
+ return { done: false, value: remaining };
13938
+ }
13939
+ return doneResult;
13940
+ }
13941
+ const data = decoder.decode(value, { stream: true });
13942
+ buffer += data;
13943
+ const lines = buffer.split('\n');
13944
+ for (let i = 0; i < lines.length - 1; i++) {
13945
+ const l = lines[i].trim();
13946
+ if (l.length > 0) {
13947
+ pendingLines.push(l);
13948
+ }
13949
+ }
13950
+ buffer = lines[lines.length - 1];
13951
+ }
13952
+ }
13953
+ };
13954
+ }
13955
+ /**
13956
+ * Splits a concatenated stream of BSON objects by emitting individual objects.
13957
+ */
13958
+ function extractBsonObjects(source) {
13959
+ // Fully read but not emitted yet.
13960
+ const completedObjects = [];
13961
+ // Whether source has returned { done: true }. We do the same once completed objects have been emitted.
13962
+ let isDone = false;
13963
+ const lengthBuffer = new DataView(new ArrayBuffer(4));
13964
+ let objectBody = null;
13965
+ // If we're parsing the length field, a number between 1 and 4 (inclusive) describing remaining bytes in the header.
13966
+ // If we're consuming a document, the bytes remaining.
13967
+ let remainingLength = 4;
13968
+ return {
13969
+ async next() {
13970
+ while (true) {
13971
+ // Before fetching new data from upstream, return completed objects.
13972
+ if (completedObjects.length) {
13973
+ return valueResult(completedObjects.shift());
13974
+ }
13975
+ if (isDone) {
13976
+ return doneResult;
13977
+ }
13978
+ const upstreamEvent = await source.next();
13979
+ if (upstreamEvent.done) {
13980
+ isDone = true;
13981
+ if (objectBody || remainingLength != 4) {
13982
+ throw new Error('illegal end of stream in BSON object');
13983
+ }
13984
+ return doneResult;
13985
+ }
13986
+ const chunk = upstreamEvent.value;
13987
+ for (let i = 0; i < chunk.length;) {
13988
+ const availableInData = chunk.length - i;
13989
+ if (objectBody) {
13990
+ // We're in the middle of reading a BSON document.
13991
+ const bytesToRead = Math.min(availableInData, remainingLength);
13992
+ const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
13993
+ objectBody.set(copySource, objectBody.length - remainingLength);
13994
+ i += bytesToRead;
13995
+ remainingLength -= bytesToRead;
13996
+ if (remainingLength == 0) {
13997
+ completedObjects.push(objectBody);
13998
+ // Prepare to read another document, starting with its length
13999
+ objectBody = null;
14000
+ remainingLength = 4;
14001
+ }
14002
+ }
14003
+ else {
14004
+ // Copy up to 4 bytes into lengthBuffer, depending on how many we still need.
14005
+ const bytesToRead = Math.min(availableInData, remainingLength);
14006
+ for (let j = 0; j < bytesToRead; j++) {
14007
+ lengthBuffer.setUint8(4 - remainingLength + j, chunk[i + j]);
14008
+ }
14009
+ i += bytesToRead;
14010
+ remainingLength -= bytesToRead;
14011
+ if (remainingLength == 0) {
14012
+ // Transition from reading length header to reading document. Subtracting 4 because the length of the
14013
+ // header is included in length.
14014
+ const length = lengthBuffer.getInt32(0, true /* little endian */);
14015
+ remainingLength = length - 4;
14016
+ if (remainingLength < 1) {
14017
+ throw new Error(`invalid length for bson: ${length}`);
14018
+ }
14019
+ objectBody = new Uint8Array(length);
14020
+ new DataView(objectBody.buffer).setInt32(0, length, true);
14021
+ }
14022
+ }
14023
+ }
14024
+ }
14025
+ }
14026
+ };
14027
+ }
14028
+
13908
14029
  const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
13909
14030
  const POWERSYNC_JS_VERSION = PACKAGE.version;
14031
+ const SYNC_QUEUE_REQUEST_HIGH_WATER = 10;
13910
14032
  const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
13911
14033
  // Keep alive message is sent every period
13912
14034
  const KEEP_ALIVE_MS = 20_000;
@@ -14086,13 +14208,14 @@ class AbstractRemote {
14086
14208
  return new WebSocket(url);
14087
14209
  }
14088
14210
  /**
14089
- * Returns a data stream of sync line data.
14211
+ * Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
14212
+ *
14213
+ * The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
14090
14214
  *
14091
- * @param map Maps received payload frames to the typed event value.
14092
14215
  * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
14093
14216
  * (required for compatibility with older sync services).
14094
14217
  */
14095
- async socketStreamRaw(options, map, bson) {
14218
+ async socketStreamRaw(options, bson) {
14096
14219
  const { path, fetchStrategy = FetchStrategy.Buffered } = options;
14097
14220
  const mimeType = bson == null ? 'application/json' : 'application/bson';
14098
14221
  function toBuffer(js) {
@@ -14107,52 +14230,55 @@ class AbstractRemote {
14107
14230
  }
14108
14231
  const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
14109
14232
  const request = await this.buildRequest(path);
14233
+ const url = this.options.socketUrlTransformer(request.url);
14110
14234
  // Add the user agent in the setup payload - we can't set custom
14111
14235
  // headers with websockets on web. The browser userAgent is however added
14112
14236
  // automatically as a header.
14113
14237
  const userAgent = this.getUserAgent();
14114
- const stream = new DataStream({
14115
- logger: this.logger,
14116
- pressure: {
14117
- lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER
14118
- },
14119
- mapLine: map
14120
- });
14238
+ // While we're connecting (a process that can't be aborted in RSocket), the WebSocket instance to close if we wanted
14239
+ // to abort the connection.
14240
+ let pendingSocket = null;
14241
+ let keepAliveTimeout;
14242
+ let rsocket = null;
14243
+ let queue = null;
14244
+ let didClose = false;
14245
+ const abortRequest = () => {
14246
+ if (didClose) {
14247
+ return;
14248
+ }
14249
+ didClose = true;
14250
+ clearTimeout(keepAliveTimeout);
14251
+ if (pendingSocket) {
14252
+ pendingSocket.close();
14253
+ }
14254
+ if (rsocket) {
14255
+ rsocket.close();
14256
+ }
14257
+ if (queue) {
14258
+ queue.stop();
14259
+ }
14260
+ };
14121
14261
  // Handle upstream abort
14122
- if (options.abortSignal?.aborted) {
14262
+ if (options.abortSignal.aborted) {
14123
14263
  throw new AbortOperation('Connection request aborted');
14124
14264
  }
14125
14265
  else {
14126
- options.abortSignal?.addEventListener('abort', () => {
14127
- stream.close();
14128
- }, { once: true });
14266
+ options.abortSignal.addEventListener('abort', abortRequest);
14129
14267
  }
14130
- let keepAliveTimeout;
14131
14268
  const resetTimeout = () => {
14132
14269
  clearTimeout(keepAliveTimeout);
14133
14270
  keepAliveTimeout = setTimeout(() => {
14134
14271
  this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
14135
- stream.close();
14272
+ abortRequest();
14136
14273
  }, SOCKET_TIMEOUT_MS);
14137
14274
  };
14138
14275
  resetTimeout();
14139
- // Typescript complains about this being `never` if it's not assigned here.
14140
- // This is assigned in `wsCreator`.
14141
- let disposeSocketConnectionTimeout = () => { };
14142
- const url = this.options.socketUrlTransformer(request.url);
14143
14276
  const connector = new distExports.RSocketConnector({
14144
14277
  transport: new WebsocketClientTransport({
14145
14278
  url,
14146
14279
  wsCreator: (url) => {
14147
- const socket = this.createSocket(url);
14148
- disposeSocketConnectionTimeout = stream.registerListener({
14149
- closed: () => {
14150
- // Allow closing the underlying WebSocket if the stream was closed before the
14151
- // RSocket connect completed. This should effectively abort the request.
14152
- socket.close();
14153
- }
14154
- });
14155
- socket.addEventListener('message', (event) => {
14280
+ const socket = (pendingSocket = this.createSocket(url));
14281
+ socket.addEventListener('message', () => {
14156
14282
  resetTimeout();
14157
14283
  });
14158
14284
  return socket;
@@ -14172,43 +14298,40 @@ class AbstractRemote {
14172
14298
  }
14173
14299
  }
14174
14300
  });
14175
- let rsocket;
14176
14301
  try {
14177
14302
  rsocket = await connector.connect();
14178
14303
  // The connection is established, we no longer need to monitor the initial timeout
14179
- disposeSocketConnectionTimeout();
14304
+ pendingSocket = null;
14180
14305
  }
14181
14306
  catch (ex) {
14182
14307
  this.logger.error(`Failed to connect WebSocket`, ex);
14183
- clearTimeout(keepAliveTimeout);
14184
- if (!stream.closed) {
14185
- await stream.close();
14186
- }
14308
+ abortRequest();
14187
14309
  throw ex;
14188
14310
  }
14189
14311
  resetTimeout();
14190
- let socketIsClosed = false;
14191
- const closeSocket = () => {
14192
- clearTimeout(keepAliveTimeout);
14193
- if (socketIsClosed) {
14194
- return;
14195
- }
14196
- socketIsClosed = true;
14197
- rsocket.close();
14198
- };
14199
14312
  // Helps to prevent double close scenarios
14200
- rsocket.onClose(() => (socketIsClosed = true));
14201
- // We initially request this amount and expect these to arrive eventually
14202
- let pendingEventsCount = syncQueueRequestSize;
14203
- const disposeClosedListener = stream.registerListener({
14204
- closed: () => {
14205
- closeSocket();
14206
- disposeClosedListener();
14207
- }
14208
- });
14209
- const socket = await new Promise((resolve, reject) => {
14313
+ rsocket.onClose(() => (rsocket = null));
14314
+ return await new Promise((resolve, reject) => {
14210
14315
  let connectionEstablished = false;
14211
- const res = rsocket.requestStream({
14316
+ let pendingEventsCount = syncQueueRequestSize;
14317
+ let paused = false;
14318
+ let res = null;
14319
+ function requestMore() {
14320
+ const delta = syncQueueRequestSize - pendingEventsCount;
14321
+ if (!paused && delta > 0) {
14322
+ res?.request(delta);
14323
+ pendingEventsCount = syncQueueRequestSize;
14324
+ }
14325
+ }
14326
+ const events = new domExports.EventIterator((q) => {
14327
+ queue = q;
14328
+ q.on('highWater', () => (paused = true));
14329
+ q.on('lowWater', () => {
14330
+ paused = false;
14331
+ requestMore();
14332
+ });
14333
+ }, { highWaterMark: SYNC_QUEUE_REQUEST_HIGH_WATER, lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER })[symbolAsyncIterator]();
14334
+ res = rsocket.requestStream({
14212
14335
  data: toBuffer(options.data),
14213
14336
  metadata: toBuffer({
14214
14337
  path
@@ -14233,7 +14356,7 @@ class AbstractRemote {
14233
14356
  }
14234
14357
  // RSocket will close the RSocket stream automatically
14235
14358
  // Close the downstream stream as well - this will close the RSocket connection and WebSocket
14236
- stream.close();
14359
+ abortRequest();
14237
14360
  // Handles cases where the connection failed e.g. auth error or connection error
14238
14361
  if (!connectionEstablished) {
14239
14362
  reject(e);
@@ -14243,41 +14366,40 @@ class AbstractRemote {
14243
14366
  // The connection is active
14244
14367
  if (!connectionEstablished) {
14245
14368
  connectionEstablished = true;
14246
- resolve(res);
14369
+ resolve(events);
14247
14370
  }
14248
14371
  const { data } = payload;
14372
+ if (data) {
14373
+ queue.push(data);
14374
+ }
14249
14375
  // Less events are now pending
14250
14376
  pendingEventsCount--;
14251
- if (!data) {
14252
- return;
14253
- }
14254
- stream.enqueueData(data);
14377
+ // Request another event (unless the downstream consumer is paused).
14378
+ requestMore();
14255
14379
  },
14256
14380
  onComplete: () => {
14257
- stream.close();
14381
+ abortRequest(); // this will also emit a done event
14258
14382
  },
14259
14383
  onExtension: () => { }
14260
14384
  });
14261
14385
  });
14262
- const l = stream.registerListener({
14263
- lowWater: async () => {
14264
- // Request to fill up the queue
14265
- const required = syncQueueRequestSize - pendingEventsCount;
14266
- if (required > 0) {
14267
- socket.request(syncQueueRequestSize - pendingEventsCount);
14268
- pendingEventsCount = syncQueueRequestSize;
14269
- }
14270
- },
14271
- closed: () => {
14272
- l();
14273
- }
14274
- });
14275
- return stream;
14276
14386
  }
14277
14387
  /**
14278
- * Connects to the sync/stream http endpoint, mapping and emitting each received string line.
14388
+ * @returns Whether the HTTP implementation on this platform can receive streamed binary responses. This is true on
14389
+ * all platforms except React Native (who would have guessed...), where we must not request BSON responses.
14390
+ *
14391
+ * @see https://github.com/react-native-community/fetch?tab=readme-ov-file#motivation
14279
14392
  */
14280
- async postStreamRaw(options, mapLine) {
14393
+ get supportsStreamingBinaryResponses() {
14394
+ return true;
14395
+ }
14396
+ /**
14397
+ * Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
14398
+ * async iterator of byte blobs.
14399
+ *
14400
+ * To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
14401
+ */
14402
+ async fetchStreamRaw(options) {
14281
14403
  const { data, path, headers, abortSignal } = options;
14282
14404
  const request = await this.buildRequest(path);
14283
14405
  /**
@@ -14289,119 +14411,94 @@ class AbstractRemote {
14289
14411
  * Aborting the active fetch request while it is being consumed seems to throw
14290
14412
  * an unhandled exception on the window level.
14291
14413
  */
14292
- if (abortSignal?.aborted) {
14293
- throw new AbortOperation('Abort request received before making postStreamRaw request');
14414
+ if (abortSignal.aborted) {
14415
+ throw new AbortOperation('Abort request received before making fetchStreamRaw request');
14294
14416
  }
14295
14417
  const controller = new AbortController();
14296
- let requestResolved = false;
14297
- abortSignal?.addEventListener('abort', () => {
14298
- if (!requestResolved) {
14418
+ let reader = null;
14419
+ abortSignal.addEventListener('abort', () => {
14420
+ const reason = abortSignal.reason ??
14421
+ new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.');
14422
+ if (reader == null) {
14299
14423
  // Only abort via the abort controller if the request has not resolved yet
14300
- controller.abort(abortSignal.reason ??
14301
- new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.'));
14424
+ controller.abort(reason);
14425
+ }
14426
+ else {
14427
+ reader.cancel(reason).catch(() => {
14428
+ // Cancelling the reader might rethrow an exception we would have handled by throwing in next(). So we can
14429
+ // ignore it here.
14430
+ });
14302
14431
  }
14303
14432
  });
14304
- const res = await this.fetch(request.url, {
14305
- method: 'POST',
14306
- headers: { ...headers, ...request.headers },
14307
- body: JSON.stringify(data),
14308
- signal: controller.signal,
14309
- cache: 'no-store',
14310
- ...(this.options.fetchOptions ?? {}),
14311
- ...options.fetchOptions
14312
- }).catch((ex) => {
14433
+ let res;
14434
+ let responseIsBson = false;
14435
+ try {
14436
+ const ndJson = 'application/x-ndjson';
14437
+ const bson = 'application/vnd.powersync.bson-stream';
14438
+ res = await this.fetch(request.url, {
14439
+ method: 'POST',
14440
+ headers: {
14441
+ ...headers,
14442
+ ...request.headers,
14443
+ accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
14444
+ },
14445
+ body: JSON.stringify(data),
14446
+ signal: controller.signal,
14447
+ cache: 'no-store',
14448
+ ...(this.options.fetchOptions ?? {}),
14449
+ ...options.fetchOptions
14450
+ });
14451
+ if (!res.ok || !res.body) {
14452
+ const text = await res.text();
14453
+ this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
14454
+ const error = new Error(`HTTP ${res.statusText}: ${text}`);
14455
+ error.status = res.status;
14456
+ throw error;
14457
+ }
14458
+ const contentType = res.headers.get('content-type');
14459
+ responseIsBson = contentType == bson;
14460
+ }
14461
+ catch (ex) {
14313
14462
  if (ex.name == 'AbortError') {
14314
14463
  throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
14315
14464
  }
14316
14465
  throw ex;
14317
- });
14318
- if (!res) {
14319
- throw new Error('Fetch request was aborted');
14320
- }
14321
- requestResolved = true;
14322
- if (!res.ok || !res.body) {
14323
- const text = await res.text();
14324
- this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
14325
- const error = new Error(`HTTP ${res.statusText}: ${text}`);
14326
- error.status = res.status;
14327
- throw error;
14328
14466
  }
14329
- // Create a new stream splitting the response at line endings while also handling cancellations
14330
- // by closing the reader.
14331
- const reader = res.body.getReader();
14332
- let readerReleased = false;
14333
- // This will close the network request and read stream
14334
- const closeReader = async () => {
14335
- try {
14336
- readerReleased = true;
14337
- await reader.cancel();
14338
- }
14339
- catch (ex) {
14340
- // an error will throw if the reader hasn't been used yet
14341
- }
14342
- reader.releaseLock();
14343
- };
14344
- const stream = new DataStream({
14345
- logger: this.logger,
14346
- mapLine: mapLine,
14347
- pressure: {
14348
- highWaterMark: 20,
14349
- lowWaterMark: 10
14350
- }
14351
- });
14352
- abortSignal?.addEventListener('abort', () => {
14353
- closeReader();
14354
- stream.close();
14355
- });
14356
- const decoder = this.createTextDecoder();
14357
- let buffer = '';
14358
- const consumeStream = async () => {
14359
- while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
14360
- const { done, value } = await reader.read();
14361
- if (done) {
14362
- const remaining = buffer.trim();
14363
- if (remaining.length != 0) {
14364
- stream.enqueueData(remaining);
14365
- }
14366
- stream.close();
14367
- await closeReader();
14368
- return;
14467
+ reader = res.body.getReader();
14468
+ const stream = {
14469
+ next: async () => {
14470
+ if (controller.signal.aborted) {
14471
+ return doneResult;
14369
14472
  }
14370
- const data = decoder.decode(value, { stream: true });
14371
- buffer += data;
14372
- const lines = buffer.split('\n');
14373
- for (var i = 0; i < lines.length - 1; i++) {
14374
- var l = lines[i].trim();
14375
- if (l.length > 0) {
14376
- stream.enqueueData(l);
14377
- }
14473
+ try {
14474
+ return await reader.read();
14378
14475
  }
14379
- buffer = lines[lines.length - 1];
14380
- // Implement backpressure by waiting for the low water mark to be reached
14381
- if (stream.dataQueue.length > stream.highWatermark) {
14382
- await new Promise((resolve) => {
14383
- const dispose = stream.registerListener({
14384
- lowWater: async () => {
14385
- resolve();
14386
- dispose();
14387
- },
14388
- closed: () => {
14389
- resolve();
14390
- dispose();
14391
- }
14392
- });
14393
- });
14476
+ catch (ex) {
14477
+ if (controller.signal.aborted) {
14478
+ // .read() completes with an error if we cancel the reader, which we do to disconnect. So this is just
14479
+ // things working as intended, we can return a done event and consider the exception handled.
14480
+ return doneResult;
14481
+ }
14482
+ throw ex;
14394
14483
  }
14395
14484
  }
14396
14485
  };
14397
- consumeStream().catch(ex => this.logger.error('Error consuming stream', ex));
14398
- const l = stream.registerListener({
14399
- closed: () => {
14400
- closeReader();
14401
- l?.();
14402
- }
14403
- });
14404
- return stream;
14486
+ return { isBson: responseIsBson, stream };
14487
+ }
14488
+ /**
14489
+ * Posts a `/sync/stream` request.
14490
+ *
14491
+ * Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
14492
+ * {@link Uint8Array}s.
14493
+ */
14494
+ async fetchStream(options) {
14495
+ const { isBson, stream } = await this.fetchStreamRaw(options);
14496
+ if (isBson) {
14497
+ return extractBsonObjects(stream);
14498
+ }
14499
+ else {
14500
+ return extractJsonLines(stream, this.createTextDecoder());
14501
+ }
14405
14502
  }
14406
14503
  }
14407
14504
 
@@ -14909,6 +15006,19 @@ The next upload iteration will be delayed.`);
14909
15006
  }
14910
15007
  });
14911
15008
  }
15009
+ async receiveSyncLines(data) {
15010
+ const { options, connection, bson } = data;
15011
+ const remote = this.options.remote;
15012
+ if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
15013
+ return await remote.fetchStream(options);
15014
+ }
15015
+ else {
15016
+ return await this.options.remote.socketStreamRaw({
15017
+ ...options,
15018
+ ...{ fetchStrategy: connection.fetchStrategy }
15019
+ }, bson);
15020
+ }
15021
+ }
14912
15022
  async legacyStreamingSyncIteration(signal, resolvedOptions) {
14913
15023
  const rawTables = resolvedOptions.serializedSchema?.raw_tables;
14914
15024
  if (rawTables != null && rawTables.length) {
@@ -14938,42 +15048,27 @@ The next upload iteration will be delayed.`);
14938
15048
  client_id: clientId
14939
15049
  }
14940
15050
  };
14941
- let stream;
14942
- if (resolvedOptions?.connectionMethod == SyncStreamConnectionMethod.HTTP) {
14943
- stream = await this.options.remote.postStreamRaw(syncOptions, (line) => {
14944
- if (typeof line == 'string') {
14945
- return JSON.parse(line);
14946
- }
14947
- else {
14948
- // Directly enqueued by us
14949
- return line;
14950
- }
14951
- });
14952
- }
14953
- else {
14954
- const bson = await this.options.remote.getBSON();
14955
- stream = await this.options.remote.socketStreamRaw({
14956
- ...syncOptions,
14957
- ...{ fetchStrategy: resolvedOptions.fetchStrategy }
14958
- }, (payload) => {
14959
- if (payload instanceof Uint8Array) {
14960
- return bson.deserialize(payload);
14961
- }
14962
- else {
14963
- // Directly enqueued by us
14964
- return payload;
14965
- }
14966
- }, bson);
14967
- }
15051
+ const bson = await this.options.remote.getBSON();
15052
+ const source = await this.receiveSyncLines({
15053
+ options: syncOptions,
15054
+ connection: resolvedOptions,
15055
+ bson
15056
+ });
15057
+ const stream = injectable(map(source, (line) => {
15058
+ if (typeof line == 'string') {
15059
+ return JSON.parse(line);
15060
+ }
15061
+ else {
15062
+ return bson.deserialize(line);
15063
+ }
15064
+ }));
14968
15065
  this.logger.debug('Stream established. Processing events');
14969
15066
  this.notifyCompletedUploads = () => {
14970
- if (!stream.closed) {
14971
- stream.enqueueData({ crud_upload_completed: null });
14972
- }
15067
+ stream.inject({ crud_upload_completed: null });
14973
15068
  };
14974
- while (!stream.closed) {
14975
- const line = await stream.read();
14976
- if (!line) {
15069
+ while (true) {
15070
+ const { value: line, done } = await stream.next();
15071
+ if (done) {
14977
15072
  // The stream has closed while waiting
14978
15073
  return;
14979
15074
  }
@@ -15152,14 +15247,17 @@ The next upload iteration will be delayed.`);
15152
15247
  const syncImplementation = this;
15153
15248
  const adapter = this.options.adapter;
15154
15249
  const remote = this.options.remote;
15250
+ const controller = new AbortController();
15251
+ const abort = () => {
15252
+ return controller.abort(signal.reason);
15253
+ };
15254
+ signal.addEventListener('abort', abort);
15155
15255
  let receivingLines = null;
15156
15256
  let hadSyncLine = false;
15157
15257
  let hideDisconnectOnRestart = false;
15158
15258
  if (signal.aborted) {
15159
15259
  throw new AbortOperation('Connection request has been aborted');
15160
15260
  }
15161
- const abortController = new AbortController();
15162
- signal.addEventListener('abort', () => abortController.abort());
15163
15261
  // Pending sync lines received from the service, as well as local events that trigger a powersync_control
15164
15262
  // invocation (local events include refreshed tokens and completed uploads).
15165
15263
  // This is a single data stream so that we can handle all control calls from a single place.
@@ -15167,49 +15265,36 @@ The next upload iteration will be delayed.`);
15167
15265
  async function connect(instr) {
15168
15266
  const syncOptions = {
15169
15267
  path: '/sync/stream',
15170
- abortSignal: abortController.signal,
15268
+ abortSignal: controller.signal,
15171
15269
  data: instr.request
15172
15270
  };
15173
- if (resolvedOptions.connectionMethod == SyncStreamConnectionMethod.HTTP) {
15174
- controlInvocations = await remote.postStreamRaw(syncOptions, (line) => {
15175
- if (typeof line == 'string') {
15176
- return {
15177
- command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
15178
- payload: line
15179
- };
15180
- }
15181
- else {
15182
- // Directly enqueued by us
15183
- return line;
15184
- }
15185
- });
15186
- }
15187
- else {
15188
- controlInvocations = await remote.socketStreamRaw({
15189
- ...syncOptions,
15190
- fetchStrategy: resolvedOptions.fetchStrategy
15191
- }, (payload) => {
15192
- if (payload instanceof Uint8Array) {
15193
- return {
15194
- command: PowerSyncControlCommand.PROCESS_BSON_LINE,
15195
- payload: payload
15196
- };
15197
- }
15198
- else {
15199
- // Directly enqueued by us
15200
- return payload;
15201
- }
15202
- });
15203
- }
15271
+ controlInvocations = injectable(map(await syncImplementation.receiveSyncLines({
15272
+ options: syncOptions,
15273
+ connection: resolvedOptions
15274
+ }), (line) => {
15275
+ if (typeof line == 'string') {
15276
+ return {
15277
+ command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
15278
+ payload: line
15279
+ };
15280
+ }
15281
+ else {
15282
+ return {
15283
+ command: PowerSyncControlCommand.PROCESS_BSON_LINE,
15284
+ payload: line
15285
+ };
15286
+ }
15287
+ }));
15204
15288
  // The rust client will set connected: true after the first sync line because that's when it gets invoked, but
15205
15289
  // we're already connected here and can report that.
15206
15290
  syncImplementation.updateSyncStatus({ connected: true });
15207
15291
  try {
15208
- while (!controlInvocations.closed) {
15209
- const line = await controlInvocations.read();
15210
- if (line == null) {
15211
- return;
15292
+ while (true) {
15293
+ let event = await controlInvocations.next();
15294
+ if (event.done) {
15295
+ break;
15212
15296
  }
15297
+ const line = event.value;
15213
15298
  await control(line.command, line.payload);
15214
15299
  if (!hadSyncLine) {
15215
15300
  syncImplementation.triggerCrudUpload();
@@ -15218,12 +15303,8 @@ The next upload iteration will be delayed.`);
15218
15303
  }
15219
15304
  }
15220
15305
  finally {
15221
- const activeInstructions = controlInvocations;
15222
- // We concurrently add events to the active data stream when e.g. a CRUD upload is completed or a token is
15223
- // refreshed. That would throw after closing (and we can't handle those events either way), so set this back
15224
- // to null.
15225
- controlInvocations = null;
15226
- await activeInstructions.close();
15306
+ abort();
15307
+ signal.removeEventListener('abort', abort);
15227
15308
  }
15228
15309
  }
15229
15310
  async function stop() {
@@ -15267,14 +15348,14 @@ The next upload iteration will be delayed.`);
15267
15348
  remote.invalidateCredentials();
15268
15349
  // Restart iteration after the credentials have been refreshed.
15269
15350
  remote.fetchCredentials().then((_) => {
15270
- controlInvocations?.enqueueData({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
15351
+ controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
15271
15352
  }, (err) => {
15272
15353
  syncImplementation.logger.warn('Could not prefetch credentials', err);
15273
15354
  });
15274
15355
  }
15275
15356
  }
15276
15357
  else if ('CloseSyncStream' in instruction) {
15277
- abortController.abort();
15358
+ controller.abort();
15278
15359
  hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
15279
15360
  }
15280
15361
  else if ('FlushFileSystem' in instruction) ;
@@ -15303,17 +15384,13 @@ The next upload iteration will be delayed.`);
15303
15384
  }
15304
15385
  await control(PowerSyncControlCommand.START, JSON.stringify(options));
15305
15386
  this.notifyCompletedUploads = () => {
15306
- if (controlInvocations && !controlInvocations?.closed) {
15307
- controlInvocations.enqueueData({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
15308
- }
15387
+ controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
15309
15388
  };
15310
15389
  this.handleActiveStreamsChange = () => {
15311
- if (controlInvocations && !controlInvocations?.closed) {
15312
- controlInvocations.enqueueData({
15313
- command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
15314
- payload: JSON.stringify(this.activeStreams)
15315
- });
15316
- }
15390
+ controlInvocations?.inject({
15391
+ command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
15392
+ payload: JSON.stringify(this.activeStreams)
15393
+ });
15317
15394
  };
15318
15395
  await receivingLines;
15319
15396
  }