@powersync/common 0.0.0-dev-20260414110516 → 0.0.0-dev-20260504100448

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 (67) hide show
  1. package/dist/bundle.cjs +33 -665
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +34 -654
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +33 -665
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +34 -654
  8. package/dist/bundle.node.mjs.map +1 -1
  9. package/dist/index.d.cts +22 -369
  10. package/legacy/sync_protocol.d.ts +103 -0
  11. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +1 -63
  12. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -1
  13. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +1 -28
  14. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -162
  15. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -1
  16. package/lib/client/sync/stream/AbstractRemote.d.ts +2 -12
  17. package/lib/client/sync/stream/AbstractRemote.js +3 -13
  18. package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
  19. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +12 -35
  20. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +29 -337
  21. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
  22. package/lib/client/sync/stream/JsonValue.d.ts +7 -0
  23. package/lib/client/sync/stream/JsonValue.js +2 -0
  24. package/lib/client/sync/stream/JsonValue.js.map +1 -0
  25. package/lib/client/sync/stream/core-instruction.d.ts +1 -2
  26. package/lib/client/sync/stream/core-instruction.js.map +1 -1
  27. package/lib/db/crud/SyncStatus.d.ts +0 -4
  28. package/lib/db/crud/SyncStatus.js +0 -4
  29. package/lib/db/crud/SyncStatus.js.map +1 -1
  30. package/lib/db/schema/RawTable.d.ts +0 -5
  31. package/lib/db/schema/Schema.d.ts +0 -2
  32. package/lib/db/schema/Schema.js +0 -2
  33. package/lib/db/schema/Schema.js.map +1 -1
  34. package/lib/index.d.ts +1 -5
  35. package/lib/index.js +1 -5
  36. package/lib/index.js.map +1 -1
  37. package/package.json +7 -4
  38. package/src/client/sync/bucket/BucketStorageAdapter.ts +1 -70
  39. package/src/client/sync/bucket/SqliteBucketStorage.ts +1 -197
  40. package/src/client/sync/stream/AbstractRemote.ts +5 -27
  41. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +41 -407
  42. package/src/client/sync/stream/JsonValue.ts +8 -0
  43. package/src/client/sync/stream/core-instruction.ts +1 -2
  44. package/src/db/crud/SyncStatus.ts +0 -4
  45. package/src/db/schema/RawTable.ts +0 -5
  46. package/src/db/schema/Schema.ts +0 -2
  47. package/src/index.ts +1 -5
  48. package/lib/client/sync/bucket/OpType.d.ts +0 -16
  49. package/lib/client/sync/bucket/OpType.js +0 -23
  50. package/lib/client/sync/bucket/OpType.js.map +0 -1
  51. package/lib/client/sync/bucket/OplogEntry.d.ts +0 -23
  52. package/lib/client/sync/bucket/OplogEntry.js +0 -36
  53. package/lib/client/sync/bucket/OplogEntry.js.map +0 -1
  54. package/lib/client/sync/bucket/SyncDataBatch.d.ts +0 -6
  55. package/lib/client/sync/bucket/SyncDataBatch.js +0 -12
  56. package/lib/client/sync/bucket/SyncDataBatch.js.map +0 -1
  57. package/lib/client/sync/bucket/SyncDataBucket.d.ts +0 -40
  58. package/lib/client/sync/bucket/SyncDataBucket.js +0 -40
  59. package/lib/client/sync/bucket/SyncDataBucket.js.map +0 -1
  60. package/lib/client/sync/stream/streaming-sync-types.d.ts +0 -143
  61. package/lib/client/sync/stream/streaming-sync-types.js +0 -26
  62. package/lib/client/sync/stream/streaming-sync-types.js.map +0 -1
  63. package/src/client/sync/bucket/OpType.ts +0 -23
  64. package/src/client/sync/bucket/OplogEntry.ts +0 -50
  65. package/src/client/sync/bucket/SyncDataBatch.ts +0 -11
  66. package/src/client/sync/bucket/SyncDataBucket.ts +0 -49
  67. package/src/client/sync/stream/streaming-sync-types.ts +0 -210
