@powersync/web 0.0.0-dev-20260503073249 → 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.
@@ -2857,9 +2857,6 @@ __webpack_require__.r(__webpack_exports__);
2857
2857
  /* harmony export */ MEMORY_TRIGGER_CLAIM_MANAGER: () => (/* binding */ MEMORY_TRIGGER_CLAIM_MANAGER),
2858
2858
  /* harmony export */ Mutex: () => (/* binding */ Mutex),
2859
2859
  /* harmony export */ OnChangeQueryProcessor: () => (/* binding */ OnChangeQueryProcessor),
2860
- /* harmony export */ OpType: () => (/* binding */ OpType),
2861
- /* harmony export */ OpTypeEnum: () => (/* binding */ OpTypeEnum),
2862
- /* harmony export */ OplogEntry: () => (/* binding */ OplogEntry),
2863
2860
  /* harmony export */ PSInternalTable: () => (/* binding */ PSInternalTable),
2864
2861
  /* harmony export */ PowerSyncControlCommand: () => (/* binding */ PowerSyncControlCommand),
2865
2862
  /* harmony export */ RowUpdateType: () => (/* binding */ RowUpdateType),
@@ -2867,8 +2864,6 @@ __webpack_require__.r(__webpack_exports__);
2867
2864
  /* harmony export */ Semaphore: () => (/* binding */ Semaphore),
2868
2865
  /* harmony export */ SqliteBucketStorage: () => (/* binding */ SqliteBucketStorage),
2869
2866
  /* harmony export */ SyncClientImplementation: () => (/* binding */ SyncClientImplementation),
2870
- /* harmony export */ SyncDataBatch: () => (/* binding */ SyncDataBatch),
2871
- /* harmony export */ SyncDataBucket: () => (/* binding */ SyncDataBucket),
2872
2867
  /* harmony export */ SyncProgress: () => (/* binding */ SyncProgress),
2873
2868
  /* harmony export */ SyncStatus: () => (/* binding */ SyncStatus),
2874
2869
  /* harmony export */ SyncStreamConnectionMethod: () => (/* binding */ SyncStreamConnectionMethod),
@@ -2886,18 +2881,10 @@ __webpack_require__.r(__webpack_exports__);
2886
2881
  /* harmony export */ createLogger: () => (/* binding */ createLogger),
2887
2882
  /* harmony export */ extractTableUpdates: () => (/* binding */ extractTableUpdates),
2888
2883
  /* harmony export */ isBatchedUpdateNotification: () => (/* binding */ isBatchedUpdateNotification),
2889
- /* harmony export */ isContinueCheckpointRequest: () => (/* binding */ isContinueCheckpointRequest),
2890
2884
  /* harmony export */ isDBAdapter: () => (/* binding */ isDBAdapter),
2891
2885
  /* harmony export */ isPowerSyncDatabaseOptionsWithSettings: () => (/* binding */ isPowerSyncDatabaseOptionsWithSettings),
2892
2886
  /* harmony export */ isSQLOpenFactory: () => (/* binding */ isSQLOpenFactory),
2893
2887
  /* harmony export */ isSQLOpenOptions: () => (/* binding */ isSQLOpenOptions),
2894
- /* harmony export */ isStreamingKeepalive: () => (/* binding */ isStreamingKeepalive),
2895
- /* harmony export */ isStreamingSyncCheckpoint: () => (/* binding */ isStreamingSyncCheckpoint),
2896
- /* harmony export */ isStreamingSyncCheckpointComplete: () => (/* binding */ isStreamingSyncCheckpointComplete),
2897
- /* harmony export */ isStreamingSyncCheckpointDiff: () => (/* binding */ isStreamingSyncCheckpointDiff),
2898
- /* harmony export */ isStreamingSyncCheckpointPartiallyComplete: () => (/* binding */ isStreamingSyncCheckpointPartiallyComplete),
2899
- /* harmony export */ isStreamingSyncData: () => (/* binding */ isStreamingSyncData),
2900
- /* harmony export */ isSyncNewCheckpointRequest: () => (/* binding */ isSyncNewCheckpointRequest),
2901
2888
  /* harmony export */ parseQuery: () => (/* binding */ parseQuery),
2902
2889
  /* harmony export */ runOnSchemaChange: () => (/* binding */ runOnSchemaChange),
2903
2890
  /* harmony export */ sanitizeSQL: () => (/* binding */ sanitizeSQL),
@@ -6393,103 +6380,6 @@ class AbortOperation extends Error {
6393
6380
  }
6394
6381
  }
6395
6382
 
