@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.node.mjs
CHANGED
|
@@ -2255,15 +2255,6 @@ class ControlledExecutor {
|
|
|
2255
2255
|
}
|
|
2256
2256
|
}
|
|
2257
2257
|
|
|
2258
|
-
/**
|
|
2259
|
-
* A ponyfill for `Symbol.asyncIterator` that is compatible with the
|
|
2260
|
-
* [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)
|
|
2261
|
-
* we recommend for React Native.
|
|
2262
|
-
*
|
|
2263
|
-
* As long as we use this symbol (instead of `for await` and `async *`) in this package, we can be compatible with async
|
|
2264
|
-
* iterators without requiring them.
|
|
2265
|
-
*/
|
|
2266
|
-
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
2267
2258
|
/**
|
|
2268
2259
|
* Throttle a function to be called at most once every "wait" milliseconds,
|
|
2269
2260
|
* on the trailing edge.
|
|
@@ -8229,177 +8220,10 @@ function requireDist () {
|
|
|
8229
8220
|
|
|
8230
8221
|
var distExports = requireDist();
|
|
8231
8222
|
|
|
8232
|
-
var version = "1.
|
|
8223
|
+
var version = "1.52.0";
|
|
8233
8224
|
var PACKAGE = {
|
|
8234
8225
|
version: version};
|
|
8235
8226
|
|
|
8236
|
-
const DEFAULT_PRESSURE_LIMITS = {
|
|
8237
|
-
highWater: 10,
|
|
8238
|
-
lowWater: 0
|
|
8239
|
-
};
|
|
8240
|
-
/**
|
|
8241
|
-
* A very basic implementation of a data stream with backpressure support which does not use
|
|
8242
|
-
* native JS streams or async iterators.
|
|
8243
|
-
* This is handy for environments such as React Native which need polyfills for the above.
|
|
8244
|
-
*/
|
|
8245
|
-
class DataStream extends BaseObserver {
|
|
8246
|
-
options;
|
|
8247
|
-
dataQueue;
|
|
8248
|
-
isClosed;
|
|
8249
|
-
processingPromise;
|
|
8250
|
-
notifyDataAdded;
|
|
8251
|
-
logger;
|
|
8252
|
-
mapLine;
|
|
8253
|
-
constructor(options) {
|
|
8254
|
-
super();
|
|
8255
|
-
this.options = options;
|
|
8256
|
-
this.processingPromise = null;
|
|
8257
|
-
this.isClosed = false;
|
|
8258
|
-
this.dataQueue = [];
|
|
8259
|
-
this.mapLine = options?.mapLine ?? ((line) => line);
|
|
8260
|
-
this.logger = options?.logger ?? Logger.get('DataStream');
|
|
8261
|
-
if (options?.closeOnError) {
|
|
8262
|
-
const l = this.registerListener({
|
|
8263
|
-
error: (ex) => {
|
|
8264
|
-
l?.();
|
|
8265
|
-
this.close();
|
|
8266
|
-
}
|
|
8267
|
-
});
|
|
8268
|
-
}
|
|
8269
|
-
}
|
|
8270
|
-
get highWatermark() {
|
|
8271
|
-
return this.options?.pressure?.highWaterMark ?? DEFAULT_PRESSURE_LIMITS.highWater;
|
|
8272
|
-
}
|
|
8273
|
-
get lowWatermark() {
|
|
8274
|
-
return this.options?.pressure?.lowWaterMark ?? DEFAULT_PRESSURE_LIMITS.lowWater;
|
|
8275
|
-
}
|
|
8276
|
-
get closed() {
|
|
8277
|
-
return this.isClosed;
|
|
8278
|
-
}
|
|
8279
|
-
async close() {
|
|
8280
|
-
this.isClosed = true;
|
|
8281
|
-
await this.processingPromise;
|
|
8282
|
-
this.iterateListeners((l) => l.closed?.());
|
|
8283
|
-
// Discard any data in the queue
|
|
8284
|
-
this.dataQueue = [];
|
|
8285
|
-
this.listeners.clear();
|
|
8286
|
-
}
|
|
8287
|
-
/**
|
|
8288
|
-
* Enqueues data for the consumers to read
|
|
8289
|
-
*/
|
|
8290
|
-
enqueueData(data) {
|
|
8291
|
-
if (this.isClosed) {
|
|
8292
|
-
throw new Error('Cannot enqueue data into closed stream.');
|
|
8293
|
-
}
|
|
8294
|
-
this.dataQueue.push(data);
|
|
8295
|
-
this.notifyDataAdded?.();
|
|
8296
|
-
this.processQueue();
|
|
8297
|
-
}
|
|
8298
|
-
/**
|
|
8299
|
-
* Reads data once from the data stream
|
|
8300
|
-
* @returns a Data payload or Null if the stream closed.
|
|
8301
|
-
*/
|
|
8302
|
-
async read() {
|
|
8303
|
-
if (this.closed) {
|
|
8304
|
-
return null;
|
|
8305
|
-
}
|
|
8306
|
-
// Wait for any pending processing to complete first.
|
|
8307
|
-
// This ensures we register our listener before calling processQueue(),
|
|
8308
|
-
// avoiding a race where processQueue() sees no reader and returns early.
|
|
8309
|
-
if (this.processingPromise) {
|
|
8310
|
-
await this.processingPromise;
|
|
8311
|
-
}
|
|
8312
|
-
// Re-check after await - stream may have closed while we were waiting
|
|
8313
|
-
if (this.closed) {
|
|
8314
|
-
return null;
|
|
8315
|
-
}
|
|
8316
|
-
return new Promise((resolve, reject) => {
|
|
8317
|
-
const l = this.registerListener({
|
|
8318
|
-
data: async (data) => {
|
|
8319
|
-
resolve(data);
|
|
8320
|
-
// Remove the listener
|
|
8321
|
-
l?.();
|
|
8322
|
-
},
|
|
8323
|
-
closed: () => {
|
|
8324
|
-
resolve(null);
|
|
8325
|
-
l?.();
|
|
8326
|
-
},
|
|
8327
|
-
error: (ex) => {
|
|
8328
|
-
reject(ex);
|
|
8329
|
-
l?.();
|
|
8330
|
-
}
|
|
8331
|
-
});
|
|
8332
|
-
this.processQueue();
|
|
8333
|
-
});
|
|
8334
|
-
}
|
|
8335
|
-
/**
|
|
8336
|
-
* Executes a callback for each data item in the stream
|
|
8337
|
-
*/
|
|
8338
|
-
forEach(callback) {
|
|
8339
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
8340
|
-
this.iterateAsyncErrored(async (l) => l.lowWater?.());
|
|
8341
|
-
}
|
|
8342
|
-
return this.registerListener({
|
|
8343
|
-
data: callback
|
|
8344
|
-
});
|
|
8345
|
-
}
|
|
8346
|
-
processQueue() {
|
|
8347
|
-
if (this.processingPromise) {
|
|
8348
|
-
return;
|
|
8349
|
-
}
|
|
8350
|
-
const promise = (this.processingPromise = this._processQueue());
|
|
8351
|
-
promise.finally(() => {
|
|
8352
|
-
this.processingPromise = null;
|
|
8353
|
-
});
|
|
8354
|
-
return promise;
|
|
8355
|
-
}
|
|
8356
|
-
hasDataReader() {
|
|
8357
|
-
return Array.from(this.listeners.values()).some((l) => !!l.data);
|
|
8358
|
-
}
|
|
8359
|
-
async _processQueue() {
|
|
8360
|
-
/**
|
|
8361
|
-
* Allow listeners to mutate the queue before processing.
|
|
8362
|
-
* This allows for operations such as dropping or compressing data
|
|
8363
|
-
* on high water or requesting more data on low water.
|
|
8364
|
-
*/
|
|
8365
|
-
if (this.dataQueue.length >= this.highWatermark) {
|
|
8366
|
-
await this.iterateAsyncErrored(async (l) => l.highWater?.());
|
|
8367
|
-
}
|
|
8368
|
-
if (this.isClosed || !this.hasDataReader()) {
|
|
8369
|
-
return;
|
|
8370
|
-
}
|
|
8371
|
-
if (this.dataQueue.length) {
|
|
8372
|
-
const data = this.dataQueue.shift();
|
|
8373
|
-
const mapped = this.mapLine(data);
|
|
8374
|
-
await this.iterateAsyncErrored(async (l) => l.data?.(mapped));
|
|
8375
|
-
}
|
|
8376
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
8377
|
-
const dataAdded = new Promise((resolve) => {
|
|
8378
|
-
this.notifyDataAdded = resolve;
|
|
8379
|
-
});
|
|
8380
|
-
await Promise.race([this.iterateAsyncErrored(async (l) => l.lowWater?.()), dataAdded]);
|
|
8381
|
-
this.notifyDataAdded = null;
|
|
8382
|
-
}
|
|
8383
|
-
if (this.dataQueue.length > 0) {
|
|
8384
|
-
setTimeout(() => this.processQueue());
|
|
8385
|
-
}
|
|
8386
|
-
}
|
|
8387
|
-
async iterateAsyncErrored(cb) {
|
|
8388
|
-
// Important: We need to copy the listeners, as calling a listener could result in adding another
|
|
8389
|
-
// listener, resulting in infinite loops.
|
|
8390
|
-
const listeners = Array.from(this.listeners.values());
|
|
8391
|
-
for (let i of listeners) {
|
|
8392
|
-
try {
|
|
8393
|
-
await cb(i);
|
|
8394
|
-
}
|
|
8395
|
-
catch (ex) {
|
|
8396
|
-
this.logger.error(ex);
|
|
8397
|
-
this.iterateListeners((l) => l.error?.(ex));
|
|
8398
|
-
}
|
|
8399
|
-
}
|
|
8400
|
-
}
|
|
8401
|
-
}
|
|
8402
|
-
|
|
8403
8227
|
var WebsocketDuplexConnection = {};
|
|
8404
8228
|
|
|
8405
8229
|
var hasRequiredWebsocketDuplexConnection;
|
|
@@ -8562,8 +8386,215 @@ class WebsocketClientTransport {
|
|
|
8562
8386
|
}
|
|
8563
8387
|
}
|
|
8564
8388
|
|
|
8389
|
+
const doneResult = { done: true, value: undefined };
|
|
8390
|
+
function valueResult(value) {
|
|
8391
|
+
return { done: false, value };
|
|
8392
|
+
}
|
|
8393
|
+
/**
|
|
8394
|
+
* A variant of {@link Array.map} for async iterators.
|
|
8395
|
+
*/
|
|
8396
|
+
function map(source, map) {
|
|
8397
|
+
return {
|
|
8398
|
+
next: async () => {
|
|
8399
|
+
const value = await source.next();
|
|
8400
|
+
if (value.done) {
|
|
8401
|
+
return value;
|
|
8402
|
+
}
|
|
8403
|
+
else {
|
|
8404
|
+
return { value: map(value.value) };
|
|
8405
|
+
}
|
|
8406
|
+
}
|
|
8407
|
+
};
|
|
8408
|
+
}
|
|
8409
|
+
/**
|
|
8410
|
+
* Expands a source async iterator by allowing to inject events asynchronously.
|
|
8411
|
+
*
|
|
8412
|
+
* The resulting iterator will emit all events from its source. Additionally though, events can be injected. These
|
|
8413
|
+
* events are dropped once the main iterator completes, but are otherwise forwarded.
|
|
8414
|
+
*
|
|
8415
|
+
* The iterator completes when its source completes, and it supports backpressure by only calling `next()` on the source
|
|
8416
|
+
* in response to a `next()` call from downstream if no pending injected events can be dispatched.
|
|
8417
|
+
*/
|
|
8418
|
+
function injectable(source) {
|
|
8419
|
+
let sourceIsDone = false;
|
|
8420
|
+
let waiter = undefined; // An active, waiting next() call.
|
|
8421
|
+
// A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
|
|
8422
|
+
let pendingSourceEvent = null;
|
|
8423
|
+
let pendingInjectedEvents = [];
|
|
8424
|
+
const consumeWaiter = () => {
|
|
8425
|
+
const pending = waiter;
|
|
8426
|
+
waiter = undefined;
|
|
8427
|
+
return pending;
|
|
8428
|
+
};
|
|
8429
|
+
const fetchFromSource = () => {
|
|
8430
|
+
const resolveWaiter = (propagate) => {
|
|
8431
|
+
const active = consumeWaiter();
|
|
8432
|
+
if (active) {
|
|
8433
|
+
propagate(active);
|
|
8434
|
+
}
|
|
8435
|
+
else {
|
|
8436
|
+
pendingSourceEvent = propagate;
|
|
8437
|
+
}
|
|
8438
|
+
};
|
|
8439
|
+
const nextFromSource = source.next();
|
|
8440
|
+
nextFromSource.then((value) => {
|
|
8441
|
+
sourceIsDone = value.done == true;
|
|
8442
|
+
resolveWaiter((w) => w.resolve(value));
|
|
8443
|
+
}, (error) => {
|
|
8444
|
+
resolveWaiter((w) => w.reject(error));
|
|
8445
|
+
});
|
|
8446
|
+
};
|
|
8447
|
+
return {
|
|
8448
|
+
next: () => {
|
|
8449
|
+
return new Promise((resolve, reject) => {
|
|
8450
|
+
// First priority: Dispatch ready upstream events.
|
|
8451
|
+
if (sourceIsDone) {
|
|
8452
|
+
return resolve(doneResult);
|
|
8453
|
+
}
|
|
8454
|
+
if (pendingSourceEvent) {
|
|
8455
|
+
pendingSourceEvent({ resolve, reject });
|
|
8456
|
+
pendingSourceEvent = null;
|
|
8457
|
+
return;
|
|
8458
|
+
}
|
|
8459
|
+
// Second priority: Dispatch injected events
|
|
8460
|
+
if (pendingInjectedEvents.length) {
|
|
8461
|
+
return resolve(valueResult(pendingInjectedEvents.shift()));
|
|
8462
|
+
}
|
|
8463
|
+
// Nothing pending? Fetch from source
|
|
8464
|
+
waiter = { resolve, reject };
|
|
8465
|
+
return fetchFromSource();
|
|
8466
|
+
});
|
|
8467
|
+
},
|
|
8468
|
+
inject: (event) => {
|
|
8469
|
+
const pending = consumeWaiter();
|
|
8470
|
+
if (pending != null) {
|
|
8471
|
+
pending.resolve(valueResult(event));
|
|
8472
|
+
}
|
|
8473
|
+
else {
|
|
8474
|
+
pendingInjectedEvents.push(event);
|
|
8475
|
+
}
|
|
8476
|
+
}
|
|
8477
|
+
};
|
|
8478
|
+
}
|
|
8479
|
+
/**
|
|
8480
|
+
* Splits a byte stream at line endings, emitting each line as a string.
|
|
8481
|
+
*/
|
|
8482
|
+
function extractJsonLines(source, decoder) {
|
|
8483
|
+
let buffer = '';
|
|
8484
|
+
const pendingLines = [];
|
|
8485
|
+
let isFinalEvent = false;
|
|
8486
|
+
return {
|
|
8487
|
+
next: async () => {
|
|
8488
|
+
while (true) {
|
|
8489
|
+
if (isFinalEvent) {
|
|
8490
|
+
return doneResult;
|
|
8491
|
+
}
|
|
8492
|
+
{
|
|
8493
|
+
const first = pendingLines.shift();
|
|
8494
|
+
if (first) {
|
|
8495
|
+
return { done: false, value: first };
|
|
8496
|
+
}
|
|
8497
|
+
}
|
|
8498
|
+
const { done, value } = await source.next();
|
|
8499
|
+
if (done) {
|
|
8500
|
+
const remaining = buffer.trim();
|
|
8501
|
+
if (remaining.length != 0) {
|
|
8502
|
+
isFinalEvent = true;
|
|
8503
|
+
return { done: false, value: remaining };
|
|
8504
|
+
}
|
|
8505
|
+
return doneResult;
|
|
8506
|
+
}
|
|
8507
|
+
const data = decoder.decode(value, { stream: true });
|
|
8508
|
+
buffer += data;
|
|
8509
|
+
const lines = buffer.split('\n');
|
|
8510
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
8511
|
+
const l = lines[i].trim();
|
|
8512
|
+
if (l.length > 0) {
|
|
8513
|
+
pendingLines.push(l);
|
|
8514
|
+
}
|
|
8515
|
+
}
|
|
8516
|
+
buffer = lines[lines.length - 1];
|
|
8517
|
+
}
|
|
8518
|
+
}
|
|
8519
|
+
};
|
|
8520
|
+
}
|
|
8521
|
+
/**
|
|
8522
|
+
* Splits a concatenated stream of BSON objects by emitting individual objects.
|
|
8523
|
+
*/
|
|
8524
|
+
function extractBsonObjects(source) {
|
|
8525
|
+
// Fully read but not emitted yet.
|
|
8526
|
+
const completedObjects = [];
|
|
8527
|
+
// Whether source has returned { done: true }. We do the same once completed objects have been emitted.
|
|
8528
|
+
let isDone = false;
|
|
8529
|
+
const lengthBuffer = new DataView(new ArrayBuffer(4));
|
|
8530
|
+
let objectBody = null;
|
|
8531
|
+
// If we're parsing the length field, a number between 1 and 4 (inclusive) describing remaining bytes in the header.
|
|
8532
|
+
// If we're consuming a document, the bytes remaining.
|
|
8533
|
+
let remainingLength = 4;
|
|
8534
|
+
return {
|
|
8535
|
+
async next() {
|
|
8536
|
+
while (true) {
|
|
8537
|
+
// Before fetching new data from upstream, return completed objects.
|
|
8538
|
+
if (completedObjects.length) {
|
|
8539
|
+
return valueResult(completedObjects.shift());
|
|
8540
|
+
}
|
|
8541
|
+
if (isDone) {
|
|
8542
|
+
return doneResult;
|
|
8543
|
+
}
|
|
8544
|
+
const upstreamEvent = await source.next();
|
|
8545
|
+
if (upstreamEvent.done) {
|
|
8546
|
+
isDone = true;
|
|
8547
|
+
if (objectBody || remainingLength != 4) {
|
|
8548
|
+
throw new Error('illegal end of stream in BSON object');
|
|
8549
|
+
}
|
|
8550
|
+
return doneResult;
|
|
8551
|
+
}
|
|
8552
|
+
const chunk = upstreamEvent.value;
|
|
8553
|
+
for (let i = 0; i < chunk.length;) {
|
|
8554
|
+
const availableInData = chunk.length - i;
|
|
8555
|
+
if (objectBody) {
|
|
8556
|
+
// We're in the middle of reading a BSON document.
|
|
8557
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
8558
|
+
const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
|
|
8559
|
+
objectBody.set(copySource, objectBody.length - remainingLength);
|
|
8560
|
+
i += bytesToRead;
|
|
8561
|
+
remainingLength -= bytesToRead;
|
|
8562
|
+
if (remainingLength == 0) {
|
|
8563
|
+
completedObjects.push(objectBody);
|
|
8564
|
+
// Prepare to read another document, starting with its length
|
|
8565
|
+
objectBody = null;
|
|
8566
|
+
remainingLength = 4;
|
|
8567
|
+
}
|
|
8568
|
+
}
|
|
8569
|
+
else {
|
|
8570
|
+
// Copy up to 4 bytes into lengthBuffer, depending on how many we still need.
|
|
8571
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
8572
|
+
for (let j = 0; j < bytesToRead; j++) {
|
|
8573
|
+
lengthBuffer.setUint8(4 - remainingLength + j, chunk[i + j]);
|
|
8574
|
+
}
|
|
8575
|
+
i += bytesToRead;
|
|
8576
|
+
remainingLength -= bytesToRead;
|
|
8577
|
+
if (remainingLength == 0) {
|
|
8578
|
+
// Transition from reading length header to reading document. Subtracting 4 because the length of the
|
|
8579
|
+
// header is included in length.
|
|
8580
|
+
const length = lengthBuffer.getInt32(0, true /* little endian */);
|
|
8581
|
+
remainingLength = length - 4;
|
|
8582
|
+
if (remainingLength < 1) {
|
|
8583
|
+
throw new Error(`invalid length for bson: ${length}`);
|
|
8584
|
+
}
|
|
8585
|
+
objectBody = new Uint8Array(length);
|
|
8586
|
+
new DataView(objectBody.buffer).setInt32(0, length, true);
|
|
8587
|
+
}
|
|
8588
|
+
}
|
|
8589
|
+
}
|
|
8590
|
+
}
|
|
8591
|
+
}
|
|
8592
|
+
};
|
|
8593
|
+
}
|
|
8594
|
+
|
|
8565
8595
|
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
|
|
8566
8596
|
const POWERSYNC_JS_VERSION = PACKAGE.version;
|
|
8597
|
+
const SYNC_QUEUE_REQUEST_HIGH_WATER = 10;
|
|
8567
8598
|
const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
|
|
8568
8599
|
// Keep alive message is sent every period
|
|
8569
8600
|
const KEEP_ALIVE_MS = 20_000;
|
|
@@ -8743,13 +8774,14 @@ class AbstractRemote {
|
|
|
8743
8774
|
return new WebSocket(url);
|
|
8744
8775
|
}
|
|
8745
8776
|
/**
|
|
8746
|
-
* Returns a data stream of sync line data.
|
|
8777
|
+
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
8778
|
+
*
|
|
8779
|
+
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
8747
8780
|
*
|
|
8748
|
-
* @param map Maps received payload frames to the typed event value.
|
|
8749
8781
|
* @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
|
|
8750
8782
|
* (required for compatibility with older sync services).
|
|
8751
8783
|
*/
|
|
8752
|
-
async socketStreamRaw(options,
|
|
8784
|
+
async socketStreamRaw(options, bson) {
|
|
8753
8785
|
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
|
|
8754
8786
|
const mimeType = bson == null ? 'application/json' : 'application/bson';
|
|
8755
8787
|
function toBuffer(js) {
|
|
@@ -8764,52 +8796,55 @@ class AbstractRemote {
|
|
|
8764
8796
|
}
|
|
8765
8797
|
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
|
|
8766
8798
|
const request = await this.buildRequest(path);
|
|
8799
|
+
const url = this.options.socketUrlTransformer(request.url);
|
|
8767
8800
|
// Add the user agent in the setup payload - we can't set custom
|
|
8768
8801
|
// headers with websockets on web. The browser userAgent is however added
|
|
8769
8802
|
// automatically as a header.
|
|
8770
8803
|
const userAgent = this.getUserAgent();
|
|
8771
|
-
|
|
8772
|
-
|
|
8773
|
-
|
|
8774
|
-
|
|
8775
|
-
|
|
8776
|
-
|
|
8777
|
-
|
|
8804
|
+
// While we're connecting (a process that can't be aborted in RSocket), the WebSocket instance to close if we wanted
|
|
8805
|
+
// to abort the connection.
|
|
8806
|
+
let pendingSocket = null;
|
|
8807
|
+
let keepAliveTimeout;
|
|
8808
|
+
let rsocket = null;
|
|
8809
|
+
let queue = null;
|
|
8810
|
+
let didClose = false;
|
|
8811
|
+
const abortRequest = () => {
|
|
8812
|
+
if (didClose) {
|
|
8813
|
+
return;
|
|
8814
|
+
}
|
|
8815
|
+
didClose = true;
|
|
8816
|
+
clearTimeout(keepAliveTimeout);
|
|
8817
|
+
if (pendingSocket) {
|
|
8818
|
+
pendingSocket.close();
|
|
8819
|
+
}
|
|
8820
|
+
if (rsocket) {
|
|
8821
|
+
rsocket.close();
|
|
8822
|
+
}
|
|
8823
|
+
if (queue) {
|
|
8824
|
+
queue.stop();
|
|
8825
|
+
}
|
|
8826
|
+
};
|
|
8778
8827
|
// Handle upstream abort
|
|
8779
|
-
if (options.abortSignal
|
|
8828
|
+
if (options.abortSignal.aborted) {
|
|
8780
8829
|
throw new AbortOperation('Connection request aborted');
|
|
8781
8830
|
}
|
|
8782
8831
|
else {
|
|
8783
|
-
options.abortSignal
|
|
8784
|
-
stream.close();
|
|
8785
|
-
}, { once: true });
|
|
8832
|
+
options.abortSignal.addEventListener('abort', abortRequest);
|
|
8786
8833
|
}
|
|
8787
|
-
let keepAliveTimeout;
|
|
8788
8834
|
const resetTimeout = () => {
|
|
8789
8835
|
clearTimeout(keepAliveTimeout);
|
|
8790
8836
|
keepAliveTimeout = setTimeout(() => {
|
|
8791
8837
|
this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
|
|
8792
|
-
|
|
8838
|
+
abortRequest();
|
|
8793
8839
|
}, SOCKET_TIMEOUT_MS);
|
|
8794
8840
|
};
|
|
8795
8841
|
resetTimeout();
|
|
8796
|
-
// Typescript complains about this being `never` if it's not assigned here.
|
|
8797
|
-
// This is assigned in `wsCreator`.
|
|
8798
|
-
let disposeSocketConnectionTimeout = () => { };
|
|
8799
|
-
const url = this.options.socketUrlTransformer(request.url);
|
|
8800
8842
|
const connector = new distExports.RSocketConnector({
|
|
8801
8843
|
transport: new WebsocketClientTransport({
|
|
8802
8844
|
url,
|
|
8803
8845
|
wsCreator: (url) => {
|
|
8804
|
-
const socket = this.createSocket(url);
|
|
8805
|
-
|
|
8806
|
-
closed: () => {
|
|
8807
|
-
// Allow closing the underlying WebSocket if the stream was closed before the
|
|
8808
|
-
// RSocket connect completed. This should effectively abort the request.
|
|
8809
|
-
socket.close();
|
|
8810
|
-
}
|
|
8811
|
-
});
|
|
8812
|
-
socket.addEventListener('message', (event) => {
|
|
8846
|
+
const socket = (pendingSocket = this.createSocket(url));
|
|
8847
|
+
socket.addEventListener('message', () => {
|
|
8813
8848
|
resetTimeout();
|
|
8814
8849
|
});
|
|
8815
8850
|
return socket;
|
|
@@ -8829,43 +8864,40 @@ class AbstractRemote {
|
|
|
8829
8864
|
}
|
|
8830
8865
|
}
|
|
8831
8866
|
});
|
|
8832
|
-
let rsocket;
|
|
8833
8867
|
try {
|
|
8834
8868
|
rsocket = await connector.connect();
|
|
8835
8869
|
// The connection is established, we no longer need to monitor the initial timeout
|
|
8836
|
-
|
|
8870
|
+
pendingSocket = null;
|
|
8837
8871
|
}
|
|
8838
8872
|
catch (ex) {
|
|
8839
8873
|
this.logger.error(`Failed to connect WebSocket`, ex);
|
|
8840
|
-
|
|
8841
|
-
if (!stream.closed) {
|
|
8842
|
-
await stream.close();
|
|
8843
|
-
}
|
|
8874
|
+
abortRequest();
|
|
8844
8875
|
throw ex;
|
|
8845
8876
|
}
|
|
8846
8877
|
resetTimeout();
|
|
8847
|
-
let socketIsClosed = false;
|
|
8848
|
-
const closeSocket = () => {
|
|
8849
|
-
clearTimeout(keepAliveTimeout);
|
|
8850
|
-
if (socketIsClosed) {
|
|
8851
|
-
return;
|
|
8852
|
-
}
|
|
8853
|
-
socketIsClosed = true;
|
|
8854
|
-
rsocket.close();
|
|
8855
|
-
};
|
|
8856
8878
|
// Helps to prevent double close scenarios
|
|
8857
|
-
rsocket.onClose(() => (
|
|
8858
|
-
|
|
8859
|
-
let pendingEventsCount = syncQueueRequestSize;
|
|
8860
|
-
const disposeClosedListener = stream.registerListener({
|
|
8861
|
-
closed: () => {
|
|
8862
|
-
closeSocket();
|
|
8863
|
-
disposeClosedListener();
|
|
8864
|
-
}
|
|
8865
|
-
});
|
|
8866
|
-
const socket = await new Promise((resolve, reject) => {
|
|
8879
|
+
rsocket.onClose(() => (rsocket = null));
|
|
8880
|
+
return await new Promise((resolve, reject) => {
|
|
8867
8881
|
let connectionEstablished = false;
|
|
8868
|
-
|
|
8882
|
+
let pendingEventsCount = syncQueueRequestSize;
|
|
8883
|
+
let paused = false;
|
|
8884
|
+
let res = null;
|
|
8885
|
+
function requestMore() {
|
|
8886
|
+
const delta = syncQueueRequestSize - pendingEventsCount;
|
|
8887
|
+
if (!paused && delta > 0) {
|
|
8888
|
+
res?.request(delta);
|
|
8889
|
+
pendingEventsCount = syncQueueRequestSize;
|
|
8890
|
+
}
|
|
8891
|
+
}
|
|
8892
|
+
const events = new EventIterator((q) => {
|
|
8893
|
+
queue = q;
|
|
8894
|
+
q.on('highWater', () => (paused = true));
|
|
8895
|
+
q.on('lowWater', () => {
|
|
8896
|
+
paused = false;
|
|
8897
|
+
requestMore();
|
|
8898
|
+
});
|
|
8899
|
+
}, { highWaterMark: SYNC_QUEUE_REQUEST_HIGH_WATER, lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER })[Symbol.asyncIterator]();
|
|
8900
|
+
res = rsocket.requestStream({
|
|
8869
8901
|
data: toBuffer(options.data),
|
|
8870
8902
|
metadata: toBuffer({
|
|
8871
8903
|
path
|
|
@@ -8890,7 +8922,7 @@ class AbstractRemote {
|
|
|
8890
8922
|
}
|
|
8891
8923
|
// RSocket will close the RSocket stream automatically
|
|
8892
8924
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
8893
|
-
|
|
8925
|
+
abortRequest();
|
|
8894
8926
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
8895
8927
|
if (!connectionEstablished) {
|
|
8896
8928
|
reject(e);
|
|
@@ -8900,41 +8932,40 @@ class AbstractRemote {
|
|
|
8900
8932
|
// The connection is active
|
|
8901
8933
|
if (!connectionEstablished) {
|
|
8902
8934
|
connectionEstablished = true;
|
|
8903
|
-
resolve(
|
|
8935
|
+
resolve(events);
|
|
8904
8936
|
}
|
|
8905
8937
|
const { data } = payload;
|
|
8938
|
+
if (data) {
|
|
8939
|
+
queue.push(data);
|
|
8940
|
+
}
|
|
8906
8941
|
// Less events are now pending
|
|
8907
8942
|
pendingEventsCount--;
|
|
8908
|
-
|
|
8909
|
-
|
|
8910
|
-
}
|
|
8911
|
-
stream.enqueueData(data);
|
|
8943
|
+
// Request another event (unless the downstream consumer is paused).
|
|
8944
|
+
requestMore();
|
|
8912
8945
|
},
|
|
8913
8946
|
onComplete: () => {
|
|
8914
|
-
|
|
8947
|
+
abortRequest(); // this will also emit a done event
|
|
8915
8948
|
},
|
|
8916
8949
|
onExtension: () => { }
|
|
8917
8950
|
});
|
|
8918
8951
|
});
|
|
8919
|
-
const l = stream.registerListener({
|
|
8920
|
-
lowWater: async () => {
|
|
8921
|
-
// Request to fill up the queue
|
|
8922
|
-
const required = syncQueueRequestSize - pendingEventsCount;
|
|
8923
|
-
if (required > 0) {
|
|
8924
|
-
socket.request(syncQueueRequestSize - pendingEventsCount);
|
|
8925
|
-
pendingEventsCount = syncQueueRequestSize;
|
|
8926
|
-
}
|
|
8927
|
-
},
|
|
8928
|
-
closed: () => {
|
|
8929
|
-
l();
|
|
8930
|
-
}
|
|
8931
|
-
});
|
|
8932
|
-
return stream;
|
|
8933
8952
|
}
|
|
8934
8953
|
/**
|
|
8935
|
-
*
|
|
8954
|
+
* @returns Whether the HTTP implementation on this platform can receive streamed binary responses. This is true on
|
|
8955
|
+
* all platforms except React Native (who would have guessed...), where we must not request BSON responses.
|
|
8956
|
+
*
|
|
8957
|
+
* @see https://github.com/react-native-community/fetch?tab=readme-ov-file#motivation
|
|
8958
|
+
*/
|
|
8959
|
+
get supportsStreamingBinaryResponses() {
|
|
8960
|
+
return true;
|
|
8961
|
+
}
|
|
8962
|
+
/**
|
|
8963
|
+
* Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
|
|
8964
|
+
* async iterator of byte blobs.
|
|
8965
|
+
*
|
|
8966
|
+
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
8936
8967
|
*/
|
|
8937
|
-
async
|
|
8968
|
+
async fetchStreamRaw(options) {
|
|
8938
8969
|
const { data, path, headers, abortSignal } = options;
|
|
8939
8970
|
const request = await this.buildRequest(path);
|
|
8940
8971
|
/**
|
|
@@ -8946,119 +8977,94 @@ class AbstractRemote {
|
|
|
8946
8977
|
* Aborting the active fetch request while it is being consumed seems to throw
|
|
8947
8978
|
* an unhandled exception on the window level.
|
|
8948
8979
|
*/
|
|
8949
|
-
if (abortSignal
|
|
8950
|
-
throw new AbortOperation('Abort request received before making
|
|
8980
|
+
if (abortSignal.aborted) {
|
|
8981
|
+
throw new AbortOperation('Abort request received before making fetchStreamRaw request');
|
|
8951
8982
|
}
|
|
8952
8983
|
const controller = new AbortController();
|
|
8953
|
-
let
|
|
8954
|
-
abortSignal
|
|
8955
|
-
|
|
8984
|
+
let reader = null;
|
|
8985
|
+
abortSignal.addEventListener('abort', () => {
|
|
8986
|
+
const reason = abortSignal.reason ??
|
|
8987
|
+
new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.');
|
|
8988
|
+
if (reader == null) {
|
|
8956
8989
|
// Only abort via the abort controller if the request has not resolved yet
|
|
8957
|
-
controller.abort(
|
|
8958
|
-
|
|
8990
|
+
controller.abort(reason);
|
|
8991
|
+
}
|
|
8992
|
+
else {
|
|
8993
|
+
reader.cancel(reason).catch(() => {
|
|
8994
|
+
// Cancelling the reader might rethrow an exception we would have handled by throwing in next(). So we can
|
|
8995
|
+
// ignore it here.
|
|
8996
|
+
});
|
|
8959
8997
|
}
|
|
8960
8998
|
});
|
|
8961
|
-
|
|
8962
|
-
|
|
8963
|
-
|
|
8964
|
-
|
|
8965
|
-
|
|
8966
|
-
|
|
8967
|
-
|
|
8968
|
-
|
|
8969
|
-
|
|
8999
|
+
let res;
|
|
9000
|
+
let responseIsBson = false;
|
|
9001
|
+
try {
|
|
9002
|
+
const ndJson = 'application/x-ndjson';
|
|
9003
|
+
const bson = 'application/vnd.powersync.bson-stream';
|
|
9004
|
+
res = await this.fetch(request.url, {
|
|
9005
|
+
method: 'POST',
|
|
9006
|
+
headers: {
|
|
9007
|
+
...headers,
|
|
9008
|
+
...request.headers,
|
|
9009
|
+
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
9010
|
+
},
|
|
9011
|
+
body: JSON.stringify(data),
|
|
9012
|
+
signal: controller.signal,
|
|
9013
|
+
cache: 'no-store',
|
|
9014
|
+
...(this.options.fetchOptions ?? {}),
|
|
9015
|
+
...options.fetchOptions
|
|
9016
|
+
});
|
|
9017
|
+
if (!res.ok || !res.body) {
|
|
9018
|
+
const text = await res.text();
|
|
9019
|
+
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
9020
|
+
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
9021
|
+
error.status = res.status;
|
|
9022
|
+
throw error;
|
|
9023
|
+
}
|
|
9024
|
+
const contentType = res.headers.get('content-type');
|
|
9025
|
+
responseIsBson = contentType == bson;
|
|
9026
|
+
}
|
|
9027
|
+
catch (ex) {
|
|
8970
9028
|
if (ex.name == 'AbortError') {
|
|
8971
9029
|
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
|
|
8972
9030
|
}
|
|
8973
9031
|
throw ex;
|
|
8974
|
-
});
|
|
8975
|
-
if (!res) {
|
|
8976
|
-
throw new Error('Fetch request was aborted');
|
|
8977
|
-
}
|
|
8978
|
-
requestResolved = true;
|
|
8979
|
-
if (!res.ok || !res.body) {
|
|
8980
|
-
const text = await res.text();
|
|
8981
|
-
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
8982
|
-
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
8983
|
-
error.status = res.status;
|
|
8984
|
-
throw error;
|
|
8985
9032
|
}
|
|
8986
|
-
|
|
8987
|
-
|
|
8988
|
-
|
|
8989
|
-
|
|
8990
|
-
|
|
8991
|
-
const closeReader = async () => {
|
|
8992
|
-
try {
|
|
8993
|
-
readerReleased = true;
|
|
8994
|
-
await reader.cancel();
|
|
8995
|
-
}
|
|
8996
|
-
catch (ex) {
|
|
8997
|
-
// an error will throw if the reader hasn't been used yet
|
|
8998
|
-
}
|
|
8999
|
-
reader.releaseLock();
|
|
9000
|
-
};
|
|
9001
|
-
const stream = new DataStream({
|
|
9002
|
-
logger: this.logger,
|
|
9003
|
-
mapLine: mapLine,
|
|
9004
|
-
pressure: {
|
|
9005
|
-
highWaterMark: 20,
|
|
9006
|
-
lowWaterMark: 10
|
|
9007
|
-
}
|
|
9008
|
-
});
|
|
9009
|
-
abortSignal?.addEventListener('abort', () => {
|
|
9010
|
-
closeReader();
|
|
9011
|
-
stream.close();
|
|
9012
|
-
});
|
|
9013
|
-
const decoder = this.createTextDecoder();
|
|
9014
|
-
let buffer = '';
|
|
9015
|
-
const consumeStream = async () => {
|
|
9016
|
-
while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
|
|
9017
|
-
const { done, value } = await reader.read();
|
|
9018
|
-
if (done) {
|
|
9019
|
-
const remaining = buffer.trim();
|
|
9020
|
-
if (remaining.length != 0) {
|
|
9021
|
-
stream.enqueueData(remaining);
|
|
9022
|
-
}
|
|
9023
|
-
stream.close();
|
|
9024
|
-
await closeReader();
|
|
9025
|
-
return;
|
|
9033
|
+
reader = res.body.getReader();
|
|
9034
|
+
const stream = {
|
|
9035
|
+
next: async () => {
|
|
9036
|
+
if (controller.signal.aborted) {
|
|
9037
|
+
return doneResult;
|
|
9026
9038
|
}
|
|
9027
|
-
|
|
9028
|
-
|
|
9029
|
-
const lines = buffer.split('\n');
|
|
9030
|
-
for (var i = 0; i < lines.length - 1; i++) {
|
|
9031
|
-
var l = lines[i].trim();
|
|
9032
|
-
if (l.length > 0) {
|
|
9033
|
-
stream.enqueueData(l);
|
|
9034
|
-
}
|
|
9039
|
+
try {
|
|
9040
|
+
return await reader.read();
|
|
9035
9041
|
}
|
|
9036
|
-
|
|
9037
|
-
|
|
9038
|
-
|
|
9039
|
-
|
|
9040
|
-
|
|
9041
|
-
|
|
9042
|
-
|
|
9043
|
-
dispose();
|
|
9044
|
-
},
|
|
9045
|
-
closed: () => {
|
|
9046
|
-
resolve();
|
|
9047
|
-
dispose();
|
|
9048
|
-
}
|
|
9049
|
-
});
|
|
9050
|
-
});
|
|
9042
|
+
catch (ex) {
|
|
9043
|
+
if (controller.signal.aborted) {
|
|
9044
|
+
// .read() completes with an error if we cancel the reader, which we do to disconnect. So this is just
|
|
9045
|
+
// things working as intended, we can return a done event and consider the exception handled.
|
|
9046
|
+
return doneResult;
|
|
9047
|
+
}
|
|
9048
|
+
throw ex;
|
|
9051
9049
|
}
|
|
9052
9050
|
}
|
|
9053
9051
|
};
|
|
9054
|
-
|
|
9055
|
-
|
|
9056
|
-
|
|
9057
|
-
|
|
9058
|
-
|
|
9059
|
-
|
|
9060
|
-
|
|
9061
|
-
|
|
9052
|
+
return { isBson: responseIsBson, stream };
|
|
9053
|
+
}
|
|
9054
|
+
/**
|
|
9055
|
+
* Posts a `/sync/stream` request.
|
|
9056
|
+
*
|
|
9057
|
+
* Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
|
|
9058
|
+
* {@link Uint8Array}s.
|
|
9059
|
+
*/
|
|
9060
|
+
async fetchStream(options) {
|
|
9061
|
+
const { isBson, stream } = await this.fetchStreamRaw(options);
|
|
9062
|
+
if (isBson) {
|
|
9063
|
+
return extractBsonObjects(stream);
|
|
9064
|
+
}
|
|
9065
|
+
else {
|
|
9066
|
+
return extractJsonLines(stream, this.createTextDecoder());
|
|
9067
|
+
}
|
|
9062
9068
|
}
|
|
9063
9069
|
}
|
|
9064
9070
|
|
|
@@ -9566,6 +9572,19 @@ The next upload iteration will be delayed.`);
|
|
|
9566
9572
|
}
|
|
9567
9573
|
});
|
|
9568
9574
|
}
|
|
9575
|
+
async receiveSyncLines(data) {
|
|
9576
|
+
const { options, connection, bson } = data;
|
|
9577
|
+
const remote = this.options.remote;
|
|
9578
|
+
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
9579
|
+
return await remote.fetchStream(options);
|
|
9580
|
+
}
|
|
9581
|
+
else {
|
|
9582
|
+
return await this.options.remote.socketStreamRaw({
|
|
9583
|
+
...options,
|
|
9584
|
+
...{ fetchStrategy: connection.fetchStrategy }
|
|
9585
|
+
}, bson);
|
|
9586
|
+
}
|
|
9587
|
+
}
|
|
9569
9588
|
async legacyStreamingSyncIteration(signal, resolvedOptions) {
|
|
9570
9589
|
const rawTables = resolvedOptions.serializedSchema?.raw_tables;
|
|
9571
9590
|
if (rawTables != null && rawTables.length) {
|
|
@@ -9595,42 +9614,27 @@ The next upload iteration will be delayed.`);
|
|
|
9595
9614
|
client_id: clientId
|
|
9596
9615
|
}
|
|
9597
9616
|
};
|
|
9598
|
-
|
|
9599
|
-
|
|
9600
|
-
|
|
9601
|
-
|
|
9602
|
-
|
|
9603
|
-
|
|
9604
|
-
|
|
9605
|
-
|
|
9606
|
-
|
|
9607
|
-
|
|
9608
|
-
|
|
9609
|
-
|
|
9610
|
-
|
|
9611
|
-
|
|
9612
|
-
stream = await this.options.remote.socketStreamRaw({
|
|
9613
|
-
...syncOptions,
|
|
9614
|
-
...{ fetchStrategy: resolvedOptions.fetchStrategy }
|
|
9615
|
-
}, (payload) => {
|
|
9616
|
-
if (payload instanceof Uint8Array) {
|
|
9617
|
-
return bson.deserialize(payload);
|
|
9618
|
-
}
|
|
9619
|
-
else {
|
|
9620
|
-
// Directly enqueued by us
|
|
9621
|
-
return payload;
|
|
9622
|
-
}
|
|
9623
|
-
}, bson);
|
|
9624
|
-
}
|
|
9617
|
+
const bson = await this.options.remote.getBSON();
|
|
9618
|
+
const source = await this.receiveSyncLines({
|
|
9619
|
+
options: syncOptions,
|
|
9620
|
+
connection: resolvedOptions,
|
|
9621
|
+
bson
|
|
9622
|
+
});
|
|
9623
|
+
const stream = injectable(map(source, (line) => {
|
|
9624
|
+
if (typeof line == 'string') {
|
|
9625
|
+
return JSON.parse(line);
|
|
9626
|
+
}
|
|
9627
|
+
else {
|
|
9628
|
+
return bson.deserialize(line);
|
|
9629
|
+
}
|
|
9630
|
+
}));
|
|
9625
9631
|
this.logger.debug('Stream established. Processing events');
|
|
9626
9632
|
this.notifyCompletedUploads = () => {
|
|
9627
|
-
|
|
9628
|
-
stream.enqueueData({ crud_upload_completed: null });
|
|
9629
|
-
}
|
|
9633
|
+
stream.inject({ crud_upload_completed: null });
|
|
9630
9634
|
};
|
|
9631
|
-
while (
|
|
9632
|
-
const line = await stream.
|
|
9633
|
-
if (
|
|
9635
|
+
while (true) {
|
|
9636
|
+
const { value: line, done } = await stream.next();
|
|
9637
|
+
if (done) {
|
|
9634
9638
|
// The stream has closed while waiting
|
|
9635
9639
|
return;
|
|
9636
9640
|
}
|
|
@@ -9809,14 +9813,17 @@ The next upload iteration will be delayed.`);
|
|
|
9809
9813
|
const syncImplementation = this;
|
|
9810
9814
|
const adapter = this.options.adapter;
|
|
9811
9815
|
const remote = this.options.remote;
|
|
9816
|
+
const controller = new AbortController();
|
|
9817
|
+
const abort = () => {
|
|
9818
|
+
return controller.abort(signal.reason);
|
|
9819
|
+
};
|
|
9820
|
+
signal.addEventListener('abort', abort);
|
|
9812
9821
|
let receivingLines = null;
|
|
9813
9822
|
let hadSyncLine = false;
|
|
9814
9823
|
let hideDisconnectOnRestart = false;
|
|
9815
9824
|
if (signal.aborted) {
|
|
9816
9825
|
throw new AbortOperation('Connection request has been aborted');
|
|
9817
9826
|
}
|
|
9818
|
-
const abortController = new AbortController();
|
|
9819
|
-
signal.addEventListener('abort', () => abortController.abort());
|
|
9820
9827
|
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
|
|
9821
9828
|
// invocation (local events include refreshed tokens and completed uploads).
|
|
9822
9829
|
// This is a single data stream so that we can handle all control calls from a single place.
|
|
@@ -9824,49 +9831,36 @@ The next upload iteration will be delayed.`);
|
|
|
9824
9831
|
async function connect(instr) {
|
|
9825
9832
|
const syncOptions = {
|
|
9826
9833
|
path: '/sync/stream',
|
|
9827
|
-
abortSignal:
|
|
9834
|
+
abortSignal: controller.signal,
|
|
9828
9835
|
data: instr.request
|
|
9829
9836
|
};
|
|
9830
|
-
|
|
9831
|
-
|
|
9832
|
-
|
|
9833
|
-
|
|
9834
|
-
|
|
9835
|
-
|
|
9836
|
-
|
|
9837
|
-
|
|
9838
|
-
|
|
9839
|
-
|
|
9840
|
-
|
|
9841
|
-
|
|
9842
|
-
|
|
9843
|
-
|
|
9844
|
-
|
|
9845
|
-
|
|
9846
|
-
|
|
9847
|
-
fetchStrategy: resolvedOptions.fetchStrategy
|
|
9848
|
-
}, (payload) => {
|
|
9849
|
-
if (payload instanceof Uint8Array) {
|
|
9850
|
-
return {
|
|
9851
|
-
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
9852
|
-
payload: payload
|
|
9853
|
-
};
|
|
9854
|
-
}
|
|
9855
|
-
else {
|
|
9856
|
-
// Directly enqueued by us
|
|
9857
|
-
return payload;
|
|
9858
|
-
}
|
|
9859
|
-
});
|
|
9860
|
-
}
|
|
9837
|
+
controlInvocations = injectable(map(await syncImplementation.receiveSyncLines({
|
|
9838
|
+
options: syncOptions,
|
|
9839
|
+
connection: resolvedOptions
|
|
9840
|
+
}), (line) => {
|
|
9841
|
+
if (typeof line == 'string') {
|
|
9842
|
+
return {
|
|
9843
|
+
command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
|
|
9844
|
+
payload: line
|
|
9845
|
+
};
|
|
9846
|
+
}
|
|
9847
|
+
else {
|
|
9848
|
+
return {
|
|
9849
|
+
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
9850
|
+
payload: line
|
|
9851
|
+
};
|
|
9852
|
+
}
|
|
9853
|
+
}));
|
|
9861
9854
|
// The rust client will set connected: true after the first sync line because that's when it gets invoked, but
|
|
9862
9855
|
// we're already connected here and can report that.
|
|
9863
9856
|
syncImplementation.updateSyncStatus({ connected: true });
|
|
9864
9857
|
try {
|
|
9865
|
-
while (
|
|
9866
|
-
|
|
9867
|
-
if (
|
|
9868
|
-
|
|
9858
|
+
while (true) {
|
|
9859
|
+
let event = await controlInvocations.next();
|
|
9860
|
+
if (event.done) {
|
|
9861
|
+
break;
|
|
9869
9862
|
}
|
|
9863
|
+
const line = event.value;
|
|
9870
9864
|
await control(line.command, line.payload);
|
|
9871
9865
|
if (!hadSyncLine) {
|
|
9872
9866
|
syncImplementation.triggerCrudUpload();
|
|
@@ -9875,12 +9869,8 @@ The next upload iteration will be delayed.`);
|
|
|
9875
9869
|
}
|
|
9876
9870
|
}
|
|
9877
9871
|
finally {
|
|
9878
|
-
|
|
9879
|
-
|
|
9880
|
-
// refreshed. That would throw after closing (and we can't handle those events either way), so set this back
|
|
9881
|
-
// to null.
|
|
9882
|
-
controlInvocations = null;
|
|
9883
|
-
await activeInstructions.close();
|
|
9872
|
+
abort();
|
|
9873
|
+
signal.removeEventListener('abort', abort);
|
|
9884
9874
|
}
|
|
9885
9875
|
}
|
|
9886
9876
|
async function stop() {
|
|
@@ -9924,14 +9914,14 @@ The next upload iteration will be delayed.`);
|
|
|
9924
9914
|
remote.invalidateCredentials();
|
|
9925
9915
|
// Restart iteration after the credentials have been refreshed.
|
|
9926
9916
|
remote.fetchCredentials().then((_) => {
|
|
9927
|
-
controlInvocations?.
|
|
9917
|
+
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
|
|
9928
9918
|
}, (err) => {
|
|
9929
9919
|
syncImplementation.logger.warn('Could not prefetch credentials', err);
|
|
9930
9920
|
});
|
|
9931
9921
|
}
|
|
9932
9922
|
}
|
|
9933
9923
|
else if ('CloseSyncStream' in instruction) {
|
|
9934
|
-
|
|
9924
|
+
controller.abort();
|
|
9935
9925
|
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
9936
9926
|
}
|
|
9937
9927
|
else if ('FlushFileSystem' in instruction) ;
|
|
@@ -9960,17 +9950,13 @@ The next upload iteration will be delayed.`);
|
|
|
9960
9950
|
}
|
|
9961
9951
|
await control(PowerSyncControlCommand.START, JSON.stringify(options));
|
|
9962
9952
|
this.notifyCompletedUploads = () => {
|
|
9963
|
-
|
|
9964
|
-
controlInvocations.enqueueData({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
9965
|
-
}
|
|
9953
|
+
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
9966
9954
|
};
|
|
9967
9955
|
this.handleActiveStreamsChange = () => {
|
|
9968
|
-
|
|
9969
|
-
|
|
9970
|
-
|
|
9971
|
-
|
|
9972
|
-
});
|
|
9973
|
-
}
|
|
9956
|
+
controlInvocations?.inject({
|
|
9957
|
+
command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
|
|
9958
|
+
payload: JSON.stringify(this.activeStreams)
|
|
9959
|
+
});
|
|
9974
9960
|
};
|
|
9975
9961
|
await receivingLines;
|
|
9976
9962
|
}
|
|
@@ -10987,7 +10973,7 @@ class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
10987
10973
|
* @returns A transaction of CRUD operations to upload, or null if there are none
|
|
10988
10974
|
*/
|
|
10989
10975
|
async getNextCrudTransaction() {
|
|
10990
|
-
const iterator = this.getCrudTransactions()[
|
|
10976
|
+
const iterator = this.getCrudTransactions()[Symbol.asyncIterator]();
|
|
10991
10977
|
return (await iterator.next()).value;
|
|
10992
10978
|
}
|
|
10993
10979
|
/**
|
|
@@ -11023,7 +11009,7 @@ class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
11023
11009
|
*/
|
|
11024
11010
|
getCrudTransactions() {
|
|
11025
11011
|
return {
|
|
11026
|
-
[
|
|
11012
|
+
[Symbol.asyncIterator]: () => {
|
|
11027
11013
|
let lastCrudItemId = -1;
|
|
11028
11014
|
const sql = `
|
|
11029
11015
|
WITH RECURSIVE crud_entries AS (
|
|
@@ -12163,5 +12149,5 @@ const parseQuery = (query, parameters) => {
|
|
|
12163
12149
|
return { sqlStatement, parameters: parameters };
|
|
12164
12150
|
};
|
|
12165
12151
|
|
|
12166
|
-
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,
|
|
12152
|
+
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 };
|
|
12167
12153
|
//# sourceMappingURL=bundle.node.mjs.map
|