@@ -1979,16 +1979,12 @@ class SyncStatus {
1979
1979
  *
1980
1980
  * This returns null when the database is currently being opened and we don't have reliable information about all
1981
1981
  * included streams yet.
1982
- *
1983
- * @experimental Sync streams are currently in alpha.
1984
1982
  */
1985
1983
  get syncStreams() {
1986
1984
  return this.options.dataFlow?.internalStreamSubscriptions?.map((core) => new SyncStreamStatusView(this, core));
1987
1985
  }
1988
1986
  /**
1989
1987
  * If the `stream` appears in {@link syncStreams}, returns the current status for that stream.
1990
- *
1991
- * @experimental Sync streams are currently in alpha.
1992
1988
  */
1993
1989
  forStream(stream) {
1994
1990
  const asJson = JSON.stringify(stream.parameters);
@@ -3341,103 +3337,6 @@ class AbortOperation extends Error {
3341
3337
  }
3342
3338
  }
3343
3339
 
3344
- exports.OpTypeEnum = void 0;
3345
- (function (OpTypeEnum) {
3346
- OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
3347
- OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
3348
- OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
3349
- OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
3350
- })(exports.OpTypeEnum || (exports.OpTypeEnum = {}));
3351
- /**
3352
- * Used internally for sync buckets.
3353
- */
3354
- class OpType {
3355
- value;
3356
- static fromJSON(jsonValue) {
3357
- return new OpType(exports.OpTypeEnum[jsonValue]);
3358
- }
3359
- constructor(value) {
3360
- this.value = value;
3361
- }
3362
- toJSON() {
3363
- return Object.entries(exports.OpTypeEnum).find(([, value]) => value === this.value)[0];
3364
- }
3365
- }
3366
-
3367
- class OplogEntry {
3368
- op_id;
3369
- op;
3370
- checksum;
3371
- subkey;
3372
- object_type;
3373
- object_id;
3374
- data;
3375
- static fromRow(row) {
3376
- return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
3377
- }
3378
- constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
3379
- this.op_id = op_id;
3380
- this.op = op;
3381
- this.checksum = checksum;
3382
- this.subkey = subkey;
3383
- this.object_type = object_type;
3384
- this.object_id = object_id;
3385
- this.data = data;
3386
- }
3387
- toJSON(fixedKeyEncoding = false) {
3388
- return {
3389
- op_id: this.op_id,
3390
- op: this.op.toJSON(),
3391
- object_type: this.object_type,
3392
- object_id: this.object_id,
3393
- checksum: this.checksum,
3394
- data: this.data,
3395
- // Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
3396
- // but we need to migrate gradually to not break existing databases.
3397
- subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
3398
- };
3399
- }
3400
- }
3401
-
3402
- class SyncDataBucket {
3403
- bucket;
3404
- data;
3405
- has_more;
3406
- after;
3407
- next_after;
3408
- static fromRow(row) {
3409
- return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
3410
- }
3411
- constructor(bucket, data,
3412
- /**
3413
- * True if the response does not contain all the data for this bucket, and another request must be made.
3414
- */
3415
- has_more,
3416
- /**
3417
- * The `after` specified in the request.
3418
- */
3419
- after,
3420
- /**
3421
- * Use this for the next request.
3422
- */
3423
- next_after) {
3424
- this.bucket = bucket;
3425
- this.data = data;
3426
- this.has_more = has_more;
3427
- this.after = after;
3428
- this.next_after = next_after;
3429
- }
3430
- toJSON(fixedKeyEncoding = false) {
3431
- return {
3432
- bucket: this.bucket,
3433
- has_more: this.has_more,
3434
- after: this.after,
3435
- next_after: this.next_after,
3436
- data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
3437
- };
3438
- }
3439
- }
3440
-
3441
3340
  var dist = {};
3442
3341
 
3443
3342
  var Codecs = {};
