@syncular/client 0.57.0 → 0.59.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +7 -7
  2. package/dist/apply.d.ts +20 -11
  3. package/dist/apply.js +67 -45
  4. package/dist/blob.d.ts +17 -9
  5. package/dist/blob.js +5 -6
  6. package/dist/bun-database.d.ts +1 -0
  7. package/dist/bun-database.js +28 -0
  8. package/dist/client.d.ts +27 -10
  9. package/dist/client.js +357 -190
  10. package/dist/database.d.ts +5 -0
  11. package/dist/encryption.d.ts +2 -2
  12. package/dist/encryption.js +8 -2
  13. package/dist/errors.d.ts +2 -0
  14. package/dist/errors.js +4 -0
  15. package/dist/failed-overlay.d.ts +0 -1
  16. package/dist/failed-overlay.js +3 -6
  17. package/dist/http.d.ts +4 -1
  18. package/dist/http.js +61 -39
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.js +1 -1
  21. package/dist/invalidation.d.ts +11 -10
  22. package/dist/invalidation.js +15 -26
  23. package/dist/local-purge.js +9 -12
  24. package/dist/local-rebootstrap.js +2 -2
  25. package/dist/multi-tab.d.ts +89 -1
  26. package/dist/multi-tab.js +70 -31
  27. package/dist/native-bridge.d.ts +235 -0
  28. package/dist/native-bridge.js +789 -0
  29. package/dist/node-database.d.ts +1 -0
  30. package/dist/node-database.js +29 -0
  31. package/dist/outbox.d.ts +9 -12
  32. package/dist/outbox.js +19 -36
  33. package/dist/outcomes.js +3 -3
  34. package/dist/previous-version.js +96 -114
  35. package/dist/query-guard.d.ts +25 -24
  36. package/dist/query-guard.js +34 -191
  37. package/dist/reactive-store.js +53 -11
  38. package/dist/realtime-supervisor.d.ts +9 -2
  39. package/dist/realtime-supervisor.js +31 -7
  40. package/dist/remote.js +11 -15
  41. package/dist/schema.js +15 -10
  42. package/dist/sync-scheduler.js +28 -2
  43. package/dist/transport.d.ts +19 -4
  44. package/dist/wasm-database.js +58 -21
  45. package/dist/worker-core.d.ts +5 -0
  46. package/dist/worker-core.js +80 -72
  47. package/dist/worker-host.d.ts +10 -5
  48. package/dist/worker-host.js +64 -7
  49. package/dist/worker-local-authority.js +2 -2
  50. package/dist/worker-protocol.d.ts +6 -2
  51. package/package.json +11 -3
  52. package/src/apply.ts +91 -55
  53. package/src/blob.ts +27 -15
  54. package/src/bun-database.ts +29 -0
  55. package/src/client.ts +477 -261
  56. package/src/database.ts +5 -0
  57. package/src/encryption.ts +10 -5
  58. package/src/errors.ts +8 -0
  59. package/src/failed-overlay.ts +3 -7
  60. package/src/http.ts +90 -79
  61. package/src/index.ts +1 -1
  62. package/src/invalidation.ts +17 -32
  63. package/src/local-purge.ts +11 -13
  64. package/src/local-rebootstrap.ts +2 -3
  65. package/src/multi-tab.ts +88 -30
  66. package/src/native-bridge.ts +1189 -0
  67. package/src/node-database.ts +32 -0
  68. package/src/outbox.ts +19 -41
  69. package/src/outcomes.ts +3 -5
  70. package/src/previous-version.ts +103 -114
  71. package/src/query-guard.ts +43 -180
  72. package/src/reactive-store.ts +50 -10
  73. package/src/realtime-supervisor.ts +37 -12
  74. package/src/remote.ts +12 -16
  75. package/src/schema.ts +19 -12
  76. package/src/sync-scheduler.ts +31 -1
  77. package/src/transport.ts +21 -2
  78. package/src/wasm-database.ts +76 -27
  79. package/src/worker-core.ts +94 -74
  80. package/src/worker-host.ts +63 -16
  81. package/src/worker-local-authority.ts +2 -5
  82. package/src/worker-protocol.ts +6 -2
