@powersync/common 1.52.0 → 1.53.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.
Files changed (76) hide show
  1. package/dist/bundle.cjs +161 -766
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +162 -755
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +161 -766
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +162 -755
  8. package/dist/bundle.node.mjs.map +1 -1
  9. package/dist/index.d.cts +39 -370
  10. package/legacy/sync_protocol.d.ts +103 -0
  11. package/lib/client/ConnectionManager.js +1 -1
  12. package/lib/client/ConnectionManager.js.map +1 -1
  13. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +6 -64
  14. package/lib/client/sync/bucket/BucketStorageAdapter.js +4 -0
  15. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -1
  16. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +1 -28
  17. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -162
  18. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -1
  19. package/lib/client/sync/stream/AbstractRemote.d.ts +2 -12
  20. package/lib/client/sync/stream/AbstractRemote.js +3 -13
  21. package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
  22. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +12 -35
  23. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +145 -424
  24. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
  25. package/lib/client/sync/stream/JsonValue.d.ts +7 -0
  26. package/lib/client/sync/stream/JsonValue.js +2 -0
  27. package/lib/client/sync/stream/JsonValue.js.map +1 -0
  28. package/lib/client/sync/stream/core-instruction.d.ts +14 -9
  29. package/lib/client/sync/stream/core-instruction.js +3 -0
  30. package/lib/client/sync/stream/core-instruction.js.map +1 -1
  31. package/lib/db/DBAdapter.d.ts +9 -0
  32. package/lib/db/DBAdapter.js +8 -1
  33. package/lib/db/DBAdapter.js.map +1 -1
  34. package/lib/db/crud/SyncStatus.d.ts +3 -4
  35. package/lib/db/crud/SyncStatus.js +0 -4
  36. package/lib/db/crud/SyncStatus.js.map +1 -1
  37. package/lib/db/schema/RawTable.d.ts +0 -5
  38. package/lib/db/schema/Schema.d.ts +0 -2
  39. package/lib/db/schema/Schema.js +0 -2
  40. package/lib/db/schema/Schema.js.map +1 -1
  41. package/lib/index.d.ts +1 -5
  42. package/lib/index.js +1 -5
  43. package/lib/index.js.map +1 -1
  44. package/package.json +9 -6
  45. package/src/client/ConnectionManager.ts +1 -1
  46. package/src/client/sync/bucket/BucketStorageAdapter.ts +6 -71
  47. package/src/client/sync/bucket/SqliteBucketStorage.ts +1 -197
  48. package/src/client/sync/stream/AbstractRemote.ts +5 -27
  49. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +168 -496
  50. package/src/client/sync/stream/JsonValue.ts +8 -0
  51. package/src/client/sync/stream/core-instruction.ts +15 -5
  52. package/src/db/DBAdapter.ts +20 -2
  53. package/src/db/crud/SyncStatus.ts +4 -5
  54. package/src/db/schema/RawTable.ts +0 -5
  55. package/src/db/schema/Schema.ts +0 -2
  56. package/src/index.ts +1 -5
  57. package/lib/client/sync/bucket/OpType.d.ts +0 -16
  58. package/lib/client/sync/bucket/OpType.js +0 -23
  59. package/lib/client/sync/bucket/OpType.js.map +0 -1
  60. package/lib/client/sync/bucket/OplogEntry.d.ts +0 -23
  61. package/lib/client/sync/bucket/OplogEntry.js +0 -36
  62. package/lib/client/sync/bucket/OplogEntry.js.map +0 -1
  63. package/lib/client/sync/bucket/SyncDataBatch.d.ts +0 -6
  64. package/lib/client/sync/bucket/SyncDataBatch.js +0 -12
  65. package/lib/client/sync/bucket/SyncDataBatch.js.map +0 -1
  66. package/lib/client/sync/bucket/SyncDataBucket.d.ts +0 -40
  67. package/lib/client/sync/bucket/SyncDataBucket.js +0 -40
  68. package/lib/client/sync/bucket/SyncDataBucket.js.map +0 -1
  69. package/lib/client/sync/stream/streaming-sync-types.d.ts +0 -143
  70. package/lib/client/sync/stream/streaming-sync-types.js +0 -26
  71. package/lib/client/sync/stream/streaming-sync-types.js.map +0 -1
  72. package/src/client/sync/bucket/OpType.ts +0 -23
  73. package/src/client/sync/bucket/OplogEntry.ts +0 -50
  74. package/src/client/sync/bucket/SyncDataBatch.ts +0 -11
  75. package/src/client/sync/bucket/SyncDataBucket.ts +0 -49
  76. package/src/client/sync/stream/streaming-sync-types.ts +0 -210