@@ -8222,7 +8121,7 @@ function requireDist () {
8222
8121
 
8223
8122
  var distExports = requireDist();
8224
8123
 
8225
- var version = "1.51.0";
8124
+ var version = "1.52.0";
8226
8125
  var PACKAGE = {
8227
8126
  version: version};
8228
8127
 
@@ -8779,22 +8678,12 @@ class AbstractRemote {
8779
8678
  * Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
8780
8679
  *
8781
8680
  * The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
8782
- *
8783
- * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
8784
- * (required for compatibility with older sync services).
8785
8681
  */
8786
- async socketStreamRaw(options, bson) {
8682
+ async socketStreamRaw(options) {
8787
8683
  const { path, fetchStrategy = exports.FetchStrategy.Buffered } = options;
8788
- const mimeType = bson == null ? 'application/json' : 'application/bson';
8684
+ const mimeType = 'application/json';
8789
8685
  function toBuffer(js) {
8790
- let contents;
8791
- if (bson != null) {
8792
- contents = bson.serialize(js);
8793
- }
8794
- else {
8795
- contents = JSON.stringify(js);
8796
- }
8797
- return node_buffer.Buffer.from(contents);
8686
+ return node_buffer.Buffer.from(JSON.stringify(js));
8798
8687
  }
8799
8688
  const syncQueueRequestSize = fetchStrategy == exports.FetchStrategy.Buffered ? 10 : 1;
8800
8689
  const request = await this.buildRequest(path);
@@ -9096,32 +8985,6 @@ function coreStatusToJs(status) {
9096
8985
  };
9097
8986
  }
9098
8987
 
9099
- function isStreamingSyncData(line) {
9100
- return line.data != null;
9101
- }
9102
- function isStreamingKeepalive(line) {
9103
- return line.token_expires_in != null;
9104
- }
9105
- function isStreamingSyncCheckpoint(line) {
9106
- return line.checkpoint != null;
9107
- }
9108
- function isStreamingSyncCheckpointComplete(line) {
9109
- return line.checkpoint_complete != null;
9110
- }
9111
- function isStreamingSyncCheckpointPartiallyComplete(line) {
9112
- return line.partial_checkpoint_complete != null;
9113
- }
9114
- function isStreamingSyncCheckpointDiff(line) {
9115
- return line.checkpoint_diff != null;
9116
- }
9117
- function isContinueCheckpointRequest(request) {
9118
- return (Array.isArray(request.buckets) &&
9119
- typeof request.checkpoint_token == 'string');
9120
- }
9121
- function isSyncNewCheckpointRequest(request) {
9122
- return typeof request.request_checkpoint == 'object';
9123
- }
9124
-
9125
8988
  exports.LockType = void 0;
9126
8989
  (function (LockType) {
9127
8990
  LockType["CRUD"] = "crud";
@@ -9134,35 +8997,21 @@ exports.SyncStreamConnectionMethod = void 0;
9134
8997
  })(exports.SyncStreamConnectionMethod || (exports.SyncStreamConnectionMethod = {}));
9135
8998
  exports.SyncClientImplementation = void 0;
9136
8999
  (function (SyncClientImplementation) {
9137
- /**
9138
- * Decodes and handles sync lines received from the sync service in JavaScript.
9139
- *
9140
- * This is the default option.
9141
- *
9142
- * @deprecated We recommend the {@link RUST} client implementation for all apps. If you have issues with
9143
- * the Rust client, please file an issue or reach out to us. The JavaScript client will be removed in a future
9144
- * version of the PowerSync SDK.
9145
- */
9146
- SyncClientImplementation["JAVASCRIPT"] = "js";
9147
9000
  /**
9148
9001
  * This implementation offloads the sync line decoding and handling into the PowerSync
9149
9002
  * core extension.
9150
9003
  *
9151
- * This option is more performant than the {@link JAVASCRIPT} client, enabled by default and the
9152
- * recommended client implementation for all apps.
9004
+ * This is the only option, as an older JavaScript client implementation has been removed from the SDK.
9153
9005
  *
9154
9006
  * ## Compatibility warning
9155
9007
  *
9156
9008
  * The Rust sync client stores sync data in a format that is slightly different than the one used
9157
- * by the old {@link JAVASCRIPT} implementation. When adopting the {@link RUST} client on existing
9158
- * databases, the PowerSync SDK will migrate the format automatically.
9159
- * Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
9160
- * the version introducing {@link RUST} as an option) also supports the new format, so you can switch
9161
- * back to {@link JAVASCRIPT} later.
9009
+ * by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
9010
+ * migrate the format automatically.
9162
9011
  *
9163
- * __However__: Upgrading the SDK version, then adopting {@link RUST} as a sync client and later
9164
- * downgrading the SDK to an older version (necessarily using the JavaScript-based implementation then)
9165
- * can lead to sync issues.
9012
+ * SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
9013
+ * implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
9014
+ * possible anymore. Problematic SDK versions have been released before 2025-06-09.
9166
9015
  */
9167
9016
  SyncClientImplementation["RUST"] = "rust";
9168
9017
  })(exports.SyncClientImplementation || (exports.SyncClientImplementation = {}));
@@ -9185,13 +9034,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
9185
9034
  serializedSchema: undefined,
9186
9035
  includeDefaultStreams: true
9187
9036
  };
9188
- // The priority we assume when we receive checkpoint lines where no priority is set.
9189
- // This is the default priority used by the sync service, but can be set to an arbitrary
9190
- // value since sync services without priorities also won't send partial sync completion
9191
- // messages.
9192
- const FALLBACK_PRIORITY = 3;
9193
9037
  class AbstractStreamingSyncImplementation extends BaseObserver {
9194
- _lastSyncedAt;
9195
9038
  options;
9196
9039
  abortController;
9197
9040
  // In rare cases, mostly for tests, uploads can be triggered without being properly connected.
@@ -9201,6 +9044,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
9201
9044
  streamingSyncPromise;
9202
9045
  logger;
9203
9046
  activeStreams;
9047
+ connectionMayHaveChanged = false;
9204
9048
  isUploadingCrud = false;
9205
9049
  notifyCompletedUploads;
9206
9050
  handleActiveStreamsChange;
@@ -9280,9 +9124,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
9280
9124
  this.crudUpdateListener = undefined;
9281
9125
  this.uploadAbortController?.abort();
9282
9126
  }
9283
- async hasCompletedSync() {
9284
- return this.options.adapter.hasCompletedSync();
9285
- }
9286
9127
  async getWriteCheckpoint() {
9287
9128
  const clientId = await this.options.adapter.getClientId();
9288
9129
  let path = `/write-checkpoint2.json?client_id=${clientId}`;
@@ -9364,7 +9205,7 @@ The next upload iteration will be delayed.`);
9364
9205
  });
9365
9206
  }
9366
9207
  }
9367
- this.uploadAbortController = null;
9208
+ this.uploadAbortController = undefined;
9368
9209
  }
9369
9210
  });
9370
9211
  }
@@ -9480,6 +9321,11 @@ The next upload iteration will be delayed.`);
9480
9321
  shouldDelayRetry = false;
9481
9322
  // A disconnect was requested, we should not delay since there is no explicit retry
9482
9323
  }
9324
+ else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
9325
+ this.connectionMayHaveChanged = false;
9326
+ this.logger.info('Sync error after changed connection, retrying immediately');
9327
+ shouldDelayRetry = false;
9328
+ }
9483
9329
  else {
9484
9330
  this.logger.error(ex);
9485
9331
  }
