@terreno/syncdb 57.2.0 → 57.3.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.
package/src/client.ts CHANGED
@@ -5,7 +5,7 @@ import {createServerKeyProvider, DEFAULT_KEY_CACHE_DB_NAME} from "./crypto/keyPr
5
5
  import type {KeyProvider} from "./crypto/types";
6
6
  import {attachDebugChannel, type DebugChannelBridge} from "./debug/debugChannel";
7
7
  import {resolveDebugLog, type SyncDebugLog, type SyncDebugLogOptions} from "./debug/debugLog";
8
- import {listConflicts, pruneGhostConflicts} from "./mutations/conflicts";
8
+ import {getConflict, listConflicts, pruneGhostConflicts} from "./mutations/conflicts";
9
9
  import {createOutbox, generateMutationId, type Outbox} from "./mutations/outbox";
10
10
  import {resolveConflict as applyConflictResolution} from "./mutations/resolveConflict";
11
11
  import {createDefaultPersisterFactory} from "./persisters/defaultPersisterFactory";
@@ -329,6 +329,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
329
329
 
330
330
  let currentUserId: string | undefined;
331
331
  let persister: ReturnType<PersisterFactory> | undefined;
332
+ const pendingDurableConflicts = new Map<string, string>();
332
333
  let connected = false;
333
334
  let simulatedOffline = false;
334
335
  let syncingCount = 0;
@@ -396,6 +397,14 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
396
397
  }
397
398
  };
398
399
 
400
+ const clearPendingDurableConflicts = (): void => {
401
+ if (pendingDurableConflicts.size === 0) {
402
+ return;
403
+ }
404
+ pendingDurableConflicts.clear();
405
+ notifyStatusChange();
406
+ };
407
+
399
408
  const setConnected = (value: boolean): void => {
400
409
  if (connected === value) {
401
410
  return;
@@ -1125,6 +1134,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1125
1134
  persister,
1126
1135
  store,
1127
1136
  });
1137
+ clearPendingDurableConflicts();
1128
1138
  lastSeqJumpReconcileAt.clear();
1129
1139
  persistenceMode = "durable";
1130
1140
  await createAndStartPersister(userId);
@@ -1178,6 +1188,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1178
1188
  persister,
1179
1189
  store,
1180
1190
  });
1191
+ clearPendingDurableConflicts();
1181
1192
  lastSeqJumpReconcileAt.clear();
1182
1193
  await createAndStartPersister(userId);
1183
1194
  }
@@ -1542,6 +1553,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1542
1553
  if (persisterToStop) {
1543
1554
  // Flush any pending autosave so a clean stop never loses local writes.
1544
1555
  await persisterToStop.save();
1556
+ clearPendingDurableConflicts();
1545
1557
  await persisterToStop.destroy();
1546
1558
  }
1547
1559
  });
@@ -1659,7 +1671,49 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1659
1671
  mutationId: string;
1660
1672
  strategy: ConflictResolutionStrategy;