@@ -1817,8 +1817,15 @@ class BaseTransaction {
1817
1817
  class TransactionImplementation extends DBGetUtilsDefaultMixin(BaseTransaction) {
1818
1818
  static async runWith(ctx, fn) {
1819
1819
  let tx = new TransactionImplementation(ctx);
1820
+ // For write transactions, use BEGIN IMMEDIATE to immediately obtain a write lock on the database (instead of doing
1821
+ // that on the first statement). If we have a genuine read-only connection, we also use BEGIN IMMEDIATE there: In
1822
+ // WAL mode, that ensures we pin the current state of the database (instead of the state at the first statement in
1823
+ // the transaction). But if we have a "fake" read-only connection implemented through `pragma query_only = true`, we
1824
+ // can't use this trick because it would attempt to lock the connection. So there, we use a regular `BEGIN`
1825
+ // statement.
1826
+ const useBeginImmediate = ctx.connectionType != 'queryOnly';
1820
1827
  try {
1821
- await ctx.execute('BEGIN IMMEDIATE');
1828
+ await ctx.execute(useBeginImmediate ? 'BEGIN IMMEDIATE' : 'BEGIN');
1822
1829
  const result = await fn(tx);
1823
1830
  await tx.commit();
1824
1831
  return result;
@@ -1977,16 +1984,12 @@ class SyncStatus {
1977
1984
  *
1978
1985
  * This returns null when the database is currently being opened and we don't have reliable information about all
1979
1986
  * included streams yet.
1980
- *
1981
- * @experimental Sync streams are currently in alpha.
1982
1987
  */
1983
1988
  get syncStreams() {
1984
1989
  return this.options.dataFlow?.internalStreamSubscriptions?.map((core) => new SyncStreamStatusView(this, core));
1985
1990
  }
1986
1991
  /**
1987
1992
  * If the `stream` appears in {@link syncStreams}, returns the current status for that stream.
1988
- *
1989
- * @experimental Sync streams are currently in alpha.
1990
1993
  */
1991
1994
  forStream(stream) {
1992
1995
  const asJson = JSON.stringify(stream.parameters);
@@ -2569,7 +2572,7 @@ class SyncStreamSubscriptionHandle {
2569
2572
  constructor(subscription) {
2570
2573
  this.subscription = subscription;
2571
2574
  subscription.refcount++;
2572
- _finalizer?.register(this, subscription);
2575
+ _finalizer?.register(this, subscription, this);
2573
2576
  }
2574
2577
  get name() {
2575
2578
  return this.subscription.name;
@@ -3158,6 +3161,10 @@ var PowerSyncControlCommand;
3158
3161
  PowerSyncControlCommand["NOTIFY_TOKEN_REFRESHED"] = "refreshed_token";
3159
3162
  PowerSyncControlCommand["NOTIFY_CRUD_UPLOAD_COMPLETED"] = "completed_upload";
3160
3163
  PowerSyncControlCommand["UPDATE_SUBSCRIPTIONS"] = "update_subscriptions";
3164
+ /**
3165
+ * An `established` or `end` event for response streams.
3166
+ */
3167
+ PowerSyncControlCommand["CONNECTION_STATE"] = "connection";
3161
3168
  })(PowerSyncControlCommand || (PowerSyncControlCommand = {}));
3162
3169
 
3163
3170
  /**
@@ -3339,103 +3346,6 @@ class AbortOperation extends Error {
3339
3346
  }
3340
3347
  }
3341
3348
 
3342
- var OpTypeEnum;
3343
- (function (OpTypeEnum) {
3344
- OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
3345
- OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
3346
- OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
3347
- OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
3348
- })(OpTypeEnum || (OpTypeEnum = {}));
3349
- /**
3350
- * Used internally for sync buckets.
3351
- */
3352
- class OpType {
3353
- value;
3354
- static fromJSON(jsonValue) {
3355
- return new OpType(OpTypeEnum[jsonValue]);
3356
- }
3357
- constructor(value) {
3358
- this.value = value;
3359
- }
3360
- toJSON() {
3361
- return Object.entries(OpTypeEnum).find(([, value]) => value === this.value)[0];
3362
- }
3363
- }
3364
-
3365
- class OplogEntry {
3366
- op_id;
3367
- op;
3368
- checksum;
3369
- subkey;
3370
- object_type;
3371
- object_id;
3372
- data;
3373
- static fromRow(row) {
3374
- return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
3375
- }
3376
- constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
3377
- this.op_id = op_id;
3378
- this.op = op;
3379
- this.checksum = checksum;
3380
- this.subkey = subkey;
3381
- this.object_type = object_type;
3382
- this.object_id = object_id;
3383
- this.data = data;
3384
- }
3385
- toJSON(fixedKeyEncoding = false) {
3386
- return {
3387
- op_id: this.op_id,
3388
- op: this.op.toJSON(),
3389
- object_type: this.object_type,
3390
- object_id: this.object_id,
3391
- checksum: this.checksum,
3392
- data: this.data,
3393
- // Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
3394
- // but we need to migrate gradually to not break existing databases.
3395
- subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
3396
- };
3397
- }
3398
- }
3399
-
3400
- class SyncDataBucket {
3401
- bucket;
3402
- data;
3403
- has_more;
3404
- after;
3405
- next_after;
3406
- static fromRow(row) {
3407
- return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
3408
- }
3409
- constructor(bucket, data,
3410
- /**
3411
- * True if the response does not contain all the data for this bucket, and another request must be made.
3412
- */
3413
- has_more,
3414
- /**
3415
- * The `after` specified in the request.
3416
- */
3417
- after,
3418
- /**
3419
- * Use this for the next request.
3420
- */
3421
- next_after) {
3422
- this.bucket = bucket;
3423
- this.data = data;
3424
- this.has_more = has_more;
3425
- this.after = after;
3426
- this.next_after = next_after;
3427
- }
3428
- toJSON(fixedKeyEncoding = false) {
3429
- return {
3430
- bucket: this.bucket,
3431
- has_more: this.has_more,
3432
- after: this.after,
3433
- next_after: this.next_after,
3434
- data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
3435
- };
3436
- }
3437
- }
3438
-
3439
3349
  var dist = {};
3440
3350
 
3441
3351
  var Codecs = {};
@@ -8220,7 +8130,7 @@ function requireDist () {
8220
8130
 
8221
8131
  var distExports = requireDist();
8222
8132
 
8223
- var version = "1.52.0";
8133
+ var version = "1.53.0";
8224
8134
  var PACKAGE = {
8225
8135
  version: version};
8226
8136
 
@@ -8390,22 +8300,6 @@ const doneResult = { done: true, value: undefined };
8390
8300
  function valueResult(value) {
8391
8301
  return { done: false, value };
8392
8302
  }
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
8303
  /**
8410
8304
  * Expands a source async iterator by allowing to inject events asynchronously.
8411
8305
  *
@@ -8777,22 +8671,12 @@ class AbstractRemote {
8777
8671
  * Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
8778
8672
  *
8779
8673
  * The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
8780
- *
8781
- * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
8782
- * (required for compatibility with older sync services).
8783
8674
  */
8784
- async socketStreamRaw(options, bson) {
8675
+ async socketStreamRaw(options) {
8785
8676
  const { path, fetchStrategy = FetchStrategy.Buffered } = options;
8786
- const mimeType = bson == null ? 'application/json' : 'application/bson';
8677
+ const mimeType = 'application/json';
8787
8678
  function toBuffer(js) {
8788
- let contents;
8789
- if (bson != null) {
8790
- contents = bson.serialize(js);
8791
- }
8792
- else {
8793
- contents = JSON.stringify(js);
8794
- }
8795
- return Buffer.from(contents);
8679
+ return Buffer.from(JSON.stringify(js));
8796
8680
  }
8797
8681
  const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
8798
8682
  const request = await this.buildRequest(path);
@@ -9093,31 +8977,8 @@ function coreStatusToJs(status) {
9093
8977
  priorityStatusEntries: status.priority_status.map(priorityToJs)
9094
8978
  };
9095
8979
  }
9096
-
9097
- function isStreamingSyncData(line) {
9098
- return line.data != null;
9099
- }
9100
- function isStreamingKeepalive(line) {
9101
- return line.token_expires_in != null;
9102
- }
9103
- function isStreamingSyncCheckpoint(line) {
9104
- return line.checkpoint != null;
9105
- }
9106
- function isStreamingSyncCheckpointComplete(line) {
9107
- return line.checkpoint_complete != null;
9108
- }
9109
- function isStreamingSyncCheckpointPartiallyComplete(line) {
9110
- return line.partial_checkpoint_complete != null;
9111
- }
9112
- function isStreamingSyncCheckpointDiff(line) {
9113
- return line.checkpoint_diff != null;
9114
- }
9115
- function isContinueCheckpointRequest(request) {
9116
- return (Array.isArray(request.buckets) &&
9117
- typeof request.checkpoint_token == 'string');
9118
- }
9119
- function isSyncNewCheckpointRequest(request) {
9120
- return typeof request.request_checkpoint == 'object';
8980
+ function isInterruptingInstruction(instruction) {
8981
+ return 'EstablishSyncStream' in instruction || 'CloseSyncStream' in instruction;
9121
8982
  }
9122
8983
 
9123
8984
  var LockType;
@@ -9132,35 +8993,21 @@ var SyncStreamConnectionMethod;
9132
8993
  })(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
9133
8994
  var SyncClientImplementation;
9134
8995
  (function (SyncClientImplementation) {
9135
- /**
9136
- * Decodes and handles sync lines received from the sync service in JavaScript.
9137
- *
9138
- * This is the default option.
9139
- *
9140
- * @deprecated We recommend the {@link RUST} client implementation for all apps. If you have issues with
9141
- * the Rust client, please file an issue or reach out to us. The JavaScript client will be removed in a future
9142
- * version of the PowerSync SDK.
9143
- */
9144
- SyncClientImplementation["JAVASCRIPT"] = "js";
9145
8996
  /**
9146
8997
  * This implementation offloads the sync line decoding and handling into the PowerSync
9147
8998
  * core extension.
9148
8999
  *
9149
- * This option is more performant than the {@link JAVASCRIPT} client, enabled by default and the
9150
- * recommended client implementation for all apps.
9000
+ * This is the only option, as an older JavaScript client implementation has been removed from the SDK.
9151
9001
  *
9152
9002
  * ## Compatibility warning
9153
9003
  *
9154
9004
  * The Rust sync client stores sync data in a format that is slightly different than the one used
9155
- * by the old {@link JAVASCRIPT} implementation. When adopting the {@link RUST} client on existing
9156
- * databases, the PowerSync SDK will migrate the format automatically.
9157
- * Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
9158
- * the version introducing {@link RUST} as an option) also supports the new format, so you can switch
9159
- * back to {@link JAVASCRIPT} later.
9005
+ * by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
9006
+ * migrate the format automatically.
9160
9007
  *
9161
- * __However__: Upgrading the SDK version, then adopting {@link RUST} as a sync client and later
9162
- * downgrading the SDK to an older version (necessarily using the JavaScript-based implementation then)
9163
- * can lead to sync issues.
9008
+ * SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
9009
+ * implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
9010
+ * possible anymore. Problematic SDK versions have been released before 2025-06-09.
9164
9011
  */
9165
9012
  SyncClientImplementation["RUST"] = "rust";
9166
9013
  })(SyncClientImplementation || (SyncClientImplementation = {}));
@@ -9183,13 +9030,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
9183
9030
  serializedSchema: undefined,
9184
9031
  includeDefaultStreams: true
9185
9032
  };
9186
- // The priority we assume when we receive checkpoint lines where no priority is set.
9187
- // This is the default priority used by the sync service, but can be set to an arbitrary
9188
- // value since sync services without priorities also won't send partial sync completion
9189
- // messages.
9190
- const FALLBACK_PRIORITY = 3;
9191
9033
  class AbstractStreamingSyncImplementation extends BaseObserver {
9192
- _lastSyncedAt;
9193
9034
  options;
9194
9035
  abortController;
9195
9036
  // In rare cases, mostly for tests, uploads can be triggered without being properly connected.
@@ -9199,6 +9040,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
9199
9040
  streamingSyncPromise;
9200
9041
  logger;
9201
9042
  activeStreams;
9043
+ connectionMayHaveChanged = false;
9202
9044
  isUploadingCrud = false;
9203
9045
  notifyCompletedUploads;
9204
9046
  handleActiveStreamsChange;
@@ -9278,9 +9120,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
9278
9120
  this.crudUpdateListener = undefined;
9279
9121
  this.uploadAbortController?.abort();
9280
9122
  }
9281
- async hasCompletedSync() {
9282
- return this.options.adapter.hasCompletedSync();
9283
- }
9284
9123
  async getWriteCheckpoint() {
9285
9124
  const clientId = await this.options.adapter.getClientId();
9286
9125
  let path = `/write-checkpoint2.json?client_id=${clientId}`;
@@ -9362,7 +9201,7 @@ The next upload iteration will be delayed.`);
9362
9201
  });
9363
9202
  }
9364
9203
  }
9365
- this.uploadAbortController = null;
9204
+ this.uploadAbortController = undefined;
9366
9205
  }
9367
9206
  });
9368
9207
  }
@@ -9478,6 +9317,11 @@ The next upload iteration will be delayed.`);
9478
9317
  shouldDelayRetry = false;
9479
9318
  // A disconnect was requested, we should not delay since there is no explicit retry
9480
9319
  }
9320
+ else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
9321
+ this.connectionMayHaveChanged = false;
9322
+ this.logger.info('Sync error after changed connection, retrying immediately');
9323
+ shouldDelayRetry = false;
9324
+ }
9481
9325
  else {
9482
9326
  this.logger.error(ex);
9483
9327
  }
@@ -9508,17 +9352,14 @@ The next upload iteration will be delayed.`);
9508
9352
  // Mark as disconnected if here
9509
9353
  this.updateSyncStatus({ connected: false, connecting: false });
9510
9354
  }
9511
- async collectLocalBucketState() {
9512
- const bucketEntries = await this.options.adapter.getBucketStates();
9513
- const req = bucketEntries.map((entry) => ({
9514
- name: entry.bucket,
9515
- after: entry.op_id
9516
- }));
9517
- const localDescriptions = new Map();
9518
- for (const entry of bucketEntries) {
9519
- localDescriptions.set(entry.bucket, null);
9520
- }
9521
- return [req, localDescriptions];
9355
+ markConnectionMayHaveChanged() {
9356
+ // By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
9357
+ // an active sync iteration on the connection in use.
9358
+ this.connectionMayHaveChanged = true;
9359
+ // This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
9360
+ // make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
9361
+ // iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
9362
+ this.handleActiveStreamsChange?.();
9522
9363
  }
9523
9364
  /**
9524
9365
  * Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
@@ -9559,328 +9400,98 @@ The next upload iteration will be delayed.`);
9559
9400
  if (invalidMetadata.length > 0) {
9560
9401
  throw new Error(`Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`);
9561
9402
  }
9562
- const clientImplementation = resolvedOptions.clientImplementation;
9563
- this.updateSyncStatus({ clientImplementation });
9564
- if (clientImplementation == SyncClientImplementation.JAVASCRIPT) {
9565
- await this.legacyStreamingSyncIteration(signal, resolvedOptions);
9566
- return null;
9567
- }
9568
- else {
9569
- await this.requireKeyFormat(true);
9570
- return await this.rustSyncIteration(signal, resolvedOptions);
9571
- }
9403
+ await this.requireKeyFormat(true);
9404
+ return await this.rustSyncIteration(signal, resolvedOptions);
9572
9405
  }
9573
9406
  });
