@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
@@ -14,6 +14,7 @@ import {
14
14
  type SqlRow,
15
15
  type SqlValue,
16
16
  } from './database';
17
+ import { prepareReadOnly, SQL_BLANK } from './query-guard';
17
18
 
18
19
  function coerceParams(params: readonly SqlValue[]): SQLInputValue[] {
19
20
  return params.map((value): SQLInputValue => {
@@ -61,6 +62,37 @@ export class NodeClientDatabase implements ClientDatabase {
61
62
  return rows.map(normalizeRow);
62
63
  }
63
64
 
65
+ queryReadOnly(sql: string, params: readonly SqlValue[] = []): SqlRow[] {
66
+ const statement = prepareReadOnly(sql, () => {
67
+ const prepared = this.db.prepare(sql);
68
+ // node:sqlite exposes no `sqlite3_stmt_readonly` (and no column count
69
+ // before Node 22.16), so the verb decides: SELECT, VALUES, EXPLAIN, or
70
+ // a WITH clause whose first keyword outside parentheses is SELECT or
71
+ // VALUES.
72
+ const body = prepared.sourceSQL.replace(SQL_BLANK, '');
73
+ const verb = /^[a-z]+/i.exec(body)?.[0].toLowerCase();
74
+ let reads = verb === 'select' || verb === 'values' || verb === 'explain';
75
+ if (verb === 'with') {
76
+ let depth = 0;
77
+ for (const [token] of body.matchAll(
78
+ /'[^']*'|"[^"]*"|`[^`]*`|\[[^\]]*\]|--[^\n]*|\/\*(?:[^*]|\*(?!\/))*(?:\*\/|$)|[\w$\u0080-\uffff]+|[\s\S]/g,
79
+ )) {
80
+ if (token === '(') depth += 1;
81
+ else if (token === ')') depth -= 1;
82
+ else if (
83
+ depth === 0 &&
84
+ /^(?:select|values|insert|update|delete|replace)$/i.test(token)
85
+ ) {
86
+ reads = /^(?:select|values)$/i.test(token);
87
+ break;
88
+ }
89
+ }
90
+ }
91
+ return [prepared, prepared.sourceSQL, reads] as const;
92
+ });
93
+ return statement.all(...coerceParams(params)).map(normalizeRow);
94
+ }
95
+
64
96
  /** RFC 0005: a sibling file beside the replica, e.g. `<path>.prev-context`. */
65
97
  openSibling(name: string): SiblingDatabase {
66
98
  const siblingPath =
package/src/outbox.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { BLOB_COMMIT_REFS } from './blob';
2
2
  export const OUTBOX_TABLE = '_syncular_outbox';
3
3
  export const OUTBOX_IMAGES = '_syncular_outbox_before_images';
4
- import { overlayScopePredicate, type OverlayScope } from './failed-overlay';
4
+ import type { OverlayScope } from './failed-overlay';
5
5
  /**
6
6
  * The durable outbox (SPEC.md §7.1) with encode-at-send (the §0 binary-push
7
7
  * outbox rule): local mutations are persisted in a schema-agnostic JSON
@@ -13,18 +13,16 @@ import {
13
13
  encodeSparseRow,
14
14
  type PushCommitFrame,
15
15
  type PushOperation,
16
- type ScopeMap,
17
16
  type SparseRowValue,
18
17
  } from '@syncular/core';
19
18
  import type { ClientDatabase, SqlRow } from './database';
20
19
  import type { EncryptionConfig } from './encryption';
21
- import { ClientSyncError } from './errors';
20
+ import { ClientSyncError, invalidRequest } from './errors';
22
21
  import {
23
22
  type CompiledClientSchema,
24
23
  type CompiledClientTable,
25
24
  type JsonRowValue,
26
25
  jsonToRowValue,
27
- matchesLocalScopes,
28
26
  } from './schema';
29
27
 
30
28
  export interface OutboxOperation {
@@ -80,10 +78,14 @@ export function appendOutboxCommit(
80
78
  `INSERT INTO ${OUTBOX_TABLE}(client_commit_id,created_at_ms,operations)VALUES(?,?,?)`,
81
79
  [clientCommitId, nowMs, JSON.stringify(operations)],
82
80
  );
83
- replaceOutboxBeforeImages(db, clientCommitId, beforeImages);
81
+ replaceOutboxBeforeImages(db, { clientCommitId, operations }, beforeImages);
84
82
  }
85
83
 
86
- /** Pending commits in FIFO creation order (§7.1). Full reads serve replay and the public listing. */
84
+ /**
85
+ * Pending commits in FIFO creation order (§7.1). Full reads serve replay and
86
+ * the public listing; a scoped read finds commits through the before-image
87
+ * row index and never parses operation bodies it does not return.
88
+ */
87
89
  export function listOutbox(
88
90
  db: ClientDatabase,
89
91
  scope?: OverlayScope,
@@ -92,7 +94,7 @@ export function listOutbox(
92
94
  .query(
93
95
  scope === undefined
94
96
  ? `SELECT * FROM ${OUTBOX_TABLE} ORDER BY seq ASC`
95
- : `SELECT * FROM ${OUTBOX_TABLE} WHERE EXISTS(SELECT 1 FROM json_each(operations) WHERE ${overlayScopePredicate("json_extract(value,'$.table')", "json_extract(value,'$.rowId')")}) ORDER BY seq`,
97
+ : `SELECT * FROM ${OUTBOX_TABLE} WHERE client_commit_id IN(SELECT client_commit_id FROM ${OUTBOX_IMAGES} WHERE (tbl,row_id) IN(SELECT json_extract(value,'$[0]'),json_extract(value,'$[1]') FROM json_each(?1)) UNION SELECT client_commit_id FROM ${OUTBOX_IMAGES} WHERE tbl IN(SELECT json_extract(value,'$[0]') FROM json_each(?1) WHERE json_array_length(value)=1)) ORDER BY seq`,
96
98
  scope && [JSON.stringify(scope)],
97
99
  )
98
100
  .map(decodeOutboxRow);
@@ -176,17 +178,22 @@ export function listOutboxBeforeImages(
176
178
  }));
177
179
  }
178
180
 
181
+ /** Each image carries its operation's row key, the index of every per-row outbox lookup. */
179
182
  export function replaceOutboxBeforeImages(
180
183
  db: ClientDatabase,
181
- clientCommitId: string,
184
+ commit: Pick<OutboxCommit, 'clientCommitId' | 'operations'>,
182
185
  replacements: readonly OutboxBeforeImage[],
183
186
  ): void {
184
187
  for (const image of replacements) {
188
+ // Images are captured per operation index of this same commit.
189
+ const operation = commit.operations[image.opIndex]!;
185
190
  db.exec(
186
- `INSERT INTO ${OUTBOX_IMAGES}(client_commit_id,op_index,existed,sync_version,values_json)VALUES(?,?,?,?,?) ON CONFLICT(client_commit_id,op_index)DO UPDATE SET existed=excluded.existed,sync_version=excluded.sync_version,values_json=excluded.values_json`,
191
+ `INSERT INTO ${OUTBOX_IMAGES}(client_commit_id,op_index,tbl,row_id,existed,sync_version,values_json)VALUES(?,?,?,?,?,?,?) ON CONFLICT(client_commit_id,op_index)DO UPDATE SET existed=excluded.existed,sync_version=excluded.sync_version,values_json=excluded.values_json`,
187
192
  [
188
- clientCommitId,
193
+ commit.clientCommitId,
189
194
  image.opIndex,
195
+ operation.table,
196
+ operation.rowId,
190
197
  image.existed ? 1 : 0,
191
198
  image.syncVersion ?? null,
192
199
  image.values === undefined ? null : JSON.stringify(image.values),
@@ -273,13 +280,12 @@ export async function encodeOutboxCommit(
273
280
  );
274
281
  }
275
282
  if (op.values === undefined) {
276
- throw new ClientSyncError(
277
- 'sync.invalid_request',
283
+ throw invalidRequest(
278
284
  `outbox upsert on ${op.table}/${op.rowId} has no values`,
279
285
  );
280
286
  }
281
287
  let values = sparseValues(table, op.values, op.insert === true);
282
- if (encryption !== undefined && table.hasEncryptedColumns) {
288
+ if (table.hasEncryptedColumns) {
283
289
  // Lazy: opt-in E2EE never enters an encryption-free app's bundle.
284
290
  const { encryptRowValues } = await import('./encryption');
285
291
  values = await encryptRowValues(
@@ -304,31 +310,3 @@ export async function encodeOutboxCommit(
304
310
  operations,
305
311
  };
306
312
  }
307
-
308
- /**
309
- * §3.3: drop pending commits that write into a revoked scope instead of
310
- * replaying them into guaranteed rejections. A whole commit is dropped when
311
- * any of its upserts provably lands in the revoked effective scopes —
312
- * commits are atomic and their content is pinned by the idempotency key,
313
- * so ops are never removed individually. Returns dropped commit ids.
314
- */
315
- export function dropOutboxCommitsInScope(
316
- db: ClientDatabase,
317
- table: CompiledClientTable,
318
- effective: ScopeMap,
319
- ): string[] {
320
- const entries = Object.entries(effective);
321
- if (entries.length === 0) return [];
322
- const dropped: string[] = [];
323
- for (const commit of listOutbox(db)) {
324
- const inScope = commit.operations.some((op) => {
325
- if (op.table !== table.name || op.values === undefined) return false;
326
- return matchesLocalScopes(table, effective, op.values);
327
- });
328
- if (inScope) {
329
- deleteOutboxCommit(db, commit.clientCommitId);
330
- dropped.push(commit.clientCommitId);
331
- }
332
- }
333
- return dropped;
334
- }
package/src/outcomes.ts CHANGED
@@ -15,7 +15,7 @@ import {
15
15
  type RowValue,
16
16
  } from '@syncular/core';
17
17
  import type { ClientDatabase } from './database';
18
- import { ClientSyncError } from './errors';
18
+ import { ClientSyncError, invalidRequest } from './errors';
19
19
  import type { OutboxOperation } from './outbox';
20
20
  import {
21
21
  isBytesEnvelope,
@@ -453,8 +453,7 @@ export function listCommitOutcomes(
453
453
  ): CommitOutcome[] {
454
454
  const limit = query.limit;
455
455
  if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 1)) {
456
- throw new ClientSyncError(
457
- 'sync.invalid_request',
456
+ throw invalidRequest(
458
457
  'commit outcome limit must be a positive safe integer',
459
458
  );
460
459
  }
@@ -494,8 +493,7 @@ export function pruneCommitOutcomes(
494
493
  maxEntries: number,
495
494
  ): number {
496
495
  if (!Number.isSafeInteger(maxEntries) || maxEntries < 1) {
497
- throw new ClientSyncError(
498
- 'sync.invalid_request',
496
+ throw invalidRequest(
499
497
  'outcome retention maxEntries must be a positive safe integer',
500
498
  );
501
499
  }
@@ -202,6 +202,10 @@ export interface PreviousVersionSnapshot {
202
202
  readonly truncated: boolean;
203
203
  }
204
204
 
205
+ function isRecord(value: unknown): value is Record<string, unknown> {
206
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
207
+ }
208
+
205
209
  function isCount(value: unknown): value is number {
206
210
  return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
207
211
  }
@@ -225,10 +229,7 @@ function isSqlValue(value: unknown): value is SqlValue {
225
229
  }
226
230
 
227
231
  function isSqlRow(value: unknown): value is SqlRow {
228
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
229
- return false;
230
- }
231
- return Object.values(value as Record<string, unknown>).every(isSqlValue);
232
+ return isRecord(value) && Object.values(value).every(isSqlValue);
232
233
  }
233
234
 
234
235
  const PREVIOUS_VERSION_REASONS: readonly PreviousVersionReason[] = [
@@ -266,16 +267,22 @@ const AUDIT_ENTRY_KEYS = ['column', 'commitId', 'reason', 'table'] as const;
266
267
  function parseJsonObject(raw: string): Record<string, unknown> | undefined {
267
268
  try {
268
269
  const parsed: unknown = JSON.parse(raw);
269
- return parsed !== null &&
270
- typeof parsed === 'object' &&
271
- !Array.isArray(parsed)
272
- ? (parsed as Record<string, unknown>)
273
- : undefined;
270
+ return isRecord(parsed) ? parsed : undefined;
274
271
  } catch {
275
272
  return undefined;
276
273
  }
277
274
  }
278
275
 
276
+ function hasExactKeys(
277
+ record: Record<string, unknown>,
278
+ expected: readonly string[],
279
+ ): boolean {
280
+ return (
281
+ Object.keys(record).length === expected.length &&
282
+ expected.every((key) => Object.hasOwn(record, key))
283
+ );
284
+ }
285
+
279
286
  function localCorrupt(what: string): never {
280
287
  throw new ClientSyncError(
281
288
  'sync.local_corrupt',
@@ -301,45 +308,36 @@ const COLUMN_TYPES = new Set<RowColumn['type']>([
301
308
  function decodeColumns(value: unknown): LocalSchemaDescriptorColumn[] {
302
309
  if (!Array.isArray(value)) return localCorrupt('schema descriptor');
303
310
  return value.map((entry) => {
304
- if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) {
311
+ if (!isRecord(entry)) {
305
312
  return localCorrupt('schema descriptor');
306
313
  }
307
- const column = entry as Record<string, unknown>;
308
- const keys = Object.keys(column).sort();
309
314
  if (
310
- keys.length !== 2 ||
311
- keys[0] !== 'name' ||
312
- keys[1] !== 'type' ||
313
- typeof column.name !== 'string' ||
314
- typeof column.type !== 'string' ||
315
- !COLUMN_TYPES.has(column.type as RowColumn['type'])
315
+ !hasExactKeys(entry, ['name', 'type']) ||
316
+ typeof entry.name !== 'string' ||
317
+ typeof entry.type !== 'string' ||
318
+ !COLUMN_TYPES.has(entry.type as RowColumn['type'])
316
319
  ) {
317
320
  return localCorrupt('schema descriptor');
318
321
  }
319
- return { name: column.name, type: column.type as RowColumn['type'] };
322
+ return { name: entry.name, type: entry.type as RowColumn['type'] };
320
323
  });
321
324
  }
322
325
 
323
326
  function decodeTable(value: unknown): LocalSchemaDescriptorTable {
324
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
327
+ if (!isRecord(value)) {
325
328
  return localCorrupt('schema descriptor');
326
329
  }
327
- const table = value as Record<string, unknown>;
328
- const keys = Object.keys(table).sort();
329
330
  if (
330
- keys.length !== 3 ||
331
- keys[0] !== 'columns' ||
332
- keys[1] !== 'name' ||
333
- keys[2] !== 'primaryKey' ||
334
- typeof table.name !== 'string' ||
335
- typeof table.primaryKey !== 'string'
331
+ !hasExactKeys(value, ['columns', 'name', 'primaryKey']) ||
332
+ typeof value.name !== 'string' ||
333
+ typeof value.primaryKey !== 'string'
336
334
  ) {
337
335
  return localCorrupt('schema descriptor');
338
336
  }
339
337
  return {
340
- name: table.name,
341
- primaryKey: table.primaryKey,
342
- columns: decodeColumns(table.columns),
338
+ name: value.name,
339
+ primaryKey: value.primaryKey,
340
+ columns: decodeColumns(value.columns),
343
341
  };
344
342
  }
345
343
 
@@ -368,12 +366,8 @@ function buildLocalSchemaDescriptor(
368
366
  function decodeLocalSchemaDescriptor(value: string): LocalSchemaDescriptor {
369
367
  const record = parseJsonObject(value);
370
368
  if (record === undefined) return localCorrupt('schema descriptor');
371
- const keys = Object.keys(record).sort();
372
369
  if (
373
- keys.length !== 3 ||
374
- keys[0] !== 'tables' ||
375
- keys[1] !== 'v' ||
376
- keys[2] !== 'version' ||
370
+ !hasExactKeys(record, ['tables', 'v', 'version']) ||
377
371
  record.v !== DESCRIPTOR_VERSION ||
378
372
  !isCount(record.version) ||
379
373
  !Array.isArray(record.tables)
@@ -474,16 +468,16 @@ export function readPreviousVersionContainer(
474
468
  function decodeRecord(value: string): PreviousVersionRecord {
475
469
  const record = parseJsonObject(value);
476
470
  if (record === undefined) return localCorrupt('previous-version context');
477
- const keys = Object.keys(record).sort();
478
471
  if (
479
- keys.length !== 7 ||
480
- keys[0] !== 'bytes' ||
481
- keys[1] !== 'createdAtMs' ||
482
- keys[2] !== 'currentVersion' ||
483
- keys[3] !== 'previousVersion' ||
484
- keys[4] !== 'rows' ||
485
- keys[5] !== 'tables' ||
486
- keys[6] !== 'v' ||
472
+ !hasExactKeys(record, [
473
+ 'bytes',
474
+ 'createdAtMs',
475
+ 'currentVersion',
476
+ 'previousVersion',
477
+ 'rows',
478
+ 'tables',
479
+ 'v',
480
+ ]) ||
487
481
  record.v !== 1 ||
488
482
  !isCount(record.bytes) ||
489
483
  !isCount(record.createdAtMs) ||
@@ -606,7 +600,7 @@ export function capturePreviousVersion(
606
600
  const selectColumns = table.columns
607
601
  .map((column) => quoteIdent(column.name))
608
602
  .join(', ');
609
- let lastRowId = -1;
603
+ let lastRowId = -(2 ** 63);
610
604
  for (;;) {
611
605
  const rows = replica.query(
612
606
  `SELECT rowid AS _rid,${selectColumns} FROM ${quoteIdent(table.name)} WHERE rowid>? ORDER BY rowid ASC LIMIT ?`,
@@ -875,25 +869,24 @@ export function storedPreviousVersionAudit(
875
869
  }
876
870
  const incompatible: PreviousVersionAuditEntry[] = [];
877
871
  for (const entry of record.incompatible) {
878
- if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) {
872
+ if (!isRecord(entry)) {
879
873
  return undefined;
880
874
  }
881
- const item = entry as Record<string, unknown>;
882
875
  if (
883
- typeof item.commitId !== 'string' ||
884
- typeof item.table !== 'string' ||
885
- (item.reason !== 'unknown-table' &&
886
- item.reason !== 'unknown-column' &&
887
- item.reason !== 'missing-required-column') ||
888
- (item.column !== undefined && typeof item.column !== 'string')
876
+ typeof entry.commitId !== 'string' ||
877
+ typeof entry.table !== 'string' ||
878
+ (entry.reason !== 'unknown-table' &&
879
+ entry.reason !== 'unknown-column' &&
880
+ entry.reason !== 'missing-required-column') ||
881
+ (entry.column !== undefined && typeof entry.column !== 'string')
889
882
  ) {
890
883
  return undefined;
891
884
  }
892
885
  incompatible.push({
893
- commitId: item.commitId,
894
- table: item.table,
895
- reason: item.reason,
896
- ...(typeof item.column === 'string' ? { column: item.column } : {}),
886
+ commitId: entry.commitId,
887
+ table: entry.table,
888
+ reason: entry.reason,
889
+ ...(typeof entry.column === 'string' ? { column: entry.column } : {}),
897
890
  });
898
891
  }
899
892
  return {
@@ -936,31 +929,30 @@ export function decodePreviousVersionSnapshot(
936
929
  value: unknown,
937
930
  ): PreviousVersionSnapshot {
938
931
  const method = 'previousVersionSnapshot';
939
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
932
+ if (!isRecord(value)) {
940
933
  return invalidHostResponse(method);
941
934
  }
942
- const source = value as Record<string, unknown>;
943
935
  const allowed = new Set<string>([
944
936
  ...SNAPSHOT_REQUIRED_KEYS,
945
937
  ...SNAPSHOT_OPTIONAL_KEYS,
946
938
  ]);
947
- const keys = Object.keys(source);
939
+ const keys = Object.keys(value);
948
940
  if (
949
941
  !keys.every((key) => allowed.has(key)) ||
950
- SNAPSHOT_REQUIRED_KEYS.some((key) => !(key in source)) ||
951
- source.state !== 'previousVersion' ||
952
- typeof source.available !== 'boolean' ||
953
- !isCount(source.currentVersion) ||
954
- typeof source.truncated !== 'boolean' ||
955
- !Array.isArray(source.rows) ||
956
- !source.rows.every(isSqlRow)
942
+ SNAPSHOT_REQUIRED_KEYS.some((key) => !(key in value)) ||
943
+ value.state !== 'previousVersion' ||
944
+ typeof value.available !== 'boolean' ||
945
+ !isCount(value.currentVersion) ||
946
+ typeof value.truncated !== 'boolean' ||
947
+ !Array.isArray(value.rows) ||
948
+ !value.rows.every(isSqlRow)
957
949
  ) {
958
950
  return invalidHostResponse(method);
959
951
  }
960
- const reason = source.reason;
961
- const previousVersion = source.previousVersion;
962
- const rows = source.rows as SqlRow[];
963
- if (source.available) {
952
+ const reason = value.reason;
953
+ const previousVersion = value.previousVersion;
954
+ const rows = value.rows as SqlRow[];
955
+ if (value.available) {
964
956
  // An available read names the previous version and never a refusal.
965
957
  if (!isCount(previousVersion) || reason !== undefined) {
966
958
  return invalidHostResponse(method);
@@ -969,9 +961,9 @@ export function decodePreviousVersionSnapshot(
969
961
  state: 'previousVersion',
970
962
  available: true,
971
963
  previousVersion,
972
- currentVersion: source.currentVersion,
964
+ currentVersion: value.currentVersion,
973
965
  rows,
974
- truncated: source.truncated,
966
+ truncated: value.truncated,
975
967
  };
976
968
  }
977
969
  if (
@@ -984,10 +976,10 @@ export function decodePreviousVersionSnapshot(
984
976
  return {
985
977
  state: 'previousVersion',
986
978
  available: false,
987
- currentVersion: source.currentVersion,
979
+ currentVersion: value.currentVersion,
988
980
  reason: reason as PreviousVersionReason,
989
981
  rows,
990
- truncated: source.truncated,
982
+ truncated: value.truncated,
991
983
  };
992
984
  }
993
985
 
@@ -1000,62 +992,60 @@ export function decodePreviousVersionAuditResult(
1000
992
  ): PreviousVersionAudit | undefined {
1001
993
  const method = 'previousVersionAudit';
1002
994
  if (value === undefined) return undefined;
1003
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
995
+ if (!isRecord(value)) {
1004
996
  return invalidHostResponse(method);
1005
997
  }
1006
- const source = value as Record<string, unknown>;
1007
- const keys = Object.keys(source).sort();
998
+ const keys = Object.keys(value).sort();
1008
999
  if (keys.length !== AUDIT_KEYS.length) return invalidHostResponse(method);
1009
1000
  if (keys.some((key, index) => key !== AUDIT_KEYS[index])) {
1010
1001
  return invalidHostResponse(method);
1011
1002
  }
1012
1003
  if (
1013
- source.v !== AUDIT_VERSION ||
1014
- !isCount(source.atMs) ||
1015
- !isCount(source.fromVersion) ||
1016
- !isCount(source.toVersion) ||
1017
- !isCount(source.pending) ||
1018
- !isCount(source.encodable) ||
1019
- typeof source.truncated !== 'boolean' ||
1020
- !Array.isArray(source.incompatible) ||
1021
- source.incompatible.length > MAX_AUDIT_INCOMPATIBLE
1004
+ value.v !== AUDIT_VERSION ||
1005
+ !isCount(value.atMs) ||
1006
+ !isCount(value.fromVersion) ||
1007
+ !isCount(value.toVersion) ||
1008
+ !isCount(value.pending) ||
1009
+ !isCount(value.encodable) ||
1010
+ typeof value.truncated !== 'boolean' ||
1011
+ !Array.isArray(value.incompatible) ||
1012
+ value.incompatible.length > MAX_AUDIT_INCOMPATIBLE
1022
1013
  ) {
1023
1014
  return invalidHostResponse(method);
1024
1015
  }
1025
1016
  const incompatible: PreviousVersionAuditEntry[] = [];
1026
- for (const entry of source.incompatible) {
1027
- if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) {
1017
+ for (const entry of value.incompatible) {
1018
+ if (!isRecord(entry)) {
1028
1019
  return invalidHostResponse(method);
1029
1020
  }
1030
- const item = entry as Record<string, unknown>;
1031
1021
  if (
1032
- !Object.keys(item).every((key) =>
1022
+ !Object.keys(entry).every((key) =>
1033
1023
  (AUDIT_ENTRY_KEYS as readonly string[]).includes(key),
1034
1024
  ) ||
1035
- typeof item.commitId !== 'string' ||
1036
- typeof item.table !== 'string' ||
1037
- (item.reason !== 'unknown-table' &&
1038
- item.reason !== 'unknown-column' &&
1039
- item.reason !== 'missing-required-column') ||
1040
- (item.column !== undefined && typeof item.column !== 'string')
1025
+ typeof entry.commitId !== 'string' ||
1026
+ typeof entry.table !== 'string' ||
1027
+ (entry.reason !== 'unknown-table' &&
1028
+ entry.reason !== 'unknown-column' &&
1029
+ entry.reason !== 'missing-required-column') ||
1030
+ (entry.column !== undefined && typeof entry.column !== 'string')
1041
1031
  ) {
1042
1032
  return invalidHostResponse(method);
1043
1033
  }
1044
1034
  incompatible.push({
1045
- commitId: item.commitId,
1046
- table: item.table,
1047
- reason: item.reason,
1048
- ...(typeof item.column === 'string' ? { column: item.column } : {}),
1035
+ commitId: entry.commitId,
1036
+ table: entry.table,
1037
+ reason: entry.reason,
1038
+ ...(typeof entry.column === 'string' ? { column: entry.column } : {}),
1049
1039
  });
1050
1040
  }
1051
1041
  return {
1052
1042
  v: 1,
1053
- atMs: source.atMs,
1054
- fromVersion: source.fromVersion,
1055
- toVersion: source.toVersion,
1056
- pending: source.pending,
1057
- encodable: source.encodable,
1058
- truncated: source.truncated,
1043
+ atMs: value.atMs,
1044
+ fromVersion: value.fromVersion,
1045
+ toVersion: value.toVersion,
1046
+ pending: value.pending,
1047
+ encodable: value.encodable,
1048
+ truncated: value.truncated,
1059
1049
  incompatible,
1060
1050
  };
1061
1051
  }
@@ -1066,18 +1056,17 @@ export function decodePreviousVersionDiscardResult(value: unknown): {
1066
1056
  readonly discarded: boolean;
1067
1057
  } {
1068
1058
  const method = 'previousVersionDiscard';
1069
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
1059
+ if (!isRecord(value)) {
1070
1060
  return invalidHostResponse(method);
1071
1061
  }
1072
- const source = value as Record<string, unknown>;
1073
- const keys = Object.keys(source).sort();
1062
+ const keys = Object.keys(value).sort();
1074
1063
  if (
1075
1064
  keys.length !== DISCARD_KEYS.length ||
1076
1065
  keys.some((key, index) => key !== DISCARD_KEYS[index]) ||
1077
- typeof source.present !== 'boolean' ||
1078
- typeof source.discarded !== 'boolean'
1066
+ typeof value.present !== 'boolean' ||
1067
+ typeof value.discarded !== 'boolean'
1079
1068
  ) {
1080
1069
  return invalidHostResponse(method);
1081
1070
  }
1082
- return { present: source.present, discarded: source.discarded };
1071
+ return { present: value.present, discarded: value.discarded };
1083
1072
  }