@terreno/syncdb 57.2.0 → 57.4.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 (147) hide show
  1. package/README.md +13 -3
  2. package/dist/auth/betterAuthReactClient.d.ts +17 -0
  3. package/dist/auth/betterAuthReactClient.d.ts.map +1 -0
  4. package/dist/auth/betterAuthReactClient.js +11 -0
  5. package/dist/auth/betterAuthReactClient.js.map +1 -0
  6. package/dist/auth/betterAuthReactClient.test.d.ts +2 -0
  7. package/dist/auth/betterAuthReactClient.test.d.ts.map +1 -0
  8. package/dist/auth/betterAuthReactClient.test.js +43 -0
  9. package/dist/auth/betterAuthReactClient.test.js.map +1 -0
  10. package/dist/client.adminWindowMutation.test.d.ts +2 -0
  11. package/dist/client.adminWindowMutation.test.d.ts.map +1 -0
  12. package/dist/client.adminWindowMutation.test.js +65 -0
  13. package/dist/client.adminWindowMutation.test.js.map +1 -0
  14. package/dist/client.d.ts +22 -0
  15. package/dist/client.d.ts.map +1 -1
  16. package/dist/client.js +109 -7
  17. package/dist/client.js.map +1 -1
  18. package/dist/client.test.js +435 -2
  19. package/dist/client.test.js.map +1 -1
  20. package/dist/codegen/emitSdk.js +3 -3
  21. package/dist/codegen/emitSdk.js.map +1 -1
  22. package/dist/codegen/generate.test.js +122 -4
  23. package/dist/codegen/generate.test.js.map +1 -1
  24. package/dist/codegen/safeIdentifiers.d.ts +5 -0
  25. package/dist/codegen/safeIdentifiers.d.ts.map +1 -1
  26. package/dist/codegen/safeIdentifiers.js +8 -0
  27. package/dist/codegen/safeIdentifiers.js.map +1 -1
  28. package/dist/codegen/safeIdentifiers.test.js +20 -8
  29. package/dist/codegen/safeIdentifiers.test.js.map +1 -1
  30. package/dist/codegen/types.d.ts +4 -3
  31. package/dist/codegen/types.d.ts.map +1 -1
  32. package/dist/debug/debugChannel.test.js +5 -0
  33. package/dist/debug/debugChannel.test.js.map +1 -1
  34. package/dist/debug/debugLog.d.ts +1 -0
  35. package/dist/debug/debugLog.d.ts.map +1 -1
  36. package/dist/debug/debugLog.js +1 -0
  37. package/dist/debug/debugLog.js.map +1 -1
  38. package/dist/index.d.ts +1 -0
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +1 -0
  41. package/dist/index.js.map +1 -1
  42. package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts +2 -0
  43. package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts.map +1 -0
  44. package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js +19 -0
  45. package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js.map +1 -0
  46. package/dist/mutations/outbox.d.ts +7 -3
  47. package/dist/mutations/outbox.d.ts.map +1 -1
  48. package/dist/mutations/outbox.js +6 -0
  49. package/dist/mutations/outbox.js.map +1 -1
  50. package/dist/mutations/outbox.test.js +17 -0
  51. package/dist/mutations/outbox.test.js.map +1 -1
  52. package/dist/persisters/encryptedIndexedDbPersister.d.ts +2 -1
  53. package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -1
  54. package/dist/persisters/encryptedIndexedDbPersister.js +24 -0
  55. package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -1
  56. package/dist/persisters/encryptedIndexedDbPersister.test.js +26 -0
  57. package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -1
  58. package/dist/persisters/types.d.ts +9 -1
  59. package/dist/persisters/types.d.ts.map +1 -1
  60. package/dist/storage/idb.d.ts +1 -0
  61. package/dist/storage/idb.d.ts.map +1 -1
  62. package/dist/storage/idb.js +1 -0
  63. package/dist/storage/idb.js.map +1 -1
  64. package/dist/storage/schema.d.ts.map +1 -1
  65. package/dist/storage/schema.js +1 -0
  66. package/dist/storage/schema.js.map +1 -1
  67. package/dist/storage/store.d.ts +3 -2
  68. package/dist/storage/store.d.ts.map +1 -1
  69. package/dist/storage/types.d.ts +1 -7
  70. package/dist/storage/types.d.ts.map +1 -1
  71. package/dist/sync/deltaApplier.d.ts +2 -0
  72. package/dist/sync/deltaApplier.d.ts.map +1 -1
  73. package/dist/sync/deltaApplier.js +9 -0
  74. package/dist/sync/deltaApplier.js.map +1 -1
  75. package/dist/sync/deltaApplier.test.js +63 -0
  76. package/dist/sync/deltaApplier.test.js.map +1 -1
  77. package/dist/sync/entityRepair.d.ts +1 -0
  78. package/dist/sync/entityRepair.d.ts.map +1 -1
  79. package/dist/sync/entityRepair.js +1 -0
  80. package/dist/sync/entityRepair.js.map +1 -1
  81. package/dist/sync/httpChannel.d.ts +3 -1
  82. package/dist/sync/httpChannel.d.ts.map +1 -1
  83. package/dist/sync/httpChannel.js +3 -1
  84. package/dist/sync/httpChannel.js.map +1 -1
  85. package/dist/sync/httpChannel.test.js +68 -0
  86. package/dist/sync/httpChannel.test.js.map +1 -1
  87. package/dist/sync/replayCoordinator.d.ts +6 -12
  88. package/dist/sync/replayCoordinator.d.ts.map +1 -1
  89. package/dist/sync/replayCoordinator.js +9 -2
  90. package/dist/sync/replayCoordinator.js.map +1 -1
  91. package/dist/sync/socketTransport.d.ts +3 -45
  92. package/dist/sync/socketTransport.d.ts.map +1 -1
  93. package/dist/sync/socketTransport.js +31 -9
  94. package/dist/sync/socketTransport.js.map +1 -1
  95. package/dist/sync/socketTransport.test.js +7 -0
  96. package/dist/sync/socketTransport.test.js.map +1 -1
  97. package/dist/sync/transport.d.ts +3 -1
  98. package/dist/sync/transport.d.ts.map +1 -1
  99. package/dist/sync/windowHydrate.d.ts +25 -0
  100. package/dist/sync/windowHydrate.d.ts.map +1 -0
  101. package/dist/sync/windowHydrate.js +67 -0
  102. package/dist/sync/windowHydrate.js.map +1 -0
  103. package/dist/sync/windowHydrate.test.d.ts +2 -0
  104. package/dist/sync/windowHydrate.test.d.ts.map +1 -0
  105. package/dist/sync/windowHydrate.test.js +164 -0
  106. package/dist/sync/windowHydrate.test.js.map +1 -0
  107. package/dist/testing/fakeTransport.js +1 -1
  108. package/dist/testing/fakeTransport.js.map +1 -1
  109. package/dist/types.d.ts +23 -0
  110. package/dist/types.d.ts.map +1 -1
  111. package/package.json +17 -2
  112. package/src/auth/betterAuthReactClient.test.ts +50 -0
  113. package/src/auth/betterAuthReactClient.ts +33 -0
  114. package/src/client.adminWindowMutation.test.ts +69 -0
  115. package/src/client.test.ts +480 -2
  116. package/src/client.ts +147 -6
  117. package/src/codegen/emitSdk.ts +3 -3
  118. package/src/codegen/generate.test.ts +137 -4
  119. package/src/codegen/safeIdentifiers.test.ts +37 -10
  120. package/src/codegen/safeIdentifiers.ts +17 -0
  121. package/src/codegen/types.ts +3 -3
  122. package/src/debug/debugChannel.test.ts +6 -0
  123. package/src/debug/debugLog.ts +1 -0
  124. package/src/index.ts +4 -0
  125. package/src/isolated/defaultPersisterFactoryNativeErrors.isolated.ts +23 -0
  126. package/src/mutations/outbox.test.ts +20 -0
  127. package/src/mutations/outbox.ts +11 -3
  128. package/src/persisters/encryptedIndexedDbPersister.test.ts +29 -0
  129. package/src/persisters/encryptedIndexedDbPersister.ts +27 -1
  130. package/src/persisters/types.ts +10 -1
  131. package/src/storage/idb.ts +1 -0
  132. package/src/storage/schema.ts +1 -0
  133. package/src/storage/store.ts +2 -2
  134. package/src/storage/types.ts +1 -8
  135. package/src/sync/deltaApplier.test.ts +70 -0
  136. package/src/sync/deltaApplier.ts +10 -0
  137. package/src/sync/entityRepair.ts +1 -0
  138. package/src/sync/httpChannel.test.ts +82 -0
  139. package/src/sync/httpChannel.ts +5 -0
  140. package/src/sync/replayCoordinator.ts +10 -3
  141. package/src/sync/socketTransport.test.ts +10 -2
  142. package/src/sync/socketTransport.ts +64 -22
  143. package/src/sync/transport.ts +1 -1
  144. package/src/sync/windowHydrate.test.ts +179 -0
  145. package/src/sync/windowHydrate.ts +97 -0
  146. package/src/testing/fakeTransport.ts +1 -1
  147. package/src/types.ts +24 -0
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";
@@ -22,6 +22,7 @@ import {AuthRequiredError, createHttpChannel, type HttpChannel} from "./sync/htt
22
22
  import {createReplayCoordinator, type ReplayCoordinator} from "./sync/replayCoordinator";
