@powersync/common 1.52.0 → 1.53.1

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 (79) hide show
  1. package/dist/bundle.cjs +167 -767
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +168 -756
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +167 -767
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +168 -756
  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/lib/utils/stream_transform.js +6 -1
  45. package/lib/utils/stream_transform.js.map +1 -1
  46. package/package.json +9 -6
  47. package/src/client/ConnectionManager.ts +1 -1
  48. package/src/client/sync/bucket/BucketStorageAdapter.ts +6 -71
  49. package/src/client/sync/bucket/SqliteBucketStorage.ts +1 -197
  50. package/src/client/sync/stream/AbstractRemote.ts +5 -27
  51. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +168 -496
  52. package/src/client/sync/stream/JsonValue.ts +8 -0
  53. package/src/client/sync/stream/core-instruction.ts +15 -5
  54. package/src/db/DBAdapter.ts +20 -2
  55. package/src/db/crud/SyncStatus.ts +4 -5
  56. package/src/db/schema/RawTable.ts +0 -5
  57. package/src/db/schema/Schema.ts +0 -2
  58. package/src/index.ts +1 -5
  59. package/src/utils/stream_transform.ts +7 -1
  60. package/lib/client/sync/bucket/OpType.d.ts +0 -16
  61. package/lib/client/sync/bucket/OpType.js +0 -23
  62. package/lib/client/sync/bucket/OpType.js.map +0 -1
  63. package/lib/client/sync/bucket/OplogEntry.d.ts +0 -23
  64. package/lib/client/sync/bucket/OplogEntry.js +0 -36
  65. package/lib/client/sync/bucket/OplogEntry.js.map +0 -1
  66. package/lib/client/sync/bucket/SyncDataBatch.d.ts +0 -6
  67. package/lib/client/sync/bucket/SyncDataBatch.js +0 -12
  68. package/lib/client/sync/bucket/SyncDataBatch.js.map +0 -1
  69. package/lib/client/sync/bucket/SyncDataBucket.d.ts +0 -40
  70. package/lib/client/sync/bucket/SyncDataBucket.js +0 -40
  71. package/lib/client/sync/bucket/SyncDataBucket.js.map +0 -1
  72. package/lib/client/sync/stream/streaming-sync-types.d.ts +0 -143
  73. package/lib/client/sync/stream/streaming-sync-types.js +0 -26
  74. package/lib/client/sync/stream/streaming-sync-types.js.map +0 -1
  75. package/src/client/sync/bucket/OpType.ts +0 -23
  76. package/src/client/sync/bucket/OplogEntry.ts +0 -50
  77. package/src/client/sync/bucket/SyncDataBatch.ts +0 -11
  78. package/src/client/sync/bucket/SyncDataBucket.ts +0 -49
  79. 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.1";
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
  *
