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