23
23
  import {createSocketTransport} from "./sync/socketTransport";
24
24
  import type {SendMutationBatchResult, SendMutationResult, SyncTransport} from "./sync/transport";
25
+ import {type HydrateWindowEntitiesResult, hydrateWindowEntities} from "./sync/windowHydrate";
25
26
  import type {
26
27
  AuthProvider,
27
28
  ConflictResolutionStrategy,
@@ -67,6 +68,12 @@ export interface SyncDbConfig {
67
68
  baseUrl?: string;
68
69
  /** Transport override (tests inject a fake; default is the socket transport). */
69
70
  transport?: SyncTransport;
71
+ /**
72
+ * Optional selected organization id for `X-Organization-Id` on HTTP sync
73
+ * calls and the `organizationId` field on socket mutate payloads. Read at
74
+ * send time so admin org switches apply without reconnecting.
75
+ */
76
+ organizationIdProvider?: () => string | undefined;
70
77
  /** HTTP channel override (default is built from baseUrl when present). */
71
78
  httpChannel?: HttpChannel;
72
79
  /** Persister factory override (default is the platform default factory). */
@@ -83,6 +90,12 @@ export interface SyncDbConfig {
83
90
  idbSetImpl?: DefaultPersisterFactoryConfig["idbSetImpl"];
84
91
  /** Periodic reconcile interval in ms; 0 disables (default 5 minutes). */
85
92
  reconcileIntervalMs?: number;
93
+ /**
94
+ * Collections that subscribe in admin window mode: join `{collection}|admin`,
95
+ * skip `GET /sync/snapshot` paging (startup, catch-up, and the reconcile timer).
96
+ * Hydrate rows via REST membership + `/sync/entities` instead.
97
+ */
98
+ windowCollections?: string[];
86
99
  /** Rate limit for seq-jump-triggered reconciles per stream (default 30s). */
87
100
  seqJumpReconcileMinIntervalMs?: number;
88
101
  /** Millisecond clock, injectable for deterministic rate-limit tests. */
@@ -229,6 +242,15 @@ export interface SyncDb {
229
242
  mutate: (args: MutateArgs) => {mutationId: string; id: string};
230
243
  /** Snapshot-from-cursor catch-up for every collection (no-op without HTTP). */
231
244
  reconcile: () => Promise<void>;
245
+ /**
246
+ * Upsert a window of known ids (admin membership). Missing rows are fetched
247
+ * from `GET /sync/entities`; ids the server does not return are ignored.
248
+ */
249
+ hydrateWindow: (args: {
250
+ collection: string;
251
+ ids: string[];
252
+ restRows?: Record<string, unknown>;
253
+ }) => Promise<HydrateWindowEntitiesResult>;
232
254
  /**
233
255
  * Purge every known stream locally and re-bootstrap from cursor 0. Use when
234
256
  * devices have diverged and a full server snapshot is needed without wiping
@@ -295,17 +317,43 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
295
317
  authProvider: config.authProvider,
296
318
  // biome-ignore lint/style/noNonNullAssertion: guarded above — no transport implies baseUrl.
297
319
  baseUrl: config.baseUrl!,
320
+ organizationIdProvider: config.organizationIdProvider,
298
321
  });
299
322
  const httpChannel =
300
323
  config.httpChannel ??
301
324
  (config.baseUrl
302
- ? createHttpChannel({authProvider: config.authProvider, baseUrl: config.baseUrl})
325
+ ? createHttpChannel({
326
+ authProvider: config.authProvider,
327
+ baseUrl: config.baseUrl,
328
+ organizationIdProvider: config.organizationIdProvider,
329
+ })
303
330
  : undefined);
304
331
 
305
332
  const store = createSyncStore({
306
333
  collections: config.collections,
307
334
  now: () => DateTime.fromMillis(now()).toISO() ?? new Date(now()).toISOString(),
308
335
  });
336
+ const windowCollectionSet = new Set(config.windowCollections ?? []);
337
+ const isWindowCollection = (collection: string): boolean => windowCollectionSet.has(collection);
338
+ const skipSnapshotPaging = ({
339
+ collection,
340
+ stream,
341
+ mode,
342
+ }: {
343
+ collection: string;
344
+ stream: string;
345
+ mode?: "window";
346
+ }): boolean => isWindowCollection(collection) || mode === "window" || stream.endsWith("|admin");
347
+ const subscribeConfiguredCollections = (): void => {
348
+ const windowed = config.collections.filter((collection) => isWindowCollection(collection));
349
+ const full = config.collections.filter((collection) => !isWindowCollection(collection));
350
+ if (full.length > 0) {
351
+ transport.subscribe(full);
352
+ }
353
+ if (windowed.length > 0) {
354
+ transport.subscribe(windowed, {mode: "window"});
355
+ }
356
+ };
309
357
  const outbox = createOutbox({store});
310
358
  const debugLog = resolveDebugLog(config.debug);
311
359
  // Mirror the debug log across browser windows/tabs (web only) so a debugger
@@ -329,6 +377,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
329
377
 
330
378
  let currentUserId: string | undefined;
331
379
  let persister: ReturnType<PersisterFactory> | undefined;
380
+ const pendingDurableConflicts = new Map<string, string>();
332
381
  let connected = false;
333
382
  let simulatedOffline = false;
334
383
  let syncingCount = 0;
@@ -396,6 +445,14 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
396
445
  }
397
446
  };
398
447
 
448
+ const clearPendingDurableConflicts = (): void => {
449
+ if (pendingDurableConflicts.size === 0) {
450
+ return;
451
+ }
452
+ pendingDurableConflicts.clear();
453
+ notifyStatusChange();
454
+ };
455
+
399
456
  const setConnected = (value: boolean): void => {
400
457
  if (connected === value) {
401
458
  return;
@@ -640,6 +697,9 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
640
697
  return;
641
698
  }
642
699
  for (const [stream, collection] of streams) {
700
+ if (skipSnapshotPaging({collection, stream})) {
701
+ continue;
702
+ }
643
703
  await bootstrapStream({channel: httpChannel, collection, store, stream});
644
704
  if (isSuperseded()) {
645
705
  return;
@@ -730,9 +790,11 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
730
790
  */
731
791
  const catchUpSubscribedStreams = async ({
732
792
  collection,
793
+ mode,
733
794
  streams,
734
795
  }: {
735
796
  collection: string;
797
+ mode?: "window";
736
798
  streams: string[];
737
799
  }): Promise<void> => {
738
800
  if (!httpChannel || simulatedOffline || authPaused || streams.length === 0) {
@@ -741,6 +803,9 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
741
803
  if (!store.collections.includes(collection)) {
742
804
  return;
743
805
  }
806
+ if (mode === "window" || isWindowCollection(collection)) {
807
+ return;
808
+ }
744
809
  const myGeneration = generation;
745
810
  const myUserId = currentUserId;
746
811
  const isSuperseded = (): boolean => generation !== myGeneration || currentUserId !== myUserId;
@@ -750,6 +815,9 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
750
815
  if (isSuperseded()) {
751
816
  return;
752
817
  }
818
+ if (skipSnapshotPaging({collection, mode, stream})) {
819
+ continue;
820
+ }
753
821
  store.addKnownStream({collection, stream});
754
822
  await bootstrapStream({channel: httpChannel, collection, store, stream});
755
823
  }
@@ -1125,6 +1193,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1125
1193
  persister,
1126
1194
  store,
1127
1195
  });
1196
+ clearPendingDurableConflicts();
1128
1197
  lastSeqJumpReconcileAt.clear();
1129
1198
  persistenceMode = "durable";
1130
1199
  await createAndStartPersister(userId);
@@ -1178,6 +1247,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1178
1247
  persister,
1179
1248
  store,
1180
1249
  });
1250
+ clearPendingDurableConflicts();
1181
1251
  lastSeqJumpReconcileAt.clear();
1182
1252
  await createAndStartPersister(userId);
1183
1253
  }