6396
- var OpTypeEnum;
6397
- (function (OpTypeEnum) {
6398
- OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
6399
- OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
6400
- OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
6401
- OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
6402
- })(OpTypeEnum || (OpTypeEnum = {}));
6403
- /**
6404
- * Used internally for sync buckets.
6405
- */
6406
- class OpType {
6407
- value;
6408
- static fromJSON(jsonValue) {
6409
- return new OpType(OpTypeEnum[jsonValue]);
6410
- }
6411
- constructor(value) {
6412
- this.value = value;
6413
- }
6414
- toJSON() {
6415
- return Object.entries(OpTypeEnum).find(([, value]) => value === this.value)[0];
6416
- }
6417
- }
6418
-
6419
- class OplogEntry {
6420
- op_id;
6421
- op;
6422
- checksum;
6423
- subkey;
6424
- object_type;
6425
- object_id;
6426
- data;
6427
- static fromRow(row) {
6428
- return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
6429
- }
6430
- constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
6431
- this.op_id = op_id;
6432
- this.op = op;
6433
- this.checksum = checksum;
6434
- this.subkey = subkey;
6435
- this.object_type = object_type;
6436
- this.object_id = object_id;
6437
- this.data = data;
6438
- }
6439
- toJSON(fixedKeyEncoding = false) {
6440
- return {
6441
- op_id: this.op_id,
6442
- op: this.op.toJSON(),
6443
- object_type: this.object_type,
6444
- object_id: this.object_id,
6445
- checksum: this.checksum,
6446
- data: this.data,
6447
- // Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
6448
- // but we need to migrate gradually to not break existing databases.
6449
- subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
6450
- };
6451
- }
6452
- }
6453
-
6454
- class SyncDataBucket {
6455
- bucket;
6456
- data;
6457
- has_more;
6458
- after;
6459
- next_after;
6460
- static fromRow(row) {
6461
- return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
6462
- }
6463
- constructor(bucket, data,
6464
- /**
6465
- * True if the response does not contain all the data for this bucket, and another request must be made.
6466
- */
6467
- has_more,
6468
- /**
6469
- * The `after` specified in the request.
6470
- */
6471
- after,
6472
- /**
6473
- * Use this for the next request.
6474
- */
6475
- next_after) {
6476
- this.bucket = bucket;
6477
- this.data = data;
6478
- this.has_more = has_more;
6479
- this.after = after;
6480
- this.next_after = next_after;
6481
- }
6482
- toJSON(fixedKeyEncoding = false) {
6483
- return {
6484
- bucket: this.bucket,
6485
- has_more: this.has_more,
6486
- after: this.after,
6487
- next_after: this.next_after,
6488
- data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
6489
- };
6490
- }
6491
- }
6492
-
6493
6383
  var buffer = {};
6494
6384
 
6495
6385
  var base64Js = {};
