@powersync/common 0.0.0-dev-20260311103504 → 0.0.0-dev-20260503073249
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 +791 -489
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +785 -485
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +789 -488
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +783 -484
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +165 -103
- package/lib/attachments/AttachmentQueue.d.ts +10 -4
- package/lib/attachments/AttachmentQueue.js +10 -4
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/AttachmentService.js +2 -3
- package/lib/attachments/AttachmentService.js.map +1 -1
- package/lib/attachments/SyncingService.d.ts +2 -1
- package/lib/attachments/SyncingService.js +4 -5
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/client/AbstractPowerSyncDatabase.d.ts +5 -1
- package/lib/client/AbstractPowerSyncDatabase.js +9 -5
- 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 +4 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +88 -88
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +55 -9
- package/lib/db/DBAdapter.js +126 -0
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +0 -4
- package/lib/db/crud/SyncStatus.js +0 -4
- package/lib/db/crud/SyncStatus.js.map +1 -1
- package/lib/db/schema/RawTable.d.ts +0 -5
- package/lib/db/schema/Schema.d.ts +0 -2
- package/lib/db/schema/Schema.js +0 -2
- package/lib/db/schema/Schema.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/mutex.d.ts +47 -5
- package/lib/utils/mutex.js +146 -21
- package/lib/utils/mutex.js.map +1 -1
- package/lib/utils/queue.d.ts +16 -0
- package/lib/utils/queue.js +42 -0
- package/lib/utils/queue.js.map +1 -0
- 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 -8
- package/src/attachments/AttachmentQueue.ts +10 -4
- package/src/attachments/AttachmentService.ts +2 -3
- package/src/attachments/README.md +6 -4
- package/src/attachments/SyncingService.ts +4 -5
- package/src/client/AbstractPowerSyncDatabase.ts +9 -5
- package/src/client/sync/stream/AbstractRemote.ts +182 -206
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +96 -83
- package/src/db/DBAdapter.ts +167 -9
- package/src/db/crud/SyncStatus.ts +0 -4
- package/src/db/schema/RawTable.ts +0 -5
- package/src/db/schema/Schema.ts +0 -2
- package/src/index.ts +1 -1
- package/src/utils/async.ts +0 -11
- package/src/utils/mutex.ts +184 -26
- package/src/utils/queue.ts +48 -0
- 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
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Mutex } from 'async-mutex';
|
|
2
1
|
import { EventIterator } from 'event-iterator';
|
|
3
2
|
import { Buffer } from 'node:buffer';
|
|
4
3
|
|
|
@@ -659,7 +658,7 @@ class SyncingService {
|
|
|
659
658
|
updatedAttachments.push(downloaded);
|
|
660
659
|
break;
|
|
661
660
|
case AttachmentState.QUEUED_DELETE:
|
|
662
|
-
const deleted = await this.deleteAttachment(attachment);
|
|
661
|
+
const deleted = await this.deleteAttachment(attachment, context);
|
|
663
662
|
updatedAttachments.push(deleted);
|
|
664
663
|
break;
|
|
665
664
|
}
|
|
@@ -737,17 +736,16 @@ class SyncingService {
|
|
|
737
736
|
* On failure, defers to error handler or archives.
|
|
738
737
|
*
|
|
739
738
|
* @param attachment - The attachment record to delete
|
|
739
|
+
* @param context - Attachment context for database operations
|
|
740
740
|
* @returns Updated attachment record
|
|
741
741
|
*/
|
|
742
|
-
async deleteAttachment(attachment) {
|
|
742
|
+
async deleteAttachment(attachment, context) {
|
|
743
743
|
try {
|
|
744
744
|
await this.remoteStorage.deleteFile(attachment);
|
|
745
745
|
if (attachment.localUri) {
|
|
746
746
|
await this.localStorage.deleteFile(attachment.localUri);
|
|
747
747
|
}
|
|
748
|
-
await
|
|
749
|
-
await ctx.deleteAttachment(attachment.id);
|
|
750
|
-
});
|
|
748
|
+
await context.deleteAttachment(attachment.id);
|
|
751
749
|
return {
|
|
752
750
|
...attachment,
|
|
753
751
|
state: AttachmentState.ARCHIVED
|
|
@@ -785,32 +783,198 @@ class SyncingService {
|
|
|
785
783
|
}
|
|
786
784
|
|
|
787
785
|
/**
|
|
788
|
-
*
|
|
786
|
+
* A simple fixed-capacity queue implementation.
|
|
787
|
+
*
|
|
788
|
+
* Unlike a naive queue implemented by `array.push()` and `array.shift()`, this avoids moving array elements around
|
|
789
|
+
* and is `O(1)` for {@link addLast} and {@link removeFirst}.
|
|
789
790
|
*/
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
791
|
+
class Queue {
|
|
792
|
+
table;
|
|
793
|
+
// Index of the first element in the table.
|
|
794
|
+
head;
|
|
795
|
+
// Amount of items currently in the queue.
|
|
796
|
+
_length;
|
|
797
|
+
constructor(initialItems) {
|
|
798
|
+
this.table = [...initialItems];
|
|
799
|
+
this.head = 0;
|
|
800
|
+
this._length = this.table.length;
|
|
801
|
+
}
|
|
802
|
+
get isEmpty() {
|
|
803
|
+
return this.length == 0;
|
|
804
|
+
}
|
|
805
|
+
get length() {
|
|
806
|
+
return this._length;
|
|
807
|
+
}
|
|
808
|
+
removeFirst() {
|
|
809
|
+
if (this.isEmpty) {
|
|
810
|
+
throw new Error('Queue is empty');
|
|
811
|
+
}
|
|
812
|
+
const result = this.table[this.head];
|
|
813
|
+
this._length--;
|
|
814
|
+
this.table[this.head] = undefined;
|
|
815
|
+
this.head = (this.head + 1) % this.table.length;
|
|
816
|
+
return result;
|
|
817
|
+
}
|
|
818
|
+
addLast(element) {
|
|
819
|
+
if (this.length == this.table.length) {
|
|
820
|
+
throw new Error('Queue is full');
|
|
821
|
+
}
|
|
822
|
+
this.table[(this.head + this._length) % this.table.length] = element;
|
|
823
|
+
this._length++;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* An asynchronous semaphore implementation with associated items per lease.
|
|
829
|
+
*
|
|
830
|
+
* @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
|
|
831
|
+
*/
|
|
832
|
+
class Semaphore {
|
|
833
|
+
// Available items that are not currently assigned to a waiter.
|
|
834
|
+
available;
|
|
835
|
+
size;
|
|
836
|
+
// Linked list of waiters. We don't expect the wait list to become particularly large, and this allows removing
|
|
837
|
+
// aborted waiters from the middle of the list efficiently.
|
|
838
|
+
firstWaiter;
|
|
839
|
+
lastWaiter;
|
|
840
|
+
constructor(elements) {
|
|
841
|
+
this.available = new Queue(elements);
|
|
842
|
+
this.size = this.available.length;
|
|
843
|
+
}
|
|
844
|
+
addWaiter(requestedItems, onAcquire) {
|
|
845
|
+
const node = {
|
|
846
|
+
isActive: true,
|
|
847
|
+
acquiredItems: [],
|
|
848
|
+
remainingItems: requestedItems,
|
|
849
|
+
onAcquire,
|
|
850
|
+
prev: this.lastWaiter
|
|
851
|
+
};
|
|
852
|
+
if (this.lastWaiter) {
|
|
853
|
+
this.lastWaiter.next = node;
|
|
854
|
+
this.lastWaiter = node;
|
|
855
|
+
}
|
|
856
|
+
else {
|
|
857
|
+
// First waiter
|
|
858
|
+
this.lastWaiter = this.firstWaiter = node;
|
|
859
|
+
}
|
|
860
|
+
return node;
|
|
861
|
+
}
|
|
862
|
+
deactivateWaiter(waiter) {
|
|
863
|
+
const { prev, next } = waiter;
|
|
864
|
+
waiter.isActive = false;
|
|
865
|
+
if (prev)
|
|
866
|
+
prev.next = next;
|
|
867
|
+
if (next)
|
|
868
|
+
next.prev = prev;
|
|
869
|
+
if (waiter == this.firstWaiter)
|
|
870
|
+
this.firstWaiter = next;
|
|
871
|
+
if (waiter == this.lastWaiter)
|
|
872
|
+
this.lastWaiter = prev;
|
|
873
|
+
}
|
|
874
|
+
requestPermits(amount, abort) {
|
|
875
|
+
if (amount <= 0 || amount > this.size) {
|
|
876
|
+
throw new Error(`Invalid amount of items requested (${amount}), must be between 1 and ${this.size}`);
|
|
877
|
+
}
|
|
878
|
+
return new Promise((resolve, reject) => {
|
|
879
|
+
function rejectAborted() {
|
|
880
|
+
reject(abort?.reason ?? new Error('Semaphore acquire aborted'));
|
|
881
|
+
}
|
|
882
|
+
if (abort?.aborted) {
|
|
883
|
+
return rejectAborted();
|
|
884
|
+
}
|
|
885
|
+
let waiter;
|
|
886
|
+
const markCompleted = () => {
|
|
887
|
+
const items = waiter.acquiredItems;
|
|
888
|
+
waiter.acquiredItems = []; // Avoid releasing items twice.
|
|
889
|
+
for (const element of items) {
|
|
890
|
+
// Give to next waiter, if possible.
|
|
891
|
+
const nextWaiter = this.firstWaiter;
|
|
892
|
+
if (nextWaiter) {
|
|
893
|
+
nextWaiter.acquiredItems.push(element);
|
|
894
|
+
nextWaiter.remainingItems--;
|
|
895
|
+
if (nextWaiter.remainingItems == 0) {
|
|
896
|
+
nextWaiter.onAcquire();
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
else {
|
|
900
|
+
// No pending waiter, return lease into pool.
|
|
901
|
+
this.available.addLast(element);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
const onAbort = () => {
|
|
906
|
+
abort?.removeEventListener('abort', onAbort);
|
|
907
|
+
if (waiter.isActive) {
|
|
908
|
+
this.deactivateWaiter(waiter);
|
|
909
|
+
rejectAborted();
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
const resolvePromise = () => {
|
|
913
|
+
this.deactivateWaiter(waiter);
|
|
914
|
+
abort?.removeEventListener('abort', onAbort);
|
|
915
|
+
const items = waiter.acquiredItems;
|
|
916
|
+
resolve({ items, release: markCompleted });
|
|
917
|
+
};
|
|
918
|
+
waiter = this.addWaiter(amount, resolvePromise);
|
|
919
|
+
// If there are items in the pool that haven't been assigned, we can pull them into this waiter. Note that this is
|
|
920
|
+
// only the case if we're the first waiter (otherwise, items would have been assigned to an earlier waiter).
|
|
921
|
+
while (!this.available.isEmpty && waiter.remainingItems > 0) {
|
|
922
|
+
waiter.acquiredItems.push(this.available.removeFirst());
|
|
923
|
+
waiter.remainingItems--;
|
|
808
924
|
}
|
|
809
|
-
|
|
810
|
-
|
|
925
|
+
if (waiter.remainingItems == 0) {
|
|
926
|
+
return resolvePromise();
|
|
811
927
|
}
|
|
928
|
+
abort?.addEventListener('abort', onAbort);
|
|
812
929
|
});
|
|
813
|
-
}
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* Requests a single item from the pool.
|
|
933
|
+
*
|
|
934
|
+
* The returned `release` callback must be invoked to return the item into the pool.
|
|
935
|
+
*/
|
|
936
|
+
async requestOne(abort) {
|
|
937
|
+
const { items, release } = await this.requestPermits(1, abort);
|
|
938
|
+
return { release, item: items[0] };
|
|
939
|
+
}
|
|
940
|
+
/**
|
|
941
|
+
* Requests access to all items from the pool.
|
|
942
|
+
*
|
|
943
|
+
* The returned `release` callback must be invoked to return items into the pool.
|
|
944
|
+
*/
|
|
945
|
+
requestAll(abort) {
|
|
946
|
+
return this.requestPermits(this.size, abort);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* An asynchronous mutex implementation.
|
|
951
|
+
*
|
|
952
|
+
* @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
|
|
953
|
+
*/
|
|
954
|
+
class Mutex {
|
|
955
|
+
inner = new Semaphore([null]);
|
|
956
|
+
async acquire(abort) {
|
|
957
|
+
const { release } = await this.inner.requestOne(abort);
|
|
958
|
+
return release;
|
|
959
|
+
}
|
|
960
|
+
async runExclusive(fn, abort) {
|
|
961
|
+
const returnMutex = await this.acquire(abort);
|
|
962
|
+
try {
|
|
963
|
+
return await fn();
|
|
964
|
+
}
|
|
965
|
+
finally {
|
|
966
|
+
returnMutex();
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
function timeoutSignal(timeout) {
|
|
971
|
+
if (timeout == null)
|
|
972
|
+
return;
|
|
973
|
+
if ('timeout' in AbortSignal)
|
|
974
|
+
return AbortSignal.timeout(timeout);
|
|
975
|
+
const controller = new AbortController();
|
|
976
|
+
setTimeout(() => controller.abort(new Error('Timeout waiting for lock')), timeout);
|
|
977
|
+
return controller.signal;
|
|
814
978
|
}
|
|
815
979
|
|
|
816
980
|
/**
|
|
@@ -859,7 +1023,7 @@ class AttachmentService {
|
|
|
859
1023
|
* Executes a callback with exclusive access to the attachment context.
|
|
860
1024
|
*/
|
|
861
1025
|
async withContext(callback) {
|
|
862
|
-
return
|
|
1026
|
+
return this.mutex.runExclusive(async () => {
|
|
863
1027
|
return callback(this.context);
|
|
864
1028
|
});
|
|
865
1029
|
}
|
|
@@ -895,9 +1059,15 @@ class AttachmentQueue {
|
|
|
895
1059
|
tableName;
|
|
896
1060
|
/** Logger instance for diagnostic information */
|
|
897
1061
|
logger;
|
|
898
|
-
/** Interval in milliseconds between periodic sync operations.
|
|
1062
|
+
/** Interval in milliseconds between periodic sync operations. Acts as a polling timer to retry
|
|
1063
|
+
* failed uploads/downloads, especially after the app goes offline. Default: 30000 (30 seconds) */
|
|
899
1064
|
syncIntervalMs = 30 * 1000;
|
|
900
|
-
/**
|
|
1065
|
+
/** Throttle duration in milliseconds for the reactive watch query on the attachments table.
|
|
1066
|
+
* When attachment records change, a watch query detects the change and triggers a sync.
|
|
1067
|
+
* This throttle prevents the sync from firing too rapidly when many changes happen in
|
|
1068
|
+
* quick succession (e.g., bulk inserts). This is distinct from syncIntervalMs — it controls
|
|
1069
|
+
* how quickly the queue reacts to changes, while syncIntervalMs controls how often it polls
|
|
1070
|
+
* for retries. Default: 30 (from DEFAULT_WATCH_THROTTLE_MS) */
|
|
901
1071
|
syncThrottleDuration;
|
|
902
1072
|
/** Whether to automatically download remote attachments. Default: true */
|
|
903
1073
|
downloadAttachments = true;
|
|
@@ -921,8 +1091,8 @@ class AttachmentQueue {
|
|
|
921
1091
|
* @param options.watchAttachments - Callback for monitoring attachment changes in your data model
|
|
922
1092
|
* @param options.tableName - Name of the table to store attachment records. Default: 'ps_attachment_queue'
|
|
923
1093
|
* @param options.logger - Logger instance. Defaults to db.logger
|
|
924
|
-
* @param options.syncIntervalMs -
|
|
925
|
-
* @param options.syncThrottleDuration - Throttle duration for
|
|
1094
|
+
* @param options.syncIntervalMs - Periodic polling interval in milliseconds for retrying failed uploads/downloads. Default: 30000
|
|
1095
|
+
* @param options.syncThrottleDuration - Throttle duration in milliseconds for the reactive watch query that detects attachment changes. Prevents rapid-fire syncs during bulk changes. Default: 30
|
|
926
1096
|
* @param options.downloadAttachments - Whether to automatically download remote attachments. Default: true
|
|
927
1097
|
* @param options.archivedCacheLimit - Maximum archived attachments before cleanup. Default: 100
|
|
928
1098
|
*/
|
|
@@ -1530,6 +1700,49 @@ var Logger = /*@__PURE__*/getDefaultExportFromCjs(loggerExports);
|
|
|
1530
1700
|
* Set of generic interfaces to allow PowerSync compatibility with
|
|
1531
1701
|
* different SQLite DB implementations.
|
|
1532
1702
|
*/
|
|
1703
|
+
/**
|
|
1704
|
+
* Implements {@link DBGetUtils} on a {@link SqlRunner}.
|
|
1705
|
+
*/
|
|
1706
|
+
function DBGetUtilsDefaultMixin(Base) {
|
|
1707
|
+
return class extends Base {
|
|
1708
|
+
async getAll(sql, parameters) {
|
|
1709
|
+
const res = await this.execute(sql, parameters);
|
|
1710
|
+
return res.rows?._array ?? [];
|
|
1711
|
+
}
|
|
1712
|
+
async getOptional(sql, parameters) {
|
|
1713
|
+
const res = await this.execute(sql, parameters);
|
|
1714
|
+
return res.rows?.item(0) ?? null;
|
|
1715
|
+
}
|
|
1716
|
+
async get(sql, parameters) {
|
|
1717
|
+
const res = await this.execute(sql, parameters);
|
|
1718
|
+
const first = res.rows?.item(0);
|
|
1719
|
+
if (!first) {
|
|
1720
|
+
throw new Error('Result set is empty');
|
|
1721
|
+
}
|
|
1722
|
+
return first;
|
|
1723
|
+
}
|
|
1724
|
+
async executeBatch(query, params = []) {
|
|
1725
|
+
// If this context can run batch statements natively, use that.
|
|
1726
|
+
// @ts-ignore
|
|
1727
|
+
if (super.executeBatch) {
|
|
1728
|
+
// @ts-ignore
|
|
1729
|
+
return super.executeBatch(query, params);
|
|
1730
|
+
}
|
|
1731
|
+
// Emulate executeBatch by running statements individually.
|
|
1732
|
+
let lastInsertId;
|
|
1733
|
+
let rowsAffected = 0;
|
|
1734
|
+
for (const set of params) {
|
|
1735
|
+
const result = await this.execute(query, set);
|
|
1736
|
+
lastInsertId = result.insertId;
|
|
1737
|
+
rowsAffected += result.rowsAffected;
|
|
1738
|
+
}
|
|
1739
|
+
return {
|
|
1740
|
+
rowsAffected,
|
|
1741
|
+
insertId: lastInsertId
|
|
1742
|
+
};
|
|
1743
|
+
}
|
|
1744
|
+
};
|
|
1745
|
+
}
|
|
1533
1746
|
/**
|
|
1534
1747
|
* Update table operation numbers from SQLite
|
|
1535
1748
|
*/
|
|
@@ -1539,6 +1752,89 @@ var RowUpdateType;
|
|
|
1539
1752
|
RowUpdateType[RowUpdateType["SQLITE_DELETE"] = 9] = "SQLITE_DELETE";
|
|
1540
1753
|
RowUpdateType[RowUpdateType["SQLITE_UPDATE"] = 23] = "SQLITE_UPDATE";
|
|
1541
1754
|
})(RowUpdateType || (RowUpdateType = {}));
|
|
1755
|
+
/**
|
|
1756
|
+
* A mixin to implement {@link DBAdapter} by delegating to {@link ConnectionPool.readLock} and
|
|
1757
|
+
* {@link ConnectionPool.writeLock}.
|
|
1758
|
+
*/
|
|
1759
|
+
function DBAdapterDefaultMixin(Base) {
|
|
1760
|
+
return class extends Base {
|
|
1761
|
+
readTransaction(fn, options) {
|
|
1762
|
+
return this.readLock((ctx) => TransactionImplementation.runWith(ctx, fn), options);
|
|
1763
|
+
}
|
|
1764
|
+
writeTransaction(fn, options) {
|
|
1765
|
+
return this.writeLock((ctx) => TransactionImplementation.runWith(ctx, fn), options);
|
|
1766
|
+
}
|
|
1767
|
+
getAll(sql, parameters) {
|
|
1768
|
+
return this.readLock((ctx) => ctx.getAll(sql, parameters));
|
|
1769
|
+
}
|
|
1770
|
+
getOptional(sql, parameters) {
|
|
1771
|
+
return this.readLock((ctx) => ctx.getOptional(sql, parameters));
|
|
1772
|
+
}
|
|
1773
|
+
get(sql, parameters) {
|
|
1774
|
+
return this.readLock((ctx) => ctx.get(sql, parameters));
|
|
1775
|
+
}
|
|
1776
|
+
execute(query, params) {
|
|
1777
|
+
return this.writeLock((ctx) => ctx.execute(query, params));
|
|
1778
|
+
}
|
|
1779
|
+
executeRaw(query, params) {
|
|
1780
|
+
return this.writeLock((ctx) => ctx.executeRaw(query, params));
|
|
1781
|
+
}
|
|
1782
|
+
executeBatch(query, params) {
|
|
1783
|
+
return this.writeTransaction((tx) => tx.executeBatch(query, params));
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1787
|
+
class BaseTransaction {
|
|
1788
|
+
inner;
|
|
1789
|
+
finalized = false;
|
|
1790
|
+
constructor(inner) {
|
|
1791
|
+
this.inner = inner;
|
|
1792
|
+
}
|
|
1793
|
+
async commit() {
|
|
1794
|
+
if (this.finalized) {
|
|
1795
|
+
return { rowsAffected: 0 };
|
|
1796
|
+
}
|
|
1797
|
+
this.finalized = true;
|
|
1798
|
+
return this.inner.execute('COMMIT');
|
|
1799
|
+
}
|
|
1800
|
+
async rollback() {
|
|
1801
|
+
if (this.finalized) {
|
|
1802
|
+
return { rowsAffected: 0 };
|
|
1803
|
+
}
|
|
1804
|
+
this.finalized = true;
|
|
1805
|
+
return this.inner.execute('ROLLBACK');
|
|
1806
|
+
}
|
|
1807
|
+
execute(query, params) {
|
|
1808
|
+
return this.inner.execute(query, params);
|
|
1809
|
+
}
|
|
1810
|
+
executeRaw(query, params) {
|
|
1811
|
+
return this.inner.executeRaw(query, params);
|
|
1812
|
+
}
|
|
1813
|
+
executeBatch(query, params) {
|
|
1814
|
+
return this.inner.executeBatch(query, params);
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
class TransactionImplementation extends DBGetUtilsDefaultMixin(BaseTransaction) {
|
|
1818
|
+
static async runWith(ctx, fn) {
|
|
1819
|
+
let tx = new TransactionImplementation(ctx);
|
|
1820
|
+
try {
|
|
1821
|
+
await ctx.execute('BEGIN IMMEDIATE');
|
|
1822
|
+
const result = await fn(tx);
|
|
1823
|
+
await tx.commit();
|
|
1824
|
+
return result;
|
|
1825
|
+
}
|
|
1826
|
+
catch (ex) {
|
|
1827
|
+
try {
|
|
1828
|
+
await tx.rollback();
|
|
1829
|
+
}
|
|
1830
|
+
catch (ex2) {
|
|
1831
|
+
// In rare cases, a rollback may fail.
|
|
1832
|
+
// Safe to ignore.
|
|
1833
|
+
}
|
|
1834
|
+
throw ex;
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1542
1838
|
function isBatchedUpdateNotification(update) {
|
|
1543
1839
|
return 'tables' in update;
|
|
1544
1840
|
}
|
|
@@ -1681,16 +1977,12 @@ class SyncStatus {
|
|
|
1681
1977
|
*
|
|
1682
1978
|
* This returns null when the database is currently being opened and we don't have reliable information about all
|
|
1683
1979
|
* included streams yet.
|
|
1684
|
-
*
|
|
1685
|
-
* @experimental Sync streams are currently in alpha.
|
|
1686
1980
|
*/
|
|
1687
1981
|
get syncStreams() {
|
|
1688
1982
|
return this.options.dataFlow?.internalStreamSubscriptions?.map((core) => new SyncStreamStatusView(this, core));
|
|
1689
1983
|
}
|
|
1690
1984
|
/**
|
|
1691
1985
|
* If the `stream` appears in {@link syncStreams}, returns the current status for that stream.
|
|
1692
|
-
*
|
|
1693
|
-
* @experimental Sync streams are currently in alpha.
|
|
1694
1986
|
*/
|
|
1695
1987
|
forStream(stream) {
|
|
1696
1988
|
const asJson = JSON.stringify(stream.parameters);
|
|
@@ -1959,15 +2251,6 @@ class ControlledExecutor {
|
|
|
1959
2251
|
}
|
|
1960
2252
|
}
|
|
1961
2253
|
|
|
1962
|
-
/**
|
|
1963
|
-
* A ponyfill for `Symbol.asyncIterator` that is compatible with the
|
|
1964
|
-
* [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)
|
|
1965
|
-
* we recommend for React Native.
|
|
1966
|
-
*
|
|
1967
|
-
* As long as we use this symbol (instead of `for await` and `async *`) in this package, we can be compatible with async
|
|
1968
|
-
* iterators without requiring them.
|
|
1969
|
-
*/
|
|
1970
|
-
const symbolAsyncIterator = Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
|
1971
2254
|
/**
|
|
1972
2255
|
* Throttle a function to be called at most once every "wait" milliseconds,
|
|
1973
2256
|
* on the trailing edge.
|
|
@@ -7933,177 +8216,10 @@ function requireDist () {
|
|
|
7933
8216
|
|
|
7934
8217
|
var distExports = requireDist();
|
|
7935
8218
|
|
|
7936
|
-
var version = "1.
|
|
8219
|
+
var version = "1.52.0";
|
|
7937
8220
|
var PACKAGE = {
|
|
7938
8221
|
version: version};
|
|
7939
8222
|
|
|
7940
|
-
const DEFAULT_PRESSURE_LIMITS = {
|
|
7941
|
-
highWater: 10,
|
|
7942
|
-
lowWater: 0
|
|
7943
|
-
};
|
|
7944
|
-
/**
|
|
7945
|
-
* A very basic implementation of a data stream with backpressure support which does not use
|
|
7946
|
-
* native JS streams or async iterators.
|
|
7947
|
-
* This is handy for environments such as React Native which need polyfills for the above.
|
|
7948
|
-
*/
|
|
7949
|
-
class DataStream extends BaseObserver {
|
|
7950
|
-
options;
|
|
7951
|
-
dataQueue;
|
|
7952
|
-
isClosed;
|
|
7953
|
-
processingPromise;
|
|
7954
|
-
notifyDataAdded;
|
|
7955
|
-
logger;
|
|
7956
|
-
mapLine;
|
|
7957
|
-
constructor(options) {
|
|
7958
|
-
super();
|
|
7959
|
-
this.options = options;
|
|
7960
|
-
this.processingPromise = null;
|
|
7961
|
-
this.isClosed = false;
|
|
7962
|
-
this.dataQueue = [];
|
|
7963
|
-
this.mapLine = options?.mapLine ?? ((line) => line);
|
|
7964
|
-
this.logger = options?.logger ?? Logger.get('DataStream');
|
|
7965
|
-
if (options?.closeOnError) {
|
|
7966
|
-
const l = this.registerListener({
|
|
7967
|
-
error: (ex) => {
|
|
7968
|
-
l?.();
|
|
7969
|
-
this.close();
|
|
7970
|
-
}
|
|
7971
|
-
});
|
|
7972
|
-
}
|
|
7973
|
-
}
|
|
7974
|
-
get highWatermark() {
|
|
7975
|
-
return this.options?.pressure?.highWaterMark ?? DEFAULT_PRESSURE_LIMITS.highWater;
|
|
7976
|
-
}
|
|
7977
|
-
get lowWatermark() {
|
|
7978
|
-
return this.options?.pressure?.lowWaterMark ?? DEFAULT_PRESSURE_LIMITS.lowWater;
|
|
7979
|
-
}
|
|
7980
|
-
get closed() {
|
|
7981
|
-
return this.isClosed;
|
|
7982
|
-
}
|
|
7983
|
-
async close() {
|
|
7984
|
-
this.isClosed = true;
|
|
7985
|
-
await this.processingPromise;
|
|
7986
|
-
this.iterateListeners((l) => l.closed?.());
|
|
7987
|
-
// Discard any data in the queue
|
|
7988
|
-
this.dataQueue = [];
|
|
7989
|
-
this.listeners.clear();
|
|
7990
|
-
}
|
|
7991
|
-
/**
|
|
7992
|
-
* Enqueues data for the consumers to read
|
|
7993
|
-
*/
|
|
7994
|
-
enqueueData(data) {
|
|
7995
|
-
if (this.isClosed) {
|
|
7996
|
-
throw new Error('Cannot enqueue data into closed stream.');
|
|
7997
|
-
}
|
|
7998
|
-
this.dataQueue.push(data);
|
|
7999
|
-
this.notifyDataAdded?.();
|
|
8000
|
-
this.processQueue();
|
|
8001
|
-
}
|
|
8002
|
-
/**
|
|
8003
|
-
* Reads data once from the data stream
|
|
8004
|
-
* @returns a Data payload or Null if the stream closed.
|
|
8005
|
-
*/
|
|
8006
|
-
async read() {
|
|
8007
|
-
if (this.closed) {
|
|
8008
|
-
return null;
|
|
8009
|
-
}
|
|
8010
|
-
// Wait for any pending processing to complete first.
|
|
8011
|
-
// This ensures we register our listener before calling processQueue(),
|
|
8012
|
-
// avoiding a race where processQueue() sees no reader and returns early.
|
|
8013
|
-
if (this.processingPromise) {
|
|
8014
|
-
await this.processingPromise;
|
|
8015
|
-
}
|
|
8016
|
-
// Re-check after await - stream may have closed while we were waiting
|
|
8017
|
-
if (this.closed) {
|
|
8018
|
-
return null;
|
|
8019
|
-
}
|
|
8020
|
-
return new Promise((resolve, reject) => {
|
|
8021
|
-
const l = this.registerListener({
|
|
8022
|
-
data: async (data) => {
|
|
8023
|
-
resolve(data);
|
|
8024
|
-
// Remove the listener
|
|
8025
|
-
l?.();
|
|
8026
|
-
},
|
|
8027
|
-
closed: () => {
|
|
8028
|
-
resolve(null);
|
|
8029
|
-
l?.();
|
|
8030
|
-
},
|
|
8031
|
-
error: (ex) => {
|
|
8032
|
-
reject(ex);
|
|
8033
|
-
l?.();
|
|
8034
|
-
}
|
|
8035
|
-
});
|
|
8036
|
-
this.processQueue();
|
|
8037
|
-
});
|
|
8038
|
-
}
|
|
8039
|
-
/**
|
|
8040
|
-
* Executes a callback for each data item in the stream
|
|
8041
|
-
*/
|
|
8042
|
-
forEach(callback) {
|
|
8043
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
8044
|
-
this.iterateAsyncErrored(async (l) => l.lowWater?.());
|
|
8045
|
-
}
|
|
8046
|
-
return this.registerListener({
|
|
8047
|
-
data: callback
|
|
8048
|
-
});
|
|
8049
|
-
}
|
|
8050
|
-
processQueue() {
|
|
8051
|
-
if (this.processingPromise) {
|
|
8052
|
-
return;
|
|
8053
|
-
}
|
|
8054
|
-
const promise = (this.processingPromise = this._processQueue());
|
|
8055
|
-
promise.finally(() => {
|
|
8056
|
-
this.processingPromise = null;
|
|
8057
|
-
});
|
|
8058
|
-
return promise;
|
|
8059
|
-
}
|
|
8060
|
-
hasDataReader() {
|
|
8061
|
-
return Array.from(this.listeners.values()).some((l) => !!l.data);
|
|
8062
|
-
}
|
|
8063
|
-
async _processQueue() {
|
|
8064
|
-
/**
|
|
8065
|
-
* Allow listeners to mutate the queue before processing.
|
|
8066
|
-
* This allows for operations such as dropping or compressing data
|
|
8067
|
-
* on high water or requesting more data on low water.
|
|
8068
|
-
*/
|
|
8069
|
-
if (this.dataQueue.length >= this.highWatermark) {
|
|
8070
|
-
await this.iterateAsyncErrored(async (l) => l.highWater?.());
|
|
8071
|
-
}
|
|
8072
|
-
if (this.isClosed || !this.hasDataReader()) {
|
|
8073
|
-
return;
|
|
8074
|
-
}
|
|
8075
|
-
if (this.dataQueue.length) {
|
|
8076
|
-
const data = this.dataQueue.shift();
|
|
8077
|
-
const mapped = this.mapLine(data);
|
|
8078
|
-
await this.iterateAsyncErrored(async (l) => l.data?.(mapped));
|
|
8079
|
-
}
|
|
8080
|
-
if (this.dataQueue.length <= this.lowWatermark) {
|
|
8081
|
-
const dataAdded = new Promise((resolve) => {
|
|
8082
|
-
this.notifyDataAdded = resolve;
|
|
8083
|
-
});
|
|
8084
|
-
await Promise.race([this.iterateAsyncErrored(async (l) => l.lowWater?.()), dataAdded]);
|
|
8085
|
-
this.notifyDataAdded = null;
|
|
8086
|
-
}
|
|
8087
|
-
if (this.dataQueue.length > 0) {
|
|
8088
|
-
setTimeout(() => this.processQueue());
|
|
8089
|
-
}
|
|
8090
|
-
}
|
|
8091
|
-
async iterateAsyncErrored(cb) {
|
|
8092
|
-
// Important: We need to copy the listeners, as calling a listener could result in adding another
|
|
8093
|
-
// listener, resulting in infinite loops.
|
|
8094
|
-
const listeners = Array.from(this.listeners.values());
|
|
8095
|
-
for (let i of listeners) {
|
|
8096
|
-
try {
|
|
8097
|
-
await cb(i);
|
|
8098
|
-
}
|
|
8099
|
-
catch (ex) {
|
|
8100
|
-
this.logger.error(ex);
|
|
8101
|
-
this.iterateListeners((l) => l.error?.(ex));
|
|
8102
|
-
}
|
|
8103
|
-
}
|
|
8104
|
-
}
|
|
8105
|
-
}
|
|
8106
|
-
|
|
8107
8223
|
var WebsocketDuplexConnection = {};
|
|
8108
8224
|
|
|
8109
8225
|
var hasRequiredWebsocketDuplexConnection;
|
|
@@ -8266,8 +8382,215 @@ class WebsocketClientTransport {
|
|
|
8266
8382
|
}
|
|
8267
8383
|
}
|
|
8268
8384
|
|
|
8385
|
+
const doneResult = { done: true, value: undefined };
|
|
8386
|
+
function valueResult(value) {
|
|
8387
|
+
return { done: false, value };
|
|
8388
|
+
}
|
|
8389
|
+
/**
|
|
8390
|
+
* A variant of {@link Array.map} for async iterators.
|
|
8391
|
+
*/
|
|
8392
|
+
function map(source, map) {
|
|
8393
|
+
return {
|
|
8394
|
+
next: async () => {
|
|
8395
|
+
const value = await source.next();
|
|
8396
|
+
if (value.done) {
|
|
8397
|
+
return value;
|
|
8398
|
+
}
|
|
8399
|
+
else {
|
|
8400
|
+
return { value: map(value.value) };
|
|
8401
|
+
}
|
|
8402
|
+
}
|
|
8403
|
+
};
|
|
8404
|
+
}
|
|
8405
|
+
/**
|
|
8406
|
+
* Expands a source async iterator by allowing to inject events asynchronously.
|
|
8407
|
+
*
|
|
8408
|
+
* The resulting iterator will emit all events from its source. Additionally though, events can be injected. These
|
|
8409
|
+
* events are dropped once the main iterator completes, but are otherwise forwarded.
|
|
8410
|
+
*
|
|
8411
|
+
* The iterator completes when its source completes, and it supports backpressure by only calling `next()` on the source
|
|
8412
|
+
* in response to a `next()` call from downstream if no pending injected events can be dispatched.
|
|
8413
|
+
*/
|
|
8414
|
+
function injectable(source) {
|
|
8415
|
+
let sourceIsDone = false;
|
|
8416
|
+
let waiter = undefined; // An active, waiting next() call.
|
|
8417
|
+
// A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
|
|
8418
|
+
let pendingSourceEvent = null;
|
|
8419
|
+
let pendingInjectedEvents = [];
|
|
8420
|
+
const consumeWaiter = () => {
|
|
8421
|
+
const pending = waiter;
|
|
8422
|
+
waiter = undefined;
|
|
8423
|
+
return pending;
|
|
8424
|
+
};
|
|
8425
|
+
const fetchFromSource = () => {
|
|
8426
|
+
const resolveWaiter = (propagate) => {
|
|
8427
|
+
const active = consumeWaiter();
|
|
8428
|
+
if (active) {
|
|
8429
|
+
propagate(active);
|
|
8430
|
+
}
|
|
8431
|
+
else {
|
|
8432
|
+
pendingSourceEvent = propagate;
|
|
8433
|
+
}
|
|
8434
|
+
};
|
|
8435
|
+
const nextFromSource = source.next();
|
|
8436
|
+
nextFromSource.then((value) => {
|
|
8437
|
+
sourceIsDone = value.done == true;
|
|
8438
|
+
resolveWaiter((w) => w.resolve(value));
|
|
8439
|
+
}, (error) => {
|
|
8440
|
+
resolveWaiter((w) => w.reject(error));
|
|
8441
|
+
});
|
|
8442
|
+
};
|
|
8443
|
+
return {
|
|
8444
|
+
next: () => {
|
|
8445
|
+
return new Promise((resolve, reject) => {
|
|
8446
|
+
// First priority: Dispatch ready upstream events.
|
|
8447
|
+
if (sourceIsDone) {
|
|
8448
|
+
return resolve(doneResult);
|
|
8449
|
+
}
|
|
8450
|
+
if (pendingSourceEvent) {
|
|
8451
|
+
pendingSourceEvent({ resolve, reject });
|
|
8452
|
+
pendingSourceEvent = null;
|
|
8453
|
+
return;
|
|
8454
|
+
}
|
|
8455
|
+
// Second priority: Dispatch injected events
|
|
8456
|
+
if (pendingInjectedEvents.length) {
|
|
8457
|
+
return resolve(valueResult(pendingInjectedEvents.shift()));
|
|
8458
|
+
}
|
|
8459
|
+
// Nothing pending? Fetch from source
|
|
8460
|
+
waiter = { resolve, reject };
|
|
8461
|
+
return fetchFromSource();
|
|
8462
|
+
});
|
|
8463
|
+
},
|
|
8464
|
+
inject: (event) => {
|
|
8465
|
+
const pending = consumeWaiter();
|
|
8466
|
+
if (pending != null) {
|
|
8467
|
+
pending.resolve(valueResult(event));
|
|
8468
|
+
}
|
|
8469
|
+
else {
|
|
8470
|
+
pendingInjectedEvents.push(event);
|
|
8471
|
+
}
|
|
8472
|
+
}
|
|
8473
|
+
};
|
|
8474
|
+
}
|
|
8475
|
+
/**
|
|
8476
|
+
* Splits a byte stream at line endings, emitting each line as a string.
|
|
8477
|
+
*/
|
|
8478
|
+
function extractJsonLines(source, decoder) {
|
|
8479
|
+
let buffer = '';
|
|
8480
|
+
const pendingLines = [];
|
|
8481
|
+
let isFinalEvent = false;
|
|
8482
|
+
return {
|
|
8483
|
+
next: async () => {
|
|
8484
|
+
while (true) {
|
|
8485
|
+
if (isFinalEvent) {
|
|
8486
|
+
return doneResult;
|
|
8487
|
+
}
|
|
8488
|
+
{
|
|
8489
|
+
const first = pendingLines.shift();
|
|
8490
|
+
if (first) {
|
|
8491
|
+
return { done: false, value: first };
|
|
8492
|
+
}
|
|
8493
|
+
}
|
|
8494
|
+
const { done, value } = await source.next();
|
|
8495
|
+
if (done) {
|
|
8496
|
+
const remaining = buffer.trim();
|
|
8497
|
+
if (remaining.length != 0) {
|
|
8498
|
+
isFinalEvent = true;
|
|
8499
|
+
return { done: false, value: remaining };
|
|
8500
|
+
}
|
|
8501
|
+
return doneResult;
|
|
8502
|
+
}
|
|
8503
|
+
const data = decoder.decode(value, { stream: true });
|
|
8504
|
+
buffer += data;
|
|
8505
|
+
const lines = buffer.split('\n');
|
|
8506
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
8507
|
+
const l = lines[i].trim();
|
|
8508
|
+
if (l.length > 0) {
|
|
8509
|
+
pendingLines.push(l);
|
|
8510
|
+
}
|
|
8511
|
+
}
|
|
8512
|
+
buffer = lines[lines.length - 1];
|
|
8513
|
+
}
|
|
8514
|
+
}
|
|
8515
|
+
};
|
|
8516
|
+
}
|
|
8517
|
+
/**
|
|
8518
|
+
* Splits a concatenated stream of BSON objects by emitting individual objects.
|
|
8519
|
+
*/
|
|
8520
|
+
function extractBsonObjects(source) {
|
|
8521
|
+
// Fully read but not emitted yet.
|
|
8522
|
+
const completedObjects = [];
|
|
8523
|
+
// Whether source has returned { done: true }. We do the same once completed objects have been emitted.
|
|
8524
|
+
let isDone = false;
|
|
8525
|
+
const lengthBuffer = new DataView(new ArrayBuffer(4));
|
|
8526
|
+
let objectBody = null;
|
|
8527
|
+
// If we're parsing the length field, a number between 1 and 4 (inclusive) describing remaining bytes in the header.
|
|
8528
|
+
// If we're consuming a document, the bytes remaining.
|
|
8529
|
+
let remainingLength = 4;
|
|
8530
|
+
return {
|
|
8531
|
+
async next() {
|
|
8532
|
+
while (true) {
|
|
8533
|
+
// Before fetching new data from upstream, return completed objects.
|
|
8534
|
+
if (completedObjects.length) {
|
|
8535
|
+
return valueResult(completedObjects.shift());
|
|
8536
|
+
}
|
|
8537
|
+
if (isDone) {
|
|
8538
|
+
return doneResult;
|
|
8539
|
+
}
|
|
8540
|
+
const upstreamEvent = await source.next();
|
|
8541
|
+
if (upstreamEvent.done) {
|
|
8542
|
+
isDone = true;
|
|
8543
|
+
if (objectBody || remainingLength != 4) {
|
|
8544
|
+
throw new Error('illegal end of stream in BSON object');
|
|
8545
|
+
}
|
|
8546
|
+
return doneResult;
|
|
8547
|
+
}
|
|
8548
|
+
const chunk = upstreamEvent.value;
|
|
8549
|
+
for (let i = 0; i < chunk.length;) {
|
|
8550
|
+
const availableInData = chunk.length - i;
|
|
8551
|
+
if (objectBody) {
|
|
8552
|
+
// We're in the middle of reading a BSON document.
|
|
8553
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
8554
|
+
const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
|
|
8555
|
+
objectBody.set(copySource, objectBody.length - remainingLength);
|
|
8556
|
+
i += bytesToRead;
|
|
8557
|
+
remainingLength -= bytesToRead;
|
|
8558
|
+
if (remainingLength == 0) {
|
|
8559
|
+
completedObjects.push(objectBody);
|
|
8560
|
+
// Prepare to read another document, starting with its length
|
|
8561
|
+
objectBody = null;
|
|
8562
|
+
remainingLength = 4;
|
|
8563
|
+
}
|
|
8564
|
+
}
|
|
8565
|
+
else {
|
|
8566
|
+
// Copy up to 4 bytes into lengthBuffer, depending on how many we still need.
|
|
8567
|
+
const bytesToRead = Math.min(availableInData, remainingLength);
|
|
8568
|
+
for (let j = 0; j < bytesToRead; j++) {
|
|
8569
|
+
lengthBuffer.setUint8(4 - remainingLength + j, chunk[i + j]);
|
|
8570
|
+
}
|
|
8571
|
+
i += bytesToRead;
|
|
8572
|
+
remainingLength -= bytesToRead;
|
|
8573
|
+
if (remainingLength == 0) {
|
|
8574
|
+
// Transition from reading length header to reading document. Subtracting 4 because the length of the
|
|
8575
|
+
// header is included in length.
|
|
8576
|
+
const length = lengthBuffer.getInt32(0, true /* little endian */);
|
|
8577
|
+
remainingLength = length - 4;
|
|
8578
|
+
if (remainingLength < 1) {
|
|
8579
|
+
throw new Error(`invalid length for bson: ${length}`);
|
|
8580
|
+
}
|
|
8581
|
+
objectBody = new Uint8Array(length);
|
|
8582
|
+
new DataView(objectBody.buffer).setInt32(0, length, true);
|
|
8583
|
+
}
|
|
8584
|
+
}
|
|
8585
|
+
}
|
|
8586
|
+
}
|
|
8587
|
+
}
|
|
8588
|
+
};
|
|
8589
|
+
}
|
|
8590
|
+
|
|
8269
8591
|
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
|
|
8270
8592
|
const POWERSYNC_JS_VERSION = PACKAGE.version;
|
|
8593
|
+
const SYNC_QUEUE_REQUEST_HIGH_WATER = 10;
|
|
8271
8594
|
const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
|
|
8272
8595
|
// Keep alive message is sent every period
|
|
8273
8596
|
const KEEP_ALIVE_MS = 20_000;
|
|
@@ -8447,13 +8770,14 @@ class AbstractRemote {
|
|
|
8447
8770
|
return new WebSocket(url);
|
|
8448
8771
|
}
|
|
8449
8772
|
/**
|
|
8450
|
-
* Returns a data stream of sync line data.
|
|
8773
|
+
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
8774
|
+
*
|
|
8775
|
+
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
8451
8776
|
*
|
|
8452
|
-
* @param map Maps received payload frames to the typed event value.
|
|
8453
8777
|
* @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
|
|
8454
8778
|
* (required for compatibility with older sync services).
|
|
8455
8779
|
*/
|
|
8456
|
-
async socketStreamRaw(options,
|
|
8780
|
+
async socketStreamRaw(options, bson) {
|
|
8457
8781
|
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
|
|
8458
8782
|
const mimeType = bson == null ? 'application/json' : 'application/bson';
|
|
8459
8783
|
function toBuffer(js) {
|
|
@@ -8468,52 +8792,55 @@ class AbstractRemote {
|
|
|
8468
8792
|
}
|
|
8469
8793
|
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
|
|
8470
8794
|
const request = await this.buildRequest(path);
|
|
8795
|
+
const url = this.options.socketUrlTransformer(request.url);
|
|
8471
8796
|
// Add the user agent in the setup payload - we can't set custom
|
|
8472
8797
|
// headers with websockets on web. The browser userAgent is however added
|
|
8473
8798
|
// automatically as a header.
|
|
8474
8799
|
const userAgent = this.getUserAgent();
|
|
8475
|
-
|
|
8476
|
-
|
|
8477
|
-
|
|
8478
|
-
|
|
8479
|
-
|
|
8480
|
-
|
|
8481
|
-
|
|
8800
|
+
// While we're connecting (a process that can't be aborted in RSocket), the WebSocket instance to close if we wanted
|
|
8801
|
+
// to abort the connection.
|
|
8802
|
+
let pendingSocket = null;
|
|
8803
|
+
let keepAliveTimeout;
|
|
8804
|
+
let rsocket = null;
|
|
8805
|
+
let queue = null;
|
|
8806
|
+
let didClose = false;
|
|
8807
|
+
const abortRequest = () => {
|
|
8808
|
+
if (didClose) {
|
|
8809
|
+
return;
|
|
8810
|
+
}
|
|
8811
|
+
didClose = true;
|
|
8812
|
+
clearTimeout(keepAliveTimeout);
|
|
8813
|
+
if (pendingSocket) {
|
|
8814
|
+
pendingSocket.close();
|
|
8815
|
+
}
|
|
8816
|
+
if (rsocket) {
|
|
8817
|
+
rsocket.close();
|
|
8818
|
+
}
|
|
8819
|
+
if (queue) {
|
|
8820
|
+
queue.stop();
|
|
8821
|
+
}
|
|
8822
|
+
};
|
|
8482
8823
|
// Handle upstream abort
|
|
8483
|
-
if (options.abortSignal
|
|
8824
|
+
if (options.abortSignal.aborted) {
|
|
8484
8825
|
throw new AbortOperation('Connection request aborted');
|
|
8485
8826
|
}
|
|
8486
8827
|
else {
|
|
8487
|
-
options.abortSignal
|
|
8488
|
-
stream.close();
|
|
8489
|
-
}, { once: true });
|
|
8828
|
+
options.abortSignal.addEventListener('abort', abortRequest);
|
|
8490
8829
|
}
|
|
8491
|
-
let keepAliveTimeout;
|
|
8492
8830
|
const resetTimeout = () => {
|
|
8493
8831
|
clearTimeout(keepAliveTimeout);
|
|
8494
8832
|
keepAliveTimeout = setTimeout(() => {
|
|
8495
8833
|
this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
|
|
8496
|
-
|
|
8834
|
+
abortRequest();
|
|
8497
8835
|
}, SOCKET_TIMEOUT_MS);
|
|
8498
8836
|
};
|
|
8499
8837
|
resetTimeout();
|
|
8500
|
-
// Typescript complains about this being `never` if it's not assigned here.
|
|
8501
|
-
// This is assigned in `wsCreator`.
|
|
8502
|
-
let disposeSocketConnectionTimeout = () => { };
|
|
8503
|
-
const url = this.options.socketUrlTransformer(request.url);
|
|
8504
8838
|
const connector = new distExports.RSocketConnector({
|
|
8505
8839
|
transport: new WebsocketClientTransport({
|
|
8506
8840
|
url,
|
|
8507
8841
|
wsCreator: (url) => {
|
|
8508
|
-
const socket = this.createSocket(url);
|
|
8509
|
-
|
|
8510
|
-
closed: () => {
|
|
8511
|
-
// Allow closing the underlying WebSocket if the stream was closed before the
|
|
8512
|
-
// RSocket connect completed. This should effectively abort the request.
|
|
8513
|
-
socket.close();
|
|
8514
|
-
}
|
|
8515
|
-
});
|
|
8516
|
-
socket.addEventListener('message', (event) => {
|
|
8842
|
+
const socket = (pendingSocket = this.createSocket(url));
|
|
8843
|
+
socket.addEventListener('message', () => {
|
|
8517
8844
|
resetTimeout();
|
|
8518
8845
|
});
|
|
8519
8846
|
return socket;
|
|
@@ -8533,43 +8860,40 @@ class AbstractRemote {
|
|
|
8533
8860
|
}
|
|
8534
8861
|
}
|
|
8535
8862
|
});
|
|
8536
|
-
let rsocket;
|
|
8537
8863
|
try {
|
|
8538
8864
|
rsocket = await connector.connect();
|
|
8539
8865
|
// The connection is established, we no longer need to monitor the initial timeout
|
|
8540
|
-
|
|
8866
|
+
pendingSocket = null;
|
|
8541
8867
|
}
|
|
8542
8868
|
catch (ex) {
|
|
8543
8869
|
this.logger.error(`Failed to connect WebSocket`, ex);
|
|
8544
|
-
|
|
8545
|
-
if (!stream.closed) {
|
|
8546
|
-
await stream.close();
|
|
8547
|
-
}
|
|
8870
|
+
abortRequest();
|
|
8548
8871
|
throw ex;
|
|
8549
8872
|
}
|
|
8550
8873
|
resetTimeout();
|
|
8551
|
-
let socketIsClosed = false;
|
|
8552
|
-
const closeSocket = () => {
|
|
8553
|
-
clearTimeout(keepAliveTimeout);
|
|
8554
|
-
if (socketIsClosed) {
|
|
8555
|
-
return;
|
|
8556
|
-
}
|
|
8557
|
-
socketIsClosed = true;
|
|
8558
|
-
rsocket.close();
|
|
8559
|
-
};
|
|
8560
8874
|
// Helps to prevent double close scenarios
|
|
8561
|
-
rsocket.onClose(() => (
|
|
8562
|
-
|
|
8563
|
-
let pendingEventsCount = syncQueueRequestSize;
|
|
8564
|
-
const disposeClosedListener = stream.registerListener({
|
|
8565
|
-
closed: () => {
|
|
8566
|
-
closeSocket();
|
|
8567
|
-
disposeClosedListener();
|
|
8568
|
-
}
|
|
8569
|
-
});
|
|
8570
|
-
const socket = await new Promise((resolve, reject) => {
|
|
8875
|
+
rsocket.onClose(() => (rsocket = null));
|
|
8876
|
+
return await new Promise((resolve, reject) => {
|
|
8571
8877
|
let connectionEstablished = false;
|
|
8572
|
-
|
|
8878
|
+
let pendingEventsCount = syncQueueRequestSize;
|
|
8879
|
+
let paused = false;
|
|
8880
|
+
let res = null;
|
|
8881
|
+
function requestMore() {
|
|
8882
|
+
const delta = syncQueueRequestSize - pendingEventsCount;
|
|
8883
|
+
if (!paused && delta > 0) {
|
|
8884
|
+
res?.request(delta);
|
|
8885
|
+
pendingEventsCount = syncQueueRequestSize;
|
|
8886
|
+
}
|
|
8887
|
+
}
|
|
8888
|
+
const events = new EventIterator((q) => {
|
|
8889
|
+
queue = q;
|
|
8890
|
+
q.on('highWater', () => (paused = true));
|
|
8891
|
+
q.on('lowWater', () => {
|
|
8892
|
+
paused = false;
|
|
8893
|
+
requestMore();
|
|
8894
|
+
});
|
|
8895
|
+
}, { highWaterMark: SYNC_QUEUE_REQUEST_HIGH_WATER, lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER })[Symbol.asyncIterator]();
|
|
8896
|
+
res = rsocket.requestStream({
|
|
8573
8897
|
data: toBuffer(options.data),
|
|
8574
8898
|
metadata: toBuffer({
|
|
8575
8899
|
path
|
|
@@ -8594,7 +8918,7 @@ class AbstractRemote {
|
|
|
8594
8918
|
}
|
|
8595
8919
|
// RSocket will close the RSocket stream automatically
|
|
8596
8920
|
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
|
|
8597
|
-
|
|
8921
|
+
abortRequest();
|
|
8598
8922
|
// Handles cases where the connection failed e.g. auth error or connection error
|
|
8599
8923
|
if (!connectionEstablished) {
|
|
8600
8924
|
reject(e);
|
|
@@ -8604,41 +8928,40 @@ class AbstractRemote {
|
|
|
8604
8928
|
// The connection is active
|
|
8605
8929
|
if (!connectionEstablished) {
|
|
8606
8930
|
connectionEstablished = true;
|
|
8607
|
-
resolve(
|
|
8931
|
+
resolve(events);
|
|
8608
8932
|
}
|
|
8609
8933
|
const { data } = payload;
|
|
8934
|
+
if (data) {
|
|
8935
|
+
queue.push(data);
|
|
8936
|
+
}
|
|
8610
8937
|
// Less events are now pending
|
|
8611
8938
|
pendingEventsCount--;
|
|
8612
|
-
|
|
8613
|
-
|
|
8614
|
-
}
|
|
8615
|
-
stream.enqueueData(data);
|
|
8939
|
+
// Request another event (unless the downstream consumer is paused).
|
|
8940
|
+
requestMore();
|
|
8616
8941
|
},
|
|
8617
8942
|
onComplete: () => {
|
|
8618
|
-
|
|
8943
|
+
abortRequest(); // this will also emit a done event
|
|
8619
8944
|
},
|
|
8620
8945
|
onExtension: () => { }
|
|
8621
8946
|
});
|
|
8622
8947
|
});
|
|
8623
|
-
const l = stream.registerListener({
|
|
8624
|
-
lowWater: async () => {
|
|
8625
|
-
// Request to fill up the queue
|
|
8626
|
-
const required = syncQueueRequestSize - pendingEventsCount;
|
|
8627
|
-
if (required > 0) {
|
|
8628
|
-
socket.request(syncQueueRequestSize - pendingEventsCount);
|
|
8629
|
-
pendingEventsCount = syncQueueRequestSize;
|
|
8630
|
-
}
|
|
8631
|
-
},
|
|
8632
|
-
closed: () => {
|
|
8633
|
-
l();
|
|
8634
|
-
}
|
|
8635
|
-
});
|
|
8636
|
-
return stream;
|
|
8637
8948
|
}
|
|
8638
8949
|
/**
|
|
8639
|
-
*
|
|
8950
|
+
* @returns Whether the HTTP implementation on this platform can receive streamed binary responses. This is true on
|
|
8951
|
+
* all platforms except React Native (who would have guessed...), where we must not request BSON responses.
|
|
8952
|
+
*
|
|
8953
|
+
* @see https://github.com/react-native-community/fetch?tab=readme-ov-file#motivation
|
|
8640
8954
|
*/
|
|
8641
|
-
|
|
8955
|
+
get supportsStreamingBinaryResponses() {
|
|
8956
|
+
return true;
|
|
8957
|
+
}
|
|
8958
|
+
/**
|
|
8959
|
+
* Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
|
|
8960
|
+
* async iterator of byte blobs.
|
|
8961
|
+
*
|
|
8962
|
+
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
8963
|
+
*/
|
|
8964
|
+
async fetchStreamRaw(options) {
|
|
8642
8965
|
const { data, path, headers, abortSignal } = options;
|
|
8643
8966
|
const request = await this.buildRequest(path);
|
|
8644
8967
|
/**
|
|
@@ -8650,119 +8973,94 @@ class AbstractRemote {
|
|
|
8650
8973
|
* Aborting the active fetch request while it is being consumed seems to throw
|
|
8651
8974
|
* an unhandled exception on the window level.
|
|
8652
8975
|
*/
|
|
8653
|
-
if (abortSignal
|
|
8654
|
-
throw new AbortOperation('Abort request received before making
|
|
8976
|
+
if (abortSignal.aborted) {
|
|
8977
|
+
throw new AbortOperation('Abort request received before making fetchStreamRaw request');
|
|
8655
8978
|
}
|
|
8656
8979
|
const controller = new AbortController();
|
|
8657
|
-
let
|
|
8658
|
-
abortSignal
|
|
8659
|
-
|
|
8980
|
+
let reader = null;
|
|
8981
|
+
abortSignal.addEventListener('abort', () => {
|
|
8982
|
+
const reason = abortSignal.reason ??
|
|
8983
|
+
new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.');
|
|
8984
|
+
if (reader == null) {
|
|
8660
8985
|
// Only abort via the abort controller if the request has not resolved yet
|
|
8661
|
-
controller.abort(
|
|
8662
|
-
|
|
8986
|
+
controller.abort(reason);
|
|
8987
|
+
}
|
|
8988
|
+
else {
|
|
8989
|
+
reader.cancel(reason).catch(() => {
|
|
8990
|
+
// Cancelling the reader might rethrow an exception we would have handled by throwing in next(). So we can
|
|
8991
|
+
// ignore it here.
|
|
8992
|
+
});
|
|
8663
8993
|
}
|
|
8664
8994
|
});
|
|
8665
|
-
|
|
8666
|
-
|
|
8667
|
-
|
|
8668
|
-
|
|
8669
|
-
|
|
8670
|
-
|
|
8671
|
-
|
|
8672
|
-
|
|
8673
|
-
|
|
8995
|
+
let res;
|
|
8996
|
+
let responseIsBson = false;
|
|
8997
|
+
try {
|
|
8998
|
+
const ndJson = 'application/x-ndjson';
|
|
8999
|
+
const bson = 'application/vnd.powersync.bson-stream';
|
|
9000
|
+
res = await this.fetch(request.url, {
|
|
9001
|
+
method: 'POST',
|
|
9002
|
+
headers: {
|
|
9003
|
+
...headers,
|
|
9004
|
+
...request.headers,
|
|
9005
|
+
accept: this.supportsStreamingBinaryResponses ? `${bson};q=0.9,${ndJson};q=0.8` : ndJson
|
|
9006
|
+
},
|
|
9007
|
+
body: JSON.stringify(data),
|
|
9008
|
+
signal: controller.signal,
|
|
9009
|
+
cache: 'no-store',
|
|
9010
|
+
...(this.options.fetchOptions ?? {}),
|
|
9011
|
+
...options.fetchOptions
|
|
9012
|
+
});
|
|
9013
|
+
if (!res.ok || !res.body) {
|
|
9014
|
+
const text = await res.text();
|
|
9015
|
+
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
9016
|
+
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
9017
|
+
error.status = res.status;
|
|
9018
|
+
throw error;
|
|
9019
|
+
}
|
|
9020
|
+
const contentType = res.headers.get('content-type');
|
|
9021
|
+
responseIsBson = contentType == bson;
|
|
9022
|
+
}
|
|
9023
|
+
catch (ex) {
|
|
8674
9024
|
if (ex.name == 'AbortError') {
|
|
8675
9025
|
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
|
|
8676
9026
|
}
|
|
8677
9027
|
throw ex;
|
|
8678
|
-
});
|
|
8679
|
-
if (!res) {
|
|
8680
|
-
throw new Error('Fetch request was aborted');
|
|
8681
|
-
}
|
|
8682
|
-
requestResolved = true;
|
|
8683
|
-
if (!res.ok || !res.body) {
|
|
8684
|
-
const text = await res.text();
|
|
8685
|
-
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
8686
|
-
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
8687
|
-
error.status = res.status;
|
|
8688
|
-
throw error;
|
|
8689
9028
|
}
|
|
8690
|
-
|
|
8691
|
-
|
|
8692
|
-
|
|
8693
|
-
|
|
8694
|
-
|
|
8695
|
-
const closeReader = async () => {
|
|
8696
|
-
try {
|
|
8697
|
-
readerReleased = true;
|
|
8698
|
-
await reader.cancel();
|
|
8699
|
-
}
|
|
8700
|
-
catch (ex) {
|
|
8701
|
-
// an error will throw if the reader hasn't been used yet
|
|
8702
|
-
}
|
|
8703
|
-
reader.releaseLock();
|
|
8704
|
-
};
|
|
8705
|
-
const stream = new DataStream({
|
|
8706
|
-
logger: this.logger,
|
|
8707
|
-
mapLine: mapLine,
|
|
8708
|
-
pressure: {
|
|
8709
|
-
highWaterMark: 20,
|
|
8710
|
-
lowWaterMark: 10
|
|
8711
|
-
}
|
|
8712
|
-
});
|
|
8713
|
-
abortSignal?.addEventListener('abort', () => {
|
|
8714
|
-
closeReader();
|
|
8715
|
-
stream.close();
|
|
8716
|
-
});
|
|
8717
|
-
const decoder = this.createTextDecoder();
|
|
8718
|
-
let buffer = '';
|
|
8719
|
-
const consumeStream = async () => {
|
|
8720
|
-
while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
|
|
8721
|
-
const { done, value } = await reader.read();
|
|
8722
|
-
if (done) {
|
|
8723
|
-
const remaining = buffer.trim();
|
|
8724
|
-
if (remaining.length != 0) {
|
|
8725
|
-
stream.enqueueData(remaining);
|
|
8726
|
-
}
|
|
8727
|
-
stream.close();
|
|
8728
|
-
await closeReader();
|
|
8729
|
-
return;
|
|
9029
|
+
reader = res.body.getReader();
|
|
9030
|
+
const stream = {
|
|
9031
|
+
next: async () => {
|
|
9032
|
+
if (controller.signal.aborted) {
|
|
9033
|
+
return doneResult;
|
|
8730
9034
|
}
|
|
8731
|
-
|
|
8732
|
-
|
|
8733
|
-
const lines = buffer.split('\n');
|
|
8734
|
-
for (var i = 0; i < lines.length - 1; i++) {
|
|
8735
|
-
var l = lines[i].trim();
|
|
8736
|
-
if (l.length > 0) {
|
|
8737
|
-
stream.enqueueData(l);
|
|
8738
|
-
}
|
|
9035
|
+
try {
|
|
9036
|
+
return await reader.read();
|
|
8739
9037
|
}
|
|
8740
|
-
|
|
8741
|
-
|
|
8742
|
-
|
|
8743
|
-
|
|
8744
|
-
|
|
8745
|
-
|
|
8746
|
-
|
|
8747
|
-
dispose();
|
|
8748
|
-
},
|
|
8749
|
-
closed: () => {
|
|
8750
|
-
resolve();
|
|
8751
|
-
dispose();
|
|
8752
|
-
}
|
|
8753
|
-
});
|
|
8754
|
-
});
|
|
9038
|
+
catch (ex) {
|
|
9039
|
+
if (controller.signal.aborted) {
|
|
9040
|
+
// .read() completes with an error if we cancel the reader, which we do to disconnect. So this is just
|
|
9041
|
+
// things working as intended, we can return a done event and consider the exception handled.
|
|
9042
|
+
return doneResult;
|
|
9043
|
+
}
|
|
9044
|
+
throw ex;
|
|
8755
9045
|
}
|
|
8756
9046
|
}
|
|
8757
9047
|
};
|
|
8758
|
-
|
|
8759
|
-
|
|
8760
|
-
|
|
8761
|
-
|
|
8762
|
-
|
|
8763
|
-
|
|
8764
|
-
|
|
8765
|
-
|
|
9048
|
+
return { isBson: responseIsBson, stream };
|
|
9049
|
+
}
|
|
9050
|
+
/**
|
|
9051
|
+
* Posts a `/sync/stream` request.
|
|
9052
|
+
*
|
|
9053
|
+
* Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
|
|
9054
|
+
* {@link Uint8Array}s.
|
|
9055
|
+
*/
|
|
9056
|
+
async fetchStream(options) {
|
|
9057
|
+
const { isBson, stream } = await this.fetchStreamRaw(options);
|
|
9058
|
+
if (isBson) {
|
|
9059
|
+
return extractBsonObjects(stream);
|
|
9060
|
+
}
|
|
9061
|
+
else {
|
|
9062
|
+
return extractJsonLines(stream, this.createTextDecoder());
|
|
9063
|
+
}
|
|
8766
9064
|
}
|
|
8767
9065
|
}
|
|
8768
9066
|
|
|
@@ -8897,6 +9195,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
8897
9195
|
streamingSyncPromise;
|
|
8898
9196
|
logger;
|
|
8899
9197
|
activeStreams;
|
|
9198
|
+
connectionMayHaveChanged = false;
|
|
8900
9199
|
isUploadingCrud = false;
|
|
8901
9200
|
notifyCompletedUploads;
|
|
8902
9201
|
handleActiveStreamsChange;
|
|
@@ -9176,6 +9475,11 @@ The next upload iteration will be delayed.`);
|
|
|
9176
9475
|
shouldDelayRetry = false;
|
|
9177
9476
|
// A disconnect was requested, we should not delay since there is no explicit retry
|
|
9178
9477
|
}
|
|
9478
|
+
else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
|
|
9479
|
+
this.connectionMayHaveChanged = false;
|
|
9480
|
+
this.logger.info('Sync error after changed connection, retrying immediately');
|
|
9481
|
+
shouldDelayRetry = false;
|
|
9482
|
+
}
|
|
9179
9483
|
else {
|
|
9180
9484
|
this.logger.error(ex);
|
|
9181
9485
|
}
|
|
@@ -9206,6 +9510,15 @@ The next upload iteration will be delayed.`);
|
|
|
9206
9510
|
// Mark as disconnected if here
|
|
9207
9511
|
this.updateSyncStatus({ connected: false, connecting: false });
|
|
9208
9512
|
}
|
|
9513
|
+
markConnectionMayHaveChanged() {
|
|
9514
|
+
// By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
|
|
9515
|
+
// an active sync iteration on the connection in use.
|
|
9516
|
+
this.connectionMayHaveChanged = true;
|
|
9517
|
+
// This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
|
|
9518
|
+
// make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
|
|
9519
|
+
// iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
|
|
9520
|
+
this.handleActiveStreamsChange?.();
|
|
9521
|
+
}
|
|
9209
9522
|
async collectLocalBucketState() {
|
|
9210
9523
|
const bucketEntries = await this.options.adapter.getBucketStates();
|
|
9211
9524
|
const req = bucketEntries.map((entry) => ({
|
|
@@ -9270,6 +9583,19 @@ The next upload iteration will be delayed.`);
|
|
|
9270
9583
|
}
|
|
9271
9584
|
});
|
|
9272
9585
|
}
|
|
9586
|
+
async receiveSyncLines(data) {
|
|
9587
|
+
const { options, connection, bson } = data;
|
|
9588
|
+
const remote = this.options.remote;
|
|
9589
|
+
if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
|
|
9590
|
+
return await remote.fetchStream(options);
|
|
9591
|
+
}
|
|
9592
|
+
else {
|
|
9593
|
+
return await this.options.remote.socketStreamRaw({
|
|
9594
|
+
...options,
|
|
9595
|
+
...{ fetchStrategy: connection.fetchStrategy }
|
|
9596
|
+
}, bson);
|
|
9597
|
+
}
|
|
9598
|
+
}
|
|
9273
9599
|
async legacyStreamingSyncIteration(signal, resolvedOptions) {
|
|
9274
9600
|
const rawTables = resolvedOptions.serializedSchema?.raw_tables;
|
|
9275
9601
|
if (rawTables != null && rawTables.length) {
|
|
@@ -9299,42 +9625,27 @@ The next upload iteration will be delayed.`);
|
|
|
9299
9625
|
client_id: clientId
|
|
9300
9626
|
}
|
|
9301
9627
|
};
|
|
9302
|
-
|
|
9303
|
-
|
|
9304
|
-
|
|
9305
|
-
|
|
9306
|
-
|
|
9307
|
-
|
|
9308
|
-
|
|
9309
|
-
|
|
9310
|
-
|
|
9311
|
-
|
|
9312
|
-
|
|
9313
|
-
|
|
9314
|
-
|
|
9315
|
-
|
|
9316
|
-
stream = await this.options.remote.socketStreamRaw({
|
|
9317
|
-
...syncOptions,
|
|
9318
|
-
...{ fetchStrategy: resolvedOptions.fetchStrategy }
|
|
9319
|
-
}, (payload) => {
|
|
9320
|
-
if (payload instanceof Uint8Array) {
|
|
9321
|
-
return bson.deserialize(payload);
|
|
9322
|
-
}
|
|
9323
|
-
else {
|
|
9324
|
-
// Directly enqueued by us
|
|
9325
|
-
return payload;
|
|
9326
|
-
}
|
|
9327
|
-
}, bson);
|
|
9328
|
-
}
|
|
9628
|
+
const bson = await this.options.remote.getBSON();
|
|
9629
|
+
const source = await this.receiveSyncLines({
|
|
9630
|
+
options: syncOptions,
|
|
9631
|
+
connection: resolvedOptions,
|
|
9632
|
+
bson
|
|
9633
|
+
});
|
|
9634
|
+
const stream = injectable(map(source, (line) => {
|
|
9635
|
+
if (typeof line == 'string') {
|
|
9636
|
+
return JSON.parse(line);
|
|
9637
|
+
}
|
|
9638
|
+
else {
|
|
9639
|
+
return bson.deserialize(line);
|
|
9640
|
+
}
|
|
9641
|
+
}));
|
|
9329
9642
|
this.logger.debug('Stream established. Processing events');
|
|
9330
9643
|
this.notifyCompletedUploads = () => {
|
|
9331
|
-
|
|
9332
|
-
stream.enqueueData({ crud_upload_completed: null });
|
|
9333
|
-
}
|
|
9644
|
+
stream.inject({ crud_upload_completed: null });
|
|
9334
9645
|
};
|
|
9335
|
-
while (
|
|
9336
|
-
const line = await stream.
|
|
9337
|
-
if (
|
|
9646
|
+
while (true) {
|
|
9647
|
+
const { value: line, done } = await stream.next();
|
|
9648
|
+
if (done) {
|
|
9338
9649
|
// The stream has closed while waiting
|
|
9339
9650
|
return;
|
|
9340
9651
|
}
|
|
@@ -9513,14 +9824,17 @@ The next upload iteration will be delayed.`);
|
|
|
9513
9824
|
const syncImplementation = this;
|
|
9514
9825
|
const adapter = this.options.adapter;
|
|
9515
9826
|
const remote = this.options.remote;
|
|
9827
|
+
const controller = new AbortController();
|
|
9828
|
+
const abort = () => {
|
|
9829
|
+
return controller.abort(signal.reason);
|
|
9830
|
+
};
|
|
9831
|
+
signal.addEventListener('abort', abort);
|
|
9516
9832
|
let receivingLines = null;
|
|
9517
9833
|
let hadSyncLine = false;
|
|
9518
9834
|
let hideDisconnectOnRestart = false;
|
|
9519
9835
|
if (signal.aborted) {
|
|
9520
9836
|
throw new AbortOperation('Connection request has been aborted');
|
|
9521
9837
|
}
|
|
9522
|
-
const abortController = new AbortController();
|
|
9523
|
-
signal.addEventListener('abort', () => abortController.abort());
|
|
9524
9838
|
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
|
|
9525
9839
|
// invocation (local events include refreshed tokens and completed uploads).
|
|
9526
9840
|
// This is a single data stream so that we can handle all control calls from a single place.
|
|
@@ -9528,49 +9842,36 @@ The next upload iteration will be delayed.`);
|
|
|
9528
9842
|
async function connect(instr) {
|
|
9529
9843
|
const syncOptions = {
|
|
9530
9844
|
path: '/sync/stream',
|
|
9531
|
-
abortSignal:
|
|
9845
|
+
abortSignal: controller.signal,
|
|
9532
9846
|
data: instr.request
|
|
9533
9847
|
};
|
|
9534
|
-
|
|
9535
|
-
|
|
9536
|
-
|
|
9537
|
-
|
|
9538
|
-
|
|
9539
|
-
|
|
9540
|
-
|
|
9541
|
-
|
|
9542
|
-
|
|
9543
|
-
|
|
9544
|
-
|
|
9545
|
-
|
|
9546
|
-
|
|
9547
|
-
|
|
9548
|
-
|
|
9549
|
-
|
|
9550
|
-
|
|
9551
|
-
fetchStrategy: resolvedOptions.fetchStrategy
|
|
9552
|
-
}, (payload) => {
|
|
9553
|
-
if (payload instanceof Uint8Array) {
|
|
9554
|
-
return {
|
|
9555
|
-
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
9556
|
-
payload: payload
|
|
9557
|
-
};
|
|
9558
|
-
}
|
|
9559
|
-
else {
|
|
9560
|
-
// Directly enqueued by us
|
|
9561
|
-
return payload;
|
|
9562
|
-
}
|
|
9563
|
-
});
|
|
9564
|
-
}
|
|
9848
|
+
controlInvocations = injectable(map(await syncImplementation.receiveSyncLines({
|
|
9849
|
+
options: syncOptions,
|
|
9850
|
+
connection: resolvedOptions
|
|
9851
|
+
}), (line) => {
|
|
9852
|
+
if (typeof line == 'string') {
|
|
9853
|
+
return {
|
|
9854
|
+
command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
|
|
9855
|
+
payload: line
|
|
9856
|
+
};
|
|
9857
|
+
}
|
|
9858
|
+
else {
|
|
9859
|
+
return {
|
|
9860
|
+
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
|
|
9861
|
+
payload: line
|
|
9862
|
+
};
|
|
9863
|
+
}
|
|
9864
|
+
}));
|
|
9565
9865
|
// The rust client will set connected: true after the first sync line because that's when it gets invoked, but
|
|
9566
9866
|
// we're already connected here and can report that.
|
|
9567
9867
|
syncImplementation.updateSyncStatus({ connected: true });
|
|
9568
9868
|
try {
|
|
9569
|
-
while (
|
|
9570
|
-
|
|
9571
|
-
if (
|
|
9572
|
-
|
|
9869
|
+
while (true) {
|
|
9870
|
+
let event = await controlInvocations.next();
|
|
9871
|
+
if (event.done) {
|
|
9872
|
+
break;
|
|
9573
9873
|
}
|
|
9874
|
+
const line = event.value;
|
|
9574
9875
|
await control(line.command, line.payload);
|
|
9575
9876
|
if (!hadSyncLine) {
|
|
9576
9877
|
syncImplementation.triggerCrudUpload();
|
|
@@ -9579,12 +9880,8 @@ The next upload iteration will be delayed.`);
|
|
|
9579
9880
|
}
|
|
9580
9881
|
}
|
|
9581
9882
|
finally {
|
|
9582
|
-
|
|
9583
|
-
|
|
9584
|
-
// refreshed. That would throw after closing (and we can't handle those events either way), so set this back
|
|
9585
|
-
// to null.
|
|
9586
|
-
controlInvocations = null;
|
|
9587
|
-
await activeInstructions.close();
|
|
9883
|
+
abort();
|
|
9884
|
+
signal.removeEventListener('abort', abort);
|
|
9588
9885
|
}
|
|
9589
9886
|
}
|
|
9590
9887
|
async function stop() {
|
|
@@ -9594,6 +9891,10 @@ The next upload iteration will be delayed.`);
|
|
|
9594
9891
|
const rawResponse = await adapter.control(op, payload ?? null);
|
|
9595
9892
|
const logger = syncImplementation.logger;
|
|
9596
9893
|
logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
|
|
9894
|
+
if (op != PowerSyncControlCommand.STOP) {
|
|
9895
|
+
// Evidently we have a working connection here, otherwise powersync_control would have failed.
|
|
9896
|
+
syncImplementation.connectionMayHaveChanged = false;
|
|
9897
|
+
}
|
|
9597
9898
|
await handleInstructions(JSON.parse(rawResponse));
|
|
9598
9899
|
}
|
|
9599
9900
|
async function handleInstruction(instruction) {
|
|
@@ -9628,14 +9929,14 @@ The next upload iteration will be delayed.`);
|
|
|
9628
9929
|
remote.invalidateCredentials();
|
|
9629
9930
|
// Restart iteration after the credentials have been refreshed.
|
|
9630
9931
|
remote.fetchCredentials().then((_) => {
|
|
9631
|
-
controlInvocations?.
|
|
9932
|
+
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
|
|
9632
9933
|
}, (err) => {
|
|
9633
9934
|
syncImplementation.logger.warn('Could not prefetch credentials', err);
|
|
9634
9935
|
});
|
|
9635
9936
|
}
|
|
9636
9937
|
}
|
|
9637
9938
|
else if ('CloseSyncStream' in instruction) {
|
|
9638
|
-
|
|
9939
|
+
controller.abort();
|
|
9639
9940
|
hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
|
|
9640
9941
|
}
|
|
9641
9942
|
else if ('FlushFileSystem' in instruction) ;
|
|
@@ -9664,17 +9965,13 @@ The next upload iteration will be delayed.`);
|
|
|
9664
9965
|
}
|
|
9665
9966
|
await control(PowerSyncControlCommand.START, JSON.stringify(options));
|
|
9666
9967
|
this.notifyCompletedUploads = () => {
|
|
9667
|
-
|
|
9668
|
-
controlInvocations.enqueueData({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
9669
|
-
}
|
|
9968
|
+
controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
|
|
9670
9969
|
};
|
|
9671
9970
|
this.handleActiveStreamsChange = () => {
|
|
9672
|
-
|
|
9673
|
-
|
|
9674
|
-
|
|
9675
|
-
|
|
9676
|
-
});
|
|
9677
|
-
}
|
|
9971
|
+
controlInvocations?.inject({
|
|
9972
|
+
command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
|
|
9973
|
+
payload: JSON.stringify(this.activeStreams)
|
|
9974
|
+
});
|
|
9678
9975
|
};
|
|
9679
9976
|
await receivingLines;
|
|
9680
9977
|
}
|
|
@@ -10691,7 +10988,7 @@ class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
10691
10988
|
* @returns A transaction of CRUD operations to upload, or null if there are none
|
|
10692
10989
|
*/
|
|
10693
10990
|
async getNextCrudTransaction() {
|
|
10694
|
-
const iterator = this.getCrudTransactions()[
|
|
10991
|
+
const iterator = this.getCrudTransactions()[Symbol.asyncIterator]();
|
|
10695
10992
|
return (await iterator.next()).value;
|
|
10696
10993
|
}
|
|
10697
10994
|
/**
|
|
@@ -10727,7 +11024,7 @@ class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
10727
11024
|
*/
|
|
10728
11025
|
getCrudTransactions() {
|
|
10729
11026
|
return {
|
|
10730
|
-
[
|
|
11027
|
+
[Symbol.asyncIterator]: () => {
|
|
10731
11028
|
let lastCrudItemId = -1;
|
|
10732
11029
|
const sql = `
|
|
10733
11030
|
WITH RECURSIVE crud_entries AS (
|
|
@@ -10790,6 +11087,10 @@ SELECT * FROM crud_entries;
|
|
|
10790
11087
|
* Execute a SQL write (INSERT/UPDATE/DELETE) query
|
|
10791
11088
|
* and optionally return results.
|
|
10792
11089
|
*
|
|
11090
|
+
* When using the default client-side [JSON-based view system](https://docs.powersync.com/architecture/client-architecture#client-side-schema-and-sqlite-database-structure),
|
|
11091
|
+
* the returned result's `rowsAffected` may be `0` for successful `UPDATE` and `DELETE` statements.
|
|
11092
|
+
* Use a `RETURNING` clause and inspect `result.rows` when you need to confirm which rows changed.
|
|
11093
|
+
*
|
|
10793
11094
|
* @param sql The SQL query to execute
|
|
10794
11095
|
* @param parameters Optional array of parameters to bind to the query
|
|
10795
11096
|
* @returns The query result as an object with structured key-value pairs
|
|
@@ -10886,7 +11187,7 @@ SELECT * FROM crud_entries;
|
|
|
10886
11187
|
async readTransaction(callback, lockTimeout = DEFAULT_LOCK_TIMEOUT_MS) {
|
|
10887
11188
|
await this.waitForReady();
|
|
10888
11189
|
return this.database.readTransaction(async (tx) => {
|
|
10889
|
-
const res = await callback(
|
|
11190
|
+
const res = await callback(tx);
|
|
10890
11191
|
await tx.rollback();
|
|
10891
11192
|
return res;
|
|
10892
11193
|
}, { timeoutMs: lockTimeout });
|
|
@@ -11663,10 +11964,8 @@ class Schema {
|
|
|
11663
11964
|
* developer instead of automatically by PowerSync.
|
|
11664
11965
|
* Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
|
|
11665
11966
|
* using client-side table and column constraints.
|
|
11666
|
-
* Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
|
|
11667
11967
|
*
|
|
11668
11968
|
* @param tables An object of (table name, raw table definition) entries.
|
|
11669
|
-
* @experimental Note that the raw tables API is still experimental and may change in the future.
|
|
11670
11969
|
*/
|
|
11671
11970
|
withRawTables(tables) {
|
|
11672
11971
|
for (const [name, rawTableDefinition] of Object.entries(tables)) {
|
|
@@ -11863,5 +12162,5 @@ const parseQuery = (query, parameters) => {
|
|
|
11863
12162
|
return { sqlStatement, parameters: parameters };
|
|
11864
12163
|
};
|
|
11865
12164
|
|
|
11866
|
-
export { ATTACHMENT_TABLE, AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, AttachmentContext, AttachmentQueue, AttachmentService, AttachmentState, AttachmentTable, BaseObserver, Column, ColumnType, ConnectionClosedError, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, 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,
|
|
12165
|
+
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 };
|
|
11867
12166
|
//# sourceMappingURL=bundle.node.mjs.map
|