@@ -1269,7 +1339,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1269
1339
  warn("transport reconnect after user change failed; continuing offline")(error);
1270
1340
  }
1271
1341
  bindDeltaHandler();
1272
- transport.subscribe(config.collections);
1342
+ subscribeConfiguredCollections();
1273
1343
  };
1274
1344
 
1275
1345
  const handleStatusChange = ({
@@ -1505,7 +1575,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1505
1575
  if (generation !== myGeneration) {
1506
1576
  return;
1507
1577
  }
1508
- transport.subscribe(config.collections);
1578
+ subscribeConfiguredCollections();
1509
1579
  // C2: run an initial stream discovery + per-stream bootstrap on start (not only via
1510
1580
  // the reconnect status event) so a client that starts offline-then-online, or with a
1511
1581
  // warm socket, still backfills newly-joined streams and drains legacy cursors.
@@ -1542,6 +1612,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1542
1612
  if (persisterToStop) {
1543
1613
  // Flush any pending autosave so a clean stop never loses local writes.
1544
1614
  await persisterToStop.save();
1615
+ clearPendingDurableConflicts();
1545
1616
  await persisterToStop.destroy();
1546
1617
  }
1547
1618
  });
@@ -1635,6 +1706,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1635
1706
  entityId,
1636
1707
  ...(maxAttempts !== undefined ? {maxAttempts} : {}),