@@ -14202,22 +14092,12 @@ class AbstractRemote {
14202
14092
  * Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
14203
14093
  *
14204
14094
  * The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
14205
- *
14206
- * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
14207
- * (required for compatibility with older sync services).
14208
14095
  */
14209
- async socketStreamRaw(options, bson) {
14096
+ async socketStreamRaw(options) {
14210
14097
  const { path, fetchStrategy = FetchStrategy.Buffered } = options;
14211
- const mimeType = bson == null ? 'application/json' : 'application/bson';
14098
+ const mimeType = 'application/json';
14212
14099
  function toBuffer(js) {
14213
- let contents;
14214
- if (bson != null) {
14215
- contents = bson.serialize(js);
14216
- }
14217
- else {
14218
- contents = JSON.stringify(js);
14219
- }
14220
- return bufferExports.Buffer.from(contents);
14100
+ return bufferExports.Buffer.from(JSON.stringify(js));
14221
14101
  }
14222
14102
  const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
14223
14103
  const request = await this.buildRequest(path);
@@ -14519,32 +14399,6 @@ function coreStatusToJs(status) {
14519
14399
  };
14520
14400
  }
14521
14401
 
14522
- function isStreamingSyncData(line) {
14523
- return line.data != null;
14524
- }
14525
- function isStreamingKeepalive(line) {
14526
- return line.token_expires_in != null;
14527
- }
14528
- function isStreamingSyncCheckpoint(line) {
14529
- return line.checkpoint != null;
14530
- }
14531
- function isStreamingSyncCheckpointComplete(line) {
14532
- return line.checkpoint_complete != null;
14533
- }
14534
- function isStreamingSyncCheckpointPartiallyComplete(line) {
14535
- return line.partial_checkpoint_complete != null;
14536
- }
14537
- function isStreamingSyncCheckpointDiff(line) {
14538
- return line.checkpoint_diff != null;
14539
- }
14540
- function isContinueCheckpointRequest(request) {
14541
- return (Array.isArray(request.buckets) &&
14542
- typeof request.checkpoint_token == 'string');
14543
- }
14544
- function isSyncNewCheckpointRequest(request) {
14545
- return typeof request.request_checkpoint == 'object';
14546
- }
14547
-
14548
14402
  var LockType;
14549
14403
  (function (LockType) {
14550
14404
  LockType["CRUD"] = "crud";
@@ -14557,35 +14411,21 @@ var SyncStreamConnectionMethod;
14557
14411
  })(SyncStreamConnectionMethod || (SyncStreamConnectionMethod = {}));
14558
14412
  var SyncClientImplementation;
14559
14413
  (function (SyncClientImplementation) {
14560
- /**
14561
- * Decodes and handles sync lines received from the sync service in JavaScript.
14562
- *
14563
- * This is the default option.
14564
- *
14565
- * @deprecated We recommend the {@link RUST} client implementation for all apps. If you have issues with
14566
- * the Rust client, please file an issue or reach out to us. The JavaScript client will be removed in a future
14567
- * version of the PowerSync SDK.
14568
- */
14569
- SyncClientImplementation["JAVASCRIPT"] = "js";
14570
14414
  /**
14571
14415
  * This implementation offloads the sync line decoding and handling into the PowerSync
14572
14416
  * core extension.
14573
14417
  *
14574
- * This option is more performant than the {@link JAVASCRIPT} client, enabled by default and the
14575
- * recommended client implementation for all apps.
14418
+ * This is the only option, as an older JavaScript client implementation has been removed from the SDK.
14576
14419
  *
14577
14420
  * ## Compatibility warning
14578
14421
  *
14579
14422
  * The Rust sync client stores sync data in a format that is slightly different than the one used
14580
- * by the old {@link JAVASCRIPT} implementation. When adopting the {@link RUST} client on existing
14581
- * databases, the PowerSync SDK will migrate the format automatically.
14582
- * Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
14583
- * the version introducing {@link RUST} as an option) also supports the new format, so you can switch
14584
- * back to {@link JAVASCRIPT} later.
14423
+ * by the old JavaScript client. When adopting the {@link RUST} client on existing databases, the PowerSync SDK will
14424
+ * migrate the format automatically.
14585
14425
  *
14586
- * __However__: Upgrading the SDK version, then adopting {@link RUST} as a sync client and later
14587
- * downgrading the SDK to an older version (necessarily using the JavaScript-based implementation then)
14588
- * can lead to sync issues.
14426
+ * SDK versions supporting both the JavaScript and the Rust client support both formats with the JavaScript client
14427
+ * implementaiton. However, downgrading to an SDK version that only supports the JavaScript client would not be
14428
+ * possible anymore. Problematic SDK versions have been released before 2025-06-09.
14589
14429
  */
14590
14430
  SyncClientImplementation["RUST"] = "rust";
14591
14431
  })(SyncClientImplementation || (SyncClientImplementation = {}));
@@ -14608,13 +14448,7 @@ const DEFAULT_STREAM_CONNECTION_OPTIONS = {
14608
14448
  serializedSchema: undefined,
14609
14449
  includeDefaultStreams: true
14610
14450
  };
14611
- // The priority we assume when we receive checkpoint lines where no priority is set.
14612
- // This is the default priority used by the sync service, but can be set to an arbitrary
14613
- // value since sync services without priorities also won't send partial sync completion
14614
- // messages.
14615
- const FALLBACK_PRIORITY = 3;
14616
14451
  class AbstractStreamingSyncImplementation extends BaseObserver {
14617
- _lastSyncedAt;
14618
14452
  options;
14619
14453
  abortController;
14620
14454
  // In rare cases, mostly for tests, uploads can be triggered without being properly connected.
@@ -14704,9 +14538,6 @@ class AbstractStreamingSyncImplementation extends BaseObserver {
14704
14538
  this.crudUpdateListener = undefined;
14705
14539
  this.uploadAbortController?.abort();
14706
14540
  }
14707
- async hasCompletedSync() {
14708
- return this.options.adapter.hasCompletedSync();
14709
- }
14710
14541
  async getWriteCheckpoint() {
14711
14542
  const clientId = await this.options.adapter.getClientId();
14712
14543
  let path = `/write-checkpoint2.json?client_id=${clientId}`;
@@ -14788,7 +14619,7 @@ The next upload iteration will be delayed.`);
14788
14619
  });
14789
14620
  }
14790
14621
  }
14791
- this.uploadAbortController = null;
14622
+ this.uploadAbortController = undefined;
14792
14623
  }
14793
14624
  });
14794
14625
  }
@@ -14948,18 +14779,6 @@ The next upload iteration will be delayed.`);
14948
14779
  // iteration is active. That allows us to reconnect ASAP, instead of having to wait for the next sync line.
14949
14780
  this.handleActiveStreamsChange?.();
14950
14781
  }
14951
- async collectLocalBucketState() {
14952
- const bucketEntries = await this.options.adapter.getBucketStates();
14953
- const req = bucketEntries.map((entry) => ({
14954
- name: entry.bucket,
14955
- after: entry.op_id
14956
- }));
14957
- const localDescriptions = new Map();
14958
- for (const entry of bucketEntries) {
14959
- localDescriptions.set(entry.bucket, null);
14960
- }
14961
- return [req, localDescriptions];
14962
- }
14963
14782
  /**
14964
14783
  * Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
14965
14784
  * Because subkeys are always strings, this leads to quotes being added around them in `ps_oplog`.
@@ -15001,19 +14820,13 @@ The next upload iteration will be delayed.`);
15001
14820
  }
15002
14821
  const clientImplementation = resolvedOptions.clientImplementation;
15003
14822
  this.updateSyncStatus({ clientImplementation });
15004
- if (clientImplementation == SyncClientImplementation.JAVASCRIPT) {
15005
- await this.legacyStreamingSyncIteration(signal, resolvedOptions);
15006
- return null;
15007
- }
15008
- else {
15009
- await this.requireKeyFormat(true);
15010
- return await this.rustSyncIteration(signal, resolvedOptions);
15011
- }
14823
+ await this.requireKeyFormat(true);
14824
+ return await this.rustSyncIteration(signal, resolvedOptions);
15012
14825
  }
15013
14826
  });
15014
14827
  }
15015
14828
  async receiveSyncLines(data) {
15016
- const { options, connection, bson } = data;
14829
+ const { options, connection } = data;
15017
14830
  const remote = this.options.remote;
15018
14831
  if (connection.connectionMethod == SyncStreamConnectionMethod.HTTP) {
15019
14832
  return await remote.fetchStream(options);
@@ -15022,232 +14835,8 @@ The next upload iteration will be delayed.`);
15022
14835
  return await this.options.remote.socketStreamRaw({
15023
14836
  ...options,
15024
14837
  ...{ fetchStrategy: connection.fetchStrategy }
15025
- }, bson);
15026
- }
15027
- }
15028
- async legacyStreamingSyncIteration(signal, resolvedOptions) {
15029
- const rawTables = resolvedOptions.serializedSchema?.raw_tables;
15030
- if (rawTables != null && rawTables.length) {
15031
- this.logger.warn('Raw tables require the Rust-based sync client. The JS client will ignore them.');
15032
- }
15033
- if (this.activeStreams.length) {
15034
- this.logger.error('Sync streams require `clientImplementation: SyncClientImplementation.RUST` when connecting.');
15035
- }
15036
- this.logger.debug('Streaming sync iteration started');
15037
- this.options.adapter.startSession();
15038
- let [req, bucketMap] = await this.collectLocalBucketState();
15039
- let targetCheckpoint = null;
15040
- // A checkpoint that has been validated but not applied (e.g. due to pending local writes)
15041
- let pendingValidatedCheckpoint = null;
15042
- const clientId = await this.options.adapter.getClientId();
15043
- const usingFixedKeyFormat = await this.requireKeyFormat(false);
15044
- this.logger.debug('Requesting stream from server');
15045
- const syncOptions = {
15046
- path: '/sync/stream',
15047
- abortSignal: signal,
15048
- data: {
15049
- buckets: req,
15050
- include_checksum: true,
15051
- raw_data: true,
15052
- parameters: resolvedOptions.params,
15053
- app_metadata: resolvedOptions.appMetadata,
15054
- client_id: clientId
15055
- }
15056
- };
15057
- const bson = await this.options.remote.getBSON();
15058
- const source = await this.receiveSyncLines({
15059
- options: syncOptions,
15060
- connection: resolvedOptions,
15061
- bson
15062
- });
15063
- const stream = injectable(map(source, (line) => {
15064
- if (typeof line == 'string') {
15065
- return JSON.parse(line);
15066
- }
15067
- else {
15068
- return bson.deserialize(line);
15069
- }
15070
- }));
15071
- this.logger.debug('Stream established. Processing events');
15072
- this.notifyCompletedUploads = () => {
15073
- stream.inject({ crud_upload_completed: null });
15074
- };
15075
- while (true) {
15076
- const { value: line, done } = await stream.next();
15077
- if (done) {
15078
- // The stream has closed while waiting
15079
- return;
15080
- }
15081
- if ('crud_upload_completed' in line) {
15082
- if (pendingValidatedCheckpoint != null) {
15083
- const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
15084
- if (applied) {
15085
- pendingValidatedCheckpoint = null;
15086
- }
15087
- else if (endIteration) {
15088
- break;
15089
- }
15090
- }
15091
- continue;
15092
- }
15093
- // A connection is active and messages are being received
15094
- if (!this.syncStatus.connected) {
15095
- // There is a connection now
15096
- Promise.resolve().then(() => this.triggerCrudUpload());
15097
- this.updateSyncStatus({
15098
- connected: true
15099
- });
15100
- }
15101
- if (isStreamingSyncCheckpoint(line)) {
15102
- targetCheckpoint = line.checkpoint;
15103
- // New checkpoint - existing validated checkpoint is no longer valid
15104
- pendingValidatedCheckpoint = null;
15105
- const bucketsToDelete = new Set(bucketMap.keys());
15106
- const newBuckets = new Map();
15107
- for (const checksum of line.checkpoint.buckets) {
15108
- newBuckets.set(checksum.bucket, {
15109
- name: checksum.bucket,
15110
- priority: checksum.priority ?? FALLBACK_PRIORITY
15111
- });
15112
- bucketsToDelete.delete(checksum.bucket);
15113
- }
15114
- if (bucketsToDelete.size > 0) {
15115
- this.logger.debug('Removing buckets', [...bucketsToDelete]);
15116
- }
15117
- bucketMap = newBuckets;
15118
- await this.options.adapter.removeBuckets([...bucketsToDelete]);
15119
- await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
15120
- await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
15121
- }
15122
- else if (isStreamingSyncCheckpointComplete(line)) {
15123
- const result = await this.applyCheckpoint(targetCheckpoint);
15124
- if (result.endIteration) {
15125
- return;
15126
- }
15127
- else if (!result.applied) {
15128
- // "Could not apply checkpoint due to local data". We need to retry after
15129
- // finishing uploads.
15130
- pendingValidatedCheckpoint = targetCheckpoint;
15131
- }
15132
- else {
15133
- // Nothing to retry later. This would likely already be null from the last
15134
- // checksum or checksum_diff operation, but we make sure.
15135
- pendingValidatedCheckpoint = null;
15136
- }
15137
- }
15138
- else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
15139
- const priority = line.partial_checkpoint_complete.priority;
15140
- this.logger.debug('Partial checkpoint complete', priority);
15141
- const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint, priority);
15142
- if (!result.checkpointValid) {
15143
- // This means checksums failed. Start again with a new checkpoint.
15144
- // TODO: better back-off
15145
- await new Promise((resolve) => setTimeout(resolve, 50));
15146
- return;
15147
- }
15148
- else if (!result.ready) ;
15149
- else {
15150
- // We'll keep on downloading, but can report that this priority is synced now.
15151
- this.logger.debug('partial checkpoint validation succeeded');
15152
- // All states with a higher priority can be deleted since this partial sync includes them.
15153
- const priorityStates = this.syncStatus.priorityStatusEntries.filter((s) => s.priority <= priority);
15154
- priorityStates.push({
15155
- priority,
15156
- lastSyncedAt: new Date(),
15157
- hasSynced: true
15158
- });
15159
- this.updateSyncStatus({
15160
- connected: true,
15161
- priorityStatusEntries: priorityStates
15162
- });
15163
- }
15164
- }
15165
- else if (isStreamingSyncCheckpointDiff(line)) {
15166
- // TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
15167
- if (targetCheckpoint == null) {
15168
- throw new Error('Checkpoint diff without previous checkpoint');
15169
- }
15170
- // New checkpoint - existing validated checkpoint is no longer valid
15171
- pendingValidatedCheckpoint = null;
15172
- const diff = line.checkpoint_diff;
15173
- const newBuckets = new Map();
15174
- for (const checksum of targetCheckpoint.buckets) {
15175
- newBuckets.set(checksum.bucket, checksum);
15176
- }
15177
- for (const checksum of diff.updated_buckets) {
15178
- newBuckets.set(checksum.bucket, checksum);
15179
- }
15180
- for (const bucket of diff.removed_buckets) {
15181
- newBuckets.delete(bucket);
15182
- }
15183
- const newCheckpoint = {
15184
- last_op_id: diff.last_op_id,
15185
- buckets: [...newBuckets.values()],
15186
- write_checkpoint: diff.write_checkpoint
15187
- };
15188
- targetCheckpoint = newCheckpoint;
15189
- await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
15190
- bucketMap = new Map();
15191
- newBuckets.forEach((checksum, name) => bucketMap.set(name, {
15192
- name: checksum.bucket,
15193
- priority: checksum.priority ?? FALLBACK_PRIORITY
15194
- }));
15195
- const bucketsToDelete = diff.removed_buckets;
15196
- if (bucketsToDelete.length > 0) {
15197
- this.logger.debug('Remove buckets', bucketsToDelete);
15198
- }
15199
- await this.options.adapter.removeBuckets(bucketsToDelete);
15200
- await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
15201
- }
15202
- else if (isStreamingSyncData(line)) {
15203
- const { data } = line;
15204
- const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
15205
- let updatedProgress = null;
15206
- if (previousProgress) {
15207
- updatedProgress = { ...previousProgress };
15208
- const progressForBucket = updatedProgress[data.bucket];
15209
- if (progressForBucket) {
15210
- updatedProgress[data.bucket] = {
15211
- ...progressForBucket,
15212
- since_last: progressForBucket.since_last + data.data.length
15213
- };
15214
- }
15215
- }
15216
- this.updateSyncStatus({
15217
- dataFlow: {
15218
- downloading: true,
15219
- downloadProgress: updatedProgress
15220
- }
15221
- });
15222
- await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
15223
- }
15224
- else if (isStreamingKeepalive(line)) {
15225
- const remaining_seconds = line.token_expires_in;
15226
- if (remaining_seconds == 0) {
15227
- // Connection would be closed automatically right after this
15228
- this.logger.debug('Token expiring; reconnect');
15229
- /**
15230
- * For a rare case where the backend connector does not update the token
15231
- * (uses the same one), this should have some delay.
15232
- */
15233
- await this.delayRetry();
15234
- return;
15235
- }
15236
- else if (remaining_seconds < 30) {
15237
- this.logger.debug('Token will expire soon; reconnect');
15238
- // Pre-emptively refresh the token
15239
- this.options.remote.invalidateCredentials();
15240
- return;
15241
- }
15242
- this.triggerCrudUpload();
15243
- }
15244
- else {
15245
- this.logger.debug('Received unknown sync line', line);
15246
- }
14838
+ });
15247
14839
  }