9574
9407
  }
9575
- async receiveSyncLines(data) {
9576
- const { options, connection, bson } = data;
9408
+ receiveSyncLines(data) {
9409
+ const { options, connection } = data;
9577
9410
  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
- }
9588
- async legacyStreamingSyncIteration(signal, resolvedOptions) {
9589
- const rawTables = resolvedOptions.serializedSchema?.raw_tables;
9590
- if (rawTables != null && rawTables.length) {
9591
- this.logger.warn('Raw tables require the Rust-based sync client. The JS client will ignore them.');
9592
- }
9593
- if (this.activeStreams.length) {
9594
- this.logger.error('Sync streams require `clientImplementation: SyncClientImplementation.RUST` when connecting.');
9595
- }
9596
- this.logger.debug('Streaming sync iteration started');
9597
- this.options.adapter.startSession();
9598
- let [req, bucketMap] = await this.collectLocalBucketState();
9599
- let targetCheckpoint = null;
9600
- // A checkpoint that has been validated but not applied (e.g. due to pending local writes)
9601
- let pendingValidatedCheckpoint = null;
9602
- const clientId = await this.options.adapter.getClientId();
9603
- const usingFixedKeyFormat = await this.requireKeyFormat(false);
9604
- this.logger.debug('Requesting stream from server');
9605
- const syncOptions = {
9606
- path: '/sync/stream',
9607
- abortSignal: signal,
9608
- data: {
9609
- buckets: req,
9610
- include_checksum: true,
9611
- raw_data: true,
9612
- parameters: resolvedOptions.params,
9613
- app_metadata: resolvedOptions.appMetadata,
9614
- client_id: clientId
9615
- }
9616
- };
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);
9411
+ const openInner = async () => {
9412
+ if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
9413
+ return await remote.fetchStream(options);
9626
9414
  }
9627
9415
  else {
9628
- return bson.deserialize(line);
9416
+ return await this.options.remote.socketStreamRaw({
9417
+ ...options,
9418
+ ...{ fetchStrategy: connection.fetchStrategy }
9419
+ });
9629
9420
  }
9630
- }));
9631
- this.logger.debug('Stream established. Processing events');
9632
- this.notifyCompletedUploads = () => {
9633
- stream.inject({ crud_upload_completed: null });
9634
9421
  };