1637
1708
  mutationId,
1709
+ ...(windowCollectionSet.has(collection) ? {mutationMode: "adminWindow" as const} : {}),
1638
1710
  operation,
1639
1711
  userId: currentUserId,
1640
1712
  });
@@ -1659,7 +1731,49 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1659
1731
  mutationId: string;
1660
1732
  strategy: ConflictResolutionStrategy;
1661
1733
  }): void => {
1662
- applyConflictResolution({mutationId, outbox, store, strategy});
1734
+ const conflict = getConflict({mutationId, store});
1735
+ const didAddPendingConflict = Boolean(conflict);
1736
+ if (conflict) {
1737
+ pendingDurableConflicts.set(mutationId, conflict.collection);
1738
+ }
1739
+ try {
1740
+ applyConflictResolution({mutationId, outbox, store, strategy});
1741
+ } catch (error) {
1742
+ if (didAddPendingConflict) {
1743
+ pendingDurableConflicts.delete(mutationId);
1744
+ }
1745
+ throw error;
1746
+ }
1747
+ // Conflict resolution is a user-confirmed durability boundary. The web
1748
+ // persister normally trails writes by 500ms; bypass that debounce now and
1749
+ // keep status conflicted until the write succeeds so reload waits on it.
1750
+ // Native and custom persisters fall back to TinyBase's immediate save().
1751
+ const resolutionGeneration = generation;
1752
+ const resolutionPersister = persister;
1753
+ const persistenceFlush = resolutionPersister
1754
+ ? (resolutionPersister.flush?.bind(resolutionPersister) ??
1755
+ resolutionPersister.save.bind(resolutionPersister))
1756
+ : undefined;
1757
+ if (persistenceFlush) {
1758
+ void persistenceFlush()
1759
+ .then(() => {
1760
+ if (generation !== resolutionGeneration || persister !== resolutionPersister) {
1761
+ return;
1762
+ }
1763
+ pendingDurableConflicts.delete(mutationId);
1764
+ notifyStatusChange();
1765
+ })
1766
+ .catch((error: unknown) => {
1767
+ if (generation !== resolutionGeneration || persister !== resolutionPersister) {
1768
+ return;
1769
+ }
1770
+ persistenceMode = "error";
1771
+ warn("post-resolve persistence flush failed")(error);
1772
+ notifyStatusChange();
1773
+ });
1774
+ } else {
1775
+ pendingDurableConflicts.delete(mutationId);
1776
+ }
1663
1777
  debugLog?.record({
1664
1778
  detail: {strategy},
1665
1779
  direction: "local",
@@ -1714,9 +1828,33 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1714
1828
  for (const conflict of conflicts) {
1715
1829
  forCollection(conflict.collection).conflictCount += 1;
1716
1830
  }
1831
+ for (const collection of pendingDurableConflicts.values()) {
1832
+ forCollection(collection).conflictCount += 1;
1833
+ }
1717
1834
  return statuses;
1718
1835
  };
1719
1836
 
1837
+ const hydrateWindow = async ({
1838
+ collection,
1839
+ ids,
1840
+ restRows,
1841
+ }: {
1842
+ collection: string;
1843
+ ids: string[];
1844
+ restRows?: Record<string, unknown>;
1845
+ }): Promise<HydrateWindowEntitiesResult> => {
1846
+ if (!httpChannel) {
1847
+ throw new Error("hydrateWindow requires an HTTP channel");
1848
+ }
1849
+ return hydrateWindowEntities({
1850
+ channel: httpChannel,
1851
+ collection,
1852
+ ids,
1853
+ restRows,
1854
+ store,
1855
+ });
1856
+ };
1857
+
1720
1858
  const getSyncStatus = (): SyncStatus => {
1721
1859
  const drainProgress = currentUserId
1722
1860
  ? coordinator.getDrainProgress({userId: currentUserId})
@@ -1727,7 +1865,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1727
1865
  ? coordinator.getBlockedEntities({userId: currentUserId}).length
1728
1866
  : 0,
1729
1867
  collections: buildCollectionStatuses({conflicts}),
1730
- conflictCount: conflicts.length,
1868
+ conflictCount: conflicts.length + pendingDurableConflicts.size,
1731
1869
  draining: drainProgress.draining,
1732
1870
  failedCount: currentUserId
1733
1871
  ? outbox.countByStatus({status: "failed", userId: currentUserId})
@@ -1789,6 +1927,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1789
1927
  persister: persisterToStop,
1790
1928
  store,
1791
1929
  });
1930
+ clearPendingDurableConflicts();
1792
1931
  lastSeqJumpReconcileAt.clear();
1793
1932
  return;
1794
1933
  }