@@ -8420,6 +8314,7 @@ function injectable(source) {
8420
8314
  let waiter = undefined; // An active, waiting next() call.
8421
8315
  // A pending upstream event that couldn't be dispatched because inject() has been called before it was resolved.
8422
8316
  let pendingSourceEvent = null;
8317
+ let sourceFetchInFlight = false;
8423
8318
  let pendingInjectedEvents = [];
8424
8319
  const consumeWaiter = () => {
8425
8320
  const pending = waiter;
@@ -8428,6 +8323,7 @@ function injectable(source) {
8428
8323
  };
8429
8324
  const fetchFromSource = () => {
8430
8325
  const resolveWaiter = (propagate) => {
8326
+ sourceFetchInFlight = false;
8431
8327
  const active = consumeWaiter();
8432
8328
  if (active) {
8433
8329
  propagate(active);
@@ -8436,6 +8332,7 @@ function injectable(source) {
8436
8332
  pendingSourceEvent = propagate;
8437
8333
  }
8438
8334
  };
8335
+ sourceFetchInFlight = true;
8439
8336
  const nextFromSource = source.next();
8440
8337
  nextFromSource.then((value) => {
8441
8338
  sourceIsDone = value.done == true;
@@ -8462,7 +8359,9 @@ function injectable(source) {
8462
8359
  }
8463
8360
  // Nothing pending? Fetch from source
8464
8361
  waiter = { resolve, reject };
8465
- return fetchFromSource();
8362
+ if (!sourceFetchInFlight) {
8363
+ fetchFromSource();
8364
+ }
8466
8365
  });
8467
8366
  },
8468
8367
  inject: (event) => {
@@ -8777,22 +8676,12 @@ class AbstractRemote {
8777
8676
  * Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
8778
8677
  *
8779
8678
  * 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
8679
  */
8784
- async socketStreamRaw(options, bson) {
8680
+ async socketStreamRaw(options) {
8785
8681
  const { path, fetchStrategy = FetchStrategy.Buffered } = options;
8786
- const mimeType = bson == null ? 'application/json' : 'application/bson';
8682
+ const mimeType = 'application/json';
8787
8683
  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);
8684
+ return Buffer.from(JSON.stringify(js));
8796
8685
  }
8797
8686
  const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
8798
8687
  const request = await this.buildRequest(path);
@@ -9093,31 +8982,8 @@ function coreStatusToJs(status) {
9093
8982
  priorityStatusEntries: status.priority_status.map(priorityToJs)
9094
8983
  };
9095
8984
  }
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';
8985
+ function isInterruptingInstruction(instruction) {
8986
+ return 'EstablishSyncStream' in instruction || 'CloseSyncStream' in instruction;
9121
8987
  }
9122
8988
 
9123
8989
  var LockType;
@@ -9132,35 +8998,21 @@ var SyncStreamConnectionMethod;
9132
8998
  })(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
9133
8999
  var SyncClientImplementation;
9134
9000
  (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
9001
  /**
9146
9002
  * This implementation offloads the sync line decoding and handling into the PowerSync
9147
9003
  * core extension.
9148
9004
  *
9149
- * This option is more performant than the {@link JAVASCRIPT} client, enabled by default and the
9150
- * recommended client implementation for all apps.
9005
+ * This is the only option, as an older JavaScript client implementation has been removed from the SDK.
9151
9006
  *
9152
9007
  * ## Compatibility warning
9153
9008
  *
9154
9009
  * 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.
9010
+ * by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
9011
+ * migrate the format automatically.
9160
9012
  *
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.
9013
+ * SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
9014
+ * implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
9015
+ * possible anymore. Problematic SDK versions have been released before 2025-06-09.
9164
9016
  */
9165
9017
  SyncClientImplementation["RUST"] = "rust";
9166
9018
  })(SyncClientImplementation || (SyncClientImplementation = {}));
@@ -9183,13 +9035,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
9183
9035
  serializedSchema: undefined,
9184
9036
  includeDefaultStreams: true
9185
9037
  };
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
9038
  class AbstractStreamingSyncImplementation extends BaseObserver {
9192
- _lastSyncedAt;
9193
9039
  options;
9194
9040
  abortController;
9195
9041
  // In rare cases, mostly for tests, uploads can be triggered without being properly connected.
@@ -9199,6 +9045,7 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
9199
9045
  streamingSyncPromise;
9200
9046
  logger;
9201
9047
  activeStreams;
9048
+ connectionMayHaveChanged = false;
9202
9049
  isUploadingCrud = false;
9203
9050
  notifyCompletedUploads;
9204
9051
  handleActiveStreamsChange;
@@ -9278,9 +9125,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
9278
9125
  this.crudUpdateListener = undefined;
9279
9126
  this.uploadAbortController?.abort();
9280
9127
  }
9281
- async hasCompletedSync() {
9282
- return this.options.adapter.hasCompletedSync();
9283
- }
9284
9128
  async getWriteCheckpoint() {
9285
9129
  const clientId = await this.options.adapter.getClientId();
9286
9130
  let path = `/write-checkpoint2.json?client_id=${clientId}`;
@@ -9362,7 +9206,7 @@ The next upload iteration will be delayed.`);
9362
9206
  });
9363
9207
  }
9364
9208
  }
9365
- this.uploadAbortController = null;
9209
+ this.uploadAbortController = undefined;
9366
9210
  }
9367
9211
  });
9368
9212
  }
@@ -9478,6 +9322,11 @@ The next upload iteration will be delayed.`);
9478
9322
  shouldDelayRetry = false;
9479
9323
  // A disconnect was requested, we should not delay since there is no explicit retry
9480
9324
  }
