@powersync/common 1.51.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 (37) hide show
  1. package/dist/bundle.cjs +431 -445
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +432 -444
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +429 -445
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +430 -444
  8. package/dist/bundle.node.mjs.map +1 -1
  9. package/dist/index.d.cts +41 -70
  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/stream_transform.d.ts +39 -0
  25. package/lib/utils/stream_transform.js +206 -0
  26. package/lib/utils/stream_transform.js.map +1 -0
  27. package/package.json +9 -7
  28. package/src/client/AbstractPowerSyncDatabase.ts +3 -3
  29. package/src/client/sync/stream/AbstractRemote.ts +182 -206
  30. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +82 -83
  31. package/src/index.ts +1 -1
  32. package/src/utils/async.ts +0 -11
  33. package/src/utils/stream_transform.ts +252 -0
  34. package/lib/utils/DataStream.d.ts +0 -62
  35. package/lib/utils/DataStream.js +0 -169
  36. package/lib/utils/DataStream.js.map +0 -1
  37. package/src/utils/DataStream.ts +0 -222
package/dist/bundle.cjs CHANGED
@@ -1398,6 +1398,8 @@ exports.EncodingType = void 0;
1398
1398
  EncodingType["Base64"] = "base64";
1399
1399
  })(exports.EncodingType || (exports.EncodingType = {}));
1400
1400
 
1401
+ const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
1402
+
1401
1403
  function getDefaultExportFromCjs (x) {
1402
1404
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
1403
1405
  }
@@ -1478,7 +1480,7 @@ function requireEventIterator () {
1478
1480
  this.removeCallback();
1479
1481
  });
1480
1482
  }
1481
- [Symbol.asyncIterator]() {
1483
+ [symbolAsyncIterator]() {
1482
1484
  return {
1483
1485
  next: (value) => {
1484
1486
  const result = this.pushQueue.shift();
@@ -1525,7 +1527,7 @@ function requireEventIterator () {
1525
1527
  queue.eventHandlers[event] = fn;
1526
1528
  },
1527
1529
  }) || (() => { });
1528
- this[Symbol.asyncIterator] = () => queue[Symbol.asyncIterator]();
1530
+ this[symbolAsyncIterator] = () => queue[symbolAsyncIterator]();
1529
1531
  Object.freeze(this);
1530
1532
  }
1531
1533
  }
@@ -2407,15 +2409,6 @@ class ControlledExecutor {
2407
2409
  }
2408
2410
  }
2409
2411
 