9635
- while (true) {
9636
- const { value: line, done } = await stream.next();
9637
- if (done) {
9638
- // The stream has closed while waiting
9639
- return;
9640
- }
9641
- if ('crud_upload_completed' in line) {
9642
- if (pendingValidatedCheckpoint != null) {
9643
- const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
9644
- if (applied) {
9645
- pendingValidatedCheckpoint = null;
9646
- }
9647
- else if (endIteration) {
9648
- break;
9649
- }
9422
+ let inner;
9423
+ let done = false;
9424
+ return {
9425
+ async next() {
9426
+ if (done) {
9427
+ return doneResult;
9650
9428
  }
9651
- continue;
9652
- }
9653
- // A connection is active and messages are being received
9654
- if (!this.syncStatus.connected) {
9655
- // There is a connection now
9656
- Promise.resolve().then(() => this.triggerCrudUpload());
9657
- this.updateSyncStatus({
9658
- connected: true
9659
- });
9660
- }
9661
- if (isStreamingSyncCheckpoint(line)) {
9662
- targetCheckpoint = line.checkpoint;
9663
- // New checkpoint - existing validated checkpoint is no longer valid
9664
- pendingValidatedCheckpoint = null;
9665
- const bucketsToDelete = new Set(bucketMap.keys());
9666
- const newBuckets = new Map();
9667
- for (const checksum of line.checkpoint.buckets) {
9668
- newBuckets.set(checksum.bucket, {
9669
- name: checksum.bucket,
9670
- priority: checksum.priority ?? FALLBACK_PRIORITY
9429
+ else if (inner == null) {
9430
+ inner = await openInner();
9431
+ // We're connected here, so we can tell the core extension about it.
9432
+ return valueResult({
9433
+ command: PowerSyncControlCommand.CONNECTION_STATE,
9434
+ payload: 'established'
9671
9435
  });
9672
- bucketsToDelete.delete(checksum.bucket);
9673
- }
9674
- if (bucketsToDelete.size > 0) {
9675
- this.logger.debug('Removing buckets', [...bucketsToDelete]);
9676
- }
9677
- bucketMap = newBuckets;
9678
- await this.options.adapter.removeBuckets([...bucketsToDelete]);
9679
- await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
9680
- await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
9681
- }
9682
- else if (isStreamingSyncCheckpointComplete(line)) {
9683
- const result = await this.applyCheckpoint(targetCheckpoint);
9684
- if (result.endIteration) {
9685
- return;
9686
- }
9687
- else if (!result.applied) {
9688
- // "Could not apply checkpoint due to local data". We need to retry after
9689
- // finishing uploads.
9690
- pendingValidatedCheckpoint = targetCheckpoint;
9691
9436
  }
9692
9437
  else {
9693
- // Nothing to retry later. This would likely already be null from the last
9694
- // checksum or checksum_diff operation, but we make sure.
9695
- pendingValidatedCheckpoint = null;
9696
- }
9697
- }
9698
- else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
9699
- const priority = line.partial_checkpoint_complete.priority;
9700
- this.logger.debug('Partial checkpoint complete', priority);
9701
- const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint, priority);
9702
- if (!result.checkpointValid) {
9703
- // This means checksums failed. Start again with a new checkpoint.
9704
- // TODO: better back-off
9705
- await new Promise((resolve) => setTimeout(resolve, 50));
9706
- return;
9707
- }
9708
- else if (!result.ready) ;
9709
- else {
9710
- // We'll keep on downloading, but can report that this priority is synced now.
9711
- this.logger.debug('partial checkpoint validation succeeded');
9712
- // All states with a higher priority can be deleted since this partial sync includes them.
9713
- const priorityStates = this.syncStatus.priorityStatusEntries.filter((s) => s.priority <= priority);
9714
- priorityStates.push({
9715
- priority,
9716
- lastSyncedAt: new Date(),
9717
- hasSynced: true
9718
- });
9719
- this.updateSyncStatus({
9720
- connected: true,
9721
- priorityStatusEntries: priorityStates
9722
- });
9723
- }
9724
- }
9725
- else if (isStreamingSyncCheckpointDiff(line)) {
9726
- // TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
9727
- if (targetCheckpoint == null) {
9728
- throw new Error('Checkpoint diff without previous checkpoint');
9729
- }
9730
- // New checkpoint - existing validated checkpoint is no longer valid
9731
- pendingValidatedCheckpoint = null;
9732
- const diff = line.checkpoint_diff;
9733
- const newBuckets = new Map();
9734
- for (const checksum of targetCheckpoint.buckets) {
9735
- newBuckets.set(checksum.bucket, checksum);
9736
- }
9737
- for (const checksum of diff.updated_buckets) {
9738
- newBuckets.set(checksum.bucket, checksum);
9739
- }
9740
- for (const bucket of diff.removed_buckets) {
9741
- newBuckets.delete(bucket);
9742
- }
9743
- const newCheckpoint = {
9744
- last_op_id: diff.last_op_id,
9745
- buckets: [...newBuckets.values()],
9746
- write_checkpoint: diff.write_checkpoint
9747
- };
9748
- targetCheckpoint = newCheckpoint;
9749
- await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
9750
- bucketMap = new Map();
9751
- newBuckets.forEach((checksum, name) => bucketMap.set(name, {
9752
- name: checksum.bucket,
9753
- priority: checksum.priority ?? FALLBACK_PRIORITY
9754
- }));
9755
- const bucketsToDelete = diff.removed_buckets;
9756
- if (bucketsToDelete.length > 0) {
9757
- this.logger.debug('Remove buckets', bucketsToDelete);
9758
- }
9759
- await this.options.adapter.removeBuckets(bucketsToDelete);
9760
- await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
9761
- }
9762
- else if (isStreamingSyncData(line)) {
9763
- const { data } = line;
9764
- const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
9765
- let updatedProgress = null;
9766
- if (previousProgress) {
9767
- updatedProgress = { ...previousProgress };
9768
- const progressForBucket = updatedProgress[data.bucket];
9769
- if (progressForBucket) {
9770
- updatedProgress[data.bucket] = {
9771
- ...progressForBucket,
9772
- since_last: progressForBucket.since_last + data.data.length
9773
- };
9438
+ const event = await inner.next();
9439
+ if (event.done) {
9440
+ done = true;
9441
+ return valueResult({ command: PowerSyncControlCommand.CONNECTION_STATE, payload: 'end' });
9774
9442
  }
9775
- }
9776
- this.updateSyncStatus({
9777
- dataFlow: {
9778
- downloading: true,
9779
- downloadProgress: updatedProgress
9443
+ else {
9444
+ return valueResult({
9445
+ command: typeof event.value == 'string'
9446
+ ? PowerSyncControlCommand.PROCESS_TEXT_LINE
9447
+ : PowerSyncControlCommand.PROCESS_BSON_LINE,
9448
+ payload: event.value
9449
+ });
9780
9450
  }
9781
- });
9782
- await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
9783
- }
9784
- else if (isStreamingKeepalive(line)) {
9785
- const remaining_seconds = line.token_expires_in;
9786
- if (remaining_seconds == 0) {
9787
- // Connection would be closed automatically right after this
9788
- this.logger.debug('Token expiring; reconnect');
9789
- /**
9790
- * For a rare case where the backend connector does not update the token
9791
- * (uses the same one), this should have some delay.
9792
- */
9793
- await this.delayRetry();
9794
- return;
9795
- }
9796
- else if (remaining_seconds < 30) {
9797
- this.logger.debug('Token will expire soon; reconnect');
9798
- // Pre-emptively refresh the token
9799
- this.options.remote.invalidateCredentials();
9800
- return;
9801
9451
  }
9802
- this.triggerCrudUpload();
9803
- }
9804
- else {
9805
- this.logger.debug('Received unknown sync line', line);
9806
9452
  }
9807
- }
9808
- this.logger.debug('Stream input empty');
9809
- // Connection closed. Likely due to auth issue.
9810
- return;
9453
+ };
9811
9454
  }
9812
9455
  async rustSyncIteration(signal, resolvedOptions) {
9813
9456
  const syncImplementation = this;
9814
9457
  const adapter = this.options.adapter;
9815
9458
  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);
9821
- let receivingLines = null;
9822
- let hadSyncLine = false;
9823
9459
  let hideDisconnectOnRestart = false;
9460
+ let notifyTokenRefreshed;
9824
9461
  if (signal.aborted) {
9825
9462
  throw new AbortOperation('Connection request has been aborted');
9826
9463
  }
9827
- // Pending sync lines received from the service, as well as local events that trigger a powersync_control
9828
- // invocation (local events include refreshed tokens and completed uploads).
9829
- // This is a single data stream so that we can handle all control calls from a single place.
9830
- let controlInvocations = null;
9831
- async function connect(instr) {
9832
- const syncOptions = {
9833
- path: '/sync/stream',
9834
- abortSignal: controller.signal,
9835
- data: instr.request
9464
+ function startCommand() {
9465
+ const options = {
9466
+ parameters: resolvedOptions.params,
9467
+ app_metadata: resolvedOptions.appMetadata,
9468
+ active_streams: syncImplementation.activeStreams,
9469
+ include_defaults: resolvedOptions.includeDefaultStreams
9836
9470
  };
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
- }));
9854
- // The rust client will set connected: true after the first sync line because that's when it gets invoked, but
9855
- // we're already connected here and can report that.
9856
- syncImplementation.updateSyncStatus({ connected: true });
9857
- try {
9858
- while (true) {
9859
- let event = await controlInvocations.next();
9860
- if (event.done) {
9861
- break;
9862
- }
9863
- const line = event.value;
9864
- await control(line.command, line.payload);
9865
- if (!hadSyncLine) {
9866
- syncImplementation.triggerCrudUpload();
9867
- hadSyncLine = true;
9868
- }
9869
- }
9870
- }
9871
- finally {
9872
- abort();
9873
- signal.removeEventListener('abort', abort);
9471
+ if (resolvedOptions.serializedSchema) {
9472
+ options.schema = resolvedOptions.serializedSchema;
9874
9473
  }
9474
+ return invokePowerSyncControl(PowerSyncControlCommand.START, JSON.stringify(options));
9875
9475
  }
9876
9476
  async function stop() {
9877
- await control(PowerSyncControlCommand.STOP);
9477
+ const instructions = await invokePowerSyncControl(PowerSyncControlCommand.STOP);
9478
+ for (const instruction of instructions) {
9479
+ // We don't need to handle interrupting instructions since we're unconditionally ending the sync iteration at
9480
+ // this point.
9481
+ if (isInterruptingInstruction(instruction))
9482
+ continue;
9483
+ await handleInstruction(instruction);
9484
+ }
9878
9485
  }
9879
- async function control(op, payload) {
9486
+ async function invokePowerSyncControl(op, payload) {
9880
9487
  const rawResponse = await adapter.control(op, payload ?? null);
9881
9488
  const logger = syncImplementation.logger;
9882
9489
  logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
9883
- await handleInstructions(JSON.parse(rawResponse));
9490
+ if (op != PowerSyncControlCommand.STOP) {
9491
+ // Evidently we have a working connection here, otherwise powersync_control would have failed.
9492
+ syncImplementation.connectionMayHaveChanged = false;
9493
+ }
9494
+ return JSON.parse(rawResponse);
9884
9495
  }
9885
9496
  async function handleInstruction(instruction) {
9886
9497
  if ('LogLine' in instruction) {
@@ -9899,13 +9510,6 @@ The next upload iteration will be delayed.`);
9899
9510
  else if ('UpdateSyncStatus' in instruction) {
9900
9511
  syncImplementation.updateSyncStatus(coreStatusToJs(instruction.UpdateSyncStatus.status));
9901
9512
  }
9902
- else if ('EstablishSyncStream' in instruction) {
9903
- if (receivingLines != null) {
9904
- // Already connected, this shouldn't happen during a single iteration.
9905
- throw 'Unexpected request to establish sync stream, already connected';
9906
- }
9907
- receivingLines = connect(instruction.EstablishSyncStream);
9908
- }
9909
9513
  else if ('FetchCredentials' in instruction) {
9910
9514
  if (instruction.FetchCredentials.did_expire) {
9911
9515
  remote.invalidateCredentials();
@@ -9914,16 +9518,12 @@ The next upload iteration will be delayed.`);
9914
9518
  remote.invalidateCredentials();
9915
9519
  // Restart iteration after the credentials have been refreshed.
9916
9520
  remote.fetchCredentials().then((_) => {
9917
- controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
9521
+ notifyTokenRefreshed?.();
9918
9522
  }, (err) => {
9919
9523
  syncImplementation.logger.warn('Could not prefetch credentials', err);
9920
9524
  });
9921
9525
  }
9922
9526
  }
9923
- else if ('CloseSyncStream' in instruction) {
9924
- controller.abort();
9925
- hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
9926
- }
9927
9527
  else if ('FlushFileSystem' in instruction) ;
9928
9528
  else if ('DidCompleteSync' in instruction) {
9929
9529
  syncImplementation.updateSyncStatus({
@@ -9933,101 +9533,83 @@ The next upload iteration will be delayed.`);
9933
9533
  });
9934
9534
  }
9935
9535
  }
9936
- async function handleInstructions(instructions) {
9937
- for (const instr of instructions) {
9938
- await handleInstruction(instr);
9939
- }
9940
- }
9941
9536
  try {
9942
- const options = {
9943
- parameters: resolvedOptions.params,
9944
- app_metadata: resolvedOptions.appMetadata,
9945
- active_streams: this.activeStreams,
9946
- include_defaults: resolvedOptions.includeDefaultStreams
9947
- };
9948
- if (resolvedOptions.serializedSchema) {
9949
- options.schema = resolvedOptions.serializedSchema;
9537
+ const defaultResult = { immediateRestart: false };
9538
+ // Pending sync lines received from the service, as well as local events that trigger a powersync_control
9539
+ // invocation (local events include refreshed tokens and completed uploads).
9540
+ // This is a single data stream so that we can handle all control calls from a single place.
9541
+ let controlInvocations = null;
9542
+ for (const startInstruction of await startCommand()) {
9543
+ if ('EstablishSyncStream' in startInstruction) {
9544
+ const syncOptions = {
9545
+ path: '/sync/stream',
9546
+ abortSignal: signal,
9547
+ data: startInstruction.EstablishSyncStream.request
9548
+ };
9549
+ controlInvocations = injectable(syncImplementation.receiveSyncLines({
9550
+ options: syncOptions,
9551
+ connection: resolvedOptions
9552
+ }));
9553
+ }
9554
+ else if ('CloseSyncStream' in startInstruction) {
9555
+ return defaultResult;
9556
+ }
9557
+ else {
9558
+ await handleInstruction(startInstruction);
9559
+ }
9950
9560
  }
9951
- await control(PowerSyncControlCommand.START, JSON.stringify(options));
9561
+ if (controlInvocations == null)
9562
+ return defaultResult;
9952
9563
  this.notifyCompletedUploads = () => {
9953
- controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
9564
+ controlInvocations.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
9954
9565
  };
9955
9566
  this.handleActiveStreamsChange = () => {
9956
- controlInvocations?.inject({
9567
+ controlInvocations.inject({
9957
9568
  command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
9958
9569
  payload: JSON.stringify(this.activeStreams)
9959
9570
  });
9960
9571
  };
9961
- await receivingLines;
9572
+ notifyTokenRefreshed = () => {
9573
+ controlInvocations.inject({
9574
+ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED
9575
+ });
9576
+ };
9577
+ let hadSyncLine = false;
9578
+ loop: while (true) {
9579
+ const { done, value } = await controlInvocations.next();
9580
+ if (done)
9581
+ break;
9582
+ if (!hadSyncLine) {
9583
+ // Trigger a local CRUD upload when the first sync line has been received, this allows uploading local changes
9584
+ // that have been made while offline or disconnected.
9585
+ if (value.command == PowerSyncControlCommand.PROCESS_TEXT_LINE ||
9586
+ value.command == PowerSyncControlCommand.PROCESS_BSON_LINE) {
9587
+ hadSyncLine = true;
9588
+ this.triggerCrudUpload?.();
9589
+ }
9590
+ }
9591
+ const instructions = await invokePowerSyncControl(value.command, value.payload);
9592
+ for (const instruction of instructions) {
9593
+ if ('EstablishSyncStream' in instruction) {
9594
+ throw new Error('Received EstablishSyncStream while already connected.');
9595
+ }
9596
+ else if ('CloseSyncStream' in instruction) {
9597
+ hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
9598
+ break loop;
9599
+ }
9600
+ else {
9601
+ await handleInstruction(instruction);
9602
+ }
9603
+ }
9604
+ }
9962
9605
  }
9963
9606
  finally {
9964
9607
  this.notifyCompletedUploads = this.handleActiveStreamsChange = undefined;
9608
+ notifyTokenRefreshed = undefined;
9965
9609
  await stop();
9966
9610
  }
9967
9611
  return { immediateRestart: hideDisconnectOnRestart };
9968
9612
  }
9969
- async updateSyncStatusForStartingCheckpoint(checkpoint) {
9970
- const localProgress = await this.options.adapter.getBucketOperationProgress();
9971
- const progress = {};
9972
- let invalidated = false;
9973
- for (const bucket of checkpoint.buckets) {
9974
- const savedProgress = localProgress[bucket.bucket];
9975
- const atLast = savedProgress?.atLast ?? 0;
9976
- const sinceLast = savedProgress?.sinceLast ?? 0;
9977
- progress[bucket.bucket] = {
9978
- // The fallback priority doesn't matter here, but 3 is the one newer versions of the sync service
9979
- // will use by default.
9980
- priority: bucket.priority ?? 3,
9981
- at_last: atLast,
9982
- since_last: sinceLast,
9983
- target_count: bucket.count ?? 0
9984
- };
9985
- if (bucket.count != null && bucket.count < atLast + sinceLast) {
9986
- // Either due to a defrag / sync rule deploy or a compaction operation, the size
9987
- // of the bucket shrank so much that the local ops exceed the ops in the updated
9988
- // bucket. We can't prossibly report progress in this case (it would overshoot 100%).
9989
- invalidated = true;
9990
- }
9991
- }
9992
- if (invalidated) {
9993
- for (const bucket in progress) {
9994
- const bucketProgress = progress[bucket];
9995
- bucketProgress.at_last = 0;
9996
- bucketProgress.since_last = 0;
9997
- }
9998
- }
9999
- this.updateSyncStatus({
10000
- dataFlow: {
10001
- downloading: true,
10002
- downloadProgress: progress
10003
- }
10004
- });
10005
- }
10006
- async applyCheckpoint(checkpoint) {
10007
- let result = await this.options.adapter.syncLocalDatabase(checkpoint);
10008
- if (!result.checkpointValid) {
10009
- this.logger.debug(`Checksum mismatch in checkpoint ${checkpoint.last_op_id}, will reconnect`);
10010
- // This means checksums failed. Start again with a new checkpoint.
10011
- // TODO: better back-off
10012
- await new Promise((resolve) => setTimeout(resolve, 50));
10013
- return { applied: false, endIteration: true };
10014
- }
10015
- else if (!result.ready) {
10016
- this.logger.debug(`Could not apply checkpoint ${checkpoint.last_op_id} due to local data. We will retry applying the checkpoint after that upload is completed.`);
10017
- return { applied: false, endIteration: false };
10018
- }
10019
- this.logger.debug(`Applied checkpoint ${checkpoint.last_op_id}`, checkpoint);
10020
- this.updateSyncStatus({
10021
- connected: true,
10022
- lastSyncedAt: new Date(),
10023
- dataFlow: {
10024
- downloading: false,
10025
- downloadProgress: null,
10026
- downloadError: undefined
10027
- }
10028
- });
10029
- return { applied: true, endIteration: false };
10030
- }
10031
9613
  updateSyncStatus(options) {
10032
9614
  const updatedStatus = new SyncStatus({
10033
9615
  connected: options.connected ?? this.syncStatus.connected,
@@ -11566,14 +11148,12 @@ class SqliteBucketStorage extends BaseObserver {
11566
11148
  db;
11567
11149
  logger;
11568
11150
  tableNames;
11569
- _hasCompletedSync;
11570
11151
  updateListener;
11571
11152
  _clientId;
11572
11153
  constructor(db, logger = Logger.get('SqliteBucketStorage')) {
11573
11154
  super();
11574
11155
  this.db = db;
11575
11156
  this.logger = logger;
11576
- this._hasCompletedSync = false;
11577
11157
  this.tableNames = new Set();
11578
11158
  this.updateListener = db.registerListener({
11579
11159
  tablesUpdated: (update) => {
@@ -11585,7 +11165,6 @@ class SqliteBucketStorage extends BaseObserver {
11585
11165
  });
11586
11166
  }
11587
11167
  async init() {
11588
- this._hasCompletedSync = false;
11589
11168
  const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
11590
11169
  for (const row of existingTableRows ?? []) {
11591
11170
  this.tableNames.add(row.name);
@@ -11607,156 +11186,6 @@ class SqliteBucketStorage extends BaseObserver {
11607
11186
  getMaxOpId() {
11608
11187
  return MAX_OP_ID;
11609
11188
  }
11610
- /**
11611
- * Reset any caches.
11612
- */
11613
- startSession() { }
11614
- async getBucketStates() {
11615
- const result = await this.db.getAll("SELECT name as bucket, cast(last_op as TEXT) as op_id FROM ps_buckets WHERE pending_delete = 0 AND name != '$local'");
11616
- return result;
11617
- }
11618
- async getBucketOperationProgress() {
11619
- const rows = await this.db.getAll('SELECT name, count_at_last, count_since_last FROM ps_buckets');
11620
- return Object.fromEntries(rows.map((r) => [r.name, { atLast: r.count_at_last, sinceLast: r.count_since_last }]));
11621
- }
11622
- async saveSyncData(batch, fixedKeyFormat = false) {
11623
- await this.writeTransaction(async (tx) => {
11624
- for (const b of batch.buckets) {
11625
- await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
11626
- 'save',
11627
- JSON.stringify({ buckets: [b.toJSON(fixedKeyFormat)] })
11628
- ]);
11629
- this.logger.debug(`Saved batch of data for bucket: ${b.bucket}, operations: ${b.data.length}`);
11630
- }
11631
- });
11632
- }
11633
- async removeBuckets(buckets) {
11634
- for (const bucket of buckets) {
11635
- await this.deleteBucket(bucket);
11636
- }
11637
- }
11638
- /**
11639
- * Mark a bucket for deletion.
11640
- */
11641
- async deleteBucket(bucket) {
11642
- await this.writeTransaction(async (tx) => {
11643
- await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', ['delete_bucket', bucket]);
11644
- });
11645
- this.logger.debug(`Done deleting bucket ${bucket}`);
11646
- }
11647
- async hasCompletedSync() {
11648
- if (this._hasCompletedSync) {
11649
- return true;
11650
- }
11651
- const r = await this.db.get(`SELECT powersync_last_synced_at() as synced_at`);
11652
- const completed = r.synced_at != null;
11653
- if (completed) {
11654
- this._hasCompletedSync = true;
11655
- }
11656
- return completed;
11657
- }
11658
- async syncLocalDatabase(checkpoint, priority) {
11659
- const r = await this.validateChecksums(checkpoint, priority);
11660
- if (!r.checkpointValid) {
11661
- this.logger.error('Checksums failed for', r.checkpointFailures);
11662
- for (const b of r.checkpointFailures ?? []) {
11663
- await this.deleteBucket(b);
11664
- }
11665
- return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
11666
- }
11667
- if (priority == null) {
11668
- this.logger.debug(`Validated checksums checkpoint ${checkpoint.last_op_id}`);
11669
- }
11670
- else {
11671
- this.logger.debug(`Validated checksums for partial checkpoint ${checkpoint.last_op_id}, priority ${priority}`);
11672
- }
11673
- let buckets = checkpoint.buckets;
11674
- if (priority !== undefined) {
11675
- buckets = buckets.filter((b) => hasMatchingPriority(priority, b));
11676
- }
11677
- const bucketNames = buckets.map((b) => b.bucket);
11678
- await this.writeTransaction(async (tx) => {
11679
- await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
11680
- checkpoint.last_op_id,
11681
- JSON.stringify(bucketNames)
11682
- ]);
11683
- if (priority == null && checkpoint.write_checkpoint) {
11684
- await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
11685
- }
11686
- });
11687
- const valid = await this.updateObjectsFromBuckets(checkpoint, priority);
11688
- if (!valid) {
11689
- return { ready: false, checkpointValid: true };
11690
- }
11691
- return {
11692
- ready: true,
11693
- checkpointValid: true
11694
- };
11695
- }
11696
- /**
11697
- * Atomically update the local state to the current checkpoint.
11698
- *
11699
- * This includes creating new tables, dropping old tables, and copying data over from the oplog.
11700
- */
11701
- async updateObjectsFromBuckets(checkpoint, priority) {
11702
- let arg = '';
11703
- if (priority !== undefined) {
11704
- const affectedBuckets = [];
11705
- for (const desc of checkpoint.buckets) {
11706
- if (hasMatchingPriority(priority, desc)) {
11707
- affectedBuckets.push(desc.bucket);
11708
- }
11709
- }
11710
- arg = JSON.stringify({ priority, buckets: affectedBuckets });
11711
- }
11712
- return this.writeTransaction(async (tx) => {
11713
- const { insertId: result } = await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
11714
- 'sync_local',
11715
- arg
11716
- ]);
11717
- if (result == 1) {
11718
- if (priority == null) {
11719
- const bucketToCount = Object.fromEntries(checkpoint.buckets.map((b) => [b.bucket, b.count]));
11720
- // The two parameters could be replaced with one, but: https://github.com/powersync-ja/better-sqlite3/pull/6
11721
- const jsonBucketCount = JSON.stringify(bucketToCount);
11722
- await tx.execute("UPDATE ps_buckets SET count_since_last = 0, count_at_last = ?->name WHERE name != '$local' AND ?->name IS NOT NULL", [jsonBucketCount, jsonBucketCount]);
11723
- }
11724
- return true;
11725
- }
11726
- else {
11727
- return false;
11728
- }
11729
- });
11730
- }
11731
- async validateChecksums(checkpoint, priority) {
11732
- if (priority !== undefined) {
11733
- // Only validate the buckets within the priority we care about
11734
- const newBuckets = checkpoint.buckets.filter((cs) => hasMatchingPriority(priority, cs));
11735
- checkpoint = { ...checkpoint, buckets: newBuckets };
11736
- }
11737
- const rs = await this.db.execute('SELECT powersync_validate_checkpoint(?) as result', [
11738
- JSON.stringify({ ...checkpoint })
11739
- ]);
11740
- const resultItem = rs.rows?.item(0);
11741
- if (!resultItem) {
11742
- return {
11743
- checkpointValid: false,
11744
- ready: false,
11745
- checkpointFailures: []
11746
- };
11747
- }
11748
- const result = JSON.parse(resultItem['result']);
11749
- if (result['valid']) {
11750
- return { ready: true, checkpointValid: true };
11751
- }
11752
- else {
11753
- return {
11754
- checkpointValid: false,
11755
- ready: false,
11756
- checkpointFailures: result['failed_buckets']
11757
- };
11758
- }
11759
- }
11760
11189
  async updateLocalTarget(cb) {
11761
11190
  const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)", [MAX_OP_ID]);
11762
11191
  if (!rs1.length) {
@@ -11847,12 +11276,6 @@ class SqliteBucketStorage extends BaseObserver {
11847
11276
  async writeTransaction(callback, options) {
11848
11277
  return this.db.writeTransaction(callback, options);
11849
11278
  }
11850
- /**
11851
- * Set a target checkpoint.
11852
- */
11853
- async setTargetCheckpoint(checkpoint) {
11854
- // No-op for now
11855
- }
11856
11279
  async control(op, payload) {
11857
11280
  return await this.writeTransaction(async (tx) => {
11858
11281
  const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
@@ -11876,20 +11299,6 @@ class SqliteBucketStorage extends BaseObserver {
11876
11299
  }
11877
11300
  static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
11878
11301
  }
11879
- function hasMatchingPriority(priority, bucket) {
11880
- return bucket.priority != null && bucket.priority <= priority;
11881
- }
11882
-
11883
- // TODO JSON
11884
- class SyncDataBatch {
11885
- buckets;
11886
- static fromJSON(json) {
11887
- return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
11888
- }
11889
- constructor(buckets) {
11890
- this.buckets = buckets;
11891
- }
11892
- }
11893
11302
 
11894
11303
  /**
11895
11304
  * Thrown when an underlying database connection is closed.
@@ -11949,10 +11358,8 @@ class Schema {
11949
11358
  * developer instead of automatically by PowerSync.
11950
11359
  * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
11951
11360
  * using client-side table and column constraints.
11952
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
11953
11361
  *
11954
11362
  * @param tables An object of (table name, raw table definition) entries.
11955
- * @experimental Note that the raw tables API is still experimental and may change in the future.
11956
11363
  */
11957
11364
  withRawTables(tables) {
11958
11365
  for (const [name, rawTableDefinition] of Object.entries(tables)) {
@@ -12149,5 +11556,5 @@ const parseQuery = (query, parameters) => {
12149
11556
  return { sqlStatement, parameters: parameters };
12150
11557
  };
12151
11558
 
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 };
11559
+ 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, PSInternalTable, PowerSyncControlCommand, RowUpdateType, Schema, Semaphore, SqliteBucketStorage, SyncClientImplementation, SyncProgress, SyncStatus, SyncStreamConnectionMethod, SyncingService, Table, TableV2, TriggerManagerImpl, UpdateType, UploadQueueStats, WatchedQueryListenerEvent, attachmentFromSql, column, compilableQueryWatch, createBaseLogger, createLogger, extractTableUpdates, isBatchedUpdateNotification, isDBAdapter, isPowerSyncDatabaseOptionsWithSettings, isSQLOpenFactory, isSQLOpenOptions, parseQuery, runOnSchemaChange, sanitizeSQL, sanitizeUUID, timeoutSignal };
12153
11560
  //# sourceMappingURL=bundle.node.mjs.map