9325
+ else if (this.connectionMayHaveChanged && ex.message?.indexOf('No iteration is active') >= 0) {
9326
+ this.connectionMayHaveChanged = false;
9327
+ this.logger.info('Sync error after changed connection, retrying immediately');
9328
+ shouldDelayRetry = false;
9329
+ }
9481
9330
  else {
9482
9331
  this.logger.error(ex);
9483
9332
  }
@@ -9508,17 +9357,14 @@ The next upload iteration will be delayed.`);
9508
9357
  // Mark as disconnected if here
9509
9358
  this.updateSyncStatus({ connected: false, connecting: false });
9510
9359
  }
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];
9360
+ markConnectionMayHaveChanged() {
9361
+ // By setting this field, we'll immediately retry if the next sync event causes an error triggered by us not having
9362
+ // an active sync iteration on the connection in use.
9363
+ this.connectionMayHaveChanged = true;
9364
+ // This triggers a `powersync_control` invocation if a sync iteration is currently active. This is a cheap call to
9365
+ // make when no subscriptions have actually changed, we're mainly interested in this immediately throwing if no
9366
+ // iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
9367
+ this.handleActiveStreamsChange?.();
9522
9368
  }
9523
9369
  /**
9524
9370
  * Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
@@ -9559,328 +9405,98 @@ The next upload iteration will be delayed.`);
9559
9405
  if (invalidMetadata.length > 0) {
9560
9406
  throw new Error(`Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`);
9561
9407
  }
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
- }
9408
+ await this.requireKeyFormat(true);
9409
+ return await this.rustSyncIteration(signal, resolvedOptions);
9572
9410
  }
9573
9411
  });
9574
9412
  }
9575
- async receiveSyncLines(data) {
9576
- const { options, connection, bson } = data;
9413
+ receiveSyncLines(data) {
9414
+ const { options, connection } = data;
9577
9415
  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);
9416
+ const openInner = async () => {
9417
+ if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
9418
+ return await remote.fetchStream(options);
9626
9419
  }
9627
9420
  else {
9628
- return bson.deserialize(line);
9421
+ return await this.options.remote.socketStreamRaw({
9422
+ ...options,
9423
+ ...{ fetchStrategy: connection.fetchStrategy }
9424
+ });
9629
9425
  }
9630
- }));
9631
- this.logger.debug('Stream established. Processing events');
9632
- this.notifyCompletedUploads = () => {
9633
- stream.inject({ crud_upload_completed: null });
9634
9426
  };
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
- }
9427
+ let inner;
9428
+ let done = false;
9429
+ return {
9430
+ async next() {
9431
+ if (done) {
9432
+ return doneResult;
9650
9433
  }
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
9434
+ else if (inner == null) {
9435
+ inner = await openInner();
9436
+ // We're connected here, so we can tell the core extension about it.
9437
+ return valueResult({
9438
+ command: PowerSyncControlCommand.CONNECTION_STATE,
9439
+ payload: 'established'
9671
9440
  });
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
9441
  }
9692
9442
  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
- };
9443
+ const event = await inner.next();
9444
+ if (event.done) {
9445
+ done = true;
9446
+ return valueResult({ command: PowerSyncControlCommand.CONNECTION_STATE, payload: 'end' });
9774
9447
  }
9775
- }
9776
- this.updateSyncStatus({
9777
- dataFlow: {
9778
- downloading: true,
9779
- downloadProgress: updatedProgress
9448
+ else {
9449
+ return valueResult({
9450
+ command: typeof event.value == 'string'
9451
+ ? PowerSyncControlCommand.PROCESS_TEXT_LINE
9452
+ : PowerSyncControlCommand.PROCESS_BSON_LINE,
9453
+ payload: event.value
9454
+ });
9780
9455
  }
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
9456
  }
9802
- this.triggerCrudUpload();
9803
- }
9804
- else {
9805
- this.logger.debug('Received unknown sync line', line);
9806
9457
  }
9807
- }
9808
- this.logger.debug('Stream input empty');
9809
- // Connection closed. Likely due to auth issue.
9810
- return;
9458
+ };
9811
9459
  }