2410
- /**
2411
- * A ponyfill for `Symbol.asyncIterator` that is compatible with the
2412
- * [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)
2413
- * we recommend for React Native.
2414
- *
2415
- * As long as we use this symbol (instead of `for await` and `async *`) in this package, we can be compatible with async
2416
- * iterators without requiring them.
2417
- */
2418
- const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
2419
2412
  /**
2420
2413
  * Throttle a function to be called at most once every "wait" milliseconds,
2421
2414
  * on the trailing edge.
@@ -10752,177 +10745,10 @@ function requireDist () {
10752
10745
 
10753
10746
  var distExports = requireDist();
10754
10747
 
10755
- var version = "1.51.0";
10748
+ var version = "1.52.0";
10756
10749
  var PACKAGE = {
10757
10750
  version: version};
10758
10751
 
10759
- const DEFAULT_PRESSURE_LIMITS = {
10760
- highWater: 10,
10761
- lowWater: 0
10762
- };
10763
- /**
10764
- * A very basic implementation of a data stream with backpressure support which does not use
10765
- * native JS streams or async iterators.
10766
- * This is handy for environments such as React Native which need polyfills for the above.
10767
- */
10768
- class DataStream extends BaseObserver {
10769
- options;
10770
- dataQueue;
10771
- isClosed;
10772
- processingPromise;
10773
- notifyDataAdded;
10774
- logger;
10775
- mapLine;
10776
- constructor(options) {
10777
- super();
10778
- this.options = options;
10779
- this.processingPromise = null;
10780
- this.isClosed = false;
10781
- this.dataQueue = [];
10782
- this.mapLine = options?.mapLine ?? ((line) => line);
10783
- this.logger = options?.logger ?? Logger.get('DataStream');
10784
- if (options?.closeOnError) {
10785
- const l = this.registerListener({
10786
- error: (ex) => {
10787
- l?.();
10788
- this.close();
10789
- }
10790
- });
10791
- }
10792
- }
10793
- get highWatermark() {
10794
- return this.options?.pressure?.highWaterMark ?? DEFAULT_PRESSURE_LIMITS.highWater;
10795
- }
10796
- get lowWatermark() {
10797
- return this.options?.pressure?.lowWaterMark ?? DEFAULT_PRESSURE_LIMITS.lowWater;
10798
- }
10799
- get closed() {
10800
- return this.isClosed;
10801
- }
10802
- async close() {
10803
- this.isClosed = true;
10804
- await this.processingPromise;
10805
- this.iterateListeners((l) => l.closed?.());
10806
- // Discard any data in the queue
10807
- this.dataQueue = [];
10808
- this.listeners.clear();
10809
- }
10810
- /**
10811
- * Enqueues data for the consumers to read
10812
- */
10813
- enqueueData(data) {
10814
- if (this.isClosed) {
10815
- throw new Error('Cannot enqueue data into closed stream.');
10816
- }
10817
- this.dataQueue.push(data);
10818
- this.notifyDataAdded?.();
10819
- this.processQueue();
10820
- }
10821
- /**
10822
- * Reads data once from the data stream
10823
- * @returns a Data payload or Null if the stream closed.
10824
- */
10825
- async read() {
10826
- if (this.closed) {
10827
- return null;
10828
- }
10829
- // Wait for any pending processing to complete first.
10830
- // This ensures we register our listener before calling processQueue(),
10831
- // avoiding a race where processQueue() sees no reader and returns early.
10832
- if (this.processingPromise) {
10833
- await this.processingPromise;
10834
- }
10835
- // Re-check after await - stream may have closed while we were waiting
10836
- if (this.closed) {
10837
- return null;
10838
- }
10839
- return new Promise((resolve, reject) => {
10840
- const l = this.registerListener({
10841
- data: async (data) => {
10842
- resolve(data);
10843
- // Remove the listener
10844
- l?.();
10845
- },
10846
- closed: () => {
10847
- resolve(null);
10848
- l?.();
10849
- },
10850
- error: (ex) => {
10851
- reject(ex);
10852
- l?.();
10853
- }
10854
- });
10855
- this.processQueue();
10856
- });
10857
- }
10858
- /**
10859
- * Executes a callback for each data item in the stream
10860
- */
10861
- forEach(callback) {
10862
- if (this.dataQueue.length <= this.lowWatermark) {
10863
- this.iterateAsyncErrored(async (l) => l.lowWater?.());
10864
- }
10865
- return this.registerListener({
10866
- data: callback
10867
- });
10868
- }
10869
- processQueue() {
10870
- if (this.processingPromise) {
10871
- return;
10872
- }
10873
- const promise = (this.processingPromise = this._processQueue());
10874
- promise.finally(() => {
10875
- this.processingPromise = null;
10876
- });
10877
- return promise;
10878
- }
10879
- hasDataReader() {
10880
- return Array.from(this.listeners.values()).some((l) => !!l.data);
10881
- }
10882
- async _processQueue() {
10883
- /**
10884
- * Allow listeners to mutate the queue before processing.
10885
- * This allows for operations such as dropping or compressing data
10886
- * on high water or requesting more data on low water.
10887
- */
10888
- if (this.dataQueue.length >= this.highWatermark) {
10889
- await this.iterateAsyncErrored(async (l) => l.highWater?.());
10890
- }
10891
- if (this.isClosed || !this.hasDataReader()) {
10892
- return;
10893
- }
10894
- if (this.dataQueue.length) {
10895
- const data = this.dataQueue.shift();
10896
- const mapped = this.mapLine(data);
10897
- await this.iterateAsyncErrored(async (l) => l.data?.(mapped));
10898
- }
10899
- if (this.dataQueue.length <= this.lowWatermark) {
10900
- const dataAdded = new Promise((resolve) => {
10901
- this.notifyDataAdded = resolve;
10902
- });
10903
- await Promise.race([this.iterateAsyncErrored(async (l) => l.lowWater?.()), dataAdded]);
10904
- this.notifyDataAdded = null;
10905
- }
10906
- if (this.dataQueue.length > 0) {
10907
- setTimeout(() => this.processQueue());
10908
- }
10909
- }
10910
- async iterateAsyncErrored(cb) {
10911
- // Important: We need to copy the listeners, as calling a listener could result in adding another
10912
- // listener, resulting in infinite loops.
10913
- const listeners = Array.from(this.listeners.values());
10914
- for (let i of listeners) {
10915
- try {
10916
- await cb(i);
10917
- }
10918
- catch (ex) {
10919
- this.logger.error(ex);
10920
- this.iterateListeners((l) => l.error?.(ex));
10921
- }
10922
- }
10923
- }
10924
- }
10925
-
10926
10752
  var WebsocketDuplexConnection = {};