1661
1673
  }): void => {
1662
- applyConflictResolution({mutationId, outbox, store, strategy});
1674
+ const conflict = getConflict({mutationId, store});
1675
+ const didAddPendingConflict = Boolean(conflict);
1676
+ if (conflict) {
1677
+ pendingDurableConflicts.set(mutationId, conflict.collection);
1678
+ }
1679
+ try {
1680
+ applyConflictResolution({mutationId, outbox, store, strategy});
1681
+ } catch (error) {
1682
+ if (didAddPendingConflict) {
1683
+ pendingDurableConflicts.delete(mutationId);
1684
+ }
1685
+ throw error;
1686
+ }
1687
+ // Conflict resolution is a user-confirmed durability boundary. The web
1688
+ // persister normally trails writes by 500ms; bypass that debounce now and
1689
+ // keep status conflicted until the write succeeds so reload waits on it.
1690
+ // Native and custom persisters fall back to TinyBase's immediate save().
1691
+ const resolutionGeneration = generation;
1692
+ const resolutionPersister = persister;
1693
+ const persistenceFlush = resolutionPersister
1694
+ ? (resolutionPersister.flush?.bind(resolutionPersister) ??
1695
+ resolutionPersister.save.bind(resolutionPersister))
1696
+ : undefined;
1697
+ if (persistenceFlush) {
1698
+ void persistenceFlush()
1699
+ .then(() => {
1700
+ if (generation !== resolutionGeneration || persister !== resolutionPersister) {
1701
+ return;
1702
+ }
1703
+ pendingDurableConflicts.delete(mutationId);
1704
+ notifyStatusChange();
1705
+ })
1706
+ .catch((error: unknown) => {
1707
+ if (generation !== resolutionGeneration || persister !== resolutionPersister) {
1708
+ return;
1709
+ }
1710
+ persistenceMode = "error";
1711
+ warn("post-resolve persistence flush failed")(error);
1712
+ notifyStatusChange();
1713
+ });
1714
+ } else {
1715
+ pendingDurableConflicts.delete(mutationId);
1716
+ }
1663
1717
  debugLog?.record({
1664
1718
  detail: {strategy},
1665
1719
  direction: "local",
@@ -1714,6 +1768,9 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1714
1768
  for (const conflict of conflicts) {
1715
1769
  forCollection(conflict.collection).conflictCount += 1;
1716
1770
  }
1771
+ for (const collection of pendingDurableConflicts.values()) {
1772
+ forCollection(collection).conflictCount += 1;
1773
+ }
1717
1774
  return statuses;
1718
1775
  };
1719
1776
 
@@ -1727,7 +1784,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1727
1784
  ? coordinator.getBlockedEntities({userId: currentUserId}).length
1728
1785
  : 0,
1729
1786
  collections: buildCollectionStatuses({conflicts}),
1730
- conflictCount: conflicts.length,
1787
+ conflictCount: conflicts.length + pendingDurableConflicts.size,
1731
1788
  draining: drainProgress.draining,
1732
1789
  failedCount: currentUserId
1733
1790
  ? outbox.countByStatus({status: "failed", userId: currentUserId})
@@ -1789,6 +1846,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1789
1846
  persister: persisterToStop,
1790
1847
  store,
1791
1848
  });
1849
+ clearPendingDurableConflicts();
1792
1850
  lastSeqJumpReconcileAt.clear();
1793
1851
  return;
1794
1852
  }
@@ -1797,6 +1855,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1797
1855
  // Flush any pending autosave so a sign-out without a wipe never loses
1798
1856
  // local writes (they belong to the user who may sign back in).
1799
1857
  await persisterToStop.save();
1858
+ clearPendingDurableConflicts();
1800
1859
  await persisterToStop.destroy();
1801
1860
  }
1802
1861
  });
@@ -1,12 +1,12 @@
1
1
  import {describe, expect, it} from "bun:test";
2
- import {mkdtemp, readFile, rm} from "node:fs/promises";
2
+ import {mkdtemp, readFile, rm, writeFile} from "node:fs/promises";
3
3
  import {tmpdir} from "node:os";
4
4
  import {join} from "node:path";
5
5
 
6
6
  import {discoverCollections} from "./discoverCollections";
7
7
  import {emitSdk} from "./emitSdk";
8
8
  import {emitInterface} from "./emitTypes";
9
- import {generateSyncDbSdk} from "./generate";
9
+ import {generateSyncDbSdk, loadConfigFile, parseCollectionsFlag} from "./generate";
10
10
  import {friendlyHookNames} from "./hookNames";
11
11
  import {loadSpec} from "./loadSpec";
12
12
 
@@ -37,6 +37,31 @@ describe("emitInterface", () => {
37
37
  })
38
38
  ).toContain("ownerId: string;");
39
39
  });
40
+
41
+ it("emits enums, arrays, empty objects, numbers, booleans, and unknown types", () => {
42
+ expect(
43
+ emitInterface({
44
+ name: "Mixed",
45
+ schema: {
46
+ properties: {
47
+ flags: {items: {type: "boolean"}, type: "array"},
48
+ kind: {enum: ["a", "b"]},
49
+ n: {type: "integer"},
50
+ nested: {type: "object"},
51
+ on: {type: "boolean"},
52
+ other: {},
53
+ },
54
+ type: "object",
55
+ },
56
+ })
57
+ ).toContain('kind?: "a" | "b";');
58
+ expect(
59
+ emitInterface({
60
+ name: "Alias",
61
+ schema: {type: "string"},
62
+ })
63
+ ).toBe("export type Alias = string;");
64
+ });
40
65
  });
41
66
 