9812
9460
  async rustSyncIteration(signal, resolvedOptions) {
9813
9461
  const syncImplementation = this;
9814
9462
  const adapter = this.options.adapter;
9815
9463
  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
9464
  let hideDisconnectOnRestart = false;
9465
+ let notifyTokenRefreshed;
9824
9466
  if (signal.aborted) {
9825
9467
  throw new AbortOperation('Connection request has been aborted');
9826
9468
  }
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
9469
+ function startCommand() {
9470
+ const options = {
9471
+ parameters: resolvedOptions.params,
9472
+ app_metadata: resolvedOptions.appMetadata,
9473
+ active_streams: syncImplementation.activeStreams,
9474
+ include_defaults: resolvedOptions.includeDefaultStreams
9836
9475
  };
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);
9476
+ if (resolvedOptions.serializedSchema) {
9477
+ options.schema = resolvedOptions.serializedSchema;
9874
9478
  }
9479
+ return invokePowerSyncControl(PowerSyncControlCommand.START, JSON.stringify(options));
9875
9480
  }
9876
9481
  async function stop() {
9877
- await control(PowerSyncControlCommand.STOP);
9482
+ const instructions = await invokePowerSyncControl(PowerSyncControlCommand.STOP);
9483
+ for (const instruction of instructions) {
9484
+ // We don't need to handle interrupting instructions since we're unconditionally ending the sync iteration at
9485
+ // this point.
9486
+ if (isInterruptingInstruction(instruction))
9487
+ continue;
9488
+ await handleInstruction(instruction);
9489
+ }
9878
9490
  }
9879
- async function control(op, payload) {
9491
+ async function invokePowerSyncControl(op, payload) {
9880
9492
  const rawResponse = await adapter.control(op, payload ?? null);
9881
9493
  const logger = syncImplementation.logger;
9882
9494
  logger.trace('powersync_control', op, payload == null || typeof payload == 'string' ? payload : '<bytes>', rawResponse);
9883
- await handleInstructions(JSON.parse(rawResponse));
9495
+ if (op != PowerSyncControlCommand.STOP) {
9496
+ // Evidently we have a working connection here, otherwise powersync_control would have failed.
9497
+ syncImplementation.connectionMayHaveChanged = false;
9498
+ }
9499
+ return JSON.parse(rawResponse);
9884
9500
  }
9885
9501
  async function handleInstruction(instruction) {
9886
9502
  if ('LogLine' in instruction) {
@@ -9899,13 +9515,6 @@ The next upload iteration will be delayed.`);
9899
9515
  else if ('UpdateSyncStatus' in instruction) {
9900
9516
  syncImplementation.updateSyncStatus(coreStatusToJs(instruction.UpdateSyncStatus.status));
9901
9517
  }
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
9518
  else if ('FetchCredentials' in instruction) {
9910
9519
  if (instruction.FetchCredentials.did_expire) {
9911
9520
  remote.invalidateCredentials();
@@ -9914,16 +9523,12 @@ The next upload iteration will be delayed.`);
9914
9523
  remote.invalidateCredentials();
9915
9524
  // Restart iteration after the credentials have been refreshed.
9916
9525
  remote.fetchCredentials().then((_) => {
9917
- controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED });
9526
+ notifyTokenRefreshed?.();
9918
9527
  }, (err) => {
9919
9528
  syncImplementation.logger.warn('Could not prefetch credentials', err);
9920
9529
  });