10927
10753
 
10928
10754
  var hasRequiredWebsocketDuplexConnection;
@@ -11085,8 +10911,215 @@ class WebsocketClientTransport {
11085
10911
  }
11086
10912
  }
11087
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
+
11088
11120
  const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
11089
11121
  const POWERSYNC_JS_VERSION = PACKAGE.version;
11122
+ const SYNC_QUEUE_REQUEST_HIGH_WATER = 10;
11090
11123
  const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
11091
11124
  // Keep alive message is sent every period
11092
11125
  const KEEP_ALIVE_MS = 20_000;
@@ -11266,13 +11299,14 @@ class AbstractRemote {
11266
11299
  return new WebSocket(url);
11267
11300
  }
11268
11301
  /**
11269
- * 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}.
11270
11305
  *
11271
- * @param map Maps received payload frames to the typed event value.
11272
11306
  * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
11273
11307
  * (required for compatibility with older sync services).
11274
11308
  */
11275
- async socketStreamRaw(options, map, bson) {
11309
+ async socketStreamRaw(options, bson) {
11276
11310
  const { path, fetchStrategy = exports.FetchStrategy.Buffered } = options;
11277
11311
  const mimeType = bson == null ? 'application/json' : 'application/bson';
11278
11312
  function toBuffer(js) {
@@ -11287,52 +11321,55 @@ class AbstractRemote {
11287
11321
  }
11288
11322
  const syncQueueRequestSize = fetchStrategy == exports.FetchStrategy.Buffered ? 10 : 1;
11289
11323
  const request = await this.buildRequest(path);
11324
+ const url = this.options.socketUrlTransformer(request.url);
11290
11325
  // Add the user agent in the setup payload - we can't set custom
11291
11326
  // headers with websockets on web. The browser userAgent is however added
11292
11327
  // automatically as a header.
11293
11328
  const userAgent = this.getUserAgent();
11294
- const stream = new DataStream({
11295
- logger: this.logger,
11296
- pressure: {
11297
- lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER
11298
- },
11299
- mapLine: map
11300
- });
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
+ };
11301
11352
  // Handle upstream abort
11302
- if (options.abortSignal?.aborted) {
11353
+ if (options.abortSignal.aborted) {
11303
11354
  throw new AbortOperation('Connection request aborted');
11304
11355
  }
11305
11356
  else {
11306
- options.abortSignal?.addEventListener('abort', () => {
11307
- stream.close();
11308
- }, { once: true });
11357
+ options.abortSignal.addEventListener('abort', abortRequest);
11309
11358
  }
11310
- let keepAliveTimeout;
11311
11359
  const resetTimeout = () => {
11312
11360
  clearTimeout(keepAliveTimeout);
11313
11361
  keepAliveTimeout = setTimeout(() => {
11314
11362
  this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
11315
- stream.close();
11363
+ abortRequest();
11316
11364
  }, SOCKET_TIMEOUT_MS);
11317
11365
  };
11318
11366
  resetTimeout();
11319
- // Typescript complains about this being `never` if it's not assigned here.
11320
- // This is assigned in `wsCreator`.
11321
- let disposeSocketConnectionTimeout = () => { };
11322
- const url = this.options.socketUrlTransformer(request.url);
11323
11367
  const connector = new distExports.RSocketConnector({
11324
11368
  transport: new WebsocketClientTransport({
11325
11369
  url,
11326
11370
  wsCreator: (url) => {
11327
- const socket = this.createSocket(url);
11328
- disposeSocketConnectionTimeout = stream.registerListener({
11329
- closed: () => {
11330
- // Allow closing the underlying WebSocket if the stream was closed before the
11331
- // RSocket connect completed. This should effectively abort the request.
11332
- socket.close();
11333
- }
11334
- });
11335
- socket.addEventListener('message', (event) => {
11371
+ const socket = (pendingSocket = this.createSocket(url));
11372
+ socket.addEventListener('message', () => {
11336
11373
  resetTimeout();
11337
11374
  });
11338
11375
  return socket;
@@ -11352,43 +11389,40 @@ class AbstractRemote {
11352
11389
  }
11353
11390
  }
11354
11391
  });
11355
- let rsocket;
11356
11392
  try {
11357
11393
  rsocket = await connector.connect();
11358
11394
  // The connection is established, we no longer need to monitor the initial timeout
11359
- disposeSocketConnectionTimeout();
11395
+ pendingSocket = null;
11360
11396
  }
11361
11397
  catch (ex) {
11362
11398
  this.logger.error(`Failed to connect WebSocket`, ex);
11363
- clearTimeout(keepAliveTimeout);
11364
- if (!stream.closed) {
11365
- await stream.close();
11366
- }
11399
+ abortRequest();
11367
11400
  throw ex;
11368
11401
  }
11369
11402
  resetTimeout();
11370
- let socketIsClosed = false;
11371
- const closeSocket = () => {
11372
- clearTimeout(keepAliveTimeout);
11373
- if (socketIsClosed) {
11374
- return;
11375
- }
11376
- socketIsClosed = true;
11377
- rsocket.close();
11378
- };
11379
11403
  // Helps to prevent double close scenarios
11380
- rsocket.onClose(() => (socketIsClosed = true));
11381
- // We initially request this amount and expect these to arrive eventually
11382
- let pendingEventsCount = syncQueueRequestSize;
11383
- const disposeClosedListener = stream.registerListener({
11384
- closed: () => {
11385
- closeSocket();
11386
- disposeClosedListener();
11387
- }
11388
- });
11389
- const socket = await new Promise((resolve, reject) => {
11404
+ rsocket.onClose(() => (rsocket = null));
11405
+ return await new Promise((resolve, reject) => {
11390
11406
  let connectionEstablished = false;
11391
- 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({
11392
11426
  data: toBuffer(options.data),
11393
11427
  metadata: toBuffer({
11394
11428
  path
@@ -11413,7 +11447,7 @@ class AbstractRemote {
11413
11447
  }
11414
11448
  // RSocket will close the RSocket stream automatically
11415
11449
  // Close the downstream stream as well - this will close the RSocket connection and WebSocket
11416
- stream.close();
11450
+ abortRequest();
11417
11451
  // Handles cases where the connection failed e.g. auth error or connection error
11418
11452
  if (!connectionEstablished) {
11419
11453
  reject(e);
@@ -11423,41 +11457,40 @@ class AbstractRemote {
11423
11457
  // The connection is active
11424
11458
  if (!connectionEstablished) {
11425
11459
  connectionEstablished = true;
11426
- resolve(res);
11460
+ resolve(events);
11427
11461
  }
11428
11462
  const { data } = payload;
11463
+ if (data) {
11464
+ queue.push(data);
11465
+ }
11429
11466
  // Less events are now pending
11430
11467
  pendingEventsCount--;
11431
- if (!data) {
11432
- return;
11433
- }
11434
- stream.enqueueData(data);
11468
+ // Request another event (unless the downstream consumer is paused).
11469
+ requestMore();
11435
11470
  },
11436
11471
  onComplete: () => {
11437
- stream.close();
11472
+ abortRequest(); // this will also emit a done event
11438
11473
  },
11439
11474
  onExtension: () => { }
11440
11475
  });
11441
11476
  });
11442
- const l = stream.registerListener({
11443
- lowWater: async () => {
11444
- // Request to fill up the queue
11445
- const required = syncQueueRequestSize - pendingEventsCount;
11446
- if (required > 0) {
11447
- socket.request(syncQueueRequestSize - pendingEventsCount);
11448
- pendingEventsCount = syncQueueRequestSize;
11449
- }
11450
- },
11451
- closed: () => {
11452
- l();
11453
- }
11454
- });
11455
- return stream;
11456
11477
  }
11457
11478
  /**
11458
- * 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
11483
+ */
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.
11459
11492
  */
11460
- async postStreamRaw(options, mapLine) {
11493
+ async fetchStreamRaw(options) {
11461
11494
  const { data, path, headers, abortSignal } = options;
11462
11495
  const request = await this.buildRequest(path);
11463
11496
  /**
@@ -11469,119 +11502,94 @@ class AbstractRemote {
11469
11502
  * Aborting the active fetch request while it is being consumed seems to throw
11470
11503
  * an unhandled exception on the window level.
11471
11504
  */
11472
- if (abortSignal?.aborted) {
11473
- 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');
11474
11507
  }
11475
11508
  const controller = new AbortController();
11476
- let requestResolved = false;
11477
- abortSignal?.addEventListener('abort', () => {
11478
- 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) {
11479
11514
  // Only abort via the abort controller if the request has not resolved yet
11480
- controller.abort(abortSignal.reason ??
11481
- 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
+ });
11482
11522
  }
11483
11523
  });
11484
- const res = await this.fetch(request.url, {
11485
- method: 'POST',
11486
- headers: { ...headers, ...request.headers },
11487
- body: JSON.stringify(data),
11488
- signal: controller.signal,
11489
- cache: 'no-store',
11490
- ...(this.options.fetchOptions ?? {}),
11491
- ...options.fetchOptions
11492
- }).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) {
11493
11553
  if (ex.name == 'AbortError') {
11494
11554
  throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
11495
11555
  }
11496
11556
  throw ex;
11497
- });
11498
- if (!res) {
11499
- throw new Error('Fetch request was aborted');
11500
- }
11501
- requestResolved = true;
11502
- if (!res.ok || !res.body) {
11503
- const text = await res.text();
11504
- this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
11505
- const error = new Error(`HTTP ${res.statusText}: ${text}`);
11506
- error.status = res.status;
11507
- throw error;
11508
11557
  }
11509
- // Create a new stream splitting the response at line endings while also handling cancellations
11510
- // by closing the reader.
11511
- const reader = res.body.getReader();
11512
- let readerReleased = false;
11513
- // This will close the network request and read stream
11514
- const closeReader = async () => {
11515
- try {
11516
- readerReleased = true;
11517
- await reader.cancel();
11518
- }
11519
- catch (ex) {
11520
- // an error will throw if the reader hasn't been used yet
11521
- }
11522
- reader.releaseLock();
11523
- };
11524
- const stream = new DataStream({
11525
- logger: this.logger,
11526
- mapLine: mapLine,
11527
- pressure: {
11528
- highWaterMark: 20,
11529
- lowWaterMark: 10
11530
- }
11531
- });
11532
- abortSignal?.addEventListener('abort', () => {
11533
- closeReader();
11534
- stream.close();
11535
- });
11536
- const decoder = this.createTextDecoder();
11537
- let buffer = '';
11538
- const consumeStream = async () => {
11539
- while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
11540
- const { done, value } = await reader.read();
11541
- if (done) {
11542
- const remaining = buffer.trim();
11543
- if (remaining.length != 0) {
11544
- stream.enqueueData(remaining);
11545
- }
11546
- stream.close();
11547
- await closeReader();
11548
- return;
11558
+ reader = res.body.getReader();
11559
+ const stream = {
11560
+ next: async () => {
11561
+ if (controller.signal.aborted) {
11562
+ return doneResult;
11549
11563
  }
11550
- const data = decoder.decode(value, { stream: true });
11551
- buffer += data;
11552
- const lines = buffer.split('\n');
11553
- for (var i = 0; i < lines.length - 1; i++) {
11554
- var l = lines[i].trim();
11555
- if (l.length > 0) {
11556
- stream.enqueueData(l);
11557
- }
11564
+ try {
11565
+ return await reader.read();
11558
11566
  }
11559
- buffer = lines[lines.length - 1];
11560
- // Implement backpressure by waiting for the low water mark to be reached
11561
- if (stream.dataQueue.length > stream.highWatermark) {
11562
- await new Promise((resolve) => {
11563
- const dispose = stream.registerListener({
11564
- lowWater: async () => {
11565
- resolve();
11566
- dispose();
11567
- },
11568
- closed: () => {
11569
- resolve();
11570
- dispose();
11571
- }
11572
- });
11573
- });
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;
11574
11574
  }
11575
11575
  }
11576
11576
  };
11577
- consumeStream().catch(ex => this.logger.error('Error consuming stream', ex));
11578
- const l = stream.registerListener({
11579
- closed: () => {
11580
- closeReader();
11581
- l?.();
11582
- }
11583
- });
11584
- 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
+ }
11585
11593
  }
11586
11594
  }
11587
11595
 
@@ -12089,6 +12097,19 @@ The next upload iteration will be delayed.`);
12089
12097
  }
12090
12098
  });
12091
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
+ }
12092
12113
  async legacyStreamingSyncIteration(signal, resolvedOptions) {
12093
12114
  const rawTables = resolvedOptions.serializedSchema?.raw_tables;
12094
12115
  if (rawTables != null && rawTables.length) {
@@ -12118,42 +12139,27 @@ The next upload iteration will be delayed.`);
12118
12139
  client_id: clientId
12119
12140
  }
12120
12141
  };
12121
- let stream;
12122
- if (resolvedOptions?.connectionMethod == exports.SyncStreamConnectionMethod.HTTP) {
12123
- stream = await this.options.remote.postStreamRaw(syncOptions, (line) => {
12124
- if (typeof line == 'string') {
12125
- return JSON.parse(line);
12126
- }
12127
- else {
12128
- // Directly enqueued by us
12129
- return line;
12130
- }
12131
- });
12132
- }
12133
- else {
12134
- const bson = await this.options.remote.getBSON();
12135
- stream = await this.options.remote.socketStreamRaw({
12136
- ...syncOptions,
12137
- ...{ fetchStrategy: resolvedOptions.fetchStrategy }
12138
- }, (payload) => {
12139
- if (payload instanceof Uint8Array) {
12140
- return bson.deserialize(payload);
12141
- }
12142
- else {
12143
- // Directly enqueued by us
12144
- return payload;
12145
- }
12146
- }, bson);
12147
- }
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
+ }));
12148
12156
  this.logger.debug('Stream established. Processing events');
12149
12157
  this.notifyCompletedUploads = () => {
12150
- if (!stream.closed) {
12151
- stream.enqueueData({ crud_upload_completed: null });
12152
- }
12158
+ stream.inject({ crud_upload_completed: null });
12153
12159
  };
12154
- while (!stream.closed) {
12155
- const line = await stream.read();
12156
- if (!line) {
12160
+ while (true) {
12161
+ const { value: line, done } = await stream.next();
12162
+ if (done) {
12157
12163
  // The stream has closed while waiting
12158
12164
  return;
12159
12165
  }
@@ -12332,14 +12338,17 @@ The next upload iteration will be delayed.`);
12332
12338
  const syncImplementation = this;
12333
12339
  const adapter = this.options.adapter;
12334
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);
12335
12346
  let receivingLines = null;
12336
12347
  let hadSyncLine = false;
12337
12348
  let hideDisconnectOnRestart = false;
12338
12349
  if (signal.aborted) {
12339
12350
  throw new AbortOperation('Connection request has been aborted');
12340
12351
  }
12341
- const abortController = new AbortController();
12342
- signal.addEventListener('abort', () => abortController.abort());
12343
12352
  // Pending sync lines received from the service, as well as local events that trigger a powersync_control
12344
12353
  // invocation (local events include refreshed tokens and completed uploads).
12345
12354
  // This is a single data stream so that we can handle all control calls from a single place.
@@ -12347,49 +12356,36 @@ The next upload iteration will be delayed.`);
12347
12356
  async function connect(instr) {
12348
12357
  const syncOptions = {
12349
12358
  path: '/sync/stream',
12350
- abortSignal: abortController.signal,
12359
+ abortSignal: controller.signal,
12351
12360
  data: instr.request
12352
12361
  };
12353
- if (resolvedOptions.connectionMethod == exports.SyncStreamConnectionMethod.HTTP) {
12354
- controlInvocations = await remote.postStreamRaw(syncOptions, (line) => {
12355
- if (typeof line == 'string') {
12356
- return {
12357
- command: exports.PowerSyncControlCommand.PROCESS_TEXT_LINE,
12358
- payload: line
12359
- };
12360
- }
12361
- else {
12362
- // Directly enqueued by us
12363
- return line;
12364
- }
12365
- });
12366
- }
12367
- else {
12368
- controlInvocations = await remote.socketStreamRaw({
12369
- ...syncOptions,
12370
- fetchStrategy: resolvedOptions.fetchStrategy
12371
- }, (payload) => {
12372
- if (payload instanceof Uint8Array) {
12373
- return {
12374
- command: exports.PowerSyncControlCommand.PROCESS_BSON_LINE,
12375
- payload: payload
12376
- };
12377
- }
12378
- else {
12379
- // Directly enqueued by us
12380
- return payload;
12381
- }
12382
- });
12383
- }
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
+ }));
12384
12379
  // The rust client will set connected: true after the first sync line because that's when it gets invoked, but
12385
12380
  // we're already connected here and can report that.
12386
12381
  syncImplementation.updateSyncStatus({ connected: true });
12387
12382
  try {
12388
- while (!controlInvocations.closed) {
12389
- const line = await controlInvocations.read();
12390
- if (line == null) {
12391
- return;
12383
+ while (true) {
12384
+ let event = await controlInvocations.next();
12385
+ if (event.done) {
12386
+ break;
12392
12387
  }
12388
+ const line = event.value;
12393
12389
  await control(line.command, line.payload);
12394
12390
  if (!hadSyncLine) {
12395
12391
  syncImplementation.triggerCrudUpload();
@@ -12398,12 +12394,8 @@ The next upload iteration will be delayed.`);
12398
12394
  }
12399
12395
  }
12400
12396
  finally {
12401
- const activeInstructions = controlInvocations;
12402
- // We concurrently add events to the active data stream when e.g. a CRUD upload is completed or a token is
12403
- // refreshed. That would throw after closing (and we can't handle those events either way), so set this back
12404
- // to null.
12405
- controlInvocations = null;
12406
- await activeInstructions.close();
12397
+ abort();
12398
+ signal.removeEventListener('abort', abort);
12407
12399
  }
12408
12400
  }
12409
12401
  async function stop() {
@@ -12447,14 +12439,14 @@ The next upload iteration will be delayed.`);
12447
12439
  remote.invalidateCredentials();
12448
12440
  // Restart iteration after the credentials have been refreshed.
12449
12441
  remote.fetchCredentials().then((_) => {
12450
- controlInvocations?.enqueueData({ command: exports.PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
12442
+ controlInvocations?.inject({ command: exports.PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
12451
12443
  }, (err) => {
12452
12444
  syncImplementation.logger.warn('Could not prefetch credentials', err);
12453
12445
  });
12454
12446
  }
12455
12447
  }
12456
12448
  else if ('CloseSyncStream' in instruction) {
12457
- abortController.abort();
12449
+ controller.abort();
12458
12450
  hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
12459
12451
  }
12460
12452
  else if ('FlushFileSystem' in instruction) ;
@@ -12483,17 +12475,13 @@ The next upload iteration will be delayed.`);
12483
12475
  }
12484
12476
  await control(exports.PowerSyncControlCommand.START, JSON.stringify(options));
12485
12477
  this.notifyCompletedUploads = () => {
12486
- if (controlInvocations && !controlInvocations?.closed) {
12487
- controlInvocations.enqueueData({ command: exports.PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
12488
- }
12478
+ controlInvocations?.inject({ command: exports.PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
12489
12479
  };
12490
12480
  this.handleActiveStreamsChange = () => {
12491
- if (controlInvocations && !controlInvocations?.closed) {
12492
- controlInvocations.enqueueData({
12493
- command: exports.PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
12494
- payload: JSON.stringify(this.activeStreams)
12495
- });
12496
- }
12481
+ controlInvocations?.inject({
12482
+ command: exports.PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
12483
+ payload: JSON.stringify(this.activeStreams)
12484
+ });
12497
12485
  };
12498
12486
  await receivingLines;
12499
12487
  }
@@ -14715,7 +14703,6 @@ exports.DEFAULT_INDEX_OPTIONS = DEFAULT_INDEX_OPTIONS;
14715
14703
  exports.DEFAULT_LOCK_TIMEOUT_MS = DEFAULT_LOCK_TIMEOUT_MS;
14716
14704
  exports.DEFAULT_POWERSYNC_CLOSE_OPTIONS = DEFAULT_POWERSYNC_CLOSE_OPTIONS;
14717
14705
  exports.DEFAULT_POWERSYNC_DB_OPTIONS = DEFAULT_POWERSYNC_DB_OPTIONS;
14718
- exports.DEFAULT_PRESSURE_LIMITS = DEFAULT_PRESSURE_LIMITS;
14719
14706
  exports.DEFAULT_REMOTE_LOGGER = DEFAULT_REMOTE_LOGGER;
14720
14707
  exports.DEFAULT_REMOTE_OPTIONS = DEFAULT_REMOTE_OPTIONS;
14721
14708
  exports.DEFAULT_RETRY_DELAY_MS = DEFAULT_RETRY_DELAY_MS;
@@ -14726,7 +14713,6 @@ exports.DEFAULT_SYNC_CLIENT_IMPLEMENTATION = DEFAULT_SYNC_CLIENT_IMPLEMENTATION;
14726
14713
  exports.DEFAULT_TABLE_OPTIONS = DEFAULT_TABLE_OPTIONS;
14727
14714
  exports.DEFAULT_WATCH_QUERY_OPTIONS = DEFAULT_WATCH_QUERY_OPTIONS;
14728
14715
  exports.DEFAULT_WATCH_THROTTLE_MS = DEFAULT_WATCH_THROTTLE_MS;
14729
- exports.DataStream = DataStream;
14730
14716
  exports.DifferentialQueryProcessor = DifferentialQueryProcessor;
14731
14717
  exports.EMPTY_DIFFERENTIAL = EMPTY_DIFFERENTIAL;
14732
14718
  exports.FalsyComparator = FalsyComparator;