42
67
  describe("discoverCollections", () => {
@@ -195,6 +220,22 @@ describe("generateSyncDbSdk", () => {
195
220
  }
196
221
  });
197
222
 
223
+ it("formats the generated file when format is true", async () => {
224
+ const dir = await mkdtemp(join(tmpdir(), "syncdb-codegen-"));
225
+ const out = join(dir, "syncDbSdk.ts");
226
+ try {
227
+ const source = await generateSyncDbSdk({
228
+ format: true,
229
+ out,
230
+ schema: fixturePath,
231
+ });
232
+ expect(source).toContain("export const SYNC_COLLECTIONS");
233
+ expect(await readFile(out, "utf8")).toBe(source);
234
+ } finally {
235
+ await rm(dir, {force: true, recursive: true});
236
+ }
237
+ });
238
+
198
239
  it("rejects collection names that are not TypeScript identifiers", async () => {
199
240
  const spec = await loadSpec(fixturePath);
200
241
  const discovered = discoverCollections({spec});
@@ -214,3 +255,62 @@ describe("generateSyncDbSdk", () => {
214
255
  ).toThrow(/not a TypeScript identifier/);
215
256
  });
216
257
  });
258
+
259
+ describe("parseCollectionsFlag", () => {
260
+ it("returns undefined for missing or empty values", () => {
261
+ expect(parseCollectionsFlag()).toBeUndefined();
262
+ expect(parseCollectionsFlag(" , ")).toBeUndefined();
263
+ });
264
+
265
+ it("splits and trims collection names", () => {
266
+ expect(parseCollectionsFlag("todos, notes")).toEqual(["todos", "notes"]);
267
+ });
268
+ });
269
+
270
+ describe("loadConfigFile", () => {
271
+ it("returns undefined when no path is provided", async () => {
272
+ expect(await loadConfigFile()).toBeUndefined();
273
+ });
274
+
275
+ it("parses a JSON config file", async () => {
276
+ const dir = await mkdtemp(join(tmpdir(), "syncdb-codegen-"));
277
+ const path = join(dir, "config.json");
278
+ try {
279
+ await writeFile(path, JSON.stringify({overrides: {todos: {retries: false}}}), "utf8");
280
+ const config = await loadConfigFile(path);
281
+ expect(config?.overrides?.todos).toEqual({retries: false});
282
+ } finally {
283
+ await rm(dir, {force: true, recursive: true});
284
+ }
285
+ });
286
+ });
287
+
288
+ describe("loadSpec", () => {
289
+ it("rejects an empty schema argument", async () => {
290
+ await expect(loadSpec("")).rejects.toThrow(/Missing --schema/);
291
+ });
292
+
293
+ it("fetches an HTTP spec and rejects non-OK responses", async () => {
294
+ const originalFetch = globalThis.fetch;
295
+ globalThis.fetch = (async () => new Response("nope", {status: 500})) as unknown as typeof fetch;
296
+ try {
297
+ await expect(loadSpec("https://example.test/openapi.json")).rejects.toThrow(
298
+ /Failed to fetch OpenAPI spec/
299
+ );
300
+ } finally {
301
+ globalThis.fetch = originalFetch;
302
+ }
303
+ });
304
+
305
+ it("parses a fetched HTTP spec", async () => {
306
+ const originalFetch = globalThis.fetch;
307
+ globalThis.fetch = (async () =>
308
+ new Response(JSON.stringify({paths: {}}), {status: 200})) as unknown as typeof fetch;
309
+ try {
310
+ const spec = await loadSpec("http://localhost/openapi.json");
311
+ expect(spec.paths).toEqual({});
312
+ } finally {
313
+ globalThis.fetch = originalFetch;
314
+ }
315
+ });
316
+ });
@@ -1,6 +1,7 @@
1
1
  import "fake-indexeddb/auto";
2
2
 
3
3
  import {describe, expect, it, mock} from "bun:test";
4
+ import {assert} from "chai";
4
5
 
5
6
  import {createAesGcmCodec} from "../crypto/aesGcmCodec";
6
7
  import {identityCodec} from "../crypto/identityCodec";
@@ -303,6 +304,34 @@ describe("createEncryptedIndexedDbPersister", () => {
303
304
  expect(target.getEntity({collection: "todos", id: "t1"})?.data).toEqual({title: "three"});
304
305
  });
305
306
 
