@powersync/common 1.57.3 → 1.58.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 +69 -15
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +69 -15
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +69 -15
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +69 -15
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +21 -3
- package/lib/client/sync/stream/AbstractRemote.d.ts +8 -1
- package/lib/client/sync/stream/AbstractRemote.js +29 -10
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +13 -2
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +10 -6
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/utils/async.d.ts +6 -0
- package/lib/utils/async.js +31 -0
- package/lib/utils/async.js.map +1 -1
- package/package.json +1 -1
- package/src/client/sync/stream/AbstractRemote.ts +53 -21
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +24 -7
- package/src/utils/async.ts +36 -0
package/dist/bundle.mjs
CHANGED
|
@@ -2748,6 +2748,37 @@ class EventQueue {
|
|
|
2748
2748
|
};
|
|
2749
2749
|
}
|
|
2750
2750
|
}
|
|
2751
|
+
/**
|
|
2752
|
+
* Runs `action` with a signal that aborts once `signal` aborts or after `timeoutMs`, whichever comes first.
|
|
2753
|
+
*
|
|
2754
|
+
* @param description Names the operation in the error thrown when it times out.
|
|
2755
|
+
*/
|
|
2756
|
+
async function runWithTimeout(signal, timeoutMs, description, action) {
|
|
2757
|
+
const controller = new AbortController();
|
|
2758
|
+
const timeoutError = new Error(`${description} timed out after ${timeoutMs}ms`);
|
|
2759
|
+
const onAbort = () => controller.abort(signal?.reason);
|
|
2760
|
+
if (signal?.aborted) {
|
|
2761
|
+
onAbort();
|
|
2762
|
+
}
|
|
2763
|
+
else {
|
|
2764
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
2765
|
+
}
|
|
2766
|
+
const timeout = setTimeout(() => controller.abort(timeoutError), timeoutMs);
|
|
2767
|
+
try {
|
|
2768
|
+
return await action(controller.signal);
|
|
2769
|
+
}
|
|
2770
|
+
catch (error) {
|
|
2771
|
+
// Some fetch implementations reject with a generic abort error instead of the signal's reason.
|
|
2772
|
+
if (controller.signal.reason === timeoutError) {
|
|
2773
|
+
throw timeoutError;
|
|
2774
|
+
}
|
|
2775
|
+
throw error;
|
|
2776
|
+
}
|
|
2777
|
+
finally {
|
|
2778
|
+
clearTimeout(timeout);
|
|
2779
|
+
signal?.removeEventListener('abort', onAbort);
|
|
2780
|
+
}
|
|
2781
|
+
}
|
|
2751
2782
|
|
|
2752
2783
|
/**
|
|
2753
2784
|
* @internal
|
|
@@ -10977,7 +11008,7 @@ function requireDist () {
|
|
|
10977
11008
|
|
|
10978
11009
|
var distExports = requireDist();
|
|
10979
11010
|
|
|
10980
|
-
var version = "1.
|
|
11011
|
+
var version = "1.58.0";
|
|
10981
11012
|
var PACKAGE = {
|
|
10982
11013
|
version: version};
|
|
10983
11014
|
|
|
@@ -11312,14 +11343,15 @@ class AbstractRemote {
|
|
|
11312
11343
|
}
|
|
11313
11344
|
return res.json();
|
|
11314
11345
|
}
|
|
11315
|
-
async get(path, headers) {
|
|
11346
|
+
async get(path, headers, signal) {
|
|
11316
11347
|
const request = await this.buildRequest(path);
|
|
11317
11348
|
const res = await this.fetch(request.url, {
|
|
11318
11349
|
method: 'GET',
|
|
11319
11350
|
headers: {
|
|
11320
11351
|
...headers,
|
|
11321
11352
|
...request.headers
|
|
11322
|
-
}
|
|
11353
|
+
},
|
|
11354
|
+
signal
|
|
11323
11355
|
});
|
|
11324
11356
|
if (res.status === 401) {
|
|
11325
11357
|
this.invalidateCredentials();
|
|
@@ -11372,9 +11404,11 @@ class AbstractRemote {
|
|
|
11372
11404
|
}
|
|
11373
11405
|
});
|
|
11374
11406
|
let didClose = false;
|
|
11375
|
-
let connectionEstablished = false;
|
|
11376
11407
|
let pendingEventsCount = syncQueueRequestSize;
|
|
11377
11408
|
let res = null;
|
|
11409
|
+
// Function to reject the promise returned by this method. Set to null while initializing the socket, and after the
|
|
11410
|
+
// promise was resolved.
|
|
11411
|
+
let rejectPendingQueue = null;
|
|
11378
11412
|
const abortRequest = () => {
|
|
11379
11413
|
if (didClose) {
|
|
11380
11414
|
return;
|
|
@@ -11387,6 +11421,10 @@ class AbstractRemote {
|
|
|
11387
11421
|
if (rsocket) {
|
|
11388
11422
|
rsocket.close();
|
|
11389
11423
|
}
|
|
11424
|
+
if (rejectPendingQueue) {
|
|
11425
|
+
rejectPendingQueue(new Error('Connection aborted'));
|
|
11426
|
+
rejectPendingQueue = null;
|
|
11427
|
+
}
|
|
11390
11428
|
// Send a bogus event to the queue to ensure a pending listener gets woken up. We check for didClose and would
|
|
11391
11429
|
// return a doneEvent.
|
|
11392
11430
|
queue.notify(null);
|
|
@@ -11458,6 +11496,7 @@ class AbstractRemote {
|
|
|
11458
11496
|
// Helps to prevent double close scenarios
|
|
11459
11497
|
rsocket.onClose(() => (rsocket = null));
|
|
11460
11498
|
return await new Promise((resolve, reject) => {
|
|
11499
|
+
rejectPendingQueue = reject;
|
|
11461
11500
|
const queueAsIterator = {
|
|
11462
11501
|
next: async () => {
|
|
11463
11502
|
if (didClose)
|
|
@@ -11498,14 +11537,15 @@ class AbstractRemote {
|
|
|
11498
11537
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
11499
11538
|
abortRequest();
|
|
11500
11539
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
11501
|
-
if (
|
|
11540
|
+
if (rejectPendingQueue) {
|
|
11502
11541
|
reject(e);
|
|
11542
|
+
rejectPendingQueue = null;
|
|
11503
11543
|
}
|
|
11504
11544
|
},
|
|
11505
11545
|
onNext: (payload) => {
|
|
11506
11546
|
// The connection is active
|
|
11507
|
-
if (
|
|
11508
|
-
|
|
11547
|
+
if (rejectPendingQueue) {
|
|
11548
|
+
rejectPendingQueue = null;
|
|
11509
11549
|
resolve(queueAsIterator);
|
|
11510
11550
|
}
|
|
11511
11551
|
const { data } = payload;
|
|
@@ -11518,6 +11558,12 @@ class AbstractRemote {
|
|
|
11518
11558
|
requestMore();
|
|
11519
11559
|
},
|
|
11520
11560
|
onComplete: () => {
|
|
11561
|
+
console.log('onComplete', rejectPendingQueue);
|
|
11562
|
+
if (rejectPendingQueue) {
|
|
11563
|
+
// Closed without ever emitting an event? Still emit an empty queue.
|
|
11564
|
+
rejectPendingQueue = null;
|
|
11565
|
+
resolve(queueAsIterator);
|
|
11566
|
+
}
|
|
11521
11567
|
abortRequest(); // this will also emit a done event
|
|
11522
11568
|
},
|
|
11523
11569
|
onExtension: () => { }
|
|
@@ -11540,7 +11586,7 @@ class AbstractRemote {
|
|
|
11540
11586
|
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
11541
11587
|
*/
|
|
11542
11588
|
async fetchStreamRaw(options) {
|
|
11543
|
-
const { data, path, headers, abortSignal } = options;
|
|
11589
|
+
const { data, path, headers, abortSignal, connectTimeoutMs } = options;
|
|
11544
11590
|
const request = await this.buildRequest(path);
|
|
11545
11591
|
/**
|
|
11546
11592
|
* This abort controller will abort pending fetch requests.
|
|
@@ -11575,7 +11621,7 @@ class AbstractRemote {
|
|
|
11575
11621
|
try {
|
|
11576
11622
|
const ndJson = 'application/x-ndjson';
|
|
11577
11623
|
const bson = 'application/vnd.powersync.bson-stream';
|
|
11578
|
-
|
|
11624
|
+
const postStream = (signal) => this.fetch(request.url, {
|
|
11579
11625
|
method: 'POST',
|
|
11580
11626
|
headers: {
|
|
11581
11627
|
...headers,
|
|
@@ -11583,11 +11629,15 @@ class AbstractRemote {
|
|
|
11583
11629
|
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
11584
11630
|
},
|
|
11585
11631
|
body: JSON.stringify(data),
|
|
11586
|
-
signal
|
|
11632
|
+
signal,
|
|
11587
11633
|
cache: 'no-store',
|
|
11588
11634
|
...(this.options.fetchOptions ?? {}),
|
|
11589
11635
|
...options.fetchOptions
|
|
11590
11636
|
});
|
|
11637
|
+
// The timeout ends once the response starts, so it never aborts the response body.
|
|
11638
|
+
res = await (connectTimeoutMs == null
|
|
11639
|
+
? postStream(controller.signal)
|
|
11640
|
+
: runWithTimeout(controller.signal, connectTimeoutMs, 'Sync stream connection', postStream));
|
|
11591
11641
|
if (!res.ok || !res.body) {
|
|
11592
11642
|
const text = await res.text();
|
|
11593
11643
|
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
@@ -11746,7 +11796,8 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
|
11746
11796
|
fetchStrategy: FetchStrategy.Buffered,
|
|
11747
11797
|
params: {},
|
|
11748
11798
|
serializedSchema: undefined,
|
|
11749
|
-
includeDefaultStreams: true
|
|
11799
|
+
includeDefaultStreams: true,
|
|
11800
|
+
connectTimeoutMs: 30_000
|
|
11750
11801
|
};
|
|
11751
11802
|
/**
|
|
11752
11803
|
* @internal
|
|
@@ -11760,6 +11811,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
11760
11811
|
activeStreams;
|
|
11761
11812
|
connectionMayHaveChanged = false;
|
|
11762
11813
|
crudUploadNotifier = asyncNotifier();
|
|
11814
|
+
connectTimeoutMs = DEFAULT_STREAM_CONNECTION_OPTIONS.connectTimeoutMs;
|
|
11763
11815
|
notifyCompletedUploads;
|
|
11764
11816
|
handleActiveStreamsChange;
|
|
11765
11817
|
syncStatus;
|
|
@@ -11829,10 +11881,10 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
11829
11881
|
this.crudUpdateListener?.();
|
|
11830
11882
|
this.crudUpdateListener = undefined;
|
|
11831
11883
|
}
|
|
11832
|
-
async getWriteCheckpoint() {
|
|
11884
|
+
async getWriteCheckpoint(abort) {
|
|
11833
11885
|
const clientId = await this.options.adapter.getClientId();
|
|
11834
11886
|
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
11835
|
-
const response = await this.options.remote.get(path);
|
|
11887
|
+
const response = await runWithTimeout(abort, this.connectTimeoutMs, 'Write checkpoint request', (signal) => this.options.remote.get(path, undefined, signal));
|
|
11836
11888
|
const checkpoint = response['data']['write_checkpoint'];
|
|
11837
11889
|
this.logger.debug(`Created write checkpoint: ${checkpoint}`);
|
|
11838
11890
|
return checkpoint;
|
|
@@ -11885,7 +11937,7 @@ The next upload iteration will be delayed.`);
|
|
|
11885
11937
|
}
|
|
11886
11938
|
else {
|
|
11887
11939
|
// Uploading is completed
|
|
11888
|
-
const neededUpdate = await this.options.adapter.updateLocalTarget(() => this.getWriteCheckpoint());
|
|
11940
|
+
const neededUpdate = await this.options.adapter.updateLocalTarget(() => this.getWriteCheckpoint(signal));
|
|
11889
11941
|
if (neededUpdate) {
|
|
11890
11942
|
this.notifyCompletedUploads?.();
|
|
11891
11943
|
}
|
|
@@ -11928,6 +11980,7 @@ The next upload iteration will be delayed.`);
|
|
|
11928
11980
|
}
|
|
11929
11981
|
const controller = new AbortController();
|
|
11930
11982
|
this.abortController = controller;
|
|
11983
|
+
this.connectTimeoutMs = options?.connectTimeoutMs ?? DEFAULT_STREAM_CONNECTION_OPTIONS.connectTimeoutMs;
|
|
11931
11984
|
this.streamingSyncPromise = Promise.all([
|
|
11932
11985
|
this.crudUploadLoop(controller.signal).catch((ex) => this.logger.error('Error in crud upload loop', ex)),
|
|
11933
11986
|
this.streamingSync(controller.signal, options)
|
|
@@ -12257,7 +12310,8 @@ The next upload iteration will be delayed.`);
|
|
|
12257
12310
|
const syncOptions = {
|
|
12258
12311
|
path: '/sync/stream',
|
|
12259
12312
|
abortSignal: signal,
|
|
12260
|
-
data: startInstruction.EstablishSyncStream.request
|
|
12313
|
+
data: startInstruction.EstablishSyncStream.request,
|
|
12314
|
+
connectTimeoutMs: resolvedOptions.connectTimeoutMs
|
|
12261
12315
|
};
|
|
12262
12316
|
controlInvocations = injectable(syncImplementation.receiveSyncLines({
|
|
12263
12317
|
options: syncOptions,
|