@powersync/common 1.57.2 → 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 +81 -22
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +81 -22
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +81 -22
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +81 -22
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +22 -4
- package/lib/client/AbstractPowerSyncDatabase.d.ts +1 -1
- package/lib/client/AbstractPowerSyncDatabase.js +1 -1
- 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 +11 -6
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/utils/ControlledExecutor.js +10 -6
- package/lib/utils/ControlledExecutor.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/AbstractPowerSyncDatabase.ts +1 -1
- package/src/client/sync/stream/AbstractRemote.ts +53 -21
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +25 -7
- package/src/utils/ControlledExecutor.ts +9 -6
- package/src/utils/async.ts +36 -0
package/dist/bundle.mjs
CHANGED
|
@@ -2388,12 +2388,16 @@ class ControlledExecutor {
|
|
|
2388
2388
|
}
|
|
2389
2389
|
async execute(param) {
|
|
2390
2390
|
this.runningTask = this.task(param);
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
this.
|
|
2396
|
-
this.
|
|
2391
|
+
try {
|
|
2392
|
+
await this.runningTask;
|
|
2393
|
+
}
|
|
2394
|
+
finally {
|
|
2395
|
+
this.runningTask = undefined;
|
|
2396
|
+
if (this.pendingTaskParam) {
|
|
2397
|
+
const pendingParam = this.pendingTaskParam;
|
|
2398
|
+
this.pendingTaskParam = undefined;
|
|
2399
|
+
this.execute(pendingParam);
|
|
2400
|
+
}
|
|
2397
2401
|
}
|
|
2398
2402
|
}
|
|
2399
2403
|
}
|
|
@@ -2744,6 +2748,37 @@ class EventQueue {
|
|
|
2744
2748
|
};
|
|
2745
2749
|
}
|
|
2746
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
|
+
}
|
|
2747
2782
|
|
|
2748
2783
|
/**
|
|
2749
2784
|
* @internal
|
|
@@ -10973,7 +11008,7 @@ function requireDist () {
|
|
|
10973
11008
|
|
|
10974
11009
|
var distExports = requireDist();
|
|
10975
11010
|
|
|
10976
|
-
var version = "1.
|
|
11011
|
+
var version = "1.58.0";
|
|
10977
11012
|
var PACKAGE = {
|
|
10978
11013
|
version: version};
|
|
10979
11014
|
|
|
@@ -11308,14 +11343,15 @@ class AbstractRemote {
|
|
|
11308
11343
|
}
|
|
11309
11344
|
return res.json();
|
|
11310
11345
|
}
|
|
11311
|
-
async get(path, headers) {
|
|
11346
|
+
async get(path, headers, signal) {
|
|
11312
11347
|
const request = await this.buildRequest(path);
|
|
11313
11348
|
const res = await this.fetch(request.url, {
|
|
11314
11349
|
method: 'GET',
|
|
11315
11350
|
headers: {
|
|
11316
11351
|
...headers,
|
|
11317
11352
|
...request.headers
|
|
11318
|
-
}
|
|
11353
|
+
},
|
|
11354
|
+
signal
|
|
11319
11355
|
});
|
|
11320
11356
|
if (res.status === 401) {
|
|
11321
11357
|
this.invalidateCredentials();
|
|
@@ -11368,9 +11404,11 @@ class AbstractRemote {
|
|
|
11368
11404
|
}
|
|
11369
11405
|
});
|
|
11370
11406
|
let didClose = false;
|
|
11371
|
-
let connectionEstablished = false;
|
|
11372
11407
|
let pendingEventsCount = syncQueueRequestSize;
|
|
11373
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;
|
|
11374
11412
|
const abortRequest = () => {
|
|
11375
11413
|
if (didClose) {
|
|
11376
11414
|
return;
|
|
@@ -11383,6 +11421,10 @@ class AbstractRemote {
|
|
|
11383
11421
|
if (rsocket) {
|
|
11384
11422
|
rsocket.close();
|
|
11385
11423
|
}
|
|
11424
|
+
if (rejectPendingQueue) {
|
|
11425
|
+
rejectPendingQueue(new Error('Connection aborted'));
|
|
11426
|
+
rejectPendingQueue = null;
|
|
11427
|
+
}
|
|
11386
11428
|
// Send a bogus event to the queue to ensure a pending listener gets woken up. We check for didClose and would
|
|
11387
11429
|
// return a doneEvent.
|
|
11388
11430
|
queue.notify(null);
|
|
@@ -11454,6 +11496,7 @@ class AbstractRemote {
|
|
|
11454
11496
|
// Helps to prevent double close scenarios
|
|
11455
11497
|
rsocket.onClose(() => (rsocket = null));
|
|
11456
11498
|
return await new Promise((resolve, reject) => {
|
|
11499
|
+
rejectPendingQueue = reject;
|
|
11457
11500
|
const queueAsIterator = {
|
|
11458
11501
|
next: async () => {
|
|
11459
11502
|
if (didClose)
|
|
@@ -11494,14 +11537,15 @@ class AbstractRemote {
|
|
|
11494
11537
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
11495
11538
|
abortRequest();
|
|
11496
11539
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
11497
|
-
if (
|
|
11540
|
+
if (rejectPendingQueue) {
|
|
11498
11541
|
reject(e);
|
|
11542
|
+
rejectPendingQueue = null;
|
|
11499
11543
|
}
|
|
11500
11544
|
},
|
|
11501
11545
|
onNext: (payload) => {
|
|
11502
11546
|
// The connection is active
|
|
11503
|
-
if (
|
|
11504
|
-
|
|
11547
|
+
if (rejectPendingQueue) {
|
|
11548
|
+
rejectPendingQueue = null;
|
|
11505
11549
|
resolve(queueAsIterator);
|
|
11506
11550
|
}
|
|
11507
11551
|
const { data } = payload;
|
|
@@ -11514,6 +11558,12 @@ class AbstractRemote {
|
|
|
11514
11558
|
requestMore();
|
|
11515
11559
|
},
|
|
11516
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
|
+
}
|
|
11517
11567
|
abortRequest(); // this will also emit a done event
|
|
11518
11568
|
},
|
|
11519
11569
|
onExtension: () => { }
|
|
@@ -11536,7 +11586,7 @@ class AbstractRemote {
|
|
|
11536
11586
|
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
11537
11587
|
*/
|
|
11538
11588
|
async fetchStreamRaw(options) {
|
|
11539
|
-
const { data, path, headers, abortSignal } = options;
|
|
11589
|
+
const { data, path, headers, abortSignal, connectTimeoutMs } = options;
|
|
11540
11590
|
const request = await this.buildRequest(path);
|
|
11541
11591
|
/**
|
|
11542
11592
|
* This abort controller will abort pending fetch requests.
|
|
@@ -11571,7 +11621,7 @@ class AbstractRemote {
|
|
|
11571
11621
|
try {
|
|
11572
11622
|
const ndJson = 'application/x-ndjson';
|
|
11573
11623
|
const bson = 'application/vnd.powersync.bson-stream';
|
|
11574
|
-
|
|
11624
|
+
const postStream = (signal) => this.fetch(request.url, {
|
|
11575
11625
|
method: 'POST',
|
|
11576
11626
|
headers: {
|
|
11577
11627
|
...headers,
|
|
@@ -11579,11 +11629,15 @@ class AbstractRemote {
|
|
|
11579
11629
|
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
11580
11630
|
},
|
|
11581
11631
|
body: JSON.stringify(data),
|
|
11582
|
-
signal
|
|
11632
|
+
signal,
|
|
11583
11633
|
cache: 'no-store',
|
|
11584
11634
|
...(this.options.fetchOptions ?? {}),
|
|
11585
11635
|
...options.fetchOptions
|
|
11586
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));
|
|
11587
11641
|
if (!res.ok || !res.body) {
|
|
11588
11642
|
const text = await res.text();
|
|
11589
11643
|
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
@@ -11742,7 +11796,8 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
|
11742
11796
|
fetchStrategy: FetchStrategy.Buffered,
|
|
11743
11797
|
params: {},
|
|
11744
11798
|
serializedSchema: undefined,
|
|
11745
|
-
includeDefaultStreams: true
|
|
11799
|
+
includeDefaultStreams: true,
|
|
11800
|
+
connectTimeoutMs: 30_000
|
|
11746
11801
|
};
|
|
11747
11802
|
/**
|
|
11748
11803
|
* @internal
|
|
@@ -11756,6 +11811,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
11756
11811
|
activeStreams;
|
|
11757
11812
|
connectionMayHaveChanged = false;
|
|
11758
11813
|
crudUploadNotifier = asyncNotifier();
|
|
11814
|
+
connectTimeoutMs = DEFAULT_STREAM_CONNECTION_OPTIONS.connectTimeoutMs;
|
|
11759
11815
|
notifyCompletedUploads;
|
|
11760
11816
|
handleActiveStreamsChange;
|
|
11761
11817
|
syncStatus;
|
|
@@ -11825,10 +11881,10 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
11825
11881
|
this.crudUpdateListener?.();
|
|
11826
11882
|
this.crudUpdateListener = undefined;
|
|
11827
11883
|
}
|
|
11828
|
-
async getWriteCheckpoint() {
|
|
11884
|
+
async getWriteCheckpoint(abort) {
|
|
11829
11885
|
const clientId = await this.options.adapter.getClientId();
|
|
11830
11886
|
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
11831
|
-
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));
|
|
11832
11888
|
const checkpoint = response['data']['write_checkpoint'];
|
|
11833
11889
|
this.logger.debug(`Created write checkpoint: ${checkpoint}`);
|
|
11834
11890
|
return checkpoint;
|
|
@@ -11846,6 +11902,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
11846
11902
|
async _uploadAllCrud(signal) {
|
|
11847
11903
|
return this.obtainLock({
|
|
11848
11904
|
type: LockType.CRUD,
|
|
11905
|
+
signal,
|
|
11849
11906
|
callback: async () => {
|
|
11850
11907
|
/**
|
|
11851
11908
|
* Keep track of the first item in the CRUD queue for the last `uploadCrud` iteration.
|
|
@@ -11880,7 +11937,7 @@ The next upload iteration will be delayed.`);
|
|
|
11880
11937
|
}
|
|
11881
11938
|
else {
|
|
11882
11939
|
// Uploading is completed
|
|
11883
|
-
const neededUpdate = await this.options.adapter.updateLocalTarget(() => this.getWriteCheckpoint());
|
|
11940
|
+
const neededUpdate = await this.options.adapter.updateLocalTarget(() => this.getWriteCheckpoint(signal));
|
|
11884
11941
|
if (neededUpdate) {
|
|
11885
11942
|
this.notifyCompletedUploads?.();
|
|
11886
11943
|
}
|
|
@@ -11923,6 +11980,7 @@ The next upload iteration will be delayed.`);
|
|
|
11923
11980
|
}
|
|
11924
11981
|
const controller = new AbortController();
|
|
11925
11982
|
this.abortController = controller;
|
|
11983
|
+
this.connectTimeoutMs = options?.connectTimeoutMs ?? DEFAULT_STREAM_CONNECTION_OPTIONS.connectTimeoutMs;
|
|
11926
11984
|
this.streamingSyncPromise = Promise.all([
|
|
11927
11985
|
this.crudUploadLoop(controller.signal).catch((ex) => this.logger.error('Error in crud upload loop', ex)),
|
|
11928
11986
|
this.streamingSync(controller.signal, options)
|
|
@@ -12252,7 +12310,8 @@ The next upload iteration will be delayed.`);
|
|
|
12252
12310
|
const syncOptions = {
|
|
12253
12311
|
path: '/sync/stream',
|
|
12254
12312
|
abortSignal: signal,
|
|
12255
|
-
data: startInstruction.EstablishSyncStream.request
|
|
12313
|
+
data: startInstruction.EstablishSyncStream.request,
|
|
12314
|
+
connectTimeoutMs: resolvedOptions.connectTimeoutMs
|
|
12256
12315
|
};
|
|
12257
12316
|
controlInvocations = injectable(syncImplementation.receiveSyncLines({
|
|
12258
12317
|
options: syncOptions,
|
|
@@ -13470,7 +13529,7 @@ SELECT * FROM crud_entries;
|
|
|
13470
13529
|
}
|
|
13471
13530
|
/**
|
|
13472
13531
|
* Open a read-only transaction.
|
|
13473
|
-
*
|
|
13532
|
+
* When multiple connections are available, read transactions can run concurrently to a write transaction.
|
|
13474
13533
|
* Changes from any write transaction are not visible to read transactions started before it.
|
|
13475
13534
|
*
|
|
13476
13535
|
* @param callback - Function to execute within the transaction
|