15248
- this.logger.debug('Stream input empty');
15249
- // Connection closed. Likely due to auth issue.
15250
- return;
15251
14840
  }
15252
14841
  async rustSyncIteration(signal, resolvedOptions) {
15253
14842
  const syncImplementation = this;
@@ -15410,68 +14999,6 @@ The next upload iteration will be delayed.`);
15410
14999
  }
15411
15000
  return { immediateRestart: hideDisconnectOnRestart };
15412
15001
  }
15413
- async updateSyncStatusForStartingCheckpoint(checkpoint) {
15414
- const localProgress = await this.options.adapter.getBucketOperationProgress();
15415
- const progress = {};
15416
- let invalidated = false;
15417
- for (const bucket of checkpoint.buckets) {
15418
- const savedProgress = localProgress[bucket.bucket];
15419
- const atLast = savedProgress?.atLast ?? 0;
15420
- const sinceLast = savedProgress?.sinceLast ?? 0;
15421
- progress[bucket.bucket] = {
15422
- // The fallback priority doesn't matter here, but 3 is the one newer versions of the sync service
15423
- // will use by default.
15424
- priority: bucket.priority ?? 3,
15425
- at_last: atLast,
15426
- since_last: sinceLast,
15427
- target_count: bucket.count ?? 0
15428
- };
15429
- if (bucket.count != null && bucket.count < atLast + sinceLast) {
15430
- // Either due to a defrag / sync rule deploy or a compaction operation, the size
15431
- // of the bucket shrank so much that the local ops exceed the ops in the updated
15432
- // bucket. We can't prossibly report progress in this case (it would overshoot 100%).
15433
- invalidated = true;
15434
- }
15435
- }
15436
- if (invalidated) {
15437
- for (const bucket in progress) {
15438
- const bucketProgress = progress[bucket];
15439
- bucketProgress.at_last = 0;
15440
- bucketProgress.since_last = 0;
15441
- }
15442
- }
15443
- this.updateSyncStatus({
15444
- dataFlow: {
15445
- downloading: true,
15446
- downloadProgress: progress
15447
- }
15448
- });
15449
- }
15450
- async applyCheckpoint(checkpoint) {
15451
- let result = await this.options.adapter.syncLocalDatabase(checkpoint);
15452
- if (!result.checkpointValid) {
15453
- this.logger.debug(`Checksum mismatch in checkpoint ${checkpoint.last_op_id}, will reconnect`);
15454
- // This means checksums failed. Start again with a new checkpoint.
15455
- // TODO: better back-off
15456
- await new Promise((resolve) => setTimeout(resolve, 50));
15457
- return { applied: false, endIteration: true };
15458
- }
15459
- else if (!result.ready) {
15460
- 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.`);
15461
- return { applied: false, endIteration: false };
15462
- }
15463
- this.logger.debug(`Applied checkpoint ${checkpoint.last_op_id}`, checkpoint);
15464
- this.updateSyncStatus({
15465
- connected: true,
15466
- lastSyncedAt: new Date(),
15467
- dataFlow: {
15468
- downloading: false,
15469
- downloadProgress: null,
15470
- downloadError: undefined
15471
- }
15472
- });
15473
- return { applied: true, endIteration: false };
15474
- }
15475
15002
  updateSyncStatus(options) {
15476
15003
  const updatedStatus = new SyncStatus({
15477
15004
  connected: options.connected ?? this.syncStatus.connected,
@@ -17010,14 +16537,12 @@ class SqliteBucketStorage extends BaseObserver {
17010
16537
  db;
17011
16538
  logger;
17012
16539
  tableNames;
17013
- _hasCompletedSync;
17014
16540
  updateListener;
17015
16541
  _clientId;
17016
16542
  constructor(db, logger = Logger.get('SqliteBucketStorage')) {
17017
16543
  super();
17018
16544
  this.db = db;
17019
16545
  this.logger = logger;
17020
- this._hasCompletedSync = false;
17021
16546
  this.tableNames = new Set();
17022
16547
  this.updateListener = db.registerListener({
17023
16548
  tablesUpdated: (update) => {
@@ -17029,7 +16554,6 @@ class SqliteBucketStorage extends BaseObserver {
17029
16554
  });
17030
16555
  }
17031
16556
  async init() {
17032
- this._hasCompletedSync = false;
17033
16557
  const existingTableRows = await this.db.getAll(`SELECT name FROM sqlite_master WHERE type='table' AND name GLOB 'ps_data_*'`);
17034
16558
  for (const row of existingTableRows ?? []) {
17035
16559
  this.tableNames.add(row.name);
@@ -17051,156 +16575,6 @@ class SqliteBucketStorage extends BaseObserver {
17051
16575
  getMaxOpId() {
17052
16576
  return MAX_OP_ID;
17053
16577
  }
17054
- /**
17055
- * Reset any caches.
17056
- */
17057
- startSession() { }
17058
- async getBucketStates() {
17059
- 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'");
17060
- return result;
17061
- }
17062
- async getBucketOperationProgress() {
17063
- const rows = await this.db.getAll('SELECT name, count_at_last, count_since_last FROM ps_buckets');
17064
- return Object.fromEntries(rows.map((r) => [r.name, { atLast: r.count_at_last, sinceLast: r.count_since_last }]));
17065
- }
17066
- async saveSyncData(batch, fixedKeyFormat = false) {
17067
- await this.writeTransaction(async (tx) => {
17068
- for (const b of batch.buckets) {
17069
- await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
17070
- 'save',
17071
- JSON.stringify({ buckets: [b.toJSON(fixedKeyFormat)] })
17072
- ]);
17073
- this.logger.debug(`Saved batch of data for bucket: ${b.bucket}, operations: ${b.data.length}`);
17074
- }
17075
- });
17076
- }
17077
- async removeBuckets(buckets) {
17078
- for (const bucket of buckets) {
17079
- await this.deleteBucket(bucket);
17080
- }
17081
- }
17082
- /**
17083
- * Mark a bucket for deletion.
17084
- */
17085
- async deleteBucket(bucket) {
17086
- await this.writeTransaction(async (tx) => {
17087
- await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', ['delete_bucket', bucket]);
17088
- });
17089
- this.logger.debug(`Done deleting bucket ${bucket}`);
17090
- }
17091
- async hasCompletedSync() {
17092
- if (this._hasCompletedSync) {
17093
- return true;
17094
- }
17095
- const r = await this.db.get(`SELECT powersync_last_synced_at() as synced_at`);
17096
- const completed = r.synced_at != null;
17097
- if (completed) {
17098
- this._hasCompletedSync = true;
17099
- }
17100
- return completed;
17101
- }
17102
- async syncLocalDatabase(checkpoint, priority) {
17103
- const r = await this.validateChecksums(checkpoint, priority);
17104
- if (!r.checkpointValid) {
17105
- this.logger.error('Checksums failed for', r.checkpointFailures);
17106
- for (const b of r.checkpointFailures ?? []) {
17107
- await this.deleteBucket(b);
17108
- }
17109
- return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
17110
- }
17111
- if (priority == null) {
17112
- this.logger.debug(`Validated checksums checkpoint ${checkpoint.last_op_id}`);
17113
- }
17114
- else {
17115
- this.logger.debug(`Validated checksums for partial checkpoint ${checkpoint.last_op_id}, priority ${priority}`);
17116
- }
17117
- let buckets = checkpoint.buckets;
17118
- if (priority !== undefined) {
17119
- buckets = buckets.filter((b) => hasMatchingPriority(priority, b));
17120
- }
17121
- const bucketNames = buckets.map((b) => b.bucket);
17122
- await this.writeTransaction(async (tx) => {
17123
- await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
17124
- checkpoint.last_op_id,
17125
- JSON.stringify(bucketNames)
17126
- ]);
17127
- if (priority == null && checkpoint.write_checkpoint) {
17128
- await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
17129
- }
17130
- });
17131
- const valid = await this.updateObjectsFromBuckets(checkpoint, priority);
17132
- if (!valid) {
17133
- return { ready: false, checkpointValid: true };
17134
- }
17135
- return {
17136
- ready: true,
17137
- checkpointValid: true
17138
- };
17139
- }
17140
- /**
17141
- * Atomically update the local state to the current checkpoint.
17142
- *
17143
- * This includes creating new tables, dropping old tables, and copying data over from the oplog.
17144
- */
17145
- async updateObjectsFromBuckets(checkpoint, priority) {
17146
- let arg = '';
17147
- if (priority !== undefined) {
17148
- const affectedBuckets = [];
17149
- for (const desc of checkpoint.buckets) {
17150
- if (hasMatchingPriority(priority, desc)) {
17151
- affectedBuckets.push(desc.bucket);
17152
- }
17153
- }
17154
- arg = JSON.stringify({ priority, buckets: affectedBuckets });
17155
- }
17156
- return this.writeTransaction(async (tx) => {
17157
- const { insertId: result } = await tx.execute('INSERT INTO powersync_operations(op, data) VALUES(?, ?)', [
17158
- 'sync_local',
17159
- arg
17160
- ]);
17161
- if (result == 1) {
17162
- if (priority == null) {
17163
- const bucketToCount = Object.fromEntries(checkpoint.buckets.map((b) => [b.bucket, b.count]));
17164
- // The two parameters could be replaced with one, but: https://github.com/powersync-ja/better-sqlite3/pull/6
17165
- const jsonBucketCount = JSON.stringify(bucketToCount);
17166
- 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]);
17167
- }
17168
- return true;
17169
- }
17170
- else {
17171
- return false;
17172
- }
17173
- });
17174
- }
17175
- async validateChecksums(checkpoint, priority) {
17176
- if (priority !== undefined) {
17177
- // Only validate the buckets within the priority we care about
17178
- const newBuckets = checkpoint.buckets.filter((cs) => hasMatchingPriority(priority, cs));
17179
- checkpoint = { ...checkpoint, buckets: newBuckets };
17180
- }
17181
- const rs = await this.db.execute('SELECT powersync_validate_checkpoint(?) as result', [
17182
- JSON.stringify({ ...checkpoint })
17183
- ]);
17184
- const resultItem = rs.rows?.item(0);
17185
- if (!resultItem) {
17186
- return {
17187
- checkpointValid: false,
17188
- ready: false,
17189
- checkpointFailures: []
17190
- };
17191
- }
17192
- const result = JSON.parse(resultItem['result']);
17193
- if (result['valid']) {
17194
- return { ready: true, checkpointValid: true };
17195
- }
17196
- else {
17197
- return {
17198
- checkpointValid: false,
17199
- ready: false,
17200
- checkpointFailures: result['failed_buckets']
17201
- };
17202
- }
17203
- }
17204
16578
  async updateLocalTarget(cb) {
17205
16579
  const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)", [MAX_OP_ID]);
17206
16580
  if (!rs1.length) {
@@ -17291,12 +16665,6 @@ class SqliteBucketStorage extends BaseObserver {
17291
16665
  async writeTransaction(callback, options) {
17292
16666
  return this.db.writeTransaction(callback, options);
17293
16667
  }
17294
- /**
17295
- * Set a target checkpoint.
17296
- */
17297
- async setTargetCheckpoint(checkpoint) {
17298
- // No-op for now
17299
- }
17300
16668
  async control(op, payload) {
17301
16669
  return await this.writeTransaction(async (tx) => {
17302
16670
  const [[raw]] = await tx.executeRaw('SELECT powersync_control(?, ?)', [op, payload]);
@@ -17320,20 +16688,6 @@ class SqliteBucketStorage extends BaseObserver {
17320
16688
  }
17321
16689
  static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
17322
16690
  }
17323
- function hasMatchingPriority(priority, bucket) {
17324
- return bucket.priority != null && bucket.priority <= priority;
17325
- }
17326
-
17327
- // TODO JSON
17328
- class SyncDataBatch {
17329
- buckets;
17330
- static fromJSON(json) {
17331
- return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
17332
- }
17333
- constructor(buckets) {
17334
- this.buckets = buckets;
17335
- }
17336
- }
17337
16691
 
17338
16692
  /**
17339
16693
  * Thrown when an underlying database connection is closed.