@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.node.cjs
CHANGED
|
@@ -2752,6 +2752,37 @@ class EventQueue {
|
|
|
2752
2752
|
};
|
|
2753
2753
|
}
|
|
2754
2754
|
}
|
|
2755
|
+
/**
|
|
2756
|
+
* Runs `action` with a signal that aborts once `signal` aborts or after `timeoutMs`, whichever comes first.
|
|
2757
|
+
*
|
|
2758
|
+
* @param description Names the operation in the error thrown when it times out.
|
|
2759
|
+
*/
|
|
2760
|
+
async function runWithTimeout(signal, timeoutMs, description, action) {
|
|
2761
|
+
const controller = new AbortController();
|
|
2762
|
+
const timeoutError = new Error(`${description} timed out after ${timeoutMs}ms`);
|
|
2763
|
+
const onAbort = () => controller.abort(signal?.reason);
|
|
2764
|
+
if (signal?.aborted) {
|
|
2765
|
+
onAbort();
|
|
2766
|
+
}
|
|
2767
|
+
else {
|
|
2768
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
2769
|
+
}
|
|
2770
|
+
const timeout = setTimeout(() => controller.abort(timeoutError), timeoutMs);
|
|
2771
|
+
try {
|
|
2772
|
+
return await action(controller.signal);
|
|
2773
|
+
}
|
|
2774
|
+
catch (error) {
|
|
2775
|
+
// Some fetch implementations reject with a generic abort error instead of the signal's reason.
|
|
2776
|
+
if (controller.signal.reason === timeoutError) {
|
|
2777
|
+
throw timeoutError;
|
|
2778
|
+
}
|
|
2779
|
+
throw error;
|
|
2780
|
+
}
|
|
2781
|
+
finally {
|
|
2782
|
+
clearTimeout(timeout);
|
|
2783
|
+
signal?.removeEventListener('abort', onAbort);
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2755
2786
|
|
|
2756
2787
|
/**
|
|
2757
2788
|
* @internal
|
|
@@ -8610,7 +8641,7 @@ function requireDist () {
|
|
|
8610
8641
|
|
|
8611
8642
|
var distExports = requireDist();
|
|
8612
8643
|
|
|
8613
|
-
var version = "1.
|
|
8644
|
+
var version = "1.58.0";
|
|
8614
8645
|
var PACKAGE = {
|
|
8615
8646
|
version: version};
|
|
8616
8647
|
|
|
@@ -8945,14 +8976,15 @@ class AbstractRemote {
|
|
|
8945
8976
|
}
|
|
8946
8977
|
return res.json();
|
|
8947
8978
|
}
|
|
8948
|
-
async get(path, headers) {
|
|
8979
|
+
async get(path, headers, signal) {
|
|
8949
8980
|
const request = await this.buildRequest(path);
|
|
8950
8981
|
const res = await this.fetch(request.url, {
|
|
8951
8982
|
method: 'GET',
|
|
8952
8983
|
headers: {
|
|
8953
8984
|
...headers,
|
|
8954
8985
|
...request.headers
|
|
8955
|
-
}
|
|
8986
|
+
},
|
|
8987
|
+
signal
|
|
8956
8988
|
});
|
|
8957
8989
|
if (res.status === 401) {
|
|
8958
8990
|
this.invalidateCredentials();
|
|
@@ -9005,9 +9037,11 @@ class AbstractRemote {
|
|
|
9005
9037
|
}
|
|
9006
9038
|
});
|
|
9007
9039
|
let didClose = false;
|
|
9008
|
-
let connectionEstablished = false;
|
|
9009
9040
|
let pendingEventsCount = syncQueueRequestSize;
|
|
9010
9041
|
let res = null;
|
|
9042
|
+
// Function to reject the promise returned by this method. Set to null while initializing the socket, and after the
|
|
9043
|
+
// promise was resolved.
|
|
9044
|
+
let rejectPendingQueue = null;
|
|
9011
9045
|
const abortRequest = () => {
|
|
9012
9046
|
if (didClose) {
|
|
9013
9047
|
return;
|
|
@@ -9020,6 +9054,10 @@ class AbstractRemote {
|
|
|
9020
9054
|
if (rsocket) {
|
|
9021
9055
|
rsocket.close();
|
|
9022
9056
|
}
|
|
9057
|
+
if (rejectPendingQueue) {
|
|
9058
|
+
rejectPendingQueue(new Error('Connection aborted'));
|
|
9059
|
+
rejectPendingQueue = null;
|
|
9060
|
+
}
|
|
9023
9061
|
// Send a bogus event to the queue to ensure a pending listener gets woken up. We check for didClose and would
|
|
9024
9062
|
// return a doneEvent.
|
|
9025
9063
|
queue.notify(null);
|
|
@@ -9091,6 +9129,7 @@ class AbstractRemote {
|
|
|
9091
9129
|
// Helps to prevent double close scenarios
|
|
9092
9130
|
rsocket.onClose(() => (rsocket = null));
|
|
9093
9131
|
return await new Promise((resolve, reject) => {
|
|
9132
|
+
rejectPendingQueue = reject;
|
|
9094
9133
|
const queueAsIterator = {
|
|
9095
9134
|
next: async () => {
|
|
9096
9135
|
if (didClose)
|
|
@@ -9131,14 +9170,15 @@ class AbstractRemote {
|
|
|
9131
9170
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
9132
9171
|
abortRequest();
|
|
9133
9172
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
9134
|
-
if (
|
|
9173
|
+
if (rejectPendingQueue) {
|
|
9135
9174
|
reject(e);
|
|
9175
|
+
rejectPendingQueue = null;
|
|
9136
9176
|
}
|
|
9137
9177
|
},
|
|
9138
9178
|
onNext: (payload) => {
|
|
9139
9179
|
// The connection is active
|
|
9140
|
-
if (
|
|
9141
|
-
|
|
9180
|
+
if (rejectPendingQueue) {
|
|
9181
|
+
rejectPendingQueue = null;
|
|
9142
9182
|
resolve(queueAsIterator);
|
|
9143
9183
|
}
|
|
9144
9184
|
const { data } = payload;
|
|
@@ -9151,6 +9191,12 @@ class AbstractRemote {
|
|
|
9151
9191
|
requestMore();
|
|
9152
9192
|
},
|
|
9153
9193
|
onComplete: () => {
|
|
9194
|
+
console.log('onComplete', rejectPendingQueue);
|
|
9195
|
+
if (rejectPendingQueue) {
|
|
9196
|
+
// Closed without ever emitting an event? Still emit an empty queue.
|
|
9197
|
+
rejectPendingQueue = null;
|
|
9198
|
+
resolve(queueAsIterator);
|
|
9199
|
+
}
|
|
9154
9200
|
abortRequest(); // this will also emit a done event
|
|
9155
9201
|
},
|
|
9156
9202
|
onExtension: () => { }
|
|
@@ -9173,7 +9219,7 @@ class AbstractRemote {
|
|
|
9173
9219
|
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
9174
9220
|
*/
|
|
9175
9221
|
async fetchStreamRaw(options) {
|
|
9176
|
-
const { data, path, headers, abortSignal } = options;
|
|
9222
|
+
const { data, path, headers, abortSignal, connectTimeoutMs } = options;
|
|
9177
9223
|
const request = await this.buildRequest(path);
|
|
9178
9224
|
/**
|
|
9179
9225
|
* This abort controller will abort pending fetch requests.
|
|
@@ -9208,7 +9254,7 @@ class AbstractRemote {
|
|
|
9208
9254
|
try {
|
|
9209
9255
|
const ndJson = 'application/x-ndjson';
|
|
9210
9256
|
const bson = 'application/vnd.powersync.bson-stream';
|
|
9211
|
-
|
|
9257
|
+
const postStream = (signal) => this.fetch(request.url, {
|
|
9212
9258
|
method: 'POST',
|
|
9213
9259
|
headers: {
|
|
9214
9260
|
...headers,
|
|
@@ -9216,11 +9262,15 @@ class AbstractRemote {
|
|
|
9216
9262
|
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
9217
9263
|
},
|
|
9218
9264
|
body: JSON.stringify(data),
|
|
9219
|
-
signal
|
|
9265
|
+
signal,
|
|
9220
9266
|
cache: 'no-store',
|
|
9221
9267
|
...(this.options.fetchOptions ?? {}),
|
|
9222
9268
|
...options.fetchOptions
|
|
9223
9269
|
});
|
|
9270
|
+
// The timeout ends once the response starts, so it never aborts the response body.
|
|
9271
|
+
res = await (connectTimeoutMs == null
|
|
9272
|
+
? postStream(controller.signal)
|
|
9273
|
+
: runWithTimeout(controller.signal, connectTimeoutMs, 'Sync stream connection', postStream));
|
|
9224
9274
|
if (!res.ok || !res.body) {
|
|
9225
9275
|
const text = await res.text();
|
|
9226
9276
|
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
@@ -9379,7 +9429,8 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
|
9379
9429
|
fetchStrategy: exports.FetchStrategy.Buffered,
|
|
9380
9430
|
params: {},
|
|
9381
9431
|
serializedSchema: undefined,
|
|
9382
|
-
includeDefaultStreams: true
|
|
9432
|
+
includeDefaultStreams: true,
|
|
9433
|
+
connectTimeoutMs: 30_000
|
|
9383
9434
|
};
|
|
9384
9435
|
/**
|
|
9385
9436
|
* @internal
|
|
@@ -9393,6 +9444,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
9393
9444
|
activeStreams;
|
|
9394
9445
|
connectionMayHaveChanged = false;
|
|
9395
9446
|
crudUploadNotifier = asyncNotifier();
|
|
9447
|
+
connectTimeoutMs = DEFAULT_STREAM_CONNECTION_OPTIONS.connectTimeoutMs;
|
|
9396
9448
|
notifyCompletedUploads;
|
|
9397
9449
|
handleActiveStreamsChange;
|
|
9398
9450
|
syncStatus;
|
|
@@ -9462,10 +9514,10 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
9462
9514
|
this.crudUpdateListener?.();
|
|
9463
9515
|
this.crudUpdateListener = undefined;
|
|
9464
9516
|
}
|
|
9465
|
-
async getWriteCheckpoint() {
|
|
9517
|
+
async getWriteCheckpoint(abort) {
|
|
9466
9518
|
const clientId = await this.options.adapter.getClientId();
|
|
9467
9519
|
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
9468
|
-
const response = await this.options.remote.get(path);
|
|
9520
|
+
const response = await runWithTimeout(abort, this.connectTimeoutMs, 'Write checkpoint request', (signal) => this.options.remote.get(path, undefined, signal));
|
|
9469
9521
|
const checkpoint = response['data']['write_checkpoint'];
|
|
9470
9522
|
this.logger.debug(`Created write checkpoint: ${checkpoint}`);
|
|
9471
9523
|
return checkpoint;
|
|
@@ -9518,7 +9570,7 @@ The next upload iteration will be delayed.`);
|
|
|
9518
9570
|
}
|
|
9519
9571
|
else {
|
|
9520
9572
|
// Uploading is completed
|
|
9521
|
-
const neededUpdate = await this.options.adapter.updateLocalTarget(() => this.getWriteCheckpoint());
|
|
9573
|
+
const neededUpdate = await this.options.adapter.updateLocalTarget(() => this.getWriteCheckpoint(signal));
|
|
9522
9574
|
if (neededUpdate) {
|
|
9523
9575
|
this.notifyCompletedUploads?.();
|
|
9524
9576
|
}
|
|
@@ -9561,6 +9613,7 @@ The next upload iteration will be delayed.`);
|
|
|
9561
9613
|
}
|
|
9562
9614
|
const controller = new AbortController();
|
|
9563
9615
|
this.abortController = controller;
|
|
9616
|
+
this.connectTimeoutMs = options?.connectTimeoutMs ?? DEFAULT_STREAM_CONNECTION_OPTIONS.connectTimeoutMs;
|
|
9564
9617
|
this.streamingSyncPromise = Promise.all([
|
|
9565
9618
|
this.crudUploadLoop(controller.signal).catch((ex) => this.logger.error('Error in crud upload loop', ex)),
|
|
9566
9619
|
this.streamingSync(controller.signal, options)
|
|
@@ -9890,7 +9943,8 @@ The next upload iteration will be delayed.`);
|
|
|
9890
9943
|
const syncOptions = {
|
|
9891
9944
|
path: '/sync/stream',
|
|
9892
9945
|
abortSignal: signal,
|
|
9893
|
-
data: startInstruction.EstablishSyncStream.request
|
|
9946
|
+
data: startInstruction.EstablishSyncStream.request,
|
|
9947
|
+
connectTimeoutMs: resolvedOptions.connectTimeoutMs
|
|
9894
9948
|
};
|
|
9895
9949
|
controlInvocations = injectable(syncImplementation.receiveSyncLines({
|
|
9896
9950
|
options: syncOptions,
|