@@ -9510,17 +9356,14 @@ The next upload iteration will be delayed.`);
9510
9356
  // Mark as disconnected if here
9511
9357
  this.updateSyncStatus({ connected: false, connecting: false });
9512
9358
  }
9513
- async collectLocalBucketState() {
9514
- const bucketEntries = await this.options.adapter.getBucketStates();
9515
- const req = bucketEntries.map((entry) => ({
9516
- name: entry.bucket,
9517
- after: entry.op_id
9518
- }));
9519
- const localDescriptions = new Map();
9520
- for (const entry of bucketEntries) {
9521
- localDescriptions.set(entry.bucket, null);
9522
- }
9523
- return [req, localDescriptions];
9359
+ markConnectionMayHaveChanged() {
9360
+ // By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
9361
+ // an active sync iteration on the connection in use.
9362
+ this.connectionMayHaveChanged = true;
9363
+ // This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
9364
+ // make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
9365
+ // iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
9366
+ this.handleActiveStreamsChange?.();
9524
9367
  }
9525
9368
  /**
9526
9369
  * Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
@@ -9563,19 +9406,13 @@ The next upload iteration will be delayed.`);
9563
9406
  }
9564
9407
  const clientImplementation = resolvedOptions.clientImplementation;
9565
9408
  this.updateSyncStatus({ clientImplementation });
9566
- if (clientImplementation == exports.SyncClientImplementation.JAVASCRIPT) {
9567
- await this.legacyStreamingSyncIteration(signal, resolvedOptions);
9568
- return null;
9569
- }
9570
- else {
9571
- await this.requireKeyFormat(true);
9572
- return await this.rustSyncIteration(signal, resolvedOptions);
9573
- }
9409
+ await this.requireKeyFormat(true);
9410
+ return await this.rustSyncIteration(signal, resolvedOptions);
9574
9411
  }
9575
9412
  });
9576
9413
  }