9921
9530
  }
9922
9531
  }
9923
- else if ('CloseSyncStream' in instruction) {
9924
- controller.abort();
9925
- hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
9926
- }
9927
9532
  else if ('FlushFileSystem' in instruction) ;
9928
9533
  else if ('DidCompleteSync' in instruction) {
9929
9534
  syncImplementation.updateSyncStatus({
@@ -9933,101 +9538,83 @@ The next upload iteration will be delayed.`);
9933
9538
  });
9934
9539
  }
9935
9540
  }
9936
- async function handleInstructions(instructions) {
9937
- for (const instr of instructions) {
9938
- await handleInstruction(instr);
9939
- }
9940
- }
9941
9541
  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;
9542
+ const defaultResult = { immediateRestart: false };
9543
+ // Pending sync lines received from the service, as well as local events that trigger a powersync_control
9544
+ // invocation (local events include refreshed tokens and completed uploads).
9545
+ // This is a single data stream so that we can handle all control calls from a single place.
9546
+ let controlInvocations = null;
9547
+ for (const startInstruction of await startCommand()) {
9548
+ if ('EstablishSyncStream' in startInstruction) {
9549
+ const syncOptions = {
9550
+ path: '/sync/stream',
9551
+ abortSignal: signal,
9552
+ data: startInstruction.EstablishSyncStream.request
9553
+ };
9554
+ controlInvocations = injectable(syncImplementation.receiveSyncLines({
9555
+ options: syncOptions,
9556
+ connection: resolvedOptions
9557
+ }));
9558
+ }
9559
+ else if ('CloseSyncStream' in startInstruction) {
9560
+ return defaultResult;
9561
+ }
9562
+ else {
9563
+ await handleInstruction(startInstruction);
9564
+ }
9950
9565
  }
9951
- await control(PowerSyncControlCommand.START, JSON.stringify(options));
9566
+ if (controlInvocations == null)
9567
+ return defaultResult;
9952
9568
  this.notifyCompletedUploads = () => {
9953
- controlInvocations?.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
9569
+ controlInvocations.inject({ command: PowerSyncControlCommand.NOTIFY_CRUD_UPLOAD_COMPLETED });
9954
9570
  };
9955
9571
  this.handleActiveStreamsChange = () => {
9956
- controlInvocations?.inject({
9572
+ controlInvocations.inject({
9957
9573
  command: PowerSyncControlCommand.UPDATE_SUBSCRIPTIONS,
9958
9574
  payload: JSON.stringify(this.activeStreams)
9959
9575
  });
9960
9576
  };
9961
- await receivingLines;
9577
+ notifyTokenRefreshed = () => {
9578
+ controlInvocations.inject({
9579
+ command: PowerSyncControlCommand.NOTIFY_TOKEN_REFRESHED
9580
+ });
9581
+ };
9582
+ let hadSyncLine = false;
9583
+ loop: while (true) {
9584
+ const { done, value } = await controlInvocations.next();
9585
+ if (done)
9586
+ break;
9587
+ if (!hadSyncLine) {
9588
+ // Trigger a local CRUD upload when the first sync line has been received, this allows uploading local changes
9589
+ // that have been made while offline or disconnected.
9590
+ if (value.command == PowerSyncControlCommand.PROCESS_TEXT_LINE ||
9591
+ value.command == PowerSyncControlCommand.PROCESS_BSON_LINE) {
9592
+ hadSyncLine = true;
9593
+ this.triggerCrudUpload?.();
9594
+ }
9595
+ }
9596
+ const instructions = await invokePowerSyncControl(value.command, value.payload);
9597
+ for (const instruction of instructions) {
9598
+ if ('EstablishSyncStream' in instruction) {
9599
+ throw new Error('Received EstablishSyncStream while already connected.');
9600
+ }
9601
+ else if ('CloseSyncStream' in instruction) {
9602
+ hideDisconnectOnRestart = instruction.CloseSyncStream.hide_disconnect;
9603
+ break loop;
9604
+ }
9605
+ else {
9606
+ await handleInstruction(instruction);
9607
+ }
9608
+ }
9609
+ }
9962
9610
  }
9963
9611
  finally {
9964
9612
  this.notifyCompletedUploads = this.handleActiveStreamsChange = undefined;
9613
+ notifyTokenRefreshed = undefined;
9965
9614
  await stop();
9966
9615
  }
9967
9616
  return { immediateRestart: hideDisconnectOnRestart };
9968
9617
  }
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
9618
  updateSyncStatus(options) {
10032
9619
  const updatedStatus = new SyncStatus({
10033
9620
  connected: options.connected ?? this.syncStatus.connected,
@@ -11566,14 +11153,12 @@ class SqliteBucketStorage extends BaseObserver {
11566
11153
  db;
11567
11154
  logger;
11568
11155
  tableNames;
11569
- _hasCompletedSync;
11570
11156
  updateListener;
11571
11157
  _clientId;
11572
11158
  constructor(db, logger = Logger.get('SqliteBucketStorage')) {
11573
11159
  super();
11574
11160
  this.db = db;
11575
11161
  this.logger = logger;
11576
- this._hasCompletedSync = false;
11577
11162
  this.tableNames = new Set();
11578
11163
  this.updateListener = db.registerListener({
11579
11164
  tablesUpdated: (update) => {
@@ -11585,7 +11170,6 @@ class SqliteBucketStorage extends BaseObserver {
11585
11170
  });
11586
11171
  }
11587
11172
  async init() {
11588
- this._hasCompletedSync = false;
11589
11173
  const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
11590
11174
  for (const row of existingTableRows ?? []) {
11591
11175
  this.tableNames.add(row.name);
@@ -11607,156 +11191,6 @@ class SqliteBucketStorage extends BaseObserver {
11607
11191
  getMaxOpId() {
11608
11192
  return MAX_OP_ID;
11609
11193
  }
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
11194
  async updateLocalTarget(cb) {
11761
11195
  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
11196
  if (!rs1.length) {
@@ -11847,12 +11281,6 @@ class SqliteBucketStorage extends BaseObserver {
11847
11281
  async writeTransaction(callback, options) {
11848
11282
  return this.db.writeTransaction(callback, options);
11849
11283
  }
11850
- /**
11851
- * Set a target checkpoint.
11852
- */
11853
- async setTargetCheckpoint(checkpoint) {
11854
- // No-op for now
11855
- }
11856
11284
  async control(op, payload) {
11857
11285
  return await this.writeTransaction(async (tx) => {
11858
11286
  const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
@@ -11876,20 +11304,6 @@ class SqliteBucketStorage extends BaseObserver {
11876
11304
  }
11877
11305
  static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
11878
11306
  }
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
11307
 
11894
11308
  /**
11895
11309
  * Thrown when an underlying database connection is closed.
@@ -11949,10 +11363,8 @@ class Schema {
11949
11363
  * developer instead of automatically by PowerSync.
11950
11364
  * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
11951
11365
  * using client-side table and column constraints.
11952
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
11953
11366
  *
11954
11367
  * @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
11368
  */
11957
11369
  withRawTables(tables) {
11958
11370
  for (const [name, rawTableDefinition] of Object.entries(tables)) {
@@ -12149,5 +11561,5 @@ const parseQuery = (query, parameters) => {
12149
11561
  return { sqlStatement, parameters: parameters };
12150
11562
  };
12151
11563
 
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 };
11564
+ 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
11565
  //# sourceMappingURL=bundle.node.mjs.map