@syncular/client 0.59.0 → 0.60.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +27 -27
  2. package/dist/apply.d.ts +6 -5
  3. package/dist/apply.js +52 -25
  4. package/dist/authority.js +21 -16
  5. package/dist/blob.d.ts +4 -4
  6. package/dist/bun-database.js +3 -16
  7. package/dist/client.d.ts +57 -66
  8. package/dist/client.js +701 -796
  9. package/dist/devtools.d.ts +2 -2
  10. package/dist/devtools.js +3 -3
  11. package/dist/diagnostics.d.ts +17 -28
  12. package/dist/diagnostics.js +1 -2
  13. package/dist/errors.d.ts +1 -1
  14. package/dist/errors.js +1 -1
  15. package/dist/failed-overlay.js +4 -1
  16. package/dist/http.js +19 -5
  17. package/dist/invalidation.d.ts +3 -4
  18. package/dist/invalidation.js +9 -22
  19. package/dist/native-bridge.d.ts +7 -16
  20. package/dist/native-bridge.js +16 -41
  21. package/dist/node-database.js +16 -25
  22. package/dist/outbox.js +1 -0
  23. package/dist/progress.d.ts +2 -2
  24. package/dist/query-guard.d.ts +15 -6
  25. package/dist/query-guard.js +38 -6
  26. package/dist/reactive-store.js +2 -2
  27. package/dist/remote.js +9 -9
  28. package/dist/schema.d.ts +3 -2
  29. package/dist/schema.js +28 -12
  30. package/dist/state.d.ts +69 -36
  31. package/dist/state.js +178 -89
  32. package/dist/sync-scheduler.js +2 -2
  33. package/dist/transport.d.ts +2 -2
  34. package/dist/window.d.ts +30 -52
  35. package/dist/window.js +33 -77
  36. package/dist/worker-core.js +6 -6
  37. package/dist/worker-host.d.ts +9 -8
  38. package/dist/worker-host.js +23 -14
  39. package/dist/worker-protocol.d.ts +9 -8
  40. package/package.json +3 -3
  41. package/src/apply.ts +87 -33
  42. package/src/authority.ts +31 -17
  43. package/src/blob.ts +4 -3
  44. package/src/bun-database.ts +9 -14
  45. package/src/client.ts +886 -1037
  46. package/src/devtools.ts +5 -5
  47. package/src/diagnostics.ts +17 -37
  48. package/src/errors.ts +1 -1
  49. package/src/failed-overlay.ts +6 -1
  50. package/src/http.ts +22 -9
  51. package/src/invalidation.ts +9 -29
  52. package/src/native-bridge.ts +22 -58
  53. package/src/node-database.ts +26 -27
  54. package/src/outbox.ts +1 -0
  55. package/src/progress.ts +2 -2
  56. package/src/query-guard.ts +45 -8
  57. package/src/reactive-store.ts +2 -2
  58. package/src/remote.ts +12 -16
  59. package/src/schema.ts +48 -13
  60. package/src/state.ts +246 -125
  61. package/src/sync-scheduler.ts +2 -2
  62. package/src/transport.ts +2 -2
  63. package/src/window.ts +55 -134
  64. package/src/worker-core.ts +6 -6
  65. package/src/worker-host.ts +27 -19
  66. package/src/worker-protocol.ts +8 -8
package/README.md CHANGED
@@ -75,7 +75,7 @@ if (handle.role === 'follower') {
75
75
  // Another tab owns the core for this origin. The default multi-tab mode
76
76
  // transparently proxies this handle to that leader.
77
77
  }
78
- await handle.subscribe({ id: 'todos', table: 'todos', scopes: { list_id: ['l1'] } });
78
+ await handle.subscribe({ list_id: 'l1' });
79
79
  await handle.syncUntilIdle();
80
80
  const rows = await handle.query('SELECT * FROM todos');