9577
9414
  async receiveSyncLines(data) {
9578
- const { options, connection, bson } = data;
9415
+ const { options, connection } = data;
9579
9416
  const remote = this.options.remote;
9580
9417
  if (connection.connectionMethod == exports.SyncStreamConnectionMethod.HTTP) {
9581
9418
  return await remote.fetchStream(options);
@@ -9584,232 +9421,8 @@ The next upload iteration will be delayed.`);
9584
9421
  return await this.options.remote.socketStreamRaw({
9585
9422
  ...options,
9586
9423
  ...{ fetchStrategy: connection.fetchStrategy }
9587
- }, bson);
9588
- }
9589
- }
9590
- async legacyStreamingSyncIteration(signal, resolvedOptions) {
9591
- const rawTables = resolvedOptions.serializedSchema?.raw_tables;
9592
- if (rawTables != null && rawTables.length) {
9593
- this.logger.warn('Raw tables require the Rust-based sync client. The JS client will ignore them.');
9594
- }
9595
- if (this.activeStreams.length) {
9596
- this.logger.error('Sync streams require `clientImplementation: SyncClientImplementation.RUST` when connecting.');
9597
- }
9598
- this.logger.debug('Streaming sync iteration started');
9599
- this.options.adapter.startSession();
9600
- let [req, bucketMap] = await this.collectLocalBucketState();
9601
- let targetCheckpoint = null;
9602
- // A checkpoint that has been validated but not applied (e.g. due to pending local writes)
9603
- let pendingValidatedCheckpoint = null;
9604
- const clientId = await this.options.adapter.getClientId();
9605
- const usingFixedKeyFormat = await this.requireKeyFormat(false);
9606
- this.logger.debug('Requesting stream from server');
9607
- const syncOptions = {
9608
- path: '/sync/stream',
9609
- abortSignal: signal,
9610
- data: {
9611
- buckets: req,
9612
- include_checksum: true,
9613
- raw_data: true,
9614
- parameters: resolvedOptions.params,
9615
- app_metadata: resolvedOptions.appMetadata,
9616
- client_id: clientId
9617
- }
9618
- };
9619
- const bson = await this.options.remote.getBSON();
9620
- const source = await this.receiveSyncLines({
9621
- options: syncOptions,
9622
- connection: resolvedOptions,
9623
- bson
9624
- });
9625
- const stream = injectable(map(source, (line) => {
9626
- if (typeof line == 'string') {
9627
- return JSON.parse(line);
9628
- }
9629
- else {
9630
- return bson.deserialize(line);
9631
- }
9632
- }));
9633
- this.logger.debug('Stream established. Processing events');
9634
- this.notifyCompletedUploads = () => {
9635
- stream.inject({ crud_upload_completed: null });
9636
- };
9637
- while (true) {
9638
- const { value: line, done } = await stream.next();
9639
- if (done) {
9640
- // The stream has closed while waiting
9641
- return;
9642
- }
9643
- if ('crud_upload_completed' in line) {
9644
- if (pendingValidatedCheckpoint != null) {
9645
- const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
9646
- if (applied) {
9647
- pendingValidatedCheckpoint = null;
9648
- }
9649
- else if (endIteration) {
9650
- break;
9651
- }
9652
- }
9653
- continue;
9654
- }
9655
- // A connection is active and messages are being received
9656
- if (!this.syncStatus.connected) {
9657
- // There is a connection now
9658
- Promise.resolve().then(() => this.triggerCrudUpload());
9659
- this.updateSyncStatus({
9660
- connected: true
9661
- });
9662
- }
9663
- if (isStreamingSyncCheckpoint(line)) {
9664
- targetCheckpoint = line.checkpoint;
9665
- // New checkpoint - existing validated checkpoint is no longer valid
9666
- pendingValidatedCheckpoint = null;
9667
- const bucketsToDelete = new Set(bucketMap.keys());
9668
- const newBuckets = new Map();
9669
- for (const checksum of line.checkpoint.buckets) {
9670
- newBuckets.set(checksum.bucket, {
9671
- name: checksum.bucket,
9672
- priority: checksum.priority ?? FALLBACK_PRIORITY
9673
- });
9674
- bucketsToDelete.delete(checksum.bucket);
9675
- }
9676
- if (bucketsToDelete.size > 0) {
9677
- this.logger.debug('Removing buckets', [...bucketsToDelete]);
9678
- }
9679
- bucketMap = newBuckets;
9680
- await this.options.adapter.removeBuckets([...bucketsToDelete]);
9681
- await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
9682
- await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
9683
- }
9684
- else if (isStreamingSyncCheckpointComplete(line)) {
9685
- const result = await this.applyCheckpoint(targetCheckpoint);
9686
- if (result.endIteration) {
9687
- return;
9688
- }
9689
- else if (!result.applied) {
9690
- // "Could not apply checkpoint due to local data". We need to retry after
9691
- // finishing uploads.
9692
- pendingValidatedCheckpoint = targetCheckpoint;
9693
- }
9694
- else {
9695
- // Nothing to retry later. This would likely already be null from the last
9696
- // checksum or checksum_diff operation, but we make sure.
9697
- pendingValidatedCheckpoint = null;
9698
- }
9699
- }
9700
- else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
9701
- const priority = line.partial_checkpoint_complete.priority;
9702
- this.logger.debug('Partial checkpoint complete', priority);
9703
- const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint, priority);
9704
- if (!result.checkpointValid) {
9705
- // This means checksums failed. Start again with a new checkpoint.
9706
- // TODO: better back-off
9707
- await new Promise((resolve) => setTimeout(resolve, 50));
9708
- return;
9709
- }
9710
- else if (!result.ready) ;
9711
- else {
9712
- // We'll keep on downloading, but can report that this priority is synced now.
9713
- this.logger.debug('partial checkpoint validation succeeded');
9714
- // All states with a higher priority can be deleted since this partial sync includes them.
9715
- const priorityStates = this.syncStatus.priorityStatusEntries.filter((s) => s.priority <= priority);
9716
- priorityStates.push({
9717
- priority,
9718
- lastSyncedAt: new Date(),
9719
- hasSynced: true
9720
- });
9721
- this.updateSyncStatus({
9722
- connected: true,
9723
- priorityStatusEntries: priorityStates
9724
- });
9725
- }
9726
- }
9727
- else if (isStreamingSyncCheckpointDiff(line)) {
9728
- // TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
9729
- if (targetCheckpoint == null) {
9730
- throw new Error('Checkpoint diff without previous checkpoint');
9731
- }
9732
- // New checkpoint - existing validated checkpoint is no longer valid
9733
- pendingValidatedCheckpoint = null;
9734
- const diff = line.checkpoint_diff;
9735
- const newBuckets = new Map();
9736
- for (const checksum of targetCheckpoint.buckets) {
9737
- newBuckets.set(checksum.bucket, checksum);
9738
- }
9739
- for (const checksum of diff.updated_buckets) {
9740
- newBuckets.set(checksum.bucket, checksum);
9741
- }
9742
- for (const bucket of diff.removed_buckets) {
9743
- newBuckets.delete(bucket);
9744
- }
9745
- const newCheckpoint = {
9746
- last_op_id: diff.last_op_id,
9747
- buckets: [...newBuckets.values()],
9748
- write_checkpoint: diff.write_checkpoint
9749
- };
9750
- targetCheckpoint = newCheckpoint;
9751
- await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
9752
- bucketMap = new Map();
9753
- newBuckets.forEach((checksum, name) => bucketMap.set(name, {
9754
- name: checksum.bucket,
9755
- priority: checksum.priority ?? FALLBACK_PRIORITY
9756
- }));
9757
- const bucketsToDelete = diff.removed_buckets;
9758
- if (bucketsToDelete.length > 0) {
9759
- this.logger.debug('Remove buckets', bucketsToDelete);
9760
- }
9761
- await this.options.adapter.removeBuckets(bucketsToDelete);
9762
- await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
9763
- }
9764
- else if (isStreamingSyncData(line)) {
9765
- const { data } = line;
9766
- const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
9767
- let updatedProgress = null;
9768
- if (previousProgress) {
9769
- updatedProgress = { ...previousProgress };
9770
- const progressForBucket = updatedProgress[data.bucket];
9771
- if (progressForBucket) {
9772
- updatedProgress[data.bucket] = {
9773
- ...progressForBucket,
9774
- since_last: progressForBucket.since_last + data.data.length
9775
- };
9776
- }
9777
- }
9778
- this.updateSyncStatus({
9779
- dataFlow: {
9780
- downloading: true,
9781
- downloadProgress: updatedProgress
9782
- }
9783
- });
9784
- await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
9785
- }
9786
- else if (isStreamingKeepalive(line)) {
9787
- const remaining_seconds = line.token_expires_in;
9788
- if (remaining_seconds == 0) {
9789
- // Connection would be closed automatically right after this
9790
- this.logger.debug('Token expiring; reconnect');
9791
- /**
9792
- * For a rare case where the backend connector does not update the token
9793
- * (uses the same one), this should have some delay.
9794
- */
9795
- await this.delayRetry();
9796
- return;
9797
- }
9798
- else if (remaining_seconds < 30) {
9799
- this.logger.debug('Token will expire soon; reconnect');
9800
- // Pre-emptively refresh the token
9801
- this.options.remote.invalidateCredentials();
9802
- return;
9803
- }
9804
- this.triggerCrudUpload();
9805
- }
9806
- else {
9807
- this.logger.debug('Received unknown sync line', line);
9808
- }
9424
+ });
9809
9425
  }
9810
- this.logger.debug('Stream input empty');
9811
- // Connection closed. Likely due to auth issue.
9812
- return;
9813
9426
  }
9814
9427
  async rustSyncIteration(signal, resolvedOptions) {
9815
9428
  const syncImplementation = this;
@@ -9882,6 +9495,10 @@ The next upload iteration will be delayed.`);
9882
9495
  const rawResponse = await adapter.control(op, payload ?? null);
9883
9496
  const logger = syncImplementation.logger;
9884
9497
  logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
9498
+ if (op != exports.PowerSyncControlCommand.STOP) {
9499
+ // Evidently we have a working connection here, otherwise powersync_control would have failed.
9500
+ syncImplementation.connectionMayHaveChanged = false;
9501
+ }
9885
9502
  await handleInstructions(JSON.parse(rawResponse));
9886
9503
  }