package/src/client.ts CHANGED
@@ -40,9 +40,11 @@ import {
40
40
  isRoundFailureCode,
41
41
  MessageStreamScanner,
42
42
  PROTOCOL_WIRE_VERSION,
43
- type PresenceKind,
43
+ type PresenceErrorCode,
44
44
  type PushResultFrame,
45
+ parseJsonDocument,
45
46
  parseRealtimeServerEvent,
47
+ type RealtimePresenceEvent,
46
48
  REALTIME_TAG_DELTA,
47
49
  REALTIME_TAG_ROUND,
48
50
  type RejectionDetails,
@@ -64,6 +66,7 @@ import {
64
66
  deleteLocalRow,
65
67
  deleteScopedRows,
66
68
  localScopePredicate,
69
+ ownsLocalRow,
67
70
  evictScopedRows,
68
71
  yieldToHost,
69
72
  upsertLocalRow,
@@ -113,6 +116,7 @@ import {
113
116
  classifySqliteFailure,
114
117
  REALTIME_LOST_CODE,
115
118
  RealtimeUnavailableError,
119
+ transportFailed,
116
120
  } from './errors';
117
121
  import {
118
122
  ChangeAccumulator,
@@ -154,7 +158,6 @@ import {
154
158
  import {
155
159
  appendOutboxCommit,
156
160
  deleteOutboxCommit,
157
- dropOutboxCommitsInScope,
158
161
  encodeOutboxCommit,
159
162
  listOutbox,
160
163
  iterateOutbox,
@@ -183,7 +186,6 @@ import {
183
186
  commitOutcome as readCommitOutcome,
184
187
  recordCommitOutcome,
185
188
  } from './outcomes';
186
- import { assertReadOnlyQuery } from './query-guard';
187
189
  import {
188
190
  buildPreviousVersionAudit,
189
191
  capturePreviousVersion,
@@ -446,6 +448,15 @@ export interface SyncClientConfig {
446
448
  * `required` without a `realtime` connector is refused at construction.
447
449
  */
448
450
  readonly realtimePolicy?: RealtimePolicy;
451
+ /**
452
+ * SPEC §8.5: the server's heartbeat interval in ms, default 30,000. The
453
+ * socket's first `heartbeat` event arms a watchdog; once armed, twice the
454
+ * interval without an inbound frame loses the socket with
455
+ * `transport.timeout` (`causeKind` `heartbeat`).
456
+ */
457
+ readonly realtimeHeartbeatMs?: number;
458
+ /** Timer seam for the heartbeat watchdog; defaults to `setTimeout`. */
459
+ readonly schedule?: (callback: () => void, delayMs: number) => () => void;
449
460
  /** Stable per-device id (§1.5); defaults to a persisted random UUID. */
450
461
  readonly clientId?: string;
451
462
  readonly leaderLock?: LeaderLock;
@@ -467,9 +478,11 @@ export interface SyncClientConfig {
467
478
  /**
468
479
  * §8.6 presence: a scope-mate's presence on a key this client holds
469
480
  * changed (join/update/leave). Fired after the local presence map is
470
- * updated — the app's cue to re-render who's-online.
481
+ * updated — the app's cue to re-render who's-online. `error` is set when
482
+ * the server refused this client's own publish on `scopeKey` (§8.6.2);
483
+ * the map is unchanged then.
471
484
  */
472
- readonly onPresence?: (scopeKey: string) => void;
485
+ readonly onPresence?: (scopeKey: string, error?: PresenceErrorCode) => void;
473
486
  /**
474
487
  * §5.11 client-side encryption. Supplies the key material (`keyProvider`)
475
488
  * and key selection (`keyIdFor`) for columns the generated schema marks
@@ -770,6 +783,15 @@ const PREVIOUS_VERSION_DEFAULT_MAX_TABLES = 32;
770
783
  const PREVIOUS_VERSION_DEFAULT_MAX_ROW_BYTES = 1024 * 1024;
771
784
  const PREVIOUS_VERSION_DEFAULT_MAX_AGE_MS = 24 * 60 * 60 * 1000;
772
785
 
786
+ /** §7.7: an explicit round budget is a strict positive u32 count. */
787
+ function requireRoundBudget(maxRounds: number | undefined): void {
788
+ if (
789
+ maxRounds !== undefined &&
790
+ (!Number.isInteger(maxRounds) || maxRounds < 1 || maxRounds > 0xffffffff)
791
+ )
792
+ throw invalidRequest('maxRounds must be a positive integer');
793
+ }
794
+
773
795
  function boundedBudget(
774
796
  value: number | undefined,
775
797
  fallback: number,
@@ -918,6 +940,10 @@ export class SyncClient {
918
940
  #realtimeRetryDelayMs: number | undefined;
919
941
  #realtimeConnectPromise: Promise<void> | undefined;
920
942
  #realtimeGeneration = 0;
943
+ readonly #heartbeatMs: number;
944
+ readonly #schedule: NonNullable<SyncClientConfig['schedule']>;
945
+ /** SPEC §8.5: cancels the armed heartbeat watchdog; unset while unarmed. */
946
+ #watchdog: (() => void) | undefined;
921
947
  #pendingRound: PendingRound | undefined;
922
948
  #needsPull = false;
923
949
  readonly #progress = new ProgressEmitter();
@@ -964,7 +990,9 @@ export class SyncClient {
964
990
  /** Compatibility projection from exact batches; never bridge-inferred. */
965
991
  readonly #invalidation = new InvalidationEmitter();
966
992
  /** §8.6: subscribable presence-change listeners (twin of onPresence). */
967
- readonly #presenceListeners = new Set<(scopeKey: string) => void>();
993
+ readonly #presenceListeners = new Set<
994
+ (scopeKey: string, error?: PresenceErrorCode) => void
995
+ >();
968
996
  readonly #syncNeededListeners = new Set<
969
997
  (reason: 'startup' | 'hello' | WakeReason) => void
970
998
  >();
@@ -1052,6 +1080,23 @@ export class SyncClient {
1052
1080
  "realtimePolicy 'required' needs a realtime connector",
1053
1081
  );
1054
1082
  }
1083
+ // Twice the interval must stay within the 2^31 - 1 ms timer range.
1084
+ const heartbeatMs = config.realtimeHeartbeatMs ?? 30_000;
1085
+ if (
1086
+ !Number.isInteger(heartbeatMs) ||
1087
+ heartbeatMs < 1 ||
1088
+ heartbeatMs > 2 ** 30
1089
+ )
1090
+ throw invalidRequest(
1091
+ 'realtimeHeartbeatMs must be an integer in 1..=2^30',
1092
+ );
1093
+ this.#heartbeatMs = heartbeatMs;
1094
+ this.#schedule =
1095
+ config.schedule ??
1096
+ ((callback, delayMs) => {
1097
+ const timer = setTimeout(callback, delayMs);
1098
+ return () => clearTimeout(timer);
1099
+ });
1055
1100
  this.#config = config;
1056
1101
  this.#realtimePolicy = realtimePolicy;
1057
1102
  this.#realtimeState =
@@ -1563,6 +1608,7 @@ export class SyncClient {
1563
1608
  if (this.#transportEnabled === enabled) return;
1564
1609
  this.#transportEnabled = enabled;
1565
1610
  this.#transportEpoch += 1;
1611
+ this.#disarm();
1566
1612
  if (!enabled && !this.#syncOutstanding) this.disconnectRealtime();
1567
1613
  this.#emitSyncIntent(
1568
1614
  enabled && this.#securityLifecycle === 'active'
@@ -1673,8 +1719,8 @@ export class SyncClient {
1673
1719
  }
1674
1720
 
1675
1721
  /**
1676
- * The raw-SQL read tier. Guarded (query-guard.ts): a single read-only
1677
- * statement only — writes must go through `mutate()` so they hit the
1722
+ * The raw-SQL read tier. Guarded (query-guard.ts): a single statement
1723
+ * SQLite reports read-only — writes must go through `mutate()` so they hit the
1678
1724
  * outbox (SPEC §7.1). Reserved `_sync_*` columns are stripped from the
1679
1725
  * result, so a `SELECT *` row is safe to feed back into `mutate()`
1680
1726
  * values; alias explicitly (`_sync_version AS v`) to read one. Engine
@@ -1682,11 +1728,10 @@ export class SyncClient {
1682
1728
  */
1683
1729
  query(sql: string, params?: readonly SqlValue[]): SqlRow[] {
1684
1730
  this.#requireActive();
1685
- assertReadOnlyQuery(sql);
1686
1731
  // RFC 0005's container lives in a separate database file, so this
1687
1732
  // connection cannot see it and there is no table-access guard to run.
1688
1733
  try {
1689
- return stripSyncColumns(this.#db.query(sql, params));
1734
+ return stripSyncColumns(this.#db.queryReadOnly(sql, params));
1690
1735
  } catch (error) {
1691
1736
  throw classifySqliteFailure(error).error;
1692
1737
  }
@@ -1851,13 +1896,12 @@ export class SyncClient {
1851
1896
  };
1852
1897
  let read: SnapshotRead;
1853
1898
  try {
1854
- for (const statement of request.statements) {
1855
- assertReadOnlyQuery(statement.sql);
1856
- }
1857
1899
  read = this.#db.transaction(() => {
1858
1900
  const revision = getLocalRevision(this.#db);
1859
1901
  const queries = request.statements.map((statement) =>
1860
- stripSyncColumns(this.#db.query(statement.sql, statement.params)),
1902
+ stripSyncColumns(
1903
+ this.#db.queryReadOnly(statement.sql, statement.params),
1904
+ ),
1861
1905
  );
1862
1906
  const pending: WindowUnitRef[] = [];
1863
1907
  const missing: WindowUnitRef[] = [];
@@ -2576,9 +2620,7 @@ export class SyncClient {
2576
2620
  options?: { readonly mediaType?: string; readonly name?: string },
2577
2621
  ): Promise<BlobRef> {
2578
2622
  if (this.#config.blobs === undefined) {
2579
- throw invalidRequest(
2580
- 'uploadBlob requires a blob transport (SyncClientConfig.blobs, §5.9)',
2581
- );
2623
+ throw invalidRequest('uploadBlob requires a blob transport');
2582
2624
  }
2583
2625
  const blobId = await computeBlobId(bytes);
2584
2626
  const nowMs = this.#now();
@@ -2611,29 +2653,28 @@ export class SyncClient {
2611
2653
  }
2612
2654
 
2613
2655
  async #fetchBlob(blobIdOrRef: string): Promise<CachedBlob> {
2614
- const blobId = blobIdOrRef.startsWith('sha256:')
2615
- ? blobIdOrRef
2616
- : parseBlobRef(blobIdOrRef).blobId;
2656
+ // A BlobRef declares the size that caps the download (§5.9.5); a bare
2657
+ // blobId declares none.
2658
+ const ref = blobIdOrRef.startsWith('sha256:')
2659
+ ? undefined
2660
+ : parseBlobRef(blobIdOrRef);
2661
+ const blobId = ref?.blobId ?? blobIdOrRef;
2617
2662
  const cached = getCachedBlob(this.#db, blobId);
2618
2663
  if (cached !== undefined) return cached;
2619
2664
  const transport = this.#config.blobs;
2620
2665
  if (transport === undefined) {
2621
- throw invalidRequest(
2622
- 'fetchBlob requires a blob transport (SyncClientConfig.blobs, §5.9)',
2623
- );
2666
+ throw invalidRequest('fetchBlob requires a blob transport');
2624
2667
  }
2625
2668
  // §5.9.5: the authorized endpoint serves bytes inline OR (always-issue,
2626
2669
  // presign configured) a signed url the client fetches directly. On a url
2627
2670
  // arm the client MUST NOT attach host auth and MUST NOT fall through:
2628
2671
  // failure => re-request, the caller's next fetchBlob mints a fresh url.
2629
2672
  this.#requireTransport();
2630
- const response = await transport.download(blobId);
2673
+ const response = await transport.download(blobId, ref?.byteLength);
2631
2674
  let bytes: Uint8Array;
2632
2675
  if (response.kind === 'url') {
2633
2676
  if (transport.fetchUrl === undefined) {
2634
- throw invalidRequest(
2635
- 'blob download returned a url but the transport cannot fetch urls (§5.9.5)',
2636
- );
2677
+ throw invalidRequest('blob transport cannot fetch urls');
2637
2678
  }
2638
2679
  if (
2639
2680
  response.urlExpiresAtMs !== undefined &&
@@ -2642,22 +2683,22 @@ export class SyncClient {
2642
2683
  // §5.9.5: MUST NOT start a fetch at/past expiry — re-request recovers.
2643
2684
  throw new ClientSyncError(
2644
2685
  'sync.segment_expired',
2645
- `blob url for ${blobId} expired before fetch — re-request mints a fresh url (§5.9.5)`,
2686
+ 'blob url expired before fetch',
2646
2687
  true,
2647
2688
  );
2648
2689
  }
2649
- bytes = await transport.fetchUrl(response.url);
2690
+ bytes = await transport.fetchUrl(response.url, ref?.byteLength);
2650
2691
  } else {
2651
2692
  bytes = response.bytes;
2652
2693
  }
2653
- const computed = await computeBlobId(bytes);
2654
- if (computed !== blobId) {
2655
- // §5.9.5 inherits §5.1: verify the content address, reject on mismatch.
2656
- // On the url path this invalidates the fetch (no fall-through) — the
2657
- // next fetchBlob re-requests the authorized endpoint (§5.9.5 recovery).
2658
- throw invalidRequest(
2659
- `blob content address mismatch for ${blobId} (§5.9.5)`,
2660
- );
2694
+ // §5.9.5 inherits §5.1: the declared size caps the body and the content
2695
+ // address must match. On the url path either failure invalidates the
2696
+ // fetch (no fall-through); the next fetchBlob re-requests the endpoint.
2697
+ if (bytes.byteLength > (ref?.byteLength ?? bytes.byteLength)) {
2698
+ throw invalidRequest('blob exceeds its byteLength');
2699
+ }
2700
+ if ((await computeBlobId(bytes)) !== blobId) {
2701
+ throw invalidRequest('blob content address mismatch');
2661
2702
  }
2662
2703
  putCachedBlob(this.#db, blobId, bytes, this.#now());
2663
2704
  this.#db.exec('PRAGMA wal_checkpoint(TRUNCATE)');
@@ -2676,15 +2717,25 @@ export class SyncClient {
2676
2717
  enforceBlobCacheCap(this.#db, this.#schema, cap);
2677
2718
  }
2678
2719
 
2679
- /** Flush any queued blob uploads (§5.9.7 B4); safe to call standalone. */
2720
+ /**
2721
+ * Flush any queued blob uploads (§5.9.7 B4); safe to call standalone. It
2722
+ * runs on the round's operation chain because a refused upload drops
2723
+ * outbox commits a round may have captured.
2724
+ */
2680
2725
  flushBlobUploads(): Promise<void> {
2681
2726
  this.#requireTransport();
2682
- return this.#runProtectedAsync(() => this.#flushBlobUploads());
2727
+ return this.#runProtectedAsync(async () => {
2728
+ await this.#serialize(() => this.#flushBlobUploads(undefined));
2729
+ });
2683
2730
  }
2684
2731
 
2685
- async #flushBlobUploads(): Promise<void> {
2732
+ /** Returns the ids of the commits a refused upload dropped (§5.9.7). */
2733
+ async #flushBlobUploads(
2734
+ signal: AbortSignal | undefined,
2735
+ ): Promise<ReadonlySet<string>> {
2736
+ const refused = new Set<string>();
2686
2737
  const transport = this.#config.blobs;
2687
- if (transport === undefined || !this.#hasBlobs) return;
2738
+ if (transport === undefined || !this.#hasBlobs) return refused;
2688
2739
  for (const pending of listPendingUploads(this.#db)) {
2689
2740
  const cached = getCachedBlob(this.#db, pending.blobId);
2690
2741
  if (
@@ -2699,14 +2750,47 @@ export class SyncClient {
2699
2750
  'Pending blob upload body is missing or corrupt',
2700
2751
  );
2701
2752
  }
2702
- await this.#uploadOne(
2703
- transport,
2704
- pending.blobId,
2705
- cached.bytes,
2706
- pending.mediaType,
2707
- );
2753
+ try {
2754
+ await this.#uploadOne(
2755
+ transport,
2756
+ pending.blobId,
2757
+ cached.bytes,
2758
+ pending.mediaType,
2759
+ signal,
2760
+ );
2761
+ } catch (error) {
2762
+ // §5.9.7: a non-retryable server refusal is this body's final
2763
+ // outcome. Retryable refusals and transport failures fail the round.
2764
+ // A failure without a server code (`sync.transport_failed`: no
2765
+ // response, or a response without a JSON error body) is not one.
2766
+ if (
2767
+ !(error instanceof Error) ||
2768
+ !('code' in error) ||
2769
+ typeof error.code !== 'string' ||
2770
+ error.code === 'sync.transport_failed' ||
2771
+ !('retryable' in error) ||
2772
+ error.retryable !== false
2773
+ )
2774
+ throw error;
2775
+ clearPendingUpload(this.#db, pending.blobId);
2776
+ const referencing = new Set(
2777
+ this.#db
2778
+ .query(
2779
+ `SELECT commit_id FROM ${BLOB_COMMIT_REFS} WHERE blob_id=?`,
2780
+ [pending.blobId],
2781
+ )
2782
+ .map((row) => row.commit_id),
2783
+ );
2784
+ for (const commit of listOutbox(this.#db)) {
2785
+ if (!referencing.has(commit.clientCommitId)) continue;
2786
+ this.#dropIncompatibleCommit(commit, error.message, error.code);
2787
+ refused.add(commit.clientCommitId);
2788
+ }
2789
+ continue;
2790
+ }
2708
2791
  clearPendingUpload(this.#db, pending.blobId);
2709
2792
  }
2793
+ return refused;
2710
2794
  }
2711
2795
 
2712
2796
  /**
@@ -2721,7 +2805,8 @@ export class SyncClient {
2721
2805
  transport: BlobTransport,
2722
2806
  blobId: string,
2723
2807
  bytes: Uint8Array,
2724
- mediaType?: string,
2808
+ mediaType: string | undefined,
2809
+ signal: AbortSignal | undefined,
2725
2810
  ): Promise<void> {
2726
2811
  if (
2727
2812
  transport.uploadGrant !== undefined &&
@@ -2731,6 +2816,7 @@ export class SyncClient {
2731
2816
  blobId,
2732
2817
  bytes.length,
2733
2818
  mediaType,
2819
+ signal,
2734
2820
  );
2735
2821
  if (grant.kind === 'present') return; // idempotent §5.9.3 — no PUT needed
2736
2822
  if (grant.kind === 'url') {
@@ -2739,7 +2825,7 @@ export class SyncClient {
2739
2825
  grant.urlExpiresAtMs > this.#now()
2740
2826
  ) {
2741
2827
  try {
2742
- await transport.uploadToUrl(grant.url, bytes, mediaType);
2828
+ await transport.uploadToUrl(grant.url, bytes, mediaType, signal);
2743
2829
  return;
2744
2830
  } catch {
2745
2831
  // Grant PUT failed — stream through the direct endpoint below.
@@ -2749,7 +2835,7 @@ export class SyncClient {
2749
2835
  // grant.kind === 'none' (no presign store) or a failed/expired grant:
2750
2836
  // stream through the direct host-authenticated endpoint.
2751
2837
  }
2752
- await transport.upload(blobId, bytes, mediaType);
2838
+ await transport.upload(blobId, bytes, mediaType, signal);
2753
2839
  }
2754
2840
 
2755
2841
  conflicts(): readonly ConflictRecord[] {
@@ -2774,11 +2860,7 @@ export class SyncClient {
2774
2860
  maxRounds?: number,
2775
2861
  ): Promise<CurrentBaseProof> {
2776
2862
  this.#requireActive();
2777
- if (
2778
- maxRounds !== undefined &&
2779
- (!Number.isInteger(maxRounds) || maxRounds < 1 || maxRounds > 0xffffffff)
2780
- )
2781
- throw invalidRequest('maxRounds must be a positive integer');
2863
+ requireRoundBudget(maxRounds);
2782
2864
  const { clientCommitId, sequence } = outcome;
2783
2865
  const provable = (
2784
2866
  candidate: CommitOutcome | undefined,
@@ -3047,12 +3129,16 @@ export class SyncClient {
3047
3129
  }
3048
3130
 
3049
3131
  /**
3050
- * §8.6: subscribe to presence changes (join/update/leave on any held key).
3051
- * The subscribable twin of the `onPresence` config callback — React's
3052
- * `usePresence` targets this so many components can watch one client.
3132
+ * §8.6: subscribe to presence changes (join/update/leave on any held key)
3133
+ * and to the server's refusal of this client's own publish (`error`,
3134
+ * §8.6.2). The subscribable twin of the `onPresence` config callback —
3135
+ * React's `usePresence` targets this so many components can watch one
3136
+ * client.
3053
3137
  * Returns an unsubscribe function.
3054
3138
  */
3055
- onPresence(listener: (scopeKey: string) => void): () => void {
3139
+ onPresence(
3140
+ listener: (scopeKey: string, error?: PresenceErrorCode) => void,
3141
+ ): () => void {
3056
3142
  this.#presenceListeners.add(listener);
3057
3143
  return () => {
3058
3144
  this.#presenceListeners.delete(listener);
@@ -3328,7 +3414,13 @@ export class SyncClient {
3328
3414
  let remaining = false;
3329
3415
  this.#applyBatch((batch) => {
3330
3416
  this.#db.transaction(() => {
3331
- const result = evictScopedRows(this.#db, table, effective, pinned);
3417
+ const result = evictScopedRows(
3418
+ this.#db,
3419
+ table,
3420
+ effective,
3421
+ this.#scopeOwners(base.table, subId),
3422
+ pinned,
3423
+ );
3332
3424
  remaining = result.remaining;
3333
3425
  deleteWindowUnit(this.#db, baseKey, unit);
3334
3426
  deleteSubscription(this.#db, subId);
@@ -3379,6 +3471,7 @@ export class SyncClient {
3379
3471
  this.#db,
3380
3472
  table,
3381
3473
  entry.effective,
3474
+ this.#scopeOwners(entry.table, entry.subId),
3382
3475
  pinned,
3383
3476
  );
3384
3477
  remaining = result.remaining;
@@ -3390,6 +3483,23 @@ export class SyncClient {
3390
3483
  }
3391
3484
  }
3392
3485
 
3486
+ /**
3487
+ * §3.3/§4.8/§5.6 row ownership: the effective scopes of every other
3488
+ * active subscription on `table` that holds rows (`cursor >= 0`). A
3489
+ * scoped delete keeps every row one of them still covers.
3490
+ */
3491
+ #scopeOwners(table: string, exceptId: string): ScopeMap[] {
3492
+ return loadSubscriptions(this.#db).flatMap((sub) =>
3493
+ sub.id !== exceptId &&
3494
+ sub.table === table &&
3495
+ sub.status === 'active' &&
3496
+ sub.cursor >= 0 &&
3497
+ sub.effectiveScopes !== undefined
3498
+ ? [sub.effectiveScopes]
3499
+ : [],
3500
+ );
3501
+ }
3502
+
3393
3503
  /**
3394
3504
  * §4.8 E1: primary keys of `table` referenced by any still-pending outbox
3395
3505
  * commit — rows that MUST NOT be evicted until the commit drains.
@@ -3424,6 +3534,9 @@ export class SyncClient {
3424
3534
  const operations: OutboxOperation[] = mutations.map((mutation) => {
3425
3535
  const table = this.#table(mutation.table);
3426
3536
  if (mutation.op === 'delete') {
3537
+ // §2.4: the server rejects a rowId holding U+0000.
3538
+ if (mutation.rowId.includes('\u0000'))
3539
+ throw invalidRequest('rowId holds U+0000');
3427
3540
  return definedObject({
3428
3541
  table: mutation.table,
3429
3542
  rowId: mutation.rowId,
@@ -4198,6 +4311,11 @@ export class SyncClient {
4198
4311
  * `setWindow` at an await point.
4199
4312
  */
4200
4313
  sync(): Promise<SyncSummary> {
4314
+ return this.#sync();
4315
+ }
4316
+
4317
+ /** `signal` is the `syncUntilIdle` deadline (§7.7); `sync()` has none. */
4318
+ #sync(signal?: AbortSignal): Promise<SyncSummary> {
4201
4319
  this.#requireActive();
4202
4320
  if (this.#syncOutstanding) {
4203
4321
  return Promise.reject(
@@ -4207,7 +4325,7 @@ export class SyncClient {
4207
4325
  this.#syncOutstanding = true;
4208
4326
  this.#beginDiagnosticsDeferral();
4209
4327
  const startedAtMs = this.#now();
4210
- return this.#serialize(() => this.#runSync())
4328
+ return this.#serialize(() => this.#runSync(signal))
4211
4329
  .then(
4212
4330
  (summary) => {
4213
4331
  const completedAtMs = this.#now();
@@ -4326,7 +4444,7 @@ export class SyncClient {
4326
4444
  : 'client.unknown_failure';
4327
4445
  }
4328
4446
 
4329
- async #runSync(): Promise<SyncSummary> {
4447
+ async #runSync(signal: AbortSignal | undefined): Promise<SyncSummary> {
4330
4448
  this.#requireTransport();
4331
4449
  this.#roundRetryDelayMs = undefined;
4332
4450
  this.#progress.emit({
@@ -4384,7 +4502,14 @@ export class SyncClient {
4384
4502
  this.#hasBlobs &&
4385
4503
  this.#config.blobs !== undefined
4386
4504
  ) {
4387
- await this.#flushBlobUploads();
4505
+ const refused = await this.#flushBlobUploads(signal);
4506
+ // §5.9.7: a commit whose blob the server refused left the outbox.
4507
+ for (let index = outbox.length - 1; index >= 0; index--) {
4508
+ if (!refused.has(outbox[index]!.clientCommitId)) continue;
4509
+ outbox.splice(index, 1);
4510
+ pushFrames.splice(index, 1);
4511
+ frameBytes.splice(index, 1);
4512
+ }
4388
4513
  }
4389
4514
  // Captured after encoding: a subscribe/unsubscribe during the
4390
4515
  // encryption await must not make the final request exceed the byte
@@ -4459,7 +4584,7 @@ export class SyncClient {
4459
4584
  msgKind: 'request',
4460
4585
  frames,
4461
4586
  });
4462
- const responseBytes = await this.#roundTrip(requestBytes);
4587
+ const responseBytes = await this.#roundTrip(requestBytes, signal);
4463
4588
  const message = decodeMessage(responseBytes);
4464
4589
  if (message.msgKind !== 'response') {
4465
4590
  throw invalidRequest('transport returned a non-response message');
@@ -4472,6 +4597,7 @@ export class SyncClient {
4472
4597
  subs,
4473
4598
  'pull',
4474
4599
  resetEpoch,
4600
+ signal,
4475
4601
  );
4476
4602
  // §4.8 E1: the push half may have drained commits that pinned rows of
4477
4603
  // a shrunk window unit — retry any deferred evictions now.
@@ -4482,6 +4608,9 @@ export class SyncClient {
4482
4608
  : { state: 'complete' },
4483
4609
  );
4484
4610
  this.#retryDelayMs = 250;
4611
+ // §4.7: a subscription that still holds a resume token needs another
4612
+ // round, also when the drive that started its bootstrap ends early.
4613
+ if (summary.bootstrapping.length > 0) this.#setSyncNeeded(true);
4485
4614
  if (deferred > 0) {
4486
4615
  // §6.1 splitBatch remainder: more queued commits than this request
4487
4616
  // could carry — keep the sync-needed signal raised for the host.
@@ -4489,11 +4618,14 @@ export class SyncClient {
4489
4618
  return { ...summary, deferredCommits: deferred };
4490
4619
  }
4491
4620
  return summary;
4492
- } catch (error) {
4621
+ } catch (caught) {
4493
4622
  // §1.5: a round that ended without a complete, decodable response is a
4494
4623
  // failure the server could not see — keep its code for the next request.
4495
- const unseen = this.#unseenRoundFailureCode(error);
4624
+ const unseen = this.#unseenRoundFailureCode(caught);
4496
4625
  if (unseen !== undefined) this.#roundFailureCode = unseen;
4626
+ // §7.7: a round the drive deadline aborted ends with the deadline's
4627
+ // error, whichever transport call observed the abort.
4628
+ const error: unknown = signal?.aborted === true ? signal.reason : caught;
4497
4629
  // §7.3.5: a request-level lease code stops-and-surfaces — record it
4498
4630
  // in leaseState (no local-data purge, §7.3.4) and re-throw. Not a
4499
4631
  // silent retry: the app drives recovery to a live resolver. The
@@ -4527,6 +4659,8 @@ export class SyncClient {
4527
4659
  ? details.causeKind
4528
4660
  : undefined;
4529
4661
  const unreachable = causeKind === 'connect' || causeKind === 'network';
4662
+ // The round's work is still owed; the host's connectivity resume runs it.
4663
+ if (unreachable) this.#setSyncNeeded(true);
4530
4664
  if (retryable && !unreachable) {
4531
4665
  const realtimeUnavailable = error instanceof RealtimeUnavailableError;
4532
4666
  const delay = realtimeUnavailable
@@ -4579,11 +4713,7 @@ export class SyncClient {
4579
4713
  : (options?.timeoutMs ?? SYNC_IDLE_TIMEOUT_MS);
4580
4714
  // §7.7: an explicit budget is a strict positive round count. Zero,
4581
4715
  // fractional, and out-of-range values are caller errors.
4582
- if (
4583
- maxRounds !== undefined &&
4584
- (!Number.isInteger(maxRounds) || maxRounds < 1 || maxRounds > 0xffffffff)
4585
- )
4586
- throw invalidRequest('maxRounds must be a positive integer');
4716
+ requireRoundBudget(maxRounds);
4587
4717
  if (
4588
4718
  timeoutMs !== Number.POSITIVE_INFINITY &&
4589
4719
  !(Number.isFinite(timeoutMs) && timeoutMs > 0)
@@ -4621,17 +4751,8 @@ export class SyncClient {
4621
4751
  ): Promise<SyncIdleResult> {
4622
4752
  const budget = maxRounds ?? 20;
4623
4753
  const aggregate = {
4624
- pushed: 0,
4625
- applied: [] as string[],
4626
- rejected: [] as string[],
4627
- retryable: [] as string[],
4628
- conflicts: [] as ConflictRecord[],
4629
- commitsApplied: 0,
4630
- segmentRowsApplied: 0,
4754
+ ...emptySummary(0),
4631
4755
  bootstrapping: [] as string[],
4632
- resets: [] as string[],
4633
- revoked: [] as string[],
4634
- failed: [] as string[],
4635
4756
  deferredCommits: 0,
4636
4757
  schemaFloor: undefined as SchemaFloor | undefined,
4637
4758
  };
@@ -4658,42 +4779,40 @@ export class SyncClient {
4658
4779
  let round = 0;
4659
4780
  let expired = false;
4660
4781
  let timer: ReturnType<typeof setTimeout> | undefined;
4782
+ // One signal reaches every transport call of the drive's rounds.
4783
+ const controller = new AbortController();
4784
+ const expiredError = () =>
4785
+ new ClientSyncError(
4786
+ 'sync.idle_timeout',
4787
+ 'syncUntilIdle exceeded its deadline',
4788
+ true,
4789
+ { timeoutMs, rounds: round },
4790
+ );
4661
4791
  const deadline =
4662
4792
  timeoutMs === Number.POSITIVE_INFINITY
4663
4793
  ? undefined
4664
4794
  : new Promise<never>((_, reject) => {
4665
4795
  timer = setTimeout(() => {
4666
4796
  expired = true;
4797
+ const error = expiredError();
4667
4798
  // The transport may never answer; release the round so the
4668
4799
  // client can run another one after the caller sees the error.
4800
+ controller.abort(error);
4669
4801
  this.#abortPendingRound('syncUntilIdle deadline exceeded');
4670
- reject(
4671
- new ClientSyncError(
4672
- 'sync.idle_timeout',
4673
- 'syncUntilIdle exceeded its deadline',
4674
- true,
4675
- { timeoutMs, rounds: round },
4676
- ),
4677
- );
4802
+ reject(error);
4678
4803
  }, timeoutMs);
4679
4804
  });
4680
4805
  const drive = (async (): Promise<SyncIdleResult> => {
4681
4806
  for (; round < budget; round++) {
4682
4807
  // The race already rejected; stop spending rounds on a dead drive.
4683
- if (expired)
4684
- throw new ClientSyncError(
4685
- 'sync.idle_timeout',
4686
- 'syncUntilIdle exceeded its deadline',
4687
- true,
4688
- { timeoutMs, rounds: round },
4689
- );
4808
+ if (expired) throw expiredError();
4690
4809
  const before = JSON.stringify(
4691
4810
  loadSubscriptions(this.#db).map((sub) => [
4692
4811
  sub.id,
4693
4812
  sub.bootstrapState,
4694
4813
  ]),
4695
4814
  );
4696
- const last = await this.sync();
4815
+ const last = await this.#sync(controller.signal);
4697
4816
  // Historical counters accumulate; current readiness (`bootstrapping`,
4698
4817
  // `deferredCommits`, `schemaFloor`) describes the latest round.
4699
4818
  aggregate.pushed += last.pushed;
@@ -4756,7 +4875,10 @@ export class SyncClient {
4756
4875
  * refuses the round with `RealtimeUnavailableError` instead of using the
4757
4876
  * seam (SPEC §8.8).
4758
4877
  */
4759
- #roundTrip(request: Uint8Array): Promise<Uint8Array> {
4878
+ #roundTrip(
4879
+ request: Uint8Array,
4880
+ signal: AbortSignal | undefined,
4881
+ ): Promise<Uint8Array> {
4760
4882
  const socket = this.#socket;
4761
4883
  if (socket !== undefined && this.#realtimePolicy !== 'off') {
4762
4884
  return new Promise<Uint8Array>((resolve, reject) => {
@@ -4777,12 +4899,9 @@ export class SyncClient {
4777
4899
  // Rust core records when its realtime round call fails (§8.8).
4778
4900
  this.#setRealtimeState('lost', 'sync.transport_failed');
4779
4901
  reject(
4780
- new ClientSyncError(
4781
- 'sync.transport_failed',
4782
- 'socket round send failed',
4783
- true,
4784
- { causeKind: 'unknown' },
4785
- ),
4902
+ transportFailed('socket round send failed', {
4903
+ causeKind: 'unknown',
4904
+ }),
4786
4905
  );
4787
4906
  }
4788
4907
  });
@@ -4793,7 +4912,7 @@ export class SyncClient {
4793
4912
  throw this.#realtimeUnavailableError();
4794
4913
  }
4795
4914
  return Promise.resolve()
4796
- .then(() => this.#config.transport(request))
4915
+ .then(() => this.#config.transport(request, signal))
4797
4916
  .catch((error: unknown) => {
4798
4917
  if (
4799
4918
  error instanceof ClientSyncError ||
@@ -4801,12 +4920,9 @@ export class SyncClient {
4801
4920
  ) {
4802
4921
  throw error;
4803
4922
  }
4804
- throw new ClientSyncError(
4805
- 'sync.transport_failed',
4806
- 'transport round failed',
4807
- true,
4808
- { causeKind: 'unknown' },
4809
- );
4923
+ throw transportFailed('transport round failed', {
4924
+ causeKind: 'unknown',
4925
+ });
4810
4926
  });
4811
4927
  }
4812
4928
 
@@ -4832,11 +4948,11 @@ export class SyncClient {
4832
4948
  }
4833
4949
 
4834
4950
  /** Abort the in-flight socket round (socket closed or disconnected). */
4835
- #abortPendingRound(reason: string): void {
4951
+ #abortPendingRound(reason: string | ClientSyncError): void {
4836
4952
  const round = this.#pendingRound;
4837
4953
  if (round === undefined) return;
4838
4954
  this.#pendingRound = undefined;
4839
- round.reject(new ClientSyncError('sync.transport_failed', reason, true));
4955
+ round.reject(typeof reason === 'string' ? transportFailed(reason) : reason);
4840
4956
  }
4841
4957
 
4842
4958
  // -- realtime (§8 client side) ----------------------------------------------
@@ -4898,27 +5014,34 @@ export class SyncClient {
4898
5014
  }
4899
5015
  let openedSocket: RealtimeSocket | undefined;
4900
5016
  try {
4901
- const socket = await connector({
4902
- onText: (text) => {
4903
- if (generation !== this.#realtimeGeneration || !this.#started) return;
4904
- this.#handleRealtimeText(text);
4905
- },
4906
- onBinary: (bytes) => {
4907
- if (generation !== this.#realtimeGeneration || !this.#started) return;
4908
- this.#routeRealtimeBinary(bytes);
4909
- },
4910
- onClose: () => {
4911
- if (openedSocket === undefined || this.#socket !== openedSocket)
4912
- return;
4913
- this.#socket = undefined;
4914
- this.#presence.clear(); // §8.6.1: presence is per-connection
4915
- // §8.8: an undesired close is a visible lost state, not a silent
4916
- // downgrade to HTTP rounds.
4917
- this.#setRealtimeState('lost', REALTIME_LOST_CODE);
4918
- this.#abortPendingRound('realtime socket closed mid-round (§8.7)');
4919
- this.#emitDiagnostics();
5017
+ const socket = await connector(
5018
+ {
5019
+ onText: (text) => {
5020
+ if (generation !== this.#realtimeGeneration || !this.#started)
5021
+ return;
5022
+ this.#heard(this.#handleRealtimeText(text));
5023
+ },
5024
+ onBinary: (bytes) => {
5025
+ if (generation !== this.#realtimeGeneration || !this.#started)
5026
+ return;
5027
+ this.#heard();
5028
+ this.#routeRealtimeBinary(bytes);
5029
+ },
5030
+ onClose: () => {
5031
+ if (openedSocket === undefined || this.#socket !== openedSocket)
5032
+ return;
5033
+ this.#socket = undefined;
5034
+ this.#disarm();
5035
+ this.#presence.clear(); // §8.6.1: presence is per-connection
5036
+ // §8.8: an undesired close is a visible lost state, not a silent
5037
+ // downgrade to HTTP rounds.
5038
+ this.#setRealtimeState('lost', REALTIME_LOST_CODE);
5039
+ this.#abortPendingRound('realtime socket closed mid-round (§8.7)');
5040
+ this.#emitDiagnostics();
5041
+ },
4920
5042
  },
4921
- });
5043
+ this.#clientId,
5044
+ );
4922
5045
  openedSocket = socket;
4923
5046
  if (!this.#transportEnabled) {
4924
5047
  socket.close();
@@ -4952,6 +5075,7 @@ export class SyncClient {
4952
5075
  this.#realtimeConnectPromise = undefined;
4953
5076
  this.#socket?.close();
4954
5077
  this.#socket = undefined;
5078
+ this.#disarm();
4955
5079
  this.#presence.clear(); // §8.6.1: presence is per-connection
4956
5080
  this.#abortPendingRound('realtime socket disconnected mid-round (§8.7)');
4957
5081
  this.#setRealtimeState(
@@ -4969,15 +5093,49 @@ export class SyncClient {
4969
5093
  * Close the socket and record the loss; later rounds use the configured
4970
5094
  * transport until the supervisor reconnects.
4971
5095
  */
4972
- #loseSocket(): void {
5096
+ #loseSocket(code = REALTIME_LOST_CODE): void {
4973
5097
  const socket = this.#socket;
4974
5098
  this.#socket = undefined;
5099
+ this.#disarm();
4975
5100
  this.#presence.clear(); // §8.6.1: presence is per-connection
4976
- this.#setRealtimeState('lost', REALTIME_LOST_CODE);
5101
+ this.#setRealtimeState('lost', code);
4977
5102
  socket?.close();
4978
5103
  this.#emitDiagnostics();
4979
5104
  }
4980
5105
 
5106
+ /**
5107
+ * SPEC §8.5 watchdog: the connection's first `heartbeat` arms it; once
5108
+ * armed, every inbound frame restarts it, and twice the interval of
5109
+ * silence loses the socket. A server that sends no heartbeats never arms it.
5110
+ */
5111
+ #heard(heartbeat?: boolean): void {
5112
+ if (
5113
+ this.#socket === undefined ||
5114
+ !this.#transportEnabled ||
5115
+ (this.#watchdog === undefined && heartbeat !== true)
5116
+ )
5117
+ return;
5118
+ this.#watchdog?.();
5119
+ this.#watchdog = this.#schedule(() => {
5120
+ this.#loseSocket('transport.timeout');
5121
+ this.#abortPendingRound(
5122
+ new ClientSyncError(
5123
+ 'transport.timeout',
5124
+ 'realtime heartbeat missed',
5125
+ true,
5126
+ {
5127
+ causeKind: 'heartbeat',
5128
+ },
5129
+ ),
5130
+ );
5131
+ }, 2 * this.#heartbeatMs);
5132
+ }
5133
+
5134
+ #disarm(): void {
5135
+ this.#watchdog?.();
5136
+ this.#watchdog = undefined;
5137
+ }
5138
+
4981
5139
  /**
4982
5140
  * §8.7 channel-tag routing (synchronous, so chunk order is preserved):
4983
5141
  * `0x01` chunks feed the in-flight round's assembler; `0x00` messages
@@ -5017,7 +5175,8 @@ export class SyncClient {
5017
5175
  // Unknown tag: tolerated and ignored (§8.7 closed registry).
5018
5176
  }
5019
5177
 
5020
- #handleRealtimeText(text: string): void {
5178
+ /** Returns whether the text was a §8.5 `heartbeat`. */
5179
+ #handleRealtimeText(text: string): boolean | undefined {
5021
5180
  let parsed: ReturnType<typeof parseRealtimeServerEvent>;
5022
5181
  try {
5023
5182
  parsed = parseRealtimeServerEvent(text);
@@ -5039,47 +5198,38 @@ export class SyncClient {
5039
5198
  this.#emitSyncNeeded(event.data.reason);
5040
5199
  return;
5041
5200
  }
5042
- if (event.event === 'presence') {
5043
- this.#applyPresence(event.data);
5044
- }
5045
- }
5046
-
5047
- /** §8.6 apply an inbound presence fanout to the local map. */
5048
- #applyPresence(data: {
5049
- scopeKey: string;
5050
- kind?: PresenceKind;
5051
- actorId?: string;
5052
- clientId?: string;
5053
- doc?: Record<string, unknown> | null;
5054
- error?: string;
5055
- }): void {
5056
- // The publisher-directed error variant (§8.6.2) surfaces via the
5057
- // sync-needed reason channel is inappropriate; it is an out-of-band
5058
- // presence rejection — record nothing to the peer map, just ignore
5059
- // here (setPresence callers observe it through the callback if wired).
5060
- if (data.error !== undefined || data.kind === undefined) return;
5061
- const { scopeKey, kind, actorId, clientId } = data;
5062
- if (actorId === undefined || clientId === undefined) return;
5063
- const peerKey = `${actorId} ${clientId}`;
5064
- let peers = this.#presence.get(scopeKey);
5065
- if (kind === 'leave') {
5066
- peers?.delete(peerKey);
5067
- if (peers !== undefined && peers.size === 0) {
5068
- this.#presence.delete(scopeKey);
5069
- }
5070
- } else {
5071
- const doc = data.doc;
5072
- if (doc === null || doc === undefined) return;
5073
- if (peers === undefined) {
5074
- peers = new Map();
5075
- this.#presence.set(scopeKey, peers);
5201
+ if (event.event === 'presence') this.#applyPresence(event.data);
5202
+ return event.event === 'heartbeat';
5203
+ }
5204
+
5205
+ /**
5206
+ * §8.6 apply an inbound presence fanout to the local map, or hand a
5207
+ * publisher-directed rejection (§8.6.2) to the listeners unchanged.
5208
+ */
5209
+ #applyPresence(data: RealtimePresenceEvent['data']): void {
5210
+ const { scopeKey, kind, actorId, clientId, doc, error } = data;
5211
+ if (error === undefined) {
5212
+ // No `kind` is the client→server shape. The parser pins both ids and a
5213
+ // join/update doc on a fanout (§8.6.2); these checks narrow types.
5214
+ if (kind === undefined || actorId === undefined || clientId === undefined)
5215
+ return;
5216
+ const peerKey = `${actorId} ${clientId}`;
5217
+ let peers = this.#presence.get(scopeKey);
5218
+ if (kind === 'leave') {
5219
+ peers?.delete(peerKey);
5220
+ if (peers?.size === 0) this.#presence.delete(scopeKey);
5221
+ } else if (doc) {
5222
+ if (peers === undefined) {
5223
+ peers = new Map();
5224
+ this.#presence.set(scopeKey, peers);
5225
+ }
5226
+ peers.set(peerKey, { actorId, clientId, doc });
5076
5227
  }
5077
- peers.set(peerKey, { actorId, clientId, doc });
5078
5228
  }
5079
- this.#config.onPresence?.(scopeKey);
5229
+ this.#config.onPresence?.(scopeKey, error);
5080
5230
  for (const listener of this.#presenceListeners) {
5081
5231
  try {
5082
- listener(scopeKey);
5232
+ listener(scopeKey, error);
5083
5233
  } catch {
5084
5234
  // A UI listener must never break presence application.
5085
5235
  }
@@ -5147,6 +5297,7 @@ export class SyncClient {
5147
5297
  sentSubs: readonly SubscriptionRecord[] | undefined,
5148
5298
  mode: 'pull' | 'delta',
5149
5299
  resetEpoch: number = this.#localResetEpoch,
5300
+ signal?: AbortSignal,
5150
5301
  ): Promise<SyncSummary> {
5151
5302
  const summary = emptySummary(sentCommits.length);
5152
5303
  const commitsById = new Map(
@@ -5179,11 +5330,7 @@ export class SyncClient {
5179
5330
  if (header?.type !== 'RESP_HEADER') {
5180
5331
  throw invalidRequest('missing RESP_HEADER');
5181
5332
  }
5182
- if (
5183
- message.wireVersion < 2 ||
5184
- header.logEpoch === undefined ||
5185
- header.resetRequired === undefined
5186
- ) {
5333
+ if (message.wireVersion < 2) {
5187
5334
  throw new ClientSyncError(
5188
5335
  'client.invalid_host_response',
5189
5336
  'the server response does not carry wire version 2 log-epoch state',
@@ -5232,19 +5379,51 @@ export class SyncClient {
5232
5379
  let section: OpenSection | undefined;
5233
5380
  let errorFrame: ClientSyncError | undefined;
5234
5381
  let deltaCursor = -1;
5382
+ let deltaDropped = false;
5383
+ // §8.2: a delta section applies only to a locally active subscription
5384
+ // that holds a cursor and no resume token. A subscription at cursor -1
5385
+ // (never synced, reset, or rebootstrapped) bootstraps on its own pull.
5386
+ const skipsSection = (sub: SubscriptionRecord | undefined) =>
5387
+ sub === undefined ||
5388
+ (mode === 'delta' &&
5389
+ (sub.status !== 'active' ||
5390
+ sub.cursor < 0 ||
5391
+ sub.bootstrapState !== undefined));
5235
5392
  const incompleteSubscriptions = new Set(
5236
5393
  mode === 'pull' ? subsById.keys() : [],
5237
5394
  );
5238
5395
 
5239
- // §5.4/§5.5: issue every segment download of this response before the
5240
- // apply loop reaches the first one, so a paged bootstrap overlaps its page
5241
- // fetches instead of paying one blocking round trip per page. Eligibility
5242
- // mirrors the loop: a descriptor outside an applied section, inside a
5243
- // skipped delta section, or from an unconfigured downloader is never
5244
- // fetched, and a prefetched failure is only observed where the loop awaits
5245
- // it. A promise left over because an earlier frame ended apply is handled
5246
- // at creation, never unhandled.
5396
+ // §5.4/§5.5: queue every segment download of this response, so a paged
5397
+ // bootstrap overlaps its page fetches instead of paying one blocking round
5398
+ // trip per page. At most four downloads run at once, the Rust native
5399
+ // transport's pool size: enough to overlap round trips, while the client
5400
+ // holds at most four bodies, the one being applied and three ahead. The
5401
+ // loop takes each body out of the map before applying it and tops the
5402
+ // window up at the next descriptor. Eligibility mirrors the loop: a
5403
+ // descriptor outside an applied section, inside a skipped delta section,
5404
+ // or from an unconfigured downloader is never queued, and a failure is
5405
+ // only observed where the loop awaits it. A promise left over because an
5406
+ // earlier frame ended apply is handled at creation, never unhandled.
5407
+ const queued: [SegmentRefFrame, SubscriptionRecord][] = [];
5247
5408
  const prefetched = new Map<SegmentRefFrame, Promise<Uint8Array>>();
5409
+ const prefetch = () => {
5410
+ for (let next; prefetched.size < 4 && (next = queued.shift());) {
5411
+ const [frame, sub] = next;
5412
+ const pending = this.#downloadSegment(
5413
+ frame,
5414
+ sub,
5415
+ (bytes) => {
5416
+ // Only the descriptor the loop is applying reports bytes; a
5417
+ // sibling still in flight would overwrite its progress.
5418
+ if (reporting === frame)
5419
+ this.#progress.update({ bytesReceived: bytes });
5420
+ },
5421
+ signal,
5422
+ );
5423
+ pending.catch(() => {});
5424
+ prefetched.set(frame, pending);
5425
+ }
5426
+ };
5248
5427
  let reporting: SegmentRefFrame | undefined;
5249
5428
  const deleteKey = (commitSeq: number, table: string, rowId: string) =>
5250
5429
  `${commitSeq}\u0000${table}\u0000${rowId}`;
@@ -5253,12 +5432,7 @@ export class SyncClient {
5253
5432
  for (const frame of message.frames) {
5254
5433
  if (frame.type === 'SUB_START') {
5255
5434
  const sub = subsById.get(frame.id);
5256
- openSub =
5257
- sub === undefined ||
5258
- (mode === 'delta' &&
5259
- (sub.status !== 'active' || sub.bootstrapState !== undefined))
5260
- ? undefined
5261
- : sub;
5435
+ openSub = skipsSection(sub) ? undefined : sub;
5262
5436
  } else if (frame.type === 'SUB_END') {
5263
5437
  openSub = undefined;
5264
5438
  } else if (frame.type === 'ERROR') {
@@ -5272,16 +5446,10 @@ export class SyncClient {
5272
5446
  (this.#acceptMask() & ACCEPT_SQLITE) === 0
5273
5447
  )
5274
5448
  ) {
5275
- const pending = this.#downloadSegment(frame, openSub, (bytes) => {
5276
- // Only the descriptor the loop is applying reports bytes; a
5277
- // sibling still in flight would overwrite its progress.
5278
- if (reporting === frame)
5279
- this.#progress.update({ bytesReceived: bytes });
5280
- });
5281
- pending.catch(() => {});
5282
- prefetched.set(frame, pending);
5449
+ queued.push([frame, openSub]);
5283
5450
  }
5284
5451
  }
5452
+ prefetch();
5285
5453
  }
5286
5454
 
5287
5455
  // §2.2 scope migration: the server emits the delete tagged with the old
@@ -5303,12 +5471,7 @@ export class SyncClient {
5303
5471
  for (const frame of message.frames) {
5304
5472
  if (frame.type === 'SUB_START') {
5305
5473
  const sub = subsById.get(frame.id);
5306
- openSub =
5307
- sub === undefined ||
5308
- (mode === 'delta' &&
5309
- (sub.status !== 'active' || sub.bootstrapState !== undefined))
5310
- ? undefined
5311
- : sub;
5474
+ openSub = skipsSection(sub) ? undefined : sub;
5312
5475
  } else if (frame.type === 'SUB_END') {
5313
5476
  openSub = undefined;
5314
5477
  } else if (frame.type === 'ERROR') {
@@ -5436,10 +5599,12 @@ export class SyncClient {
5436
5599
  sub.cursor < 0 &&
5437
5600
  sub.bootstrapState === undefined &&
5438
5601
  frame.bootstrap;
5602
+ // §4.4: a section whose registration was removed mid-round
5603
+ // (unsubscribe, eviction) is skipped; it never re-creates it.
5439
5604
  const skip =
5440
- sub === undefined ||
5441
- (mode === 'delta' &&
5442
- (sub.status !== 'active' || sub.bootstrapState !== undefined));
5605
+ skipsSection(sub) ||
5606
+ getSubscription(this.#db, frame.id) === undefined;
5607
+ if (skip && mode === 'delta') deltaDropped = true;
5443
5608
  section = { start: frame, sub, fresh, skip, cleared: false };
5444
5609
  break;
5445
5610
  }
@@ -5498,11 +5663,16 @@ export class SyncClient {
5498
5663
  rowsProcessed: 0,
5499
5664
  rowsTotal: frame.rowCount,
5500
5665
  });
5501
- const prefetch = prefetched.get(frame);
5502
- if (prefetch !== undefined) reporting = frame;
5503
- bytes = await (prefetch ??
5504
- this.#downloadSegment(frame, section.sub, (bytesReceived) =>
5505
- this.#progress.update({ bytesReceived }),
5666
+ prefetch();
5667
+ const pending = prefetched.get(frame);
5668
+ prefetched.delete(frame);
5669
+ if (pending !== undefined) reporting = frame;
5670
+ bytes = await (pending ??
5671
+ this.#downloadSegment(
5672
+ frame,
5673
+ section.sub,
5674
+ (bytesReceived) => this.#progress.update({ bytesReceived }),
5675
+ signal,
5506
5676
  ));
5507
5677
  } else {
5508
5678
  bytes = frame.payload;
@@ -5522,7 +5692,7 @@ export class SyncClient {
5522
5692
  await this.#applySegmentOrFail(
5523
5693
  section,
5524
5694
  summary,
5525
- (table, clearFirst, effective) =>
5695
+ (table, clearFirst, effective, owners) =>
5526
5696
  applySqliteSegment(
5527
5697
  this.#db,
5528
5698
  this.#schema,
@@ -5538,6 +5708,7 @@ export class SyncClient {
5538
5708
  table,
5539
5709
  clearFirst,
5540
5710
  effective,
5711
+ owners,
5541
5712
  frame.rowCount > 0,
5542
5713
  resetEpoch,
5543
5714
  ),
@@ -5564,7 +5735,7 @@ export class SyncClient {
5564
5735
  await this.#applySegmentOrFail(
5565
5736
  section,
5566
5737
  summary,
5567
- (table, clearFirst, effective) =>
5738
+ (table, clearFirst, effective, owners) =>
5568
5739
  applyRowsSegment(
5569
5740
  this.#db,
5570
5741
  this.#schema,
@@ -5574,6 +5745,7 @@ export class SyncClient {
5574
5745
  table,
5575
5746
  clearFirst,
5576
5747
  effective,
5748
+ owners,
5577
5749
  segment.blocks.some((block) => block.length > 0),
5578
5750
  resetEpoch,
5579
5751
  ),
@@ -5665,6 +5837,11 @@ export class SyncClient {
5665
5837
 
5666
5838
  if (mode === 'delta') {
5667
5839
  if (deltaCursor >= 0) this.#sendAck(deltaCursor);
5840
+ // §8.2: a skipped delta section is a drop; a pull repairs it.
5841
+ if (deltaDropped) {
5842
+ this.#setSyncNeeded(true);
5843
+ this.#emitSyncNeeded('catchup-required');
5844
+ }
5668
5845
  } else {
5669
5846
  this.#ackAfterPull();
5670
5847
  }
@@ -5957,8 +6134,12 @@ export class SyncClient {
5957
6134
  }
5958
6135
 
5959
6136
  /** Whether a fresh-bootstrap clear would remove at least one local row. */
5960
- #scopedRowsExist(table: CompiledClientTable, effective: ScopeMap): boolean {
5961
- const scope = localScopePredicate(table, effective);
6137
+ #scopedRowsExist(
6138
+ table: CompiledClientTable,
6139
+ effective: ScopeMap,
6140
+ owners: readonly ScopeMap[],
6141
+ ): boolean {
6142
+ const scope = localScopePredicate(table, effective, owners);
5962
6143
  return (
5963
6144
  scope !== undefined &&
5964
6145
  this.#db.query(
@@ -5973,19 +6154,21 @@ export class SyncClient {
5973
6154
  table: CompiledClientTable,
5974
6155
  clearFirst: boolean,
5975
6156
  effective: ScopeMap,
6157
+ owners: readonly ScopeMap[],
5976
6158
  hasRows: boolean,
5977
6159
  resetEpoch: number,
5978
6160
  ): Parameters<typeof applyRowsSegment>[4] {
5979
6161
  return {
5980
6162
  clearFirst,
5981
6163
  effective,
6164
+ owners,
5982
6165
  isCurrent: () => this.#localResetEpoch === resetEpoch,
5983
6166
  onProgress: (rowsProcessed) => this.#progress.update({ rowsProcessed }),
5984
6167
  transaction: (fn) =>
5985
6168
  this.#applyBatch((batch) => {
5986
6169
  if (
5987
6170
  hasRows ||
5988
- (clearFirst && this.#scopedRowsExist(table, effective))
6171
+ (clearFirst && this.#scopedRowsExist(table, effective, owners))
5989
6172
  )
5990
6173
  batch.table(table.name);
5991
6174
  const result = fn();
@@ -6007,6 +6190,7 @@ export class SyncClient {
6007
6190
  table: CompiledClientTable,
6008
6191
  clearFirst: boolean,
6009
6192
  effective: ScopeMap,
6193
+ owners: readonly ScopeMap[],
6010
6194
  ) => number | Promise<number>,
6011
6195
  clearFirst: boolean,
6012
6196
  ): Promise<void> {
@@ -6018,6 +6202,7 @@ export class SyncClient {
6018
6202
  table,
6019
6203
  clearFirst,
6020
6204
  section.start.effectiveScopes,
6205
+ this.#scopeOwners(sub.table, sub.id),
6021
6206
  );
6022
6207
  section.cleared = true;
6023
6208
  } catch (error) {
@@ -6025,6 +6210,8 @@ export class SyncClient {
6025
6210
  error instanceof ClientSyncError &&
6026
6211
  error.code === 'sync.scope_revoked'
6027
6212
  ) {
6213
+ section.skip = true;
6214
+ if (getSubscription(this.#db, sub.id) === undefined) return;
6028
6215
  const registered = getWindowUnitBySubId(this.#db, sub.id);
6029
6216
  this.#applyBatch((batch) => {
6030
6217
  saveSubscription(
@@ -6045,7 +6232,6 @@ export class SyncClient {
6045
6232
  }
6046
6233
  });
6047
6234
  summary.failed.push(sub.id);
6048
- section.skip = true;
6049
6235
  return;
6050
6236
  }
6051
6237
  throw error;
@@ -6056,6 +6242,7 @@ export class SyncClient {
6056
6242
  frame: SegmentRefFrame,
6057
6243
  sub: SubscriptionRecord,
6058
6244
  onProgress: (bytesReceived: number) => void,
6245
+ signal: AbortSignal | undefined,
6059
6246
  ): Promise<Uint8Array> {
6060
6247
  const downloader = this.#config.segments;
6061
6248
  if (downloader === undefined) {
@@ -6089,18 +6276,24 @@ export class SyncClient {
6089
6276
  true,
6090
6277
  );
6091
6278
  }
6092
- bytes = await fetchUrl(frame.url, onProgress);
6279
+ bytes = await fetchUrl(frame.url, onProgress, signal, frame.byteLength);
6093
6280
  } else {
6094
6281
  bytes = await downloader({
6095
6282
  segmentId: frame.segmentId,
6096
6283
  table: frame.table,
6097
6284
  requestedScopesJson: canonicalScopeJson(sub.scopes),
6285
+ byteLength: frame.byteLength,
6098
6286
  onProgress,
6287
+ ...(signal !== undefined ? { signal } : {}),
6099
6288
  });
6100
6289
  }
6101
- // §5.1: verify the content address before applying; on mismatch the
6102
- // segment is discarded and the cursor/resume token stay unpersisted,
6103
- // so the next pull re-delivers.
6290
+ // §5.4: the descriptor's byteLength caps the body; §5.1: verify the
6291
+ // content address before applying. On either failure the segment is
6292
+ // discarded and the cursor/resume token stay unpersisted, so the next
6293
+ // pull re-delivers.
6294
+ if (bytes.byteLength > frame.byteLength) {
6295
+ throw invalidRequest('segment exceeds its byteLength', true);
6296
+ }
6104
6297
  onProgress(bytes.byteLength);
6105
6298
  const hash = await sha256Hex(bytes);
6106
6299
  if (`sha256:${hash}` !== frame.segmentId) {
@@ -6120,6 +6313,10 @@ export class SyncClient {
6120
6313
  bootstrapState: string | undefined,
6121
6314
  summary: MutableSummary,
6122
6315
  ): boolean {
6316
+ // §4.4: SUB_END rewrites a live registration only. An unsubscribe or
6317
+ // eviction during this round's awaits removed it; persisting now would
6318
+ // re-create it. Everything below is synchronous with this check.
6319
+ if (getSubscription(this.#db, sub.id) === undefined) return false;
6123
6320
  if (start.status === 'active') {
6124
6321
  // §1.4 rule 4: durable cursor/resume state persists only at SUB_END;
6125
6322
  // §3.3: the effective-scope echo is persisted for the purge contract.
@@ -6213,6 +6410,7 @@ export class SyncClient {
6213
6410
  // (never the requested map), drop doomed outbox commits, stop pulling.
6214
6411
  const table = this.#table(sub.table);
6215
6412
  const lastEffective = sub.effectiveScopes;
6413
+ const owners = this.#scopeOwners(sub.table, sub.id);
6216
6414
  const registered = getWindowUnitBySubId(this.#db, sub.id);
6217
6415
  let failed = false;
6218
6416
  this.#applyBatch((batch) => {
@@ -6227,26 +6425,25 @@ export class SyncClient {
6227
6425
  batch,
6228
6426
  (name, values) =>
6229
6427
  name === table.name &&
6230
- matchesLocalScopes(table, lastEffective, values),
6428
+ ownsLocalRow(table, lastEffective, owners, values),
6231
6429
  ),
6232
6430
  ];
6233
- deleteScopedRows(this.#db, table, lastEffective);
6431
+ deleteScopedRows(this.#db, table, lastEffective, owners);
6234
6432
  batch.scopeMap(table, lastEffective);
6235
- const pendingById = new Map(
6236
- listOutbox(this.#db).map((commit) => [
6237
- commit.clientCommitId,
6238
- commit,
6239
- ]),
6240
- );
6241
- const droppedIds = dropOutboxCommitsInScope(
6242
- this.#db,
6243
- table,
6244
- lastEffective,
6433
+ // §3.3: a whole pending commit leaves when one of its upserts
6434
+ // provably lands in the revoked scopes and no other owner covers it.
6435
+ const doomed = listOutbox(this.#db).filter((commit) =>
6436
+ commit.operations.some(
6437
+ (op) =>
6438
+ op.table === table.name &&
6439
+ op.values !== undefined &&
6440
+ ownsLocalRow(table, lastEffective, owners, op.values),
6441
+ ),
6245
6442
  );
6246
- if (droppedIds.length > 0) {
6247
- for (const clientCommitId of droppedIds) {
6248
- const commit = pendingById.get(clientCommitId);
6249
- if (commit === undefined) continue;
6443
+ if (doomed.length > 0) {
6444
+ for (const commit of doomed) {
6445
+ const { clientCommitId } = commit;
6446
+ deleteOutboxCommit(this.#db, clientCommitId);
6250
6447
  const results: CommitOperationOutcome[] = commit.operations.map(
6251
6448
  (operation, opIndex) => {
6252
6449
  const rejection: RejectionRecord = {
@@ -6343,12 +6540,14 @@ export class SyncClient {
6343
6540
  return { opIndex, existed: true, syncVersion, values };
6344
6541
  }
6345
6542
 
6543
+ /** `onlyKeys` holds overlay row keys (`JSON [table,rowId]`). */
6346
6544
  #captureBeforeImages(
6347
6545
  operations: readonly OutboxOperation[],
6348
6546
  onlyKeys?: ReadonlySet<string>,
6349
6547
  ): OutboxBeforeImage[] {
6350
6548
  return operations.flatMap((operation, opIndex) =>
6351
- onlyKeys && !onlyKeys.has(`${operation.table}\u0000${operation.rowId}`)
6549
+ onlyKeys &&
6550
+ !onlyKeys.has(JSON.stringify([operation.table, operation.rowId]))
6352
6551
  ? []
6353
6552
  : [this.#captureBeforeImage(operation, opIndex)],
6354
6553
  );
@@ -6408,14 +6607,14 @@ export class SyncClient {
6408
6607
  imageByIndex.has(index),
6409
6608
  );
6410
6609
  const affectedKeys = new Set(
6411
- commit.operations.map(
6412
- (operation) => `${operation.table}\u0000${operation.rowId}`,
6610
+ commit.operations.map((operation) =>
6611
+ JSON.stringify([operation.table, operation.rowId]),
6413
6612
  ),
6414
6613
  );
6415
6614
  if (complete) {
6416
6615
  const restored = new Set<string>();
6417
6616
  commit.operations.forEach((operation, index) => {
6418
- const key = `${operation.table}\u0000${operation.rowId}`;
6617
+ const key = JSON.stringify([operation.table, operation.rowId]);
6419
6618
  if (restored.has(key)) return;
6420
6619
  const image = imageByIndex.get(index);
6421
6620
  if (image) this.#restoreBeforeImage(operation, image, batch);
@@ -6429,15 +6628,15 @@ export class SyncClient {
6429
6628
  deleteOutboxCommit(this.#db, commit.clientCommitId);
6430
6629
 
6431
6630
  if (!complete) return;
6432
- for (const later of listOutbox(this.#db)) {
6631
+ for (const later of listOutbox(this.#db, [...affectedKeys])) {
6433
6632
  if (later.seq <= commit.seq) continue;
6434
6633
  const affectedOperations = later.operations.filter((operation) =>
6435
- affectedKeys.has(`${operation.table}\u0000${operation.rowId}`),
6634
+ affectedKeys.has(JSON.stringify([operation.table, operation.rowId])),
6436
6635
  );
6437
6636
  if (affectedOperations.length === 0) continue;
6438
6637
  replaceOutboxBeforeImages(
6439
6638
  this.#db,
6440
- later.clientCommitId,
6639
+ later,
6441
6640
  this.#captureBeforeImages(later.operations, affectedKeys),
6442
6641
  );
6443
6642
  // §7.4.4: a later commit the recovery already classified refused never
@@ -6614,27 +6813,30 @@ export class SyncClient {
6614
6813
  type: localColumnType(column),
6615
6814
  }));
6616
6815
  const sparse: Array<RowValue | undefined> = [];
6617
- for (const column of table.columns) {
6618
- const raw = values[column.name];
6816
+ for (const { name, type } of declared) {
6817
+ const raw = values[name];
6619
6818
  if (raw === undefined) {
6620
6819
  sparse.push(undefined);
6621
6820
  continue;
6622
6821
  }
6623
- // §7.1: the schema-agnostic outbox form serializes through JSON, which
6624
- // turns NaN and ±Infinity into null. The authoring boundary refuses them
6625
- // instead of recording a different value; the wire `f64` encoding itself
6626
- // stays IEEE-754 binary64 (§2.4).
6822
+ // §2.4/§7.1: authoring refuses the push admission values the server
6823
+ // rejects. JSON would also turn NaN and ±Infinity into null, so the
6824
+ // outbox never records a different value.
6627
6825
  if (
6628
- localColumnType(column) === 'float' &&
6629
- typeof raw === 'number' &&
6630
- !Number.isFinite(raw)
6826
+ (type === 'float' &&
6827
+ typeof raw === 'number' &&
6828
+ !Number.isFinite(raw)) ||
6829
+ (type === 'string' && typeof raw === 'string' && raw.includes('\0'))
6631
6830
  )
6632
- return `column ${column.name} (float) requires a finite number value`;
6831
+ return `column ${name} holds a value push admission rejects`;
6633
6832
  try {
6833
+ if (typeof raw === 'string' && type === 'json')
6834
+ parseJsonDocument(raw, 'json value');
6835
+ if (typeof raw === 'string' && type === 'blob_ref') parseBlobRef(raw);
6634
6836
  sparse.push(jsonToRowValue(raw));
6635
6837
  } catch (error) {
6636
- if (error instanceof ClientSyncError)
6637
- return `column ${column.name}: ${error.message}`;
6838
+ if (error instanceof ClientSyncError || error instanceof DecodeError)
6839
+ return `column ${name}: ${error.message}`;
6638
6840
  throw error;
6639
6841
  }
6640
6842
  }
@@ -6703,11 +6905,24 @@ export class SyncClient {
6703
6905
  scope,
6704
6906
  this.#schema,
6705
6907
  );
6908
+ const outbox = new Set(listOutbox(this.#db, scope));
6706
6909
  const pending = [
6707
6910
  ...failedOverlayCommits(this.#db, false, scope, this.#schema),
6708
- ...listOutbox(this.#db, scope),
6911
+ ...outbox,
6709
6912
  ].sort((a, b) => a.createdAtMs - b.createdAtMs || a.seq - b.seq);
6710
- for (const commit of [...acknowledged, ...pending])
6913
+ // §7.2: a caller that restored the bases itself has just written the
6914
+ // server base of every exact-key row in scope. Each pending commit's
6915
+ // before-image of such a row is re-taken right before the commit
6916
+ // replays, so a later rollback restores that base plus earlier intent.
6917
+ const rebased =
6918
+ restoreBases || scope === undefined ? undefined : new Set(scope);
6919
+ for (const commit of [...acknowledged, ...pending]) {
6920
+ if (rebased !== undefined && outbox.has(commit))
6921
+ replaceOutboxBeforeImages(
6922
+ this.#db,
6923
+ commit,
6924
+ this.#captureBeforeImages(commit.operations, rebased),
6925
+ );
6711
6926
  this.#applyOperationsLocally(
6712
6927
  commit.operations.filter(
6713
6928
  (op) =>
@@ -6718,6 +6933,7 @@ export class SyncClient {
6718
6933
  batch,
6719
6934
  true,
6720
6935
  );
6936
+ }
6721
6937
  };
6722
6938
  const priorRejections = [...this.#rejections];
6723
6939
  try {