@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.
- package/dist/bundle.cjs +431 -445
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +432 -444
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +429 -445
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +430 -444
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +41 -70
- package/lib/client/AbstractPowerSyncDatabase.js +3 -3
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +29 -8
- package/lib/client/sync/stream/AbstractRemote.js +154 -177
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +69 -88
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/utils/async.d.ts +0 -9
- package/lib/utils/async.js +0 -9
- package/lib/utils/async.js.map +1 -1
- package/lib/utils/stream_transform.d.ts +39 -0
- package/lib/utils/stream_transform.js +206 -0
- package/lib/utils/stream_transform.js.map +1 -0
- package/package.json +9 -7
- package/src/client/AbstractPowerSyncDatabase.ts +3 -3
- package/src/client/sync/stream/AbstractRemote.ts +182 -206
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +82 -83
- package/src/index.ts +1 -1
- package/src/utils/async.ts +0 -11
- package/src/utils/stream_transform.ts +252 -0
- package/lib/utils/DataStream.d.ts +0 -62
- package/lib/utils/DataStream.js +0 -169
- package/lib/utils/DataStream.js.map +0 -1
- package/src/utils/DataStream.ts +0 -222
package/dist/bundle.mjs
CHANGED
|
@@ -1396,6 +1396,8 @@ var EncodingType;
|
|
|
1396
1396
|
EncodingType["Base64"] = "base64";
|
|
1397
1397
|
})(EncodingType || (EncodingType = {}));
|
|
1398
1398
|
|
|
1399
|
+
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
1400
|
+
|
|
1399
1401
|
function getDefaultExportFromCjs (x) {
|
|
1400
1402
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
1401
1403
|
}
|
|
@@ -1476,7 +1478,7 @@ function requireEventIterator () {
|
|
|
1476
1478
|
this.removeCallback();
|
|
1477
1479
|
});
|
|
1478
1480
|
}
|
|
1479
|
-
[
|
|
1481
|
+
[symbolAsyncIterator]() {
|
|
1480
1482
|
return {
|
|
1481
1483
|
next: (value) => {
|
|
1482
1484
|
const result = this.pushQueue.shift();
|
|
@@ -1523,7 +1525,7 @@ function requireEventIterator () {
|
|
|
1523
1525
|
queue.eventHandlers[event] = fn;
|
|
1524
1526
|
},
|
|
1525
1527
|
}) || (() => { });
|
|
1526
|
-
this[
|
|
1528
|
+
this[symbolAsyncIterator] = () => queue[symbolAsyncIterator]();
|
|
1527
1529
|
Object.freeze(this);
|
|
1528
1530
|
}
|
|
1529
1531
|
}
|
|
@@ -2405,15 +2407,6 @@ class ControlledExecutor {
|
|
|
2405
2407
|
}
|
|
2406
2408
|
}
|
|
2407
2409
|
|
|
2408
|
-
/**
|
|
2409
|
-
* A ponyfill for `Symbol.asyncIterator` that is compatible with the
|
|
2410
|
-
* [recommended polyfill](https://github.com/Azure/azure-sdk-for-js/blob/%40azure/core-asynciterator-polyfill_1.0.2/sdk/core/core-asynciterator-polyfill/src/index.ts#L4-L6)
|
|
2411
|
-
* we recommend for React Native.
|
|
2412
|
-
*
|
|
2413
|
-
* As long as we use this symbol (instead of `for await` and `async *`) in this package, we can be compatible with async
|
|
2414
|
-
* iterators without requiring them.
|
|
2415
|
-
*/
|
|
2416
|
-
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
2417
2410
|
/**
|
|
2418
2411
|
* Throttle a function to be called at most once every "wait" milliseconds,
|
|
2419
2412
|
* on the trailing edge.
|
|
@@ -10750,177 +10743,10 @@ function requireDist () {
|
|
|
10750
10743
|
|
|
10751
10744
|
var distExports = requireDist();
|
|
10752
10745
|
|
|
10753
|
-
var version = "1.
|
|
10746
|
+
var version = "1.52.0";
|
|
10754
10747
|
var PACKAGE = {
|
|
10755
10748
|
version: version};
|
|
10756
10749
|
|
|
10757
|
-
const DEFAULT_PRESSURE_LIMITS = {
|
|
10758
|
-
highWater: 10,
|
|
10759
|
-
lowWater: 0
|
|
10760
|
-
};
|
|
10761
|
-
/**
|
|
10762
|
-
* A very basic implementation of a data stream with backpressure support which does not use
|
|
10763
|
-
* native JS streams or async iterators.
|
|
10764
|
-
* This is handy for environments such as React Native which need polyfills for the above.
|
|
10765
|
-
*/
|
|
10766
|
-
class DataStream extends BaseObserver {
|
|
10767
|
-
options;
|
|
10768
|
-
dataQueue;
|
|
10769
|
-
isClosed;
|
|
10770
|
-
processingPromise;
|
|
10771
|
-
notifyDataAdded;
|
|
10772
|
-
logger;
|
|
10773
|
-
mapLine;
|
|
10774
|
-
constructor(options) {
|
|
10775
|
-
super();
|
|
10776
|
-
this.options = options;
|
|
10777
|
-
this.processingPromise = null;
|
|
10778
|
-
this.isClosed = false;
|
|
10779
|
-
this.dataQueue = [];
|
|
10780
|
-
this.mapLine = options?.mapLine ?? ((line) => line);
|
|
10781
|
-
this.logger = options?.logger ?? Logger.get('DataStream');
|
|
10782
|
-
if (options?.closeOnError) {
|
|
10783
|
-
const l = this.registerListener({
|
|
10784
|
-
error: (ex) => {
|
|
10785
|
-
l?.();
|
|
10786
|
-
this.close();
|
|
10787
|
-
}
|
|
10788
|
-
});
|
|
10789
|
-
}
|
|
10790
|
-
}
|
|
10791
|
-
get highWatermark() {
|
|
10792
|
-
return this.options?.pressure?.highWaterMark ?? DEFAULT_PRESSURE_LIMITS.highWater;
|
|
10793
|
-
}
|
|
10794
|
-
get lowWatermark() {
|
|
10795
|
-
return this.options?.pressure?.lowWaterMark ?? DEFAULT_PRESSURE_LIMITS.lowWater;
|
|
10796
|
-
}
|
|
10797
|
-
get closed() {
|
|
10798
|
-
return this.isClosed;
|
|
10799
|
-
}
|
|
10800
|
-
async close() {
|
|
10801
|
-
this.isClosed = true;
|
|
10802
|
-
await this.processingPromise;
|
|
10803
|
-
this.iterateListeners((l) => l.closed?.());
|
|
10804
|
-
// Discard any data in the queue
|
|
10805
|
-
this.dataQueue = [];
|
|
10806
|
-
this.listeners.clear();
|
|
10807
|
-
}
|
|
10808
|
-
/**
|
|
10809
|
-
* Enqueues data for the consumers to read
|
|
10810
|
-
*/
|
|
10811
|
-
enqueueData(data) {
|
|
10812
|
-
if (this.isClosed) {
|
|
10813
|
-
throw new Error('Cannot enqueue data into closed stream.');
|
|
10814
|
-
}
|
|
10815
|
-
this.dataQueue.push(data);
|
|
10816
|
-
this.notifyDataAdded?.();
|
|
10817
|
-
this.processQueue();
|
|
10818
|
-
}
|
|
10819
|
-
/**
|
|
10820
|
-
* Reads data once from the data stream
|
|
10821
|
-
* @returns a Data payload or Null if the stream closed.
|
|
10822
|
-
*/
|
|
10823
|
-
async read() {
|
|
10824
|
-
if (this.closed) {
|
|
10825
|
-
return null;
|
|
10826
|
-
}
|
|
10827
|
-
// Wait for any pending processing to complete first.
|
|
10828
|
-
// This ensures we register our listener before calling processQueue(),
|
|
10829
|
-
// avoiding a race where processQueue() sees no reader and returns early.
|
|
10830
|
-
if (this.processingPromise) {
|
|
10831
|
-
await this.processingPromise;
|
|
10832
|
-
}
|
|
10833
|
-
// Re-check after await - stream may have closed while we were waiting
|
|
10834
|
-
if (this.closed) {
|
|
10835
|
-
return null;
|
|
10836
|
-
}
|
|
10837
|
-
return new Promise((resolve, reject) => {
|
|
10838
|
-
const l = this.registerListener({
|
|
10839
|
-
data: async (data) => {
|
|
10840
|
-
resolve(data);
|
|
10841
|
-
// Remove the listener
|
|
10842
|
-
l?.();
|
|
10843
|
-
},
|
|
10844
|
-
closed: () => {
|
|
10845
|
-
resolve(null);
|
|
10846
|
-
l?.();
|
|
10847
|
-
},
|
|
10848
|
-
error: (ex) => {
|
|
10849
|
-
reject(ex);
|
|
10850
|
-
l?.();
|
|
10851
|
-
}
|
|
10852
|
-
});
|
|
10853
|
-
this.processQueue();
|
|
10854
|
-
});
|
|
10855
|
-
}
|
|
10856
|
-
/**
|
|
10857
|
-
* Executes a callback for each data item in the stream
|
|
10858
|
-
*/
|
|
10859
|
-
forEach(callback) {
|
|
10860
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
10861
|
-
this.iterateAsyncErrored(async (l) => l.lowWater?.());
|
|
10862
|
-
}
|
|
10863
|
-
return this.registerListener({
|
|
10864
|
-
data: callback
|
|
10865
|
-
});
|
|
10866
|
-
}
|
|
10867
|
-
processQueue() {
|
|
10868
|
-
if (this.processingPromise) {
|
|
10869
|
-
return;
|
|
10870
|
-
}
|
|
10871
|
-
const promise = (this.processingPromise = this._processQueue());
|
|
10872
|
-
promise.finally(() => {
|
|
10873
|
-
this.processingPromise = null;
|
|
10874
|
-
});
|
|
10875
|
-
return promise;
|
|
10876
|
-
}
|
|
10877
|
-
hasDataReader() {
|
|
10878
|
-
return Array.from(this.listeners.values()).some((l) => !!l.data);
|
|
10879
|
-
}
|
|
10880
|
-
async _processQueue() {
|
|
10881
|
-
/**
|
|
10882
|
-
* Allow listeners to mutate the queue before processing.
|
|
10883
|
-
* This allows for operations such as dropping or compressing data
|
|
10884
|
-
* on high water or requesting more data on low water.
|
|
10885
|
-
*/
|
|
10886
|
-
if (this.dataQueue.length >= this.highWatermark) {
|
|
10887
|
-
await this.iterateAsyncErrored(async (l) => l.highWater?.());
|
|
10888
|
-
}
|
|
10889
|
-
if (this.isClosed || !this.hasDataReader()) {
|
|
10890
|
-
return;
|
|
10891
|
-
}
|
|
10892
|
-
if (this.dataQueue.length) {
|
|
10893
|
-
const data = this.dataQueue.shift();
|
|
10894
|
-
const mapped = this.mapLine(data);
|
|
10895
|
-
await this.iterateAsyncErrored(async (l) => l.data?.(mapped));
|
|
10896
|
-
}
|
|
10897
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
10898
|
-
const dataAdded = new Promise((resolve) => {
|
|
10899
|
-
this.notifyDataAdded = resolve;
|
|
10900
|
-
});
|
|
10901
|
-
await Promise.race([this.iterateAsyncErrored(async (l) => l.lowWater?.()), dataAdded]);
|
|
10902
|
-
this.notifyDataAdded = null;
|
|
10903
|
-
}
|
|
10904
|
-
if (this.dataQueue.length > 0) {
|
|
10905
|
-
setTimeout(() => this.processQueue());
|
|
10906
|
-
}
|
|
10907
|
-
}
|
|
10908
|
-
async iterateAsyncErrored(cb) {
|
|
10909
|
-
// Important: We need to copy the listeners, as calling a listener could result in adding another
|
|
10910
|
-
// listener, resulting in infinite loops.
|
|
10911
|
-
const listeners = Array.from(this.listeners.values());
|
|
10912
|
-
for (let i of listeners) {
|
|
10913
|
-
try {
|
|
10914
|
-
await cb(i);
|
|
10915
|
-
}
|
|
10916
|
-
catch (ex) {
|
|
10917
|
-
this.logger.error(ex);
|
|
10918
|
-
this.iterateListeners((l) => l.error?.(ex));
|
|
10919
|
-
}
|
|
10920
|
-
}
|
|
10921
|
-
}
|
|
10922
|
-
}
|
|
10923
|
-
|
|
10924
10750
|
var WebsocketDuplexConnection = {};
|
|
10925
10751
|
|
|
10926
10752
|
var hasRequiredWebsocketDuplexConnection;
|
|
@@ -11083,8 +10909,215 @@ class WebsocketClientTransport {
|
|
|
11083
10909
|
}
|
|
11084
10910
|
}
|
|
11085
10911
|
|
|
10912
|
+
const doneResult = { done: true, value: undefined };
|
|
10913
|
+
function valueResult(value) {
|
|
10914
|
+
return { done: false, value };
|
|
10915
|
+
}
|
|
10916
|
+
/**
|
|
10917
|
+
* A variant of {@link Array.map} for async iterators.
|
|
10918
|
+
*/
|
|
10919
|
+
function map(source, map) {
|
|
10920
|
+
return {
|
|
10921
|
+
next: async () => {
|
|
10922
|
+
const value = await source.next();
|
|
10923
|
+
if (value.done) {
|
|
10924
|
+
return value;
|
|
10925
|
+
}
|
|
10926
|
+
else {
|
|
10927
|
+
return { value: map(value.value) };
|
|
10928
|
+
}
|
|
10929
|
+
}
|
|
10930
|
+
};
|
|
10931
|
+
}
|
|
10932
|
+
/**
|
|
10933
|
+
* Expands a source async iterator by allowing to inject events asynchronously.
|
|
10934
|
+
*
|
|
10935
|
+
* The resulting iterator will emit all events from its source. Additionally though, events can be injected. These
|
|
10936
|
+
* events are dropped once the main iterator completes, but are otherwise forwarded.
|
|
10937
|
+
*
|
|
10938
|
+
* The iterator completes when its source completes, and it supports backpressure by only calling `next()` on the source
|
|
10939
|
+
* in response to a `next()` call from downstream if no pending injected events can be dispatched.
|
|
10940
|
+
*/
|
|
10941
|
+
function injectable(source) {
|
|
10942
|
+
let sourceIsDone = false;
|
|
10943
|
+
let waiter = undefined; // An active, waiting next() call.
|
|
10944
|
+
// A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
|
|
10945
|
+
let pendingSourceEvent = null;
|
|
10946
|
+
let pendingInjectedEvents = [];
|
|
10947
|
+
const consumeWaiter = () => {
|
|
10948
|
+
const pending = waiter;
|
|
10949
|
+
waiter = undefined;
|
|
10950
|
+
return pending;
|
|
10951
|
+
};
|
|
10952
|
+
const fetchFromSource = () => {
|
|
10953
|
+
const resolveWaiter = (propagate) => {
|
|
10954
|
+
const active = consumeWaiter();
|
|
10955
|
+
if (active) {
|
|
10956
|
+
propagate(active);
|
|
10957
|
+
}
|
|
10958
|
+
else {
|
|
10959
|
+
pendingSourceEvent = propagate;
|
|
10960
|
+
}
|
|
10961
|
+
};
|
|
10962
|
+
const nextFromSource = source.next();
|
|
10963
|
+
nextFromSource.then((value) => {
|
|
10964
|
+
sourceIsDone = value.done == true;
|
|
10965
|
+
resolveWaiter((w) => w.resolve(value));
|
|
10966
|
+
}, (error) => {
|
|
10967
|
+
resolveWaiter((w) => w.reject(error));
|
|
10968
|
+
});
|
|
10969
|
+
};
|
|
10970
|
+
return {
|
|
10971
|
+
next: () => {
|
|
10972
|
+
return new Promise((resolve, reject) => {
|
|
10973
|
+
// First priority: Dispatch ready upstream events.
|
|
10974
|
+
if (sourceIsDone) {
|
|
10975
|
+
return resolve(doneResult);
|
|
10976
|
+
}
|
|
10977
|
+
if (pendingSourceEvent) {
|
|
10978
|
+
pendingSourceEvent({ resolve, reject });
|
|
10979
|
+
pendingSourceEvent = null;
|
|
10980
|
+
return;
|
|
10981
|
+
}
|
|
10982
|
+
// Second priority: Dispatch injected events
|
|
10983
|
+
if (pendingInjectedEvents.length) {
|
|
10984
|
+
return resolve(valueResult(pendingInjectedEvents.shift()));
|
|
10985
|
+
}
|
|
10986
|
+
// Nothing pending? Fetch from source
|
|
10987
|
+
waiter = { resolve, reject };
|
|
10988
|
+
return fetchFromSource();
|
|
10989
|
+
});
|
|
10990
|
+
},
|
|
10991
|
+
inject: (event) => {
|
|
10992
|
+
const pending = consumeWaiter();
|
|
10993
|
+
if (pending != null) {
|
|
10994
|
+
pending.resolve(valueResult(event));
|
|
10995
|
+
}
|
|
10996
|
+
else {
|
|
10997
|
+
pendingInjectedEvents.push(event);
|
|
10998
|
+
}
|
|
10999
|
+
}
|
|
11000
|
+
};
|
|
11001
|
+
}
|
|
11002
|
+
/**
|
|
11003
|
+
* Splits a byte stream at line endings, emitting each line as a string.
|
|
11004
|
+
*/
|
|
11005
|
+
function extractJsonLines(source, decoder) {
|
|
11006
|
+
let buffer = '';
|
|
11007
|
+
const pendingLines = [];
|
|
11008
|
+
let isFinalEvent = false;
|
|
11009
|
+
return {
|
|
11010
|
+
next: async () => {
|
|
11011
|
+
while (true) {
|
|
11012
|
+
if (isFinalEvent) {
|
|
11013
|
+
return doneResult;
|
|
11014
|
+
}
|
|
11015
|
+
{
|
|
11016
|
+
const first = pendingLines.shift();
|
|
11017
|
+
if (first) {
|
|
11018
|
+
return { done: false, value: first };
|
|
11019
|
+
}
|
|
11020
|
+
}
|
|
11021
|
+
const { done, value } = await source.next();
|
|
11022
|
+
if (done) {
|
|
11023
|
+
const remaining = buffer.trim();
|
|
11024
|
+
if (remaining.length != 0) {
|
|
11025
|
+
isFinalEvent = true;
|
|
11026
|
+
return { done: false, value: remaining };
|
|
11027
|
+
}
|
|
11028
|
+
return doneResult;
|
|
11029
|
+
}
|
|
11030
|
+
const data = decoder.decode(value, { stream: true });
|
|
11031
|
+
buffer += data;
|
|
11032
|
+
const lines = buffer.split('\n');
|
|
11033
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
11034
|
+
const l = lines[i].trim();
|
|
11035
|
+
if (l.length > 0) {
|
|
11036
|
+
pendingLines.push(l);
|
|
11037
|
+
}
|
|
11038
|
+
}
|
|
11039
|
+
buffer = lines[lines.length - 1];
|
|
11040
|
+
}
|
|
11041
|
+
}
|
|
11042
|
+
};
|
|
11043
|
+
}
|
|
11044
|
+
/**
|
|
11045
|
+
* Splits a concatenated stream of BSON objects by emitting individual objects.
|
|
11046
|
+
*/
|
|
11047
|
+
function extractBsonObjects(source) {
|
|
11048
|
+
// Fully read but not emitted yet.
|
|
11049
|
+
const completedObjects = [];
|
|
11050
|
+
// Whether source has returned { done: true }. We do the same once completed objects have been emitted.
|
|
11051
|
+
let isDone = false;
|
|
11052
|
+
const lengthBuffer = new DataView(new ArrayBuffer(4));
|
|
11053
|
+
let objectBody = null;
|
|
11054
|
+
// If we're parsing the length field, a number between 1 and 4 (inclusive) describing remaining bytes in the header.
|
|
11055
|
+
// If we're consuming a document, the bytes remaining.
|
|
11056
|
+
let remainingLength = 4;
|
|
11057
|
+
return {
|
|
11058
|
+
async next() {
|
|
11059
|
+
while (true) {
|
|
11060
|
+
// Before fetching new data from upstream, return completed objects.
|
|
11061
|
+
if (completedObjects.length) {
|
|
11062
|
+
return valueResult(completedObjects.shift());
|
|
11063
|
+
}
|
|
11064
|
+
if (isDone) {
|
|
11065
|
+
return doneResult;
|
|
11066
|
+
}
|
|
11067
|
+
const upstreamEvent = await source.next();
|
|
11068
|
+
if (upstreamEvent.done) {
|
|
11069
|
+
isDone = true;
|
|
11070
|
+
if (objectBody || remainingLength != 4) {
|
|
11071
|
+
throw new Error('illegal end of stream in BSON object');
|
|
11072
|
+
}
|
|
11073
|
+
return doneResult;
|
|
11074
|
+
}
|
|
11075
|
+
const chunk = upstreamEvent.value;
|
|
11076
|
+
for (let i = 0; i < chunk.length;) {
|
|
11077
|
+
const availableInData = chunk.length - i;
|
|
11078
|
+
if (objectBody) {
|
|
11079
|
+
// We're in the middle of reading a BSON document.
|
|
11080
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
11081
|
+
const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
|
|
11082
|
+
objectBody.set(copySource, objectBody.length - remainingLength);
|
|
11083
|
+
i += bytesToRead;
|
|
11084
|
+
remainingLength -= bytesToRead;
|
|
11085
|
+
if (remainingLength == 0) {
|
|
11086
|
+
completedObjects.push(objectBody);
|
|
11087
|
+
// Prepare to read another document, starting with its length
|
|
11088
|
+
objectBody = null;
|
|
11089
|
+
remainingLength = 4;
|
|
11090
|
+
}
|
|
11091
|
+
}
|
|
11092
|
+
else {
|
|
11093
|
+
// Copy up to 4 bytes into lengthBuffer, depending on how many we still need.
|
|
11094
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
11095
|
+
for (let j = 0; j < bytesToRead; j++) {
|
|
11096
|
+
lengthBuffer.setUint8(4 - remainingLength + j, chunk[i + j]);
|
|
11097
|
+
}
|
|
11098
|
+
i += bytesToRead;
|
|
11099
|
+
remainingLength -= bytesToRead;
|
|
11100
|
+
if (remainingLength == 0) {
|
|
11101
|
+
// Transition from reading length header to reading document. Subtracting 4 because the length of the
|
|
11102
|
+
// header is included in length.
|
|
11103
|
+
const length = lengthBuffer.getInt32(0, true /* little endian */);
|
|
11104
|
+
remainingLength = length - 4;
|
|
11105
|
+
if (remainingLength < 1) {
|
|
11106
|
+
throw new Error(`invalid length for bson: ${length}`);
|
|
11107
|
+
}
|
|
11108
|
+
objectBody = new Uint8Array(length);
|
|
11109
|
+
new DataView(objectBody.buffer).setInt32(0, length, true);
|
|
11110
|
+
}
|
|
11111
|
+
}
|
|
11112
|
+
}
|
|
11113
|
+
}
|
|
11114
|
+
}
|
|
11115
|
+
};
|
|
11116
|
+
}
|
|
11117
|
+
|
|
11086
11118
|
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
|
|
11087
11119
|
const POWERSYNC_JS_VERSION = PACKAGE.version;
|
|
11120
|
+
const SYNC_QUEUE_REQUEST_HIGH_WATER = 10;
|
|
11088
11121
|
const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
|
|
11089
11122
|
// Keep alive message is sent every period
|
|
11090
11123
|
const KEEP_ALIVE_MS = 20_000;
|
|
@@ -11264,13 +11297,14 @@ class AbstractRemote {
|
|
|
11264
11297
|
return new WebSocket(url);
|
|
11265
11298
|
}
|
|
11266
11299
|
/**
|
|
11267
|
-
* Returns a data stream of sync line data.
|
|
11300
|
+
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
11301
|
+
*
|
|
11302
|
+
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
11268
11303
|
*
|
|
11269
|
-
* @param map Maps received payload frames to the typed event value.
|
|
11270
11304
|
* @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
|
|
11271
11305
|
* (required for compatibility with older sync services).
|
|
11272
11306
|
*/
|
|
11273
|
-
async socketStreamRaw(options,
|
|
11307
|
+
async socketStreamRaw(options, bson) {
|
|
11274
11308
|
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
|
|
11275
11309
|
const mimeType = bson == null ? 'application/json' : 'application/bson';
|
|
11276
11310
|
function toBuffer(js) {
|
|
@@ -11285,52 +11319,55 @@ class AbstractRemote {
|
|
|
11285
11319
|
}
|
|
11286
11320
|
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
|
|
11287
11321
|
const request = await this.buildRequest(path);
|
|
11322
|
+
const url = this.options.socketUrlTransformer(request.url);
|
|
11288
11323
|
// Add the user agent in the setup payload - we can't set custom
|
|
11289
11324
|
// headers with websockets on web. The browser userAgent is however added
|
|
11290
11325
|
// automatically as a header.
|
|
11291
11326
|
const userAgent = this.getUserAgent();
|
|
11292
|
-
|
|
11293
|
-
|
|
11294
|
-
|
|
11295
|
-
|
|
11296
|
-
|
|
11297
|
-
|
|
11298
|
-
|
|
11327
|
+
// While we're connecting (a process that can't be aborted in RSocket), the WebSocket instance to close if we wanted
|
|
11328
|
+
// to abort the connection.
|
|
11329
|
+
let pendingSocket = null;
|
|
11330
|
+
let keepAliveTimeout;
|
|
11331
|
+
let rsocket = null;
|
|
11332
|
+
let queue = null;
|
|
11333
|
+
let didClose = false;
|
|
11334
|
+
const abortRequest = () => {
|
|
11335
|
+
if (didClose) {
|
|
11336
|
+
return;
|
|
11337
|
+
}
|
|
11338
|
+
didClose = true;
|
|
11339
|
+
clearTimeout(keepAliveTimeout);
|
|
11340
|
+
if (pendingSocket) {
|
|
11341
|
+
pendingSocket.close();
|
|
11342
|
+
}
|
|
11343
|
+
if (rsocket) {
|
|
11344
|
+
rsocket.close();
|
|
11345
|
+
}
|
|
11346
|
+
if (queue) {
|
|
11347
|
+
queue.stop();
|
|
11348
|
+
}
|
|
11349
|
+
};
|
|
11299
11350
|
// Handle upstream abort
|
|
11300
|
-
if (options.abortSignal
|
|
11351
|
+
if (options.abortSignal.aborted) {
|
|
11301
11352
|
throw new AbortOperation('Connection request aborted');
|
|
11302
11353
|
}
|
|
11303
11354
|
else {
|
|
11304
|
-
options.abortSignal
|
|
11305
|
-
stream.close();
|
|
11306
|
-
}, { once: true });
|
|
11355
|
+
options.abortSignal.addEventListener('abort', abortRequest);
|
|
11307
11356
|
}
|
|
11308
|
-
let keepAliveTimeout;
|
|
11309
11357
|
const resetTimeout = () => {
|
|
11310
11358
|
clearTimeout(keepAliveTimeout);
|
|
11311
11359
|
keepAliveTimeout = setTimeout(() => {
|
|
11312
11360
|
this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
|
|
11313
|
-
|
|
11361
|
+
abortRequest();
|
|
11314
11362
|
}, SOCKET_TIMEOUT_MS);
|
|
11315
11363
|
};
|
|
11316
11364
|
resetTimeout();
|
|
11317
|
-
// Typescript complains about this being `never` if it's not assigned here.
|
|
11318
|
-
// This is assigned in `wsCreator`.
|
|
11319
|
-
let disposeSocketConnectionTimeout = () => { };
|
|
11320
|
-
const url = this.options.socketUrlTransformer(request.url);
|
|
11321
11365
|
const connector = new distExports.RSocketConnector({
|
|
11322
11366
|
transport: new WebsocketClientTransport({
|
|
11323
11367
|
url,
|
|
11324
11368
|
wsCreator: (url) => {
|
|
11325
|
-
const socket = this.createSocket(url);
|
|
11326
|
-
|
|
11327
|
-
closed: () => {
|
|
11328
|
-
// Allow closing the underlying WebSocket if the stream was closed before the
|
|
11329
|
-
// RSocket connect completed. This should effectively abort the request.
|
|
11330
|
-
socket.close();
|
|
11331
|
-
}
|
|
11332
|
-
});
|
|
11333
|
-
socket.addEventListener('message', (event) => {
|
|
11369
|
+
const socket = (pendingSocket = this.createSocket(url));
|
|
11370
|
+
socket.addEventListener('message', () => {
|
|
11334
11371
|
resetTimeout();
|
|
11335
11372
|
});
|
|
11336
11373
|
return socket;
|
|
@@ -11350,43 +11387,40 @@ class AbstractRemote {
|
|
|
11350
11387
|
}
|
|
11351
11388
|
}
|
|
11352
11389
|
});
|
|
11353
|
-
let rsocket;
|
|
11354
11390
|
try {
|
|
11355
11391
|
rsocket = await connector.connect();
|
|
11356
11392
|
// The connection is established, we no longer need to monitor the initial timeout
|
|
11357
|
-
|
|
11393
|
+
pendingSocket = null;
|
|
11358
11394
|
}
|
|
11359
11395
|
catch (ex) {
|
|
11360
11396
|
this.logger.error(`Failed to connect WebSocket`, ex);
|
|
11361
|
-
|
|
11362
|
-
if (!stream.closed) {
|
|
11363
|
-
await stream.close();
|
|
11364
|
-
}
|
|
11397
|
+
abortRequest();
|
|
11365
11398
|
throw ex;
|
|
11366
11399
|
}
|
|
11367
11400
|
resetTimeout();
|
|
11368
|
-
let socketIsClosed = false;
|
|
11369
|
-
const closeSocket = () => {
|
|
11370
|
-
clearTimeout(keepAliveTimeout);
|
|
11371
|
-
if (socketIsClosed) {
|
|
11372
|
-
return;
|
|
11373
|
-
}
|
|
11374
|
-
socketIsClosed = true;
|
|
11375
|
-
rsocket.close();
|
|
11376
|
-
};
|
|
11377
11401
|
// Helps to prevent double close scenarios
|
|
11378
|
-
rsocket.onClose(() => (
|
|
11379
|
-
|
|
11380
|
-
let pendingEventsCount = syncQueueRequestSize;
|
|
11381
|
-
const disposeClosedListener = stream.registerListener({
|
|
11382
|
-
closed: () => {
|
|
11383
|
-
closeSocket();
|
|
11384
|
-
disposeClosedListener();
|
|
11385
|
-
}
|
|
11386
|
-
});
|
|
11387
|
-
const socket = await new Promise((resolve, reject) => {
|
|
11402
|
+
rsocket.onClose(() => (rsocket = null));
|
|
11403
|
+
return await new Promise((resolve, reject) => {
|
|
11388
11404
|
let connectionEstablished = false;
|
|
11389
|
-
|
|
11405
|
+
let pendingEventsCount = syncQueueRequestSize;
|
|
11406
|
+
let paused = false;
|
|
11407
|
+
let res = null;
|
|
11408
|
+
function requestMore() {
|
|
11409
|
+
const delta = syncQueueRequestSize - pendingEventsCount;
|
|
11410
|
+
if (!paused && delta > 0) {
|
|
11411
|
+
res?.request(delta);
|
|
11412
|
+
pendingEventsCount = syncQueueRequestSize;
|
|
11413
|
+
}
|
|
11414
|
+
}
|
|
11415
|
+
const events = new domExports.EventIterator((q) => {
|
|
11416
|
+
queue = q;
|
|
11417
|
+
q.on('highWater', () => (paused = true));
|
|
11418
|
+
q.on('lowWater', () => {
|
|
11419
|
+
paused = false;
|
|
11420
|
+
requestMore();
|
|
11421
|
+
});
|
|
11422
|
+
}, { highWaterMark: SYNC_QUEUE_REQUEST_HIGH_WATER, lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER })[symbolAsyncIterator]();
|
|
11423
|
+
res = rsocket.requestStream({
|
|
11390
11424
|
data: toBuffer(options.data),
|
|
11391
11425
|
metadata: toBuffer({
|
|
11392
11426
|
path
|
|
@@ -11411,7 +11445,7 @@ class AbstractRemote {
|
|
|
11411
11445
|
}
|
|
11412
11446
|
// RSocket will close the RSocket stream automatically
|
|
11413
11447
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
11414
|
-
|
|
11448
|
+
abortRequest();
|
|
11415
11449
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
11416
11450
|
if (!connectionEstablished) {
|
|
11417
11451
|
reject(e);
|
|
@@ -11421,41 +11455,40 @@ class AbstractRemote {
|
|
|
11421
11455
|
// The connection is active
|
|
11422
11456
|
if (!connectionEstablished) {
|
|
11423
11457
|
connectionEstablished = true;
|
|
11424
|
-
resolve(
|
|
11458
|
+
resolve(events);
|
|
11425
11459
|
}
|
|
11426
11460
|
const { data } = payload;
|
|
11461
|
+
if (data) {
|
|
11462
|
+
queue.push(data);
|
|
11463
|
+
}
|
|
11427
11464
|
// Less events are now pending
|
|
11428
11465
|
pendingEventsCount--;
|
|
11429
|
-
|
|
11430
|
-
|
|
11431
|
-
}
|
|
11432
|
-
stream.enqueueData(data);
|
|
11466
|
+
// Request another event (unless the downstream consumer is paused).
|
|
11467
|
+
requestMore();
|
|
11433
11468
|
},
|
|
11434
11469
|
onComplete: () => {
|
|
11435
|
-
|
|
11470
|
+
abortRequest(); // this will also emit a done event
|
|
11436
11471
|
},
|
|
11437
11472
|
onExtension: () => { }
|
|
11438
11473
|
});
|
|
11439
11474
|
});
|
|
11440
|
-
const l = stream.registerListener({
|
|
11441
|
-
lowWater: async () => {
|
|
11442
|
-
// Request to fill up the queue
|
|
11443
|
-
const required = syncQueueRequestSize - pendingEventsCount;
|
|
11444
|
-
if (required > 0) {
|
|
11445
|
-
socket.request(syncQueueRequestSize - pendingEventsCount);
|
|
11446
|
-
pendingEventsCount = syncQueueRequestSize;
|
|
11447
|
-
}
|
|
11448
|
-
},
|
|
11449
|
-
closed: () => {
|
|
11450
|
-
l();
|
|
11451
|
-
}
|
|
11452
|
-
});
|
|
11453
|
-
return stream;
|
|
11454
11475
|
}
|
|
11455
11476
|
/**
|
|
11456
|
-
*
|
|
11477
|
+
* @returns Whether the HTTP implementation on this platform can receive streamed binary responses. This is true on
|
|
11478
|
+
* all platforms except React Native (who would have guessed...), where we must not request BSON responses.
|
|
11479
|
+
*
|
|
11480
|
+
* @see https://github.com/react-native-community/fetch?tab=readme-ov-file#motivation
|
|
11481
|
+
*/
|
|
11482
|
+
get supportsStreamingBinaryResponses() {
|
|
11483
|
+
return true;
|
|
11484
|
+
}
|
|
11485
|
+
/**
|
|
11486
|
+
* Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
|
|
11487
|
+
* async iterator of byte blobs.
|
|
11488
|
+
*
|
|
11489
|
+
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
11457
11490
|
*/
|
|
11458
|
-
async
|
|
11491
|
+
async fetchStreamRaw(options) {
|
|
11459
11492
|
const { data, path, headers, abortSignal } = options;
|
|
11460
11493
|
const request = await this.buildRequest(path);
|
|
11461
11494
|
/**
|
|
@@ -11467,119 +11500,94 @@ class AbstractRemote {
|
|
|
11467
11500
|
* Aborting the active fetch request while it is being consumed seems to throw
|
|
11468
11501
|
* an unhandled exception on the window level.
|
|
11469
11502
|
*/
|
|
11470
|
-
if (abortSignal
|
|
11471
|
-
throw new AbortOperation('Abort request received before making
|
|
11503
|
+
if (abortSignal.aborted) {
|
|
11504
|
+
throw new AbortOperation('Abort request received before making fetchStreamRaw request');
|
|
11472
11505
|
}
|
|
11473
11506
|
const controller = new AbortController();
|
|
11474
|
-
let
|
|
11475
|
-
abortSignal
|
|
11476
|
-
|
|
11507
|
+
let reader = null;
|
|
11508
|
+
abortSignal.addEventListener('abort', () => {
|
|
11509
|
+
const reason = abortSignal.reason ??
|
|
11510
|
+
new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.');
|
|
11511
|
+
if (reader == null) {
|
|
11477
11512
|
// Only abort via the abort controller if the request has not resolved yet
|
|
11478
|
-
controller.abort(
|
|
11479
|
-
|
|
11513
|
+
controller.abort(reason);
|
|
11514
|
+
}
|
|
11515
|
+
else {
|
|
11516
|
+
reader.cancel(reason).catch(() => {
|
|
11517
|
+
// Cancelling the reader might rethrow an exception we would have handled by throwing in next(). So we can
|
|
11518
|
+
// ignore it here.
|
|
11519
|
+
});
|
|
11480
11520
|
}
|
|
11481
11521
|
});
|
|
11482
|
-
|
|
11483
|
-
|
|
11484
|
-
|
|
11485
|
-
|
|
11486
|
-
|
|
11487
|
-
|
|
11488
|
-
|
|
11489
|
-
|
|
11490
|
-
|
|
11522
|
+
let res;
|
|
11523
|
+
let responseIsBson = false;
|
|
11524
|
+
try {
|
|
11525
|
+
const ndJson = 'application/x-ndjson';
|
|
11526
|
+
const bson = 'application/vnd.powersync.bson-stream';
|
|
11527
|
+
res = await this.fetch(request.url, {
|
|
11528
|
+
method: 'POST',
|
|
11529
|
+
headers: {
|
|
11530
|
+
...headers,
|
|
11531
|
+
...request.headers,
|
|
11532
|
+
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
11533
|
+
},
|
|
11534
|
+
body: JSON.stringify(data),
|
|
11535
|
+
signal: controller.signal,
|
|
11536
|
+
cache: 'no-store',
|
|
11537
|
+
...(this.options.fetchOptions ?? {}),
|
|
11538
|
+
...options.fetchOptions
|
|
11539
|
+
});
|
|
11540
|
+
if (!res.ok || !res.body) {
|
|
11541
|
+
const text = await res.text();
|
|
11542
|
+
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
11543
|
+
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
11544
|
+
error.status = res.status;
|
|
11545
|
+
throw error;
|
|
11546
|
+
}
|
|
11547
|
+
const contentType = res.headers.get('content-type');
|
|
11548
|
+
responseIsBson = contentType == bson;
|
|
11549
|
+
}
|
|
11550
|
+
catch (ex) {
|
|
11491
11551
|
if (ex.name == 'AbortError') {
|
|
11492
11552
|
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
|
|
11493
11553
|
}
|
|
11494
11554
|
throw ex;
|
|
11495
|
-
});
|
|
11496
|
-
if (!res) {
|
|
11497
|
-
throw new Error('Fetch request was aborted');
|
|
11498
|
-
}
|
|
11499
|
-
requestResolved = true;
|
|
11500
|
-
if (!res.ok || !res.body) {
|
|
11501
|
-
const text = await res.text();
|
|
11502
|
-
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
11503
|
-
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
11504
|
-
error.status = res.status;
|
|
11505
|
-
throw error;
|
|
11506
11555
|
}
|
|
11507
|
-
|
|
11508
|
-
|
|
11509
|
-
|
|
11510
|
-
|
|
11511
|
-
|
|
11512
|
-
const closeReader = async () => {
|
|
11513
|
-
try {
|
|
11514
|
-
readerReleased = true;
|
|
11515
|
-
await reader.cancel();
|
|
11516
|
-
}
|
|
11517
|
-
catch (ex) {
|
|
11518
|
-
// an error will throw if the reader hasn't been used yet
|
|
11519
|
-
}
|
|
11520
|
-
reader.releaseLock();
|
|
11521
|
-
};
|
|
11522
|
-
const stream = new DataStream({
|
|
11523
|
-
logger: this.logger,
|
|
11524
|
-
mapLine: mapLine,
|
|
11525
|
-
pressure: {
|
|
11526
|
-
highWaterMark: 20,
|
|
11527
|
-
lowWaterMark: 10
|
|
11528
|
-
}
|
|
11529
|
-
});
|
|
11530
|
-
abortSignal?.addEventListener('abort', () => {
|
|
11531
|
-
closeReader();
|
|
11532
|
-
stream.close();
|
|
11533
|
-
});
|
|
11534
|
-
const decoder = this.createTextDecoder();
|
|
11535
|
-
let buffer = '';
|
|
11536
|
-
const consumeStream = async () => {
|
|
11537
|
-
while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
|
|
11538
|
-
const { done, value } = await reader.read();
|
|
11539
|
-
if (done) {
|
|
11540
|
-
const remaining = buffer.trim();
|
|
11541
|
-
if (remaining.length != 0) {
|
|
11542
|
-
stream.enqueueData(remaining);
|
|
11543
|
-
}
|
|
11544
|
-
stream.close();
|
|
11545
|
-
await closeReader();
|
|
11546
|
-
return;
|
|
11556
|
+
reader = res.body.getReader();
|
|
11557
|
+
const stream = {
|
|
11558
|
+
next: async () => {
|
|
11559
|
+
if (controller.signal.aborted) {
|
|
11560
|
+
return doneResult;
|
|
11547
11561
|
}
|
|
11548
|
-
|
|
11549
|
-
|
|
11550
|
-
const lines = buffer.split('\n');
|
|
11551
|
-
for (var i = 0; i < lines.length - 1; i++) {
|
|
11552
|
-
var l = lines[i].trim();
|
|
11553
|
-
if (l.length > 0) {
|
|
11554
|
-
stream.enqueueData(l);
|
|
11555
|
-
}
|
|
11562
|
+
try {
|
|
11563
|
+
return await reader.read();
|
|
11556
11564
|
}
|
|
11557
|
-
|
|
11558
|
-
|
|
11559
|
-
|
|
11560
|
-
|
|
11561
|
-
|
|
11562
|
-
|
|
11563
|
-
|
|
11564
|
-
dispose();
|
|
11565
|
-
},
|
|
11566
|
-
closed: () => {
|
|
11567
|
-
resolve();
|
|
11568
|
-
dispose();
|
|
11569
|
-
}
|
|
11570
|
-
});
|
|
11571
|
-
});
|
|
11565
|
+
catch (ex) {
|
|
11566
|
+
if (controller.signal.aborted) {
|
|
11567
|
+
// .read() completes with an error if we cancel the reader, which we do to disconnect. So this is just
|
|
11568
|
+
// things working as intended, we can return a done event and consider the exception handled.
|
|
11569
|
+
return doneResult;
|
|
11570
|
+
}
|
|
11571
|
+
throw ex;
|
|
11572
11572
|
}
|
|
11573
11573
|
}
|
|
11574
11574
|
};
|
|
11575
|
-
|
|
11576
|
-
|
|
11577
|
-
|
|
11578
|
-
|
|
11579
|
-
|
|
11580
|
-
|
|
11581
|
-
|
|
11582
|
-
|
|
11575
|
+
return { isBson: responseIsBson, stream };
|
|
11576
|
+
}
|
|
11577
|
+
/**
|
|
11578
|
+
* Posts a `/sync/stream` request.
|
|
11579
|
+
*
|
|
11580
|
+
* Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
|
|
11581
|
+
* {@link Uint8Array}s.
|
|
11582
|
+
*/
|
|
11583
|
+
async fetchStream(options) {
|
|
11584
|
+
const { isBson, stream } = await this.fetchStreamRaw(options);
|
|
11585
|
+
if (isBson) {
|
|
11586
|
+
return extractBsonObjects(stream);
|
|
11587
|
+
}
|
|
11588
|
+
else {
|
|
11589
|
+
return extractJsonLines(stream, this.createTextDecoder());
|
|
11590
|
+
}
|
|
11583
11591
|
}
|
|
11584
11592
|
}
|
|
11585
11593
|
|
|
@@ -12087,6 +12095,19 @@ The next upload iteration will be delayed.`);
|
|
|
12087
12095
|
}
|
|
12088
12096
|
});
|
|
12089
12097
|
}
|
|
12098
|
+
async receiveSyncLines(data) {
|
|
12099
|
+
const { options, connection, bson } = data;
|
|
12100
|
+
const remote = this.options.remote;
|
|
12101
|
+
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
12102
|
+
return await remote.fetchStream(options);
|
|
12103
|
+
}
|
|
12104
|
+
else {
|
|
12105
|
+
return await this.options.remote.socketStreamRaw({
|
|
12106
|
+
...options,
|
|
12107
|
+
...{ fetchStrategy: connection.fetchStrategy }
|
|
12108
|
+
}, bson);
|
|
12109
|
+
}
|
|
12110
|
+
}
|
|
12090
12111
|
async legacyStreamingSyncIteration(signal, resolvedOptions) {
|
|
12091
12112
|
const rawTables = resolvedOptions.serializedSchema?.raw_tables;
|
|
12092
12113
|
if (rawTables != null && rawTables.length) {
|
|
@@ -12116,42 +12137,27 @@ The next upload iteration will be delayed.`);
|
|
|
12116
12137
|
client_id: clientId
|
|
12117
12138
|
}
|
|
12118
12139
|
};
|
|
12119
|
-
|
|
12120
|
-
|
|
12121
|
-
|
|
12122
|
-
|
|
12123
|
-
|
|
12124
|
-
|
|
12125
|
-
|
|
12126
|
-
|
|
12127
|
-
|
|
12128
|
-
|
|
12129
|
-
|
|
12130
|
-
|
|
12131
|
-
|
|
12132
|
-
|
|
12133
|
-
stream = await this.options.remote.socketStreamRaw({
|
|
12134
|
-
...syncOptions,
|
|
12135
|
-
...{ fetchStrategy: resolvedOptions.fetchStrategy }
|
|
12136
|
-
}, (payload) => {
|
|
12137
|
-
if (payload instanceof Uint8Array) {
|
|
12138
|
-
return bson.deserialize(payload);
|
|
12139
|
-
}
|
|
12140
|
-
else {
|
|
12141
|
-
// Directly enqueued by us
|
|
12142
|
-
return payload;
|
|
12143
|
-
}
|
|
12144
|
-
}, bson);
|
|
12145
|
-
}
|
|
12140
|
+
const bson = await this.options.remote.getBSON();
|
|
12141
|
+
const source = await this.receiveSyncLines({
|
|
12142
|
+
options: syncOptions,
|
|
12143
|
+
connection: resolvedOptions,
|
|
12144
|
+
bson
|
|
12145
|
+
});
|
|
12146
|
+
const stream = injectable(map(source, (line) => {
|
|
12147
|
+
if (typeof line == 'string') {
|
|
12148
|
+
return JSON.parse(line);
|
|
12149
|
+
}
|
|
12150
|
+
else {
|
|
12151
|
+
return bson.deserialize(line);
|
|
12152
|
+
}
|
|
12153
|
+
}));
|
|
12146
12154
|
this.logger.debug('Stream established. Processing events');
|
|
12147
12155
|
this.notifyCompletedUploads = () => {
|
|
12148
|
-
|
|
12149
|
-
stream.enqueueData({ crud_upload_completed: null });
|
|
12150
|
-
}
|
|
12156
|
+
stream.inject({ crud_upload_completed: null });
|
|
12151
12157
|
};
|
|
12152
|
-
while (
|
|
12153
|
-
const line = await stream.
|
|
12154
|
-
if (
|
|
12158
|
+
while (true) {
|
|
12159
|
+
const { value: line, done } = await stream.next();
|
|
12160
|
+
if (done) {
|
|
12155
12161
|
// The stream has closed while waiting
|
|
12156
12162
|
return;
|
|
12157
12163
|
}
|
|
@@ -12330,14 +12336,17 @@ The next upload iteration will be delayed.`);
|
|
|
12330
12336
|
const syncImplementation = this;
|
|
12331
12337
|
const adapter = this.options.adapter;
|
|
12332
12338
|
const remote = this.options.remote;
|
|
12339
|
+
const controller = new AbortController();
|
|
12340
|
+
const abort = () => {
|
|
12341
|
+
return controller.abort(signal.reason);
|
|
12342
|
+
};
|
|
12343
|
+
signal.addEventListener('abort', abort);
|
|
12333
12344
|
let receivingLines = null;
|
|
12334
12345
|
let hadSyncLine = false;
|
|
12335
12346
|
let hideDisconnectOnRestart = false;
|
|
12336
12347
|
if (signal.aborted) {
|
|
12337
12348
|
throw new AbortOperation('Connection request has been aborted');
|
|
12338
12349
|
}
|
|
12339
|
-
const abortController = new AbortController();
|
|
12340
|
-
signal.addEventListener('abort', () => abortController.abort());
|
|
12341
12350
|
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
|
|
12342
12351
|
// invocation (local events include refreshed tokens and completed uploads).
|
|
12343
12352
|
// This is a single data stream so that we can handle all control calls from a single place.
|
|
@@ -12345,49 +12354,36 @@ The next upload iteration will be delayed.`);
|
|
|
12345
12354
|
async function connect(instr) {
|
|
12346
12355
|
const syncOptions = {
|
|
12347
12356
|
path: '/sync/stream',
|
|
12348
|
-
abortSignal:
|
|
12357
|
+
abortSignal: controller.signal,
|
|
12349
12358
|
data: instr.request
|
|
12350
12359
|
};
|
|
12351
|
-
|
|
12352
|
-
|
|
12353
|
-
|
|
12354
|
-
|
|
12355
|
-
|
|
12356
|
-
|
|
12357
|
-
|
|
12358
|
-
|
|
12359
|
-
|
|
12360
|
-
|
|
12361
|
-
|
|
12362
|
-
|
|
12363
|
-
|
|
12364
|
-
|
|
12365
|
-
|
|
12366
|
-
|
|
12367
|
-
|
|
12368
|
-
fetchStrategy: resolvedOptions.fetchStrategy
|
|
12369
|
-
}, (payload) => {
|
|
12370
|
-
if (payload instanceof Uint8Array) {
|
|
12371
|
-
return {
|
|
12372
|
-
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
12373
|
-
payload: payload
|
|
12374
|
-
};
|
|
12375
|
-
}
|
|
12376
|
-
else {
|
|
12377
|
-
// Directly enqueued by us
|
|
12378
|
-
return payload;
|
|
12379
|
-
}
|
|
12380
|
-
});
|
|
12381
|
-
}
|
|
12360
|
+
controlInvocations = injectable(map(await syncImplementation.receiveSyncLines({
|
|
12361
|
+
options: syncOptions,
|
|
12362
|
+
connection: resolvedOptions
|
|
12363
|
+
}), (line) => {
|
|
12364
|
+
if (typeof line == 'string') {
|
|
12365
|
+
return {
|
|
12366
|
+
command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
|
|
12367
|
+
payload: line
|
|
12368
|
+
};
|
|
12369
|
+
}
|
|
12370
|
+
else {
|
|
12371
|
+
return {
|
|
12372
|
+
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
12373
|
+
payload: line
|
|
12374
|
+
};
|
|
12375
|
+
}
|
|
12376
|
+
}));
|
|
12382
12377
|
// The rust client will set connected: true after the first sync line because that's when it gets invoked, but
|
|
12383
12378
|
// we're already connected here and can report that.
|
|
12384
12379
|
syncImplementation.updateSyncStatus({ connected: true });
|
|
12385
12380
|
try {
|
|
12386
|
-
while (
|
|
12387
|
-
|
|
12388
|
-
if (
|
|
12389
|
-
|
|
12381
|
+
while (true) {
|
|
12382
|
+
let event = await controlInvocations.next();
|
|
12383
|
+
if (event.done) {
|
|
12384
|
+
break;
|
|
12390
12385
|
}
|
|
12386
|
+
const line = event.value;
|
|
12391
12387
|
await control(line.command, line.payload);
|
|
12392
12388
|
if (!hadSyncLine) {
|
|
12393
12389
|
syncImplementation.triggerCrudUpload();
|
|
@@ -12396,12 +12392,8 @@ The next upload iteration will be delayed.`);
|
|
|
12396
12392
|
}
|
|
12397
12393
|
}
|
|
12398
12394
|
finally {
|
|
12399
|
-
|
|
12400
|
-
|
|
12401
|
-
// refreshed. That would throw after closing (and we can't handle those events either way), so set this back
|
|
12402
|
-
// to null.
|
|
12403
|
-
controlInvocations = null;
|
|
12404
|
-
await activeInstructions.close();
|
|
12395
|
+
abort();
|
|
12396
|
+
signal.removeEventListener('abort', abort);
|
|
12405
12397
|
}
|
|
12406
12398
|
}
|
|
12407
12399
|
async function stop() {
|
|
@@ -12445,14 +12437,14 @@ The next upload iteration will be delayed.`);
|
|
|
12445
12437
|
remote.invalidateCredentials();
|
|
12446
12438
|
// Restart iteration after the credentials have been refreshed.
|
|
12447
12439
|
remote.fetchCredentials().then((_) => {
|
|
12448
|
-
controlInvocations?.
|
|
12440
|
+
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
|
|
12449
12441
|
}, (err) => {
|
|
12450
12442
|
syncImplementation.logger.warn('Could not prefetch credentials', err);
|
|
12451
12443
|
});
|
|
12452
12444
|
}
|
|
12453
12445
|
}
|
|
12454
12446
|
else if ('CloseSyncStream' in instruction) {
|
|
12455
|
-
|
|
12447
|
+
controller.abort();
|
|
12456
12448
|
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
12457
12449
|
}
|
|
12458
12450
|
else if ('FlushFileSystem' in instruction) ;
|
|
@@ -12481,17 +12473,13 @@ The next upload iteration will be delayed.`);
|
|
|
12481
12473
|
}
|
|
12482
12474
|
await control(PowerSyncControlCommand.START, JSON.stringify(options));
|
|
12483
12475
|
this.notifyCompletedUploads = () => {
|
|
12484
|
-
|
|
12485
|
-
controlInvocations.enqueueData({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
12486
|
-
}
|
|
12476
|
+
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
12487
12477
|
};
|
|
12488
12478
|
this.handleActiveStreamsChange = () => {
|
|
12489
|
-
|
|
12490
|
-
|
|
12491
|
-
|
|
12492
|
-
|
|
12493
|
-
});
|
|
12494
|
-
}
|
|
12479
|
+
controlInvocations?.inject({
|
|
12480
|
+
command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
|
|
12481
|
+
payload: JSON.stringify(this.activeStreams)
|
|
12482
|
+
});
|
|
12495
12483
|
};
|
|
12496
12484
|
await receivingLines;
|
|
12497
12485
|
}
|
|
@@ -14684,5 +14672,5 @@ const parseQuery = (query, parameters) => {
|
|
|
14684
14672
|
return { sqlStatement, parameters: parameters };
|
|
14685
14673
|
};
|
|
14686
14674
|
|
|
14687
|
-
export { ATTACHMENT_TABLE, AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, AttachmentContext, AttachmentQueue, AttachmentService, AttachmentState, AttachmentTable, BaseObserver, Column, ColumnType, ConnectionClosedError, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, DBAdapterDefaultMixin, DBGetUtilsDefaultMixin, DEFAULT_CRUD_BATCH_LIMIT, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_INDEX_COLUMN_OPTIONS, DEFAULT_INDEX_OPTIONS, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_POWERSYNC_CLOSE_OPTIONS, DEFAULT_POWERSYNC_DB_OPTIONS,
|
|
14675
|
+
export { ATTACHMENT_TABLE, AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, AttachmentContext, AttachmentQueue, AttachmentService, AttachmentState, AttachmentTable, BaseObserver, Column, ColumnType, ConnectionClosedError, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, DBAdapterDefaultMixin, DBGetUtilsDefaultMixin, DEFAULT_CRUD_BATCH_LIMIT, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_INDEX_COLUMN_OPTIONS, DEFAULT_INDEX_OPTIONS, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_POWERSYNC_CLOSE_OPTIONS, DEFAULT_POWERSYNC_DB_OPTIONS, DEFAULT_REMOTE_LOGGER, DEFAULT_REMOTE_OPTIONS, DEFAULT_RETRY_DELAY_MS, DEFAULT_ROW_COMPARATOR, DEFAULT_STREAMING_SYNC_OPTIONS, DEFAULT_STREAM_CONNECTION_OPTIONS, DEFAULT_SYNC_CLIENT_IMPLEMENTATION, DEFAULT_TABLE_OPTIONS, DEFAULT_WATCH_QUERY_OPTIONS, DEFAULT_WATCH_THROTTLE_MS, DiffTriggerOperation, DifferentialQueryProcessor, EMPTY_DIFFERENTIAL, EncodingType, FalsyComparator, FetchImplementationProvider, FetchStrategy, GetAllQuery, Index, IndexedColumn, InvalidSQLCharacters, LockType, LogLevel, MAX_AMOUNT_OF_COLUMNS, MAX_OP_ID, MEMORY_TRIGGER_CLAIM_MANAGER, Mutex, OnChangeQueryProcessor, OpType, OpTypeEnum, OplogEntry, PSInternalTable, PowerSyncControlCommand, RowUpdateType, Schema, Semaphore, SqliteBucketStorage, SyncClientImplementation, SyncDataBatch, SyncDataBucket, SyncProgress, SyncStatus, SyncStreamConnectionMethod, SyncingService, Table, TableV2, TriggerManagerImpl, UpdateType, UploadQueueStats, WatchedQueryListenerEvent, attachmentFromSql, column, compilableQueryWatch, createBaseLogger, createLogger, extractTableUpdates, isBatchedUpdateNotification, isContinueCheckpointRequest, isDBAdapter, isPowerSyncDatabaseOptionsWithSettings, isSQLOpenFactory, isSQLOpenOptions, isStreamingKeepalive, isStreamingSyncCheckpoint, isStreamingSyncCheckpointComplete, isStreamingSyncCheckpointDiff, isStreamingSyncCheckpointPartiallyComplete, isStreamingSyncData, isSyncNewCheckpointRequest, parseQuery, runOnSchemaChange, sanitizeSQL, sanitizeUUID, timeoutSignal };
|
|
14688
14676
|
//# sourceMappingURL=bundle.mjs.map
|