307
+ it("flush() persists the latest store state without waiting for the save debounce", async () => {
308
+ const databaseName = uniqueDbName();
309
+ const codec = createAesGcmCodec({key: await generateKey()});
310
+ const source = makeStore();
311
+ const persister = createEncryptedIndexedDbPersister({
312
+ codec,
313
+ databaseName,
314
+ saveDebounceMs: 500,
315
+ store: source.raw,
316
+ });
317
+ await persister.startAutoSave();
318
+
319
+ source.upsertEntity({collection: "todos", data: {title: "durable now"}, id: "t1"});
320
+ await persister.flush?.();
321
+
322
+ const target = makeStore();
323
+ await createEncryptedIndexedDbPersister({
324
+ codec,
325
+ databaseName,
326
+ saveDebounceMs: 0,
327
+ store: target.raw,
328
+ }).load();
329
+ assert.deepEqual(target.getEntity({collection: "todos", id: "t1"})?.data, {
330
+ title: "durable now",
331
+ });
332
+ await persister.destroy();
333
+ });
334
+
306
335
  it("destroy() cancels a pending debounced save and writes nothing after (E3e)", async () => {
307
336
  const databaseName = uniqueDbName();
308
337
  let encodeCount = 0;
@@ -3,6 +3,7 @@ import {createCustomPersister, type Persister, Persists} from "tinybase/persiste
3
3
 
4
4
  import type {PayloadCodec} from "../crypto/types";
5
5
  import {idbGet, idbSet} from "../storage/idb";
6
+ import type {SyncPersister} from "./types";
6
7
 
7
8
  /** Object-store record key holding the single encrypted blob. */
8
9
  const RECORD_KEY = "content";
@@ -66,7 +67,7 @@ export const createEncryptedIndexedDbPersister = ({
66
67
  idbGetImpl?: typeof idbGet;
67
68
  /** Test-only override for the IndexedDB write (default: the real `idbSet`). */
68
69
  idbSetImpl?: typeof idbSet;
69
- }): Persister<Persists.MergeableStoreOnly> => {
70
+ }): Persister<Persists.MergeableStoreOnly> & SyncPersister => {
70
71
  let lastWrittenJson: string | undefined;
71
72
  let pendingSave: PendingSave | undefined;
72
73
  // E3(e): the debounce timer scheduling `flushPendingSave` — tracked so
@@ -113,6 +114,30 @@ export const createEncryptedIndexedDbPersister = ({
113
114
  }
114
115
  };
115
116
 
117
+ const flush = async (): Promise<void> => {
118
+ if (pendingSaveTimer !== undefined) {
119
+ clearTimeout(pendingSaveTimer);
120
+ pendingSaveTimer = undefined;
121
+ }
122
+ const pending = pendingSave;
123
+ if (pending) {
124
+ await flushPendingSave();
125
+ await pending.promise;
126
+ return;
127
+ }
128
+
129
+ const json = JSON.stringify(store.getMergeableContent());
130
+ if (json === lastWrittenJson) {
131
+ return;
132
+ }
133
+ try {
134
+ await writeJson(json);
135
+ } catch (error) {
136
+ onSaveFailure?.(error);
137
+ throw error;
138
+ }
139
+ };
140
+
116
141
  const getPersisted = async (): Promise<MergeableContent | undefined> => {
117
142
  let payload: unknown;
118
143
  try {
@@ -214,5 +239,6 @@ export const createEncryptedIndexedDbPersister = ({
214
239
  pendingSave = undefined;
215
240
  return originalDestroy();
216
241
  },
242
+ flush,
217
243
  };
218
244
  };
@@ -30,7 +30,16 @@ export type PersisterFactory = ({
30
30
  onLoadFailure?: () => void;
31
31
  onSaveFailure?: (error: unknown) => void;
32
32
  };
33
- }) => AnyPersister;
33
+ }) => SyncPersister;
34
+
35
+ /**
36
+ * Persister capabilities used by the sync client beyond TinyBase's standard
37
+ * lifecycle. Web persistence exposes `flush` so durability-sensitive state
38
+ * transitions can bypass its normal trailing debounce.
39
+ */
40
+ export type SyncPersister = AnyPersister & {
41
+ flush?: () => Promise<void>;
42
+ };
34
43
 
35
44
  /**
36
45
  * Options accepted by `createDefaultPersisterFactory` on every platform. Each
package/src/types.ts CHANGED
@@ -230,6 +230,10 @@ export type ConflictResolutionStrategy = "useServer" | "keepMine";
230
230
  */
231
231
  export interface SyncCollectionStatus {
232
232
  queuedCount: number;
233
+ /**
234
+ * Unresolved conflicts plus resolved conflicts whose local persistence write
235
+ * has not succeeded yet.
236
+ */
233
237
  conflictCount: number;
234
238
  failedCount: number;
235
239
  }
@@ -239,6 +243,10 @@ export interface SyncStatus {
239
243
  isOnline: boolean;
240
244
  isSyncing: boolean;
241
245
  queuedCount: number;
246
+ /**
247
+ * Unresolved conflicts plus resolved conflicts whose local persistence write
248
+ * has not succeeded yet.
249
+ */
242
250
  conflictCount: number;
243
251
  /** Count of mutations in the terminal `failed` state. */
244
252
  failedCount: number;