9887
9504
  async function handleInstruction(instruction) {
@@ -9968,68 +9585,6 @@ The next upload iteration will be delayed.`);
9968
9585
  }
9969
9586
  return { immediateRestart: hideDisconnectOnRestart };
9970
9587
  }
9971
- async updateSyncStatusForStartingCheckpoint(checkpoint) {
9972
- const localProgress = await this.options.adapter.getBucketOperationProgress();
9973
- const progress = {};
9974
- let invalidated = false;
9975
- for (const bucket of checkpoint.buckets) {
9976
- const savedProgress = localProgress[bucket.bucket];
9977
- const atLast = savedProgress?.atLast ?? 0;
9978
- const sinceLast = savedProgress?.sinceLast ?? 0;
9979
- progress[bucket.bucket] = {
9980
- // The fallback priority doesn't matter here, but 3 is the one newer versions of the sync service
9981
- // will use by default.
9982
- priority: bucket.priority ?? 3,
9983
- at_last: atLast,
9984
- since_last: sinceLast,
9985
- target_count: bucket.count ?? 0
9986
- };
9987
- if (bucket.count != null && bucket.count < atLast + sinceLast) {
9988
- // Either due to a defrag / sync rule deploy or a compaction operation, the size
9989
- // of the bucket shrank so much that the local ops exceed the ops in the updated
9990
- // bucket. We can't prossibly report progress in this case (it would overshoot 100%).
9991
- invalidated = true;
9992
- }
9993
- }
9994
- if (invalidated) {
9995
- for (const bucket in progress) {
9996
- const bucketProgress = progress[bucket];
9997
- bucketProgress.at_last = 0;
9998
- bucketProgress.since_last = 0;
9999
- }
10000
- }
10001
- this.updateSyncStatus({
10002
- dataFlow: {
10003
- downloading: true,
10004
- downloadProgress: progress
10005
- }
10006
- });
10007
- }
10008
- async applyCheckpoint(checkpoint) {
10009
- let result = await this.options.adapter.syncLocalDatabase(checkpoint);
10010
- if (!result.checkpointValid) {
10011
- this.logger.debug(`Checksum mismatch in checkpoint ${checkpoint.last_op_id}, will reconnect`);
10012
- // This means checksums failed. Start again with a new checkpoint.
10013
- // TODO: better back-off
10014
- await new Promise((resolve) => setTimeout(resolve, 50));
10015
- return { applied: false, endIteration: true };
10016
- }
10017
- else if (!result.ready) {
10018
- 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.`);
10019
- return { applied: false, endIteration: false };
10020
- }
10021
- this.logger.debug(`Applied checkpoint ${checkpoint.last_op_id}`, checkpoint);
10022
- this.updateSyncStatus({
10023
- connected: true,
10024
- lastSyncedAt: new Date(),
10025
- dataFlow: {
10026
- downloading: false,
10027
- downloadProgress: null,
10028
- downloadError: undefined
10029
- }
10030
- });
10031
- return { applied: true, endIteration: false };
10032
- }
10033
9588
  updateSyncStatus(options) {
10034
9589
  const updatedStatus = new SyncStatus({
10035
9590
  connected: options.connected ?? this.syncStatus.connected,
@@ -11568,14 +11123,12 @@ class SqliteBucketStorage extends BaseObserver {
11568
11123
  db;
11569
11124
  logger;
11570
11125
  tableNames;
11571
- _hasCompletedSync;
11572
11126
  updateListener;
11573
11127
  _clientId;
11574
11128
  constructor(db, logger = Logger.get('SqliteBucketStorage')) {
11575
11129
  super();
11576
11130
  this.db = db;
11577
11131
  this.logger = logger;
11578
- this._hasCompletedSync = false;
11579
11132
  this.tableNames = new Set();
11580
11133
  this.updateListener = db.registerListener({
11581
11134
  tablesUpdated: (update) => {
@@ -11587,7 +11140,6 @@ class SqliteBucketStorage extends BaseObserver {
11587
11140
  });
11588
11141
  }
11589
11142
  async init() {
11590
- this._hasCompletedSync = false;
11591
11143
  const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
11592
11144
  for (const row of existingTableRows ?? []) {
11593
11145
  this.tableNames.add(row.name);
@@ -11609,156 +11161,6 @@ class SqliteBucketStorage extends BaseObserver {
11609
11161
  getMaxOpId() {
11610
11162
  return MAX_OP_ID;
11611
11163
  }
11612
- /**
11613
- * Reset any caches.
11614
- */
11615
- startSession() { }
11616
- async getBucketStates() {
11617
- 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'");
11618
- return result;
11619
- }
11620
- async getBucketOperationProgress() {
11621
- const rows = await this.db.getAll('SELECT name, count_at_last, count_since_last FROM ps_buckets');
11622
- return Object.fromEntries(rows.map((r) => [r.name, { atLast: r.count_at_last, sinceLast: r.count_since_last }]));
11623
- }
11624
- async saveSyncData(batch, fixedKeyFormat = false) {
11625
- await this.writeTransaction(async (tx) => {
11626
- for (const b of batch.buckets) {
11627
- await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
11628
- 'save',
11629
- JSON.stringify({ buckets: [b.toJSON(fixedKeyFormat)] })
11630
- ]);
11631
- this.logger.debug(`Saved batch of data for bucket: ${b.bucket}, operations: ${b.data.length}`);
11632
- }
11633
- });
11634
- }
11635
- async removeBuckets(buckets) {
11636
- for (const bucket of buckets) {
11637
- await this.deleteBucket(bucket);
11638
- }
11639
- }
11640
- /**
11641
- * Mark a bucket for deletion.
11642
- */
11643
- async deleteBucket(bucket) {
11644
- await this.writeTransaction(async (tx) => {
11645
- await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', ['delete_bucket', bucket]);
11646
- });
11647
- this.logger.debug(`Done deleting bucket ${bucket}`);
11648
- }
11649
- async hasCompletedSync() {
11650
- if (this._hasCompletedSync) {
11651
- return true;
11652
- }
11653
- const r = await this.db.get(`SELECT powersync_last_synced_at() as synced_at`);
11654
- const completed = r.synced_at != null;
11655
- if (completed) {
11656
- this._hasCompletedSync = true;
11657
- }
11658
- return completed;
11659
- }
11660
- async syncLocalDatabase(checkpoint, priority) {
11661
- const r = await this.validateChecksums(checkpoint, priority);
11662
- if (!r.checkpointValid) {
11663
- this.logger.error('Checksums failed for', r.checkpointFailures);
11664
- for (const b of r.checkpointFailures ?? []) {
11665
- await this.deleteBucket(b);
11666
- }
11667
- return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
11668
- }
11669
- if (priority == null) {
11670
- this.logger.debug(`Validated checksums checkpoint ${checkpoint.last_op_id}`);
11671
- }
11672
- else {
11673
- this.logger.debug(`Validated checksums for partial checkpoint ${checkpoint.last_op_id}, priority ${priority}`);
11674
- }
11675
- let buckets = checkpoint.buckets;
11676
- if (priority !== undefined) {
11677
- buckets = buckets.filter((b) => hasMatchingPriority(priority, b));
11678
- }
11679
- const bucketNames = buckets.map((b) => b.bucket);
11680
- await this.writeTransaction(async (tx) => {
11681
- await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
11682
- checkpoint.last_op_id,
11683
- JSON.stringify(bucketNames)
11684
- ]);
11685
- if (priority == null && checkpoint.write_checkpoint) {
11686
- await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
11687
- }
11688
- });
11689
- const valid = await this.updateObjectsFromBuckets(checkpoint, priority);
11690
- if (!valid) {
11691
- return { ready: false, checkpointValid: true };
11692
- }
11693
- return {
11694
- ready: true,
11695
- checkpointValid: true
11696
- };
11697
- }
11698
- /**
11699
- * Atomically update the local state to the current checkpoint.
11700
- *
11701
- * This includes creating new tables, dropping old tables, and copying data over from the oplog.
11702
- */
11703
- async updateObjectsFromBuckets(checkpoint, priority) {
11704
- let arg = '';
11705
- if (priority !== undefined) {
11706
- const affectedBuckets = [];
11707
- for (const desc of checkpoint.buckets) {
11708
- if (hasMatchingPriority(priority, desc)) {
11709
- affectedBuckets.push(desc.bucket);
11710
- }
11711
- }
11712
- arg = JSON.stringify({ priority, buckets: affectedBuckets });
11713
- }
11714
- return this.writeTransaction(async (tx) => {
11715
- const { insertId: result } = await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
11716
- 'sync_local',
11717
- arg
11718
- ]);
11719
- if (result == 1) {
11720
- if (priority == null) {
11721
- const bucketToCount = Object.fromEntries(checkpoint.buckets.map((b) => [b.bucket, b.count]));
11722
- // The two parameters could be replaced with one, but: https://github.com/powersync-ja/better-sqlite3/pull/6
11723
- const jsonBucketCount = JSON.stringify(bucketToCount);
11724
- 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]);
11725
- }
11726
- return true;
11727
- }
11728
- else {
11729
- return false;
11730
- }
11731
- });
11732
- }
11733
- async validateChecksums(checkpoint, priority) {
11734
- if (priority !== undefined) {
11735
- // Only validate the buckets within the priority we care about
11736
- const newBuckets = checkpoint.buckets.filter((cs) => hasMatchingPriority(priority, cs));
11737
- checkpoint = { ...checkpoint, buckets: newBuckets };
11738
- }
11739
- const rs = await this.db.execute('SELECT powersync_validate_checkpoint(?) as result', [
11740
- JSON.stringify({ ...checkpoint })
11741
- ]);
11742
- const resultItem = rs.rows?.item(0);
11743
- if (!resultItem) {
11744
- return {
11745
- checkpointValid: false,
11746
- ready: false,
11747
- checkpointFailures: []
11748
- };
11749
- }
11750
- const result = JSON.parse(resultItem['result']);
11751
- if (result['valid']) {
11752
- return { ready: true, checkpointValid: true };
11753
- }
11754
- else {
11755
- return {
11756
- checkpointValid: false,
11757
- ready: false,
11758
- checkpointFailures: result['failed_buckets']
11759
- };
11760
- }
11761
- }
11762
11164
  async updateLocalTarget(cb) {
11763
11165
  const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)", [MAX_OP_ID]);
11764
11166
  if (!rs1.length) {
@@ -11849,12 +11251,6 @@ class SqliteBucketStorage extends BaseObserver {
11849
11251
  async writeTransaction(callback, options) {
11850
11252
  return this.db.writeTransaction(callback, options);
11851
11253
  }
11852
- /**
11853
- * Set a target checkpoint.
11854
- */
11855
- async setTargetCheckpoint(checkpoint) {
11856
- // No-op for now
11857
- }
11858
11254
  async control(op, payload) {
11859
11255
  return await this.writeTransaction(async (tx) => {
11860
11256
  const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
@@ -11878,20 +11274,6 @@ class SqliteBucketStorage extends BaseObserver {
11878
11274
  }
11879
11275
  static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
11880
11276
  }
11881
- function hasMatchingPriority(priority, bucket) {
11882
- return bucket.priority != null && bucket.priority <= priority;
11883
- }
11884
-
11885
- // TODO JSON
11886
- class SyncDataBatch {
11887
- buckets;
11888
- static fromJSON(json) {
11889
- return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
11890
- }
11891
- constructor(buckets) {
11892
- this.buckets = buckets;
11893
- }
11894
- }
11895
11277
 
11896
11278
  /**
11897
11279
  * Thrown when an underlying database connection is closed.
@@ -11951,10 +11333,8 @@ class Schema {
11951
11333
  * developer instead of automatically by PowerSync.
11952
11334
  * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
11953
11335
  * using client-side table and column constraints.
11954
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
11955
11336
  *
11956
11337
  * @param tables An object of (table name, raw table definition) entries.
11957
- * @experimental Note that the raw tables API is still experimental and may change in the future.
11958
11338
  */
11959
11339
  withRawTables(tables) {
11960
11340
  for (const [name, rawTableDefinition] of Object.entries(tables)) {
@@ -12204,13 +11584,9 @@ exports.MAX_OP_ID = MAX_OP_ID;
12204
11584
  exports.MEMORY_TRIGGER_CLAIM_MANAGER = MEMORY_TRIGGER_CLAIM_MANAGER;
12205
11585
  exports.Mutex = Mutex;
12206
11586
  exports.OnChangeQueryProcessor = OnChangeQueryProcessor;
12207
- exports.OpType = OpType;
12208
- exports.OplogEntry = OplogEntry;
12209
11587
  exports.Schema = Schema;
12210
11588
  exports.Semaphore = Semaphore;
12211
11589
  exports.SqliteBucketStorage = SqliteBucketStorage;
12212
- exports.SyncDataBatch = SyncDataBatch;
12213
- exports.SyncDataBucket = SyncDataBucket;
12214
11590
  exports.SyncProgress = SyncProgress;
12215
11591
  exports.SyncStatus = SyncStatus;
12216
11592
  exports.SyncingService = SyncingService;
@@ -12225,18 +11601,10 @@ exports.createBaseLogger = createBaseLogger;
12225
11601
  exports.createLogger = createLogger;
12226
11602
  exports.extractTableUpdates = extractTableUpdates;
12227
11603
  exports.isBatchedUpdateNotification = isBatchedUpdateNotification;
12228
- exports.isContinueCheckpointRequest = isContinueCheckpointRequest;
12229
11604
  exports.isDBAdapter = isDBAdapter;
12230
11605
  exports.isPowerSyncDatabaseOptionsWithSettings = isPowerSyncDatabaseOptionsWithSettings;
12231
11606
  exports.isSQLOpenFactory = isSQLOpenFactory;
12232
11607
  exports.isSQLOpenOptions = isSQLOpenOptions;
12233
- exports.isStreamingKeepalive = isStreamingKeepalive;
12234
- exports.isStreamingSyncCheckpoint = isStreamingSyncCheckpoint;
12235
- exports.isStreamingSyncCheckpointComplete = isStreamingSyncCheckpointComplete;
12236
- exports.isStreamingSyncCheckpointDiff = isStreamingSyncCheckpointDiff;
12237
- exports.isStreamingSyncCheckpointPartiallyComplete = isStreamingSyncCheckpointPartiallyComplete;
12238
- exports.isStreamingSyncData = isStreamingSyncData;
12239
- exports.isSyncNewCheckpointRequest = isSyncNewCheckpointRequest;
12240
11608
  exports.parseQuery = parseQuery;
12241
11609
  exports.runOnSchemaChange = runOnSchemaChange;
12242
11610
  exports.sanitizeSQL = sanitizeSQL;