81
81
  ```
@@ -132,7 +132,7 @@ persists, on purpose, and that is the only main-thread mode.
132
132
 
133
133
  ## Supported realtime lifecycle
134
134
 
135
- Register subscriptions, then install the cross-host supervisor:
135
+ Register units, then install the cross-host supervisor:
136
136
 
137
137
  ```ts
138
138
  import {
@@ -141,7 +141,7 @@ import {
141
141
  installRealtimeSupervisor,
142
142
  } from '@syncular/client';
143
143
 
144
- await handle.subscribe({ id: 'todos', table: 'todos', scopes });
144
+ await handle.subscribe({ list_id: 'groceries' });
145
145
  installRealtimeSupervisor(handle, {
146
146
  connectivity: browserConnectivitySignal(),
147
147
  lifecycle: documentLifecycleSignal(),
@@ -387,20 +387,18 @@ loading state.
387
387
  Every direct and Worker/multi-tab client exposes the same versioned snapshot:
388
388
 
389
389
  ```ts
390
- const snapshot = await client.diagnosticsSnapshot({
391
- expectedSubscriptions: [
392
- { id: 'membership-security', table: 'facility_memberships' },
393
- { id: 'scheduler-window', table: 'surgeries' },
394
- ],
395
- });
390
+ const snapshot = await client.diagnosticsSnapshot();
391
+ // snapshot.version === 2; snapshot.units, snapshot.activeUnits, snapshot.replicaCursor
396
392
 
397
393
  const off = client.onDiagnostics(() => refreshSupportView());
398
394
  ```
399
395
 
400
- `expectedSubscriptions` contains application intent only—stable PHI-free ids
401
- and generated table names, never scopes. It lets a support screen distinguish
402
- an absent registration from a legitimate zero-row completed bootstrap. The
403
- snapshot also distinguishes reset, revocation, failure, schema floor, lease
396
+ `units` counts the registered units per state (`bootstrapping`, `complete`,
397
+ `reset`, `revoked`, `failed`) with their distinct reason codes; `activeUnits`
398
+ is the number of units the next registration list carries, which the server's
399
+ `maxSubscriptionsPerRequest` counts; `replicaCursor` is the replica cursor,
400
+ `-1` while no unit is live. A unit id is a hash of scope values, so no unit id
401
+ enters the snapshot. The snapshot also distinguishes schema floor, lease
404
402
  stop, pending outbox, offline transport, and storage pressure/unreadability.
405
403
  Failed progress and `lastRound` diagnostics expose an optional bounded
406
404
  `causeKind` and HTTP status. Worker leaders and followers receive the same
@@ -417,8 +415,8 @@ role.
417
415
  The contract intentionally excludes scope values, rows and clinical row
418
416
  counts, SQL, paths, client/actor/lease ids, auth, keys, mutation bodies, stack
419
417
  traces, and arbitrary prose. It is safe to copy the JSON snapshot into a
420
- redacted support ticket as long as the application also keeps subscription ids
421
- free of patient/user data. Do not attach database files, console dumps, query
418
+ redacted support ticket as long as the application keeps query owner ids free
419
+ of patient/user data. Do not attach database files, console dumps, query
422
420
  results, or app state alongside it. See SPEC §7.6.
423
421
 
424
422
  ## Durable commit outcomes
@@ -480,7 +478,8 @@ security purge erase the retained aggregate's protected journal payloads.
480
478
  - `maxPushOperationsPerRequest`: operations across those commits. Default:
481
479
  500.
482
480
  - `maxPushRequestBytes`: bytes of the complete encoded SSP2 request, including
483
- the request header, pull header, subscriptions, framing, and E2EE ciphertext.
481
+ the request header, pull header, unit and progress frames, framing, and E2EE
482
+ ciphertext.
484
483
  Default: unbounded.
485
484
 
486
485
  The client sends whole commits in FIFO order and never splits one. A later
@@ -512,13 +511,13 @@ const result = await client.purgeLocalData({
512
511
  ```
513
512
 
514
513
  The host MUST first quarantine the affected feature and gate/remove any
515
- subscription that could download the protected rows again. This method does
514
+ unit that could download the protected rows again. This method does
516
515
  not authenticate a directive, revoke server authority, delete app-owned files,
517
516
  or remove a key from the OS secure store.
518
517
 
519
518
  For a race-free bootstrap, construct the client/worker handle with
520
519
  `securityPreflight: true`. Before activation, protected reads, writes,
521
- subscriptions, sync/realtime, blobs, and the automatic host loop fail with
520
+ unit registrations, sync/realtime, blobs, and the automatic host loop fail with
522
521
  `client.security_preflight_required`; status, local revision, lifecycle, and
523
522
  the exact local purge remain available.
524
523
 
@@ -552,10 +551,10 @@ counts only—never row ids or selector values.
552
551
 
553
552
  `rebootstrapLocalData({ rebootstrapId })` is the recovery primitive for a
554
553
  locally damaged or persistently inconsistent replicated projection. It drops
555
- and recreates only Syncular's server-derived tables, rewinds the existing
556
- subscription registrations, and requests a fresh bootstrap. It preserves the
557
- client id, lease state, pending outbox commits, commit outcomes, subscription
558
- intent, and protected bookkeeping.
554
+ and recreates only Syncular's server-derived tables, rewinds every registered
555
+ unit to cursor `-1`, and requests a fresh bootstrap. It preserves the client
556
+ id, lease state, pending outbox commits, commit outcomes, the registration
557
+ list, and protected bookkeeping.
559
558
 
560
559
  ```ts
561
560
  const result = await client.rebootstrapLocalData({
@@ -710,7 +709,8 @@ const client = new SyncClient({ database, schema, /* … */ });
710
709
  ```
711
710
 
712
711
  The export selects `bun:sqlite` on Bun and the built-in `node:sqlite` module on
713
- Node 22.13 or newer. No SQLite package or native addon is required. Both
712
+ Node 22.16 or newer. On an older Node, `openNodeDatabase()` throws
713
+ `client.storage_unavailable`. No SQLite package or native addon is required. Both
714
714
  adapters support synchronous `exec`, `query`, nested transactions, boolean
715
715
  bindings, `null`, `Uint8Array` BLOB values, and §5.3 SQLite-image attachment.
716
716
 
@@ -779,7 +779,7 @@ bun run test:browser
779
779
 
780
780
  Each case reloads the page using the same replica identity and requires successful
781
781
  startup in the same browser session. It checks SQLite and FTS integrity, search
782
- results, exact rows, persisted client identity, and subscription checkpoints.
782
+ results, exact rows, persisted client identity, and unit checkpoints.
783
783
  Incomplete sections must download again; completed sections must not. A held
784
784
  synchronous test request after a physical page write keeps the mid-import crash
785
785
  boundary stable until worker termination. Two additional ownership cases verify
@@ -830,7 +830,7 @@ stop();
830
830
  ```
831
831
 
832
832
  Each snapshot identifies the round with `attempt`, and the current payload with
833
- `subscriptionId`, `table`, and optional `segmentId`. Counters reset for each
833
+ `unitId`, `table`, and optional `segmentId`. Counters reset for each
834
834
  payload; `syncUntilIdle` starts a new attempt for each round. `phase` is `request`,
835
835
  `download`, or `import`; `state` is `running`, `complete`, or `failed`. Unknown
836
836
  totals remain absent. A new attempt clears prior errors and counters.
@@ -846,7 +846,7 @@ inside the current transaction; a failed import can roll those rows back. SQLite
846
846
  images commit in chunks of at most 1,024 rows and yield after each commit.
847
847
  Earlier committed chunks survive a later interruption. `complete` follows checkpoint persistence
848
848
  and optimistic-state reconciliation for the round. More bootstrap pages can
849
- remain; use subscription coverage to determine whether a query is complete.
849
+ remain; use window coverage to determine whether a query is complete.
850
850
  Listener exceptions do not fail sync. Listeners should hand UI work off and avoid
851
851
  reentering database commands during an import.
852
852
 
@@ -916,7 +916,7 @@ creation to enable the zero-argument `authoritySnapshot()`. It reads approved
916
916
  plain authority columns and accepted bases with revision and persisted scope
917
917
  coverage from one SQLite snapshot, including during security preflight.
918
918
  Unaccepted local intent remains separate. The read changes no keys, transport,
919
- subscriptions or lifecycle. See the [authority contract](../../docs/SPEC.md)
919
+ units or lifecycle. See the [authority contract](../../docs/SPEC.md)
920
920
  and [native configuration](../tauri/README.md#authority-evidence-before-activation).
921
921
 
922
922
  Workers must opt in in their entry script:
package/dist/apply.d.ts CHANGED
@@ -13,9 +13,10 @@ export type ApplyTransaction = (fn: () => OverlayScope) => unknown;
13
13
  export declare function upsertLocalRow(db: ClientDatabase, table: CompiledClientTable, values: readonly RowValue[], syncVersion: number, serverWrite?: boolean): void;
14
14
  export declare function deleteLocalRow(db: ClientDatabase, table: CompiledClientTable, rowId: string, serverWrite?: boolean): void;
15
15
  /**
16
- * Apply one `COMMIT` frame in one local transaction (§1.4 rule 4).
17
- * Upserts land with `_sync_version = rowVersion`; deletes remove the row.
18
- * Re-application is idempotent (§1.4 rule 5).
16
+ * Apply one `COMMIT` frame in one local transaction (§1.4 rule 4) under the
17
+ * §4.5 version guard: a change lands only when its `rowVersion` exceeds the
18
+ * local row's `_sync_version`. Upserts land with `_sync_version = rowVersion`;
19
+ * deletes remove the row. Re-application changes nothing.
19
20
  *
20
21
  * §5.11: encrypted columns are decrypted here — the apply seam. Because
21
22
  * WebCrypto is async and the local transaction is synchronous, every row is
@@ -80,7 +81,7 @@ export interface SqliteSegmentDescriptor {
80
81
  export declare function applySqliteSegment(db: ClientDatabase, schema: CompiledClientSchema, table: CompiledClientTable, bytes: Uint8Array, descriptor: SqliteSegmentDescriptor, options: {
81
82
  readonly clearFirst: boolean;
82
83
  readonly effective: ScopeMap;
83
- /** Effective scopes of the other row-holding subscriptions (§5.6). */
84
+ /** Pair scopes of the units that own rows of the table (§3.3). */
84
85
  readonly owners: readonly ScopeMap[];
85
86
  readonly transaction?: ApplyTransaction;
86
87
  readonly onProgress?: (rowsProcessed: number) => void;
@@ -97,7 +98,7 @@ export declare function applySqliteSegment(db: ClientDatabase, schema: CompiledC
97
98
  export declare function applyRowsSegment(db: ClientDatabase, schema: CompiledClientSchema, table: CompiledClientTable, segment: RowsSegment, options: {
98
99
  readonly clearFirst: boolean;
99
100
  readonly effective: ScopeMap;
100
- /** Effective scopes of the other row-holding subscriptions (§5.6). */
101
+ /** Pair scopes of the units that own rows of the table (§3.3). */
101
102
  readonly owners: readonly ScopeMap[];
102
103
  readonly transaction?: ApplyTransaction;
103
104
  readonly onProgress?: (rowsProcessed: number) => void;
package/dist/apply.js CHANGED
@@ -36,9 +36,29 @@ export function deleteLocalRow(db, table, rowId, serverWrite = true) {
36
36
  db.exec(`DELETE FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(table.primaryKey)}=?`, [pkSqlValue(table, rowId)]);
37
37
  }
38
38
  /**
39
- * Apply one `COMMIT` frame in one local transaction (§1.4 rule 4).
40
- * Upserts land with `_sync_version = rowVersion`; deletes remove the row.
41
- * Re-application is idempotent (§1.4 rule 5).
39
+ * §4.5 version guard for one server row: whether a write at `version` lands.
40
+ * A skipped row keeps its overlay, so its key leaves the rebase scope unless a
41
+ * retained base was restored for it; `kept` and `skipped` collect the keys.
42
+ */
43
+ function lands(db, table, rowId, version, kept, skipped) {
44
+ const key = JSON.stringify([table.name, rowId]);
45
+ const local = db.query(`SELECT ${quoteIdent(SYNC_VERSION_COLUMN)} AS v,EXISTS(SELECT 1 FROM ${RETAINED_ROWS} WHERE tbl=? AND id=?) AS retained FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(table.primaryKey)}=?`, [table.name, rowId, pkSqlValue(table, rowId)])[0];
46
+ const land = local === undefined || Number(local.v) < version;
47
+ if (land || local.retained)
48
+ kept.add(key);
49
+ else
50
+ skipped.add(key);
51
+ return land;
52
+ }
53
+ /** The scope a guarded write leaves for the overlay replay. */
54
+ function rebaseScope(scope, kept, skipped) {
55
+ return scope.filter((key) => kept.has(key) || !skipped.has(key));
56
+ }
57
+ /**
58
+ * Apply one `COMMIT` frame in one local transaction (§1.4 rule 4) under the
59
+ * §4.5 version guard: a change lands only when its `rowVersion` exceeds the
60
+ * local row's `_sync_version`. Upserts land with `_sync_version = rowVersion`;
61
+ * deletes remove the row. Re-application changes nothing.
42
62
  *
43
63
  * §5.11: encrypted columns are decrypted here — the apply seam. Because
44
64
  * WebCrypto is async and the local transaction is synchronous, every row is
@@ -59,10 +79,10 @@ export async function applyCommitFrame(db, schema, frame, encryption, transactio
59
79
  throw new ClientSyncError('sync.schema_mismatch', `COMMIT delivers unknown local table ${JSON.stringify(tableName)}`);
60
80
  }
61
81
  if (change.op === 'delete') {
62
- resolved.push({ op: 'delete', table, rowId: change.rowId });
82
+ resolved.push({ op: 'delete', table });
63
83
  continue;
64
84
  }
65
- if (change.row === undefined || change.rowVersion === undefined) {
85
+ if (change.row === undefined) {
66
86
  throw invalidRequest('upsert change without row payload');
67
87
  }
68
88
  let values = decodeRow(table.columns, change.row);
@@ -70,12 +90,7 @@ export async function applyCommitFrame(db, schema, frame, encryption, transactio
70
90
  const { decryptRowValues } = await import('./encryption.js');
71
91
  values = await decryptRowValues(encryption, table, values);
72
92
  }
73
- resolved.push({
74
- op: 'upsert',
75
- table,
76
- values,
77
- rowVersion: change.rowVersion,
78
- });
93
+ resolved.push({ op: 'upsert', table, values });
79
94
  }
80
95
  transaction(() => {
81
96
  // Exact keys name the rows this frame rebases; unique-key peers widen the
@@ -87,18 +102,21 @@ export async function applyCommitFrame(db, schema, frame, encryption, transactio
87
102
  JSON.stringify([change.table.name, frame.changes[index].rowId]),
88
103
  ]);
89
104
  restoreFailedBases(db, schema, scope);
105
+ const kept = new Set();
106
+ const skipped = new Set();
90
107
  for (const [index, change] of resolved.entries()) {
91
- const rowId = frame.changes[index].rowId;
108
+ const { rowId, rowVersion } = frame.changes[index];
109
+ const land = lands(db, change.table, rowId, rowVersion, kept, skipped);
92
110
  db.exec(`UPDATE ${OUTBOX_IMAGES} SET delivery_seq=MAX(COALESCE(delivery_seq,0),?) WHERE tbl=? AND row_id=?`, [frame.commitSeq, change.table.name, rowId]);
93
111
  db.exec(`DELETE FROM ${RETAINED_ROWS} WHERE tbl=? AND id=? AND commit_seq<=?`, [change.table.name, rowId, frame.commitSeq]);
94
- if (change.op === 'delete') {
95
- deleteLocalRow(db, change.table, change.rowId);
96
- }
97
- else {
98
- upsertLocalRow(db, change.table, change.values, change.rowVersion);
99
- }
112
+ if (!land)
113
+ continue;
114
+ if (change.op === 'delete')
115
+ deleteLocalRow(db, change.table, rowId);
116
+ else
117
+ upsertLocalRow(db, change.table, change.values, rowVersion);
100
118
  }
101
- return scope;
119
+ return rebaseScope(scope, kept, skipped);
102
120
  });
103
121
  }
104
122
  /**
@@ -310,6 +328,7 @@ export async function applySqliteSegment(db, schema, table, bytes, descriptor, o
310
328
  .filter((name) => name !== primaryKey)
311
329
  .map((name) => `${name}=excluded.${name}`)
312
330
  .join(', ');
331
+ const version = quoteIdent(SYNC_VERSION_COLUMN);
313
332
  let after;
314
333
  let applied = 0;
315
334
  for (;;) {
@@ -348,16 +367,20 @@ export async function applySqliteSegment(db, schema, table, bytes, descriptor, o
348
367
  const protectedRows = restoreFailedBases(db, schema, scope);
349
368
  if (after === undefined && options.clearFirst)
350
369
  deleteScopedRows(db, table, options.effective, options.owners);
351
- db.exec(`INSERT INTO ${quoteIdent(table.name)}(${insertNames.join(', ')})SELECT ${[...names, quoteIdent('_syncular_version')].join(', ')} FROM ${source} WHERE ${predicates.join(' AND ') || 'true'} ON CONFLICT(${primaryKey})DO UPDATE SET ${updates}`, params);
352
- applied += Number(db.query('SELECT changes()AS n')[0]?.n ?? 0);
370
+ const kept = new Set();
371
+ const skipped = new Set();
372
+ const landing = rows.filter((row) => lands(db, table, String(row[table.primaryKey]), Number(row._syncular_version), kept, skipped));
373
+ const where = predicates.join(' AND ') || 'true';
374
+ db.exec(`INSERT INTO ${quoteIdent(table.name)}(${insertNames.join(', ')})SELECT ${[...names, quoteIdent('_syncular_version')].join(', ')} FROM ${source} WHERE ${where} ON CONFLICT(${primaryKey})DO UPDATE SET ${updates} WHERE excluded.${version}>${quoteIdent(table.name)}.${version}`, params);
375
+ applied += Number(db.query(`SELECT count(*)AS n FROM ${source} WHERE ${where}`, params)[0]?.n);
353
376
  if (protectedRows)
354
- for (const row of rows) {
377
+ for (const row of landing) {
355
378
  retainedBaseWrite(db, table, String(row[table.primaryKey]), Object.fromEntries(table.columns.map((column) => [
356
379
  column.name,
357
380
  rowValueToJson(row[column.name]),
358
381
  ])), Number(row._syncular_version));
359
382
  }
360
- return scope;
383
+ return rebaseScope(scope, kept, skipped);
361
384
  });
362
385
  options.onProgress?.(applied);
363
386
  await yieldToHost();
@@ -420,13 +443,17 @@ export async function applyRowsSegment(db, schema, table, segment, options, encr
420
443
  if (clearThisBlock) {
421
444
  deleteScopedRows(db, table, options.effective, options.owners);
422
445
  }
446
+ const kept = new Set();
447
+ const skipped = new Set();
423
448
  for (const row of rows) {
424
- upsertLocalRow(db, table, row.values, row.serverVersion, protectedRows);
449
+ const rowId = String(row.values[table.primaryKeyIndex]);
450
+ if (lands(db, table, rowId, row.serverVersion, kept, skipped))
451
+ upsertLocalRow(db, table, row.values, row.serverVersion, protectedRows);
425
452
  applied += 1;
426
453
  if (applied % 1024 === 0)
427
454
  options.onProgress?.(applied);
428
455
  }
429
- return scope;
456
+ return rebaseScope(scope, kept, skipped);
430
457
  });
431
458
  options.onProgress?.(applied);
432
459
  await yieldToHost();
package/dist/authority.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ClientSyncError } from './errors.js';
2
2
  import { listOutbox, listOutboxBeforeImages } from './outbox.js';
3
3
  import { RETAINED_ROWS } from './failed-overlay.js';
4
- import { getLocalRevision, getMeta, loadSubscriptions } from './state.js';
4
+ import { bootstrapping, covers, getLocalRevision, getMeta, loadUnits, pairScopes, replicaCursor, } from './state.js';
5
5
  import { fromSqlValue, quoteIdent, SYNC_VERSION_COLUMN } from './schema.js';
6
6
  function validateAuthorityReads(schema, declarations) {
7
7
  if (!Array.isArray(declarations))
@@ -55,7 +55,8 @@ function readAuthoritySnapshot(db, schema, declarations) {
55
55
  BigInt(raw) > 18446744073709551615n)
56
56
  throw new ClientSyncError('sync.local_corrupt', 'authority revision is missing or invalid');
57
57
  const revision = getLocalRevision(db);
58
- const subscriptions = loadSubscriptions(db);
58
+ const units = loadUnits(db);
59
+ const cursor = replicaCursor(db);
59
60
  const tables = declarations.map((read) => {
60
61
  const table = schema.tables.get(read.table);
61
62
  const columns = [
@@ -150,27 +151,31 @@ function readAuthoritySnapshot(db, schema, declarations) {
150
151
  },
151
152
  ];
152
153
  });
153
- const persisted = subscriptions
154
- .filter((s) => s.table === read.table)
155
- .map((s) => {
156
- const effective = s.effectiveScopes;
157
- const complete = effective !== undefined &&
158
- Object.keys(effective).length === Object.keys(read.scopes).length &&
159
- s.status === 'active' &&
160
- s.cursor >= 0 &&
161
- s.bootstrapState === undefined &&
162
- s.params === undefined;
154
+ // Every unit covering the table, as its (unit, table) pair (§4.4).
155
+ const persisted = units
156
+ .filter((unit) => covers(table, unit.scopes))
157
+ .map((unit) => {
158
+ const effective = unit.echo === undefined
159
+ ? undefined
160
+ : pairScopes(unit.scopes, read.table, unit.echo);
163
161
  return {
164
- requestedScopes: Object.fromEntries(Object.keys(read.scopes).map((v) => [v, s.scopes[v] ?? []])),
165
- status: s.status,
166
- cursor: s.cursor,
162
+ requestedScopes: Object.fromEntries(Object.keys(read.scopes).map((v) => [
163
+ v,
164
+ unit.scopes[v] === undefined ? [] : [unit.scopes[v]],
165
+ ])),
166
+ status: unit.status,
167
+ cursor: unit.cursor ?? cursor,
167
168
  effectiveScopes: effective === undefined
168
169
  ? null
169
170
  : Object.fromEntries(Object.keys(read.scopes).map((v) => [
170
171
  v,
171
172
  effective[v] ?? [],
172
173
  ])),
173
- complete,
174
+ complete: effective !== undefined &&
175
+ Object.keys(effective).length ===
176
+ Object.keys(read.scopes).length &&
177
+ unit.status === 'active' &&
178
+ !bootstrapping(unit),
174
179
  };
175
180
  });
176
181
  const selectors = Object.entries(read.scopes);
package/dist/blob.d.ts CHANGED
@@ -42,7 +42,7 @@ export type BlobUploadGrant = {
42
42
  /**
43
43
  * The transport seam for blob upload/download (§5.9.3/§5.9.5). The upload
44
44
  * calls receive the round's `signal`, which aborts when the `syncUntilIdle`
45
- * deadline elapses (§7.7).
45
+ * deadline elapses (§7.8).
46
46
  */
47
47
  export interface BlobTransport {
48
48
  /** `PUT <mount>/blobs/{blobId}` — host-authenticated direct upload (§5.9.3). */
@@ -71,11 +71,11 @@ export interface BlobTransport {
71
71
  /**
72
72
  * §5.9.3 direct-to-storage PUT of the granted `url`. MUST attach NO host
73
73
  * authentication — the presigned URL is the entire grant (§5.4) — and MUST
74
- * send `x-amz-checksum-sha256` (base64 of the body's SHA-256), which the
75
- * grant signs beside `content-length`. Only called when `uploadGrant`
74
+ * send `x-amz-checksum-sha256` (base64 of the digest `blobId` names), which
75
+ * the grant signs beside `content-length`. Only called when `uploadGrant`
76
76
  * returned a `url` arm.
77
77
  */
78
- uploadToUrl?(url: string, bytes: Uint8Array, mediaType?: string, signal?: AbortSignal): Promise<void>;
78
+ uploadToUrl?(blobId: string, url: string, bytes: Uint8Array, mediaType?: string, signal?: AbortSignal): Promise<void>;
79
79
  }
80
80
  export interface CachedBlob {
81
81
  readonly blobId: string;
@@ -8,7 +8,7 @@ import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
8
8
  import { tmpdir } from 'node:os';
9
9
  import { join } from 'node:path';
10
10
  import { assertImageAlias, runTransaction, } from './database.js';
11
- import { prepareReadOnly, refuseRawSql } from './query-guard.js';
11
+ import { prepareReadOnly, runReadOnly } from './query-guard.js';
12
12
  function coerceParams(params) {
13
13
  return params.map((value) => {
14
14
  if (typeof value === 'boolean')
@@ -19,6 +19,7 @@ function coerceParams(params) {
19
19
  export class BunClientDatabase {
20
20
  db;
21
21
  #path;
22
+ #proven = new Set();
22
23
  #tx = { depth: 0 };
23
24
  constructor(path = ':memory:') {
24
25
  this.#path = path;
@@ -57,21 +58,7 @@ export class BunClientDatabase {
57
58
  prepared.columnNames.length > 0,
58
59
  ];
59
60
  });
60
- // bun:sqlite exposes `sqlite3_stmt_readonly` only from Bun 1.4.4, so
61
- // SQLite refuses a write when the statement runs: under `query_only` a
62
- // write transaction fails before it changes anything.
63
- this.db.run('PRAGMA query_only = ON');
64
- try {
65
- return statement.all(...coerceParams(params));
66
- }
67
- catch (error) {
68
- if (error.code === 'SQLITE_READONLY')
69
- refuseRawSql();
70
- throw error;
71
- }
72
- finally {
73
- this.db.run('PRAGMA query_only = OFF');
74
- }
61
+ return runReadOnly(this.#proven, sql, (pragma) => this.db.run(pragma), (error) => error.code === 'SQLITE_READONLY', () => statement.all(...coerceParams(params)));
75
62
  }
76
63
  /** RFC 0005: a sibling file beside the replica, e.g. `<path>.prev-context`. */
77
64
  openSibling(name) {