@@ -1797,6 +1936,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1797
1936
  // Flush any pending autosave so a sign-out without a wipe never loses
1798
1937
  // local writes (they belong to the user who may sign back in).
1799
1938
  await persisterToStop.save();
1939
+ clearPendingDurableConflicts();
1800
1940
  await persisterToStop.destroy();
1801
1941
  }
1802
1942
  });
@@ -1807,6 +1947,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
1807
1947
  getSyncStatus,
1808
1948
  goOffline,
1809
1949
  goOnline,
1950
+ hydrateWindow,
1810
1951
  mutate,
1811
1952
  onStatusChange,
1812
1953
  outbox,
@@ -1,6 +1,6 @@
1
1
  import {emitInterface} from "./emitTypes";
2
2
  import {friendlyHookNames} from "./hookNames";
3
- import {assertTsIdentifier, emitTsString} from "./safeIdentifiers";
3
+ import {assertSyncCollectionName, assertTsIdentifier, emitTsString} from "./safeIdentifiers";
4
4
  import type {CodegenConfigFile, DiscoveredCollection} from "./types";
5
5
 
6
6
  const GENERATED_HEADER = `// GENERATED by @terreno/syncdb (terreno-syncdb-codegen). Do not edit.
@@ -38,7 +38,7 @@ export const emitSdk = ({
38
38
  const names: string[] = [];
39
39
 
40
40
  for (const collection of collections) {
41
- const collectionName = assertTsIdentifier({
41
+ const collectionName = assertSyncCollectionName({
42
42
  label: "collection",
43
43
  value: collection.collection,
44
44
  });
@@ -60,7 +60,7 @@ export const emitSdk = ({
60
60
  typeBlocks.push(emitInterface({name: updateName, schema: collection.updateSchema}));
61
61
 
62
62
  const hooks = friendlyHookNames({
63
- collection: collectionName,
63
+ collection: collection.collection,
64
64
  entityName,
65
65
  });
66
66
  const list = assertTsIdentifier({label: "hook name", value: hooks.list});
@@ -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,7 +220,46 @@ describe("generateSyncDbSdk", () => {
195
220
  }
196
221
  });
197
222
 
198
- it("rejects collection names that are not TypeScript identifiers", async () => {
223
+ it("applies retries: true and numeric overrides", async () => {
224
+ const dir = await mkdtemp(join(tmpdir(), "syncdb-codegen-"));
225
+ const out = join(dir, "syncDbSdk.ts");
226
+ try {
227
+ const enabled = await generateSyncDbSdk({
228
+ config: {overrides: {todos: {retries: true}}},
229
+ format: false,
230
+ out,
231
+ schema: fixturePath,
232
+ });
233
+ expect(enabled).toContain("retries: true");
234
+ const numbered = await generateSyncDbSdk({
235
+ config: {overrides: {todos: {retries: 4}}},
236
+ format: false,
237
+ out,
238
+ schema: fixturePath,
239
+ });
240
+ expect(numbered).toContain("retries: 4");
241
+ } finally {
242
+ await rm(dir, {force: true, recursive: true});
243
+ }
244
+ });
245
+
246
+ it("formats the generated file when format is true", async () => {
247
+ const dir = await mkdtemp(join(tmpdir(), "syncdb-codegen-"));
248
+ const out = join(dir, "syncDbSdk.ts");
249
+ try {
250
+ const source = await generateSyncDbSdk({
251
+ format: true,
252
+ out,
253
+ schema: fixturePath,
254
+ });
255
+ expect(source).toContain("export const SYNC_COLLECTIONS");
256
+ expect(await readFile(out, "utf8")).toBe(source);
257
+ } finally {
258
+ await rm(dir, {force: true, recursive: true});
259
+ }
260
+ });
261
+
262
+ it("rejects unsafe sync collection names", async () => {
199
263
  const spec = await loadSpec(fixturePath);
200
264
  const discovered = discoverCollections({spec});
201
265
  const first = discovered[0];
@@ -211,6 +275,75 @@ describe("generateSyncDbSdk", () => {
211
275
  },
212
276
  ],
213
277
  })
214
- ).toThrow(/not a TypeScript identifier/);
278
+ ).toThrow(/not a valid sync collection name/);
279
+ });
280
+
281
+ it("rejects invalid retries overrides", async () => {
282
+ const spec = await loadSpec(fixturePath);
283
+ const first = discoverCollections({spec})[0];
284
+ expect(() =>
285
+ emitSdk({
286
+ collections: [{...first, retries: Number.NaN}],
287
+ })
288
+ ).toThrow(/Invalid retries/);
289
+ });
290
+ });
291
+
292
+ describe("parseCollectionsFlag", () => {
293
+ it("returns undefined for missing or empty values", () => {
294
+ expect(parseCollectionsFlag()).toBeUndefined();
295
+ expect(parseCollectionsFlag(" , ")).toBeUndefined();
296
+ });
297
+
298
+ it("splits and trims collection names", () => {
299
+ expect(parseCollectionsFlag("todos, notes")).toEqual(["todos", "notes"]);
300
+ });
301
+ });
302
+
303
+ describe("loadConfigFile", () => {
304
+ it("returns undefined when no path is provided", async () => {
305
+ expect(await loadConfigFile()).toBeUndefined();
306
+ });
307
+
308
+ it("parses a JSON config file", async () => {
309
+ const dir = await mkdtemp(join(tmpdir(), "syncdb-codegen-"));
310
+ const path = join(dir, "config.json");
311
+ try {
312
+ await writeFile(path, JSON.stringify({overrides: {todos: {retries: false}}}), "utf8");
313
+ const config = await loadConfigFile(path);
314
+ expect(config?.overrides?.todos).toEqual({retries: false});
315
+ } finally {
316
+ await rm(dir, {force: true, recursive: true});
317
+ }
318
+ });
319
+ });
320
+
321
+ describe("loadSpec", () => {
322
+ it("rejects an empty schema argument", async () => {
323
+ await expect(loadSpec("")).rejects.toThrow(/Missing --schema/);
324
+ });
325
+
326
+ it("fetches an HTTP spec and rejects non-OK responses", async () => {
327
+ const originalFetch = globalThis.fetch;
328
+ globalThis.fetch = (async () => new Response("nope", {status: 500})) as unknown as typeof fetch;
329
+ try {
330
+ await expect(loadSpec("https://example.test/openapi.json")).rejects.toThrow(
331
+ /Failed to fetch OpenAPI spec/
332
+ );
333
+ } finally {
334
+ globalThis.fetch = originalFetch;
335
+ }
336
+ });
337
+
338
+ it("parses a fetched HTTP spec", async () => {
339
+ const originalFetch = globalThis.fetch;
340
+ globalThis.fetch = (async () =>
341
+ new Response(JSON.stringify({paths: {}}), {status: 200})) as unknown as typeof fetch;
342
+ try {
343
+ const spec = await loadSpec("http://localhost/openapi.json");
344
+ expect(spec.paths).toEqual({});
345
+ } finally {
346
+ globalThis.fetch = originalFetch;
347
+ }
215
348
  });
216
349
  });
@@ -1,32 +1,59 @@
1
- import {describe, expect, it} from "bun:test";
1
+ import {describe, it} from "bun:test";
2
+ import {assert} from "chai";
2
3
 
3
- import {assertTsIdentifier, emitTsPropertyKey, emitTsString} from "./safeIdentifiers";
4
+ import {
5
+ assertSyncCollectionName,
6
+ assertTsIdentifier,
7
+ emitTsPropertyKey,
8
+ emitTsString,
9
+ } from "./safeIdentifiers";
4
10
 
5
11
  describe("assertTsIdentifier", () => {
6
12
  it("accepts TypeScript identifiers", () => {
7
- expect(assertTsIdentifier({label: "name", value: "Todo"})).toBe("Todo");
8
- expect(assertTsIdentifier({label: "name", value: "_id"})).toBe("_id");
13
+ assert.equal(assertTsIdentifier({label: "name", value: "Todo"}), "Todo");
14
+ assert.equal(assertTsIdentifier({label: "name", value: "_id"}), "_id");
9
15
  });
10
16
 
11
17
  it("rejects quote and statement injection", () => {
12
- expect(() =>
13
- assertTsIdentifier({label: "collection", value: 'todos"; process.exit(1); //'})
14
- ).toThrow(/not a TypeScript identifier/);
18
+ assert.throws(
19
+ () => assertTsIdentifier({label: "collection", value: 'todos"; process.exit(1); //'}),
20
+ /not a TypeScript identifier/
21
+ );
22
+ });
23
+ });
24
+
25
+ describe("assertSyncCollectionName", () => {
26
+ it("accepts hyphenated collection names", () => {
27
+ assert.equal(
28
+ assertSyncCollectionName({label: "collection", value: "notification-preferences"}),
29
+ "notification-preferences"
30
+ );
31
+ });
32
+
33
+ it("rejects unsafe collection names", () => {
34
+ assert.throws(
35
+ () =>
36
+ assertSyncCollectionName({
37
+ label: "collection",
38
+ value: 'notifications"; process.exit(1); //',
39
+ }),
40
+ /not a valid sync collection name/
41
+ );
15
42
  });
16
43
  });
17
44
 
18
45
  describe("emitTsString", () => {
19
46
  it("JSON-escapes quotes so they cannot close a generated string", () => {
20
- expect(emitTsString('todos"; process.exit(1); //')).toBe('"todos\\"; process.exit(1); //"');
47
+ assert.equal(emitTsString('todos"; process.exit(1); //'), '"todos\\"; process.exit(1); //"');
21
48
  });
22
49
  });
23
50
 
24
51
  describe("emitTsPropertyKey", () => {
25
52
  it("emits bare identifiers", () => {
26
- expect(emitTsPropertyKey("title")).toBe("title");
53
+ assert.equal(emitTsPropertyKey("title"), "title");
27
54
  });
28
55
 
29
56
  it("quotes keys that are not identifiers", () => {
30
- expect(emitTsPropertyKey('foo"; bar')).toBe('"foo\\"; bar"');
57
+ assert.equal(emitTsPropertyKey('foo"; bar'), '"foo\\"; bar"');
31
58
  });
32
59
  });
@@ -1,4 +1,5 @@
1
1
  const TS_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
2
+ const SYNC_COLLECTION = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
2
3
 
3
4
  export const assertTsIdentifier = ({label, value}: {label: string; value: string}): string => {
4
5
  if (!TS_IDENTIFIER.test(value)) {
@@ -7,6 +8,22 @@ export const assertTsIdentifier = ({label, value}: {label: string; value: string
7
8
  return value;
8
9
  };
9
10
 
11
+ /** Sync collection tags may use hyphens (e.g. notification-preferences) but must be safe in JSON strings. */
12
+ export const assertSyncCollectionName = ({
13
+ label,
14
+ value,
15
+ }: {
16
+ label: string;
17
+ value: string;
18
+ }): string => {
19
+ if (!SYNC_COLLECTION.test(value)) {
20
+ throw new Error(
21
+ `Invalid ${label}: ${JSON.stringify(value)} is not a valid sync collection name`
22
+ );
23
+ }
24
+ return value;
25
+ };
26
+
10
27
  export const emitTsString = (value: string): string => JSON.stringify(value);
11
28
 
12
29
  export const emitTsPropertyKey = (key: string): string => {
@@ -1,4 +1,4 @@
1
- export interface TerrenoSyncExtension {
1
+ interface TerrenoSyncExtension {
2
2
  collection: string;
3
3
  scope: string;
4
4
  }
@@ -18,7 +18,7 @@ export interface OpenApiSchema {
18
18
  description?: string;
19
19
  }
20
20
 
21
- export interface OpenApiOperation {
21
+ interface OpenApiOperation {
22
22
  "x-terreno-sync"?: TerrenoSyncExtension;
23
23
  requestBody?: {
24
24
  content?: Record<string, {schema?: OpenApiSchema}>;
@@ -31,7 +31,7 @@ export interface OpenApiOperation {
31
31
  >;
32
32
  }
33
33
 
34
- export interface OpenApiPathItem {
34
+ interface OpenApiPathItem {
35
35
  get?: OpenApiOperation;
36
36
  post?: OpenApiOperation;
37
37
  patch?: OpenApiOperation;
@@ -118,4 +118,10 @@ describe("attachDebugChannel", () => {
118
118
 
119
119
  expect(debuggerLog.getEvents()).toHaveLength(0);
120
120
  });
121
+
122
+ it("uses BroadcastChannel when the runtime provides it", () => {
123
+ const log = createSyncDebugLog();
124
+ const bridge = attachDebugChannel({log, name: "factory-probe"});
125
+ bridge?.close();
126
+ });
121
127
  });
@@ -148,6 +148,7 @@ const emptyByType = (): Record<SyncDebugEventType, number> => ({
148
148
  /**
149
149
  * Create an in-memory debug log backed by a circular buffer.
150
150
  */
151
+ /** @internal */
151
152
  export const createSyncDebugLog = (options: SyncDebugLogOptions = {}): SyncDebugLog => {
152
153
  const capacity = Math.max(1, options.capacity ?? DEFAULT_CAPACITY);
153
154
  const clock = options.clock ?? ((): string => DateTime.now().toISO() ?? new Date().toISOString());
package/src/index.ts CHANGED
@@ -85,6 +85,10 @@ export {
85
85
  betterAuthAdapter,
86
86
  DEFAULT_AUTH_POLL_INTERVAL_MS,
87
87
  } from "./auth/betterAuthAdapter";
88
+ export {
89
+ type BetterAuthReactClientLike,
90
+ bridgeBetterAuthReactClient,
91
+ } from "./auth/betterAuthReactClient";
88
92
  export type {
89
93
  BetterAuthClientLike,
90
94
  BetterAuthGetSessionResult,