@fairfox/polly 0.58.0 → 0.60.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.
@@ -27,8 +27,8 @@ export type { MeshKeyring, MeshNetworkAdapterOptions, } from "./shared/lib/mesh-
27
27
  export { DEFAULT_MESH_KEY_ID, MeshNetworkAdapter, } from "./shared/lib/mesh-network-adapter";
28
28
  export type { MeshSignalingClientOptions, SignalingMessage as MeshSignalingMessage, } from "./shared/lib/mesh-signaling-client";
29
29
  export { MeshSignalingClient } from "./shared/lib/mesh-signaling-client";
30
- export type { MeshStateOptions } from "./shared/lib/mesh-state";
31
- export { $meshCounter, $meshList, $meshState, $meshText, configureMeshState, getLastConfiguredRepoPeerId, getMeshStateModuleId, isMeshStateConfigured, MESH_STATE_MODULE_ID, resetMeshState, wasMeshStateResolved, } from "./shared/lib/mesh-state";
30
+ export type { LazyWrapperExitReason, MeshStateLazyWrapperRecord, MeshStateLoadedRejectionBreadcrumb, MeshStateOptions, } from "./shared/lib/mesh-state";
31
+ export { $meshCounter, $meshList, $meshState, $meshText, configureMeshState, getLastConfiguredRepoPeerId, getLastLoadedRejection, getLazyInvocations, getLazyReachedRepo, getLazyWrappers, getMeshStateModuleId, isMeshStateConfigured, MESH_STATE_MODULE_ID, resetMeshState, wasMeshStateResolved, } from "./shared/lib/mesh-state";
32
32
  export type { HandleSyncSnapshot, InFlightSyncSnapshot, MeshWebRTCAdapterOptions, SlotInitiationDecision, SlotInitiationRejectionReason, SweepSnapshot, SyncHandshakeAttemptSnapshot, SyncProgressEvent, TransportSnapshot, } from "./shared/lib/mesh-webrtc-adapter";
33
33
  export { DEFAULT_ICE_SERVERS, MeshWebRTCAdapter } from "./shared/lib/mesh-webrtc-adapter";
34
34
  export type { CreatePairingTokenOptions, PairingToken, } from "./shared/lib/pairing";
package/dist/src/mesh.js CHANGED
@@ -1779,6 +1779,11 @@ function configureMeshState(repo) {
1779
1779
  function resetMeshState() {
1780
1780
  defaultRepo = undefined;
1781
1781
  lastConfiguredRepoPeerId = undefined;
1782
+ meshStateEverResolved = false;
1783
+ lazyInvocations = 0;
1784
+ lazyReachedRepo = 0;
1785
+ lastLoadedRejection = undefined;
1786
+ lazyWrappers = [];
1782
1787
  }
1783
1788
  function isMeshStateConfigured() {
1784
1789
  return defaultRepo !== undefined;
@@ -1787,6 +1792,44 @@ var meshStateEverResolved = false;
1787
1792
  function wasMeshStateResolved() {
1788
1793
  return meshStateEverResolved;
1789
1794
  }
1795
+ var lazyInvocations = 0;
1796
+ function getLazyInvocations() {
1797
+ return lazyInvocations;
1798
+ }
1799
+ var lazyReachedRepo = 0;
1800
+ function getLazyReachedRepo() {
1801
+ return lazyReachedRepo;
1802
+ }
1803
+ var lastLoadedRejection;
1804
+ function getLastLoadedRejection() {
1805
+ return lastLoadedRejection;
1806
+ }
1807
+ function recordLoadedRejection(thrown) {
1808
+ const err = thrown instanceof Error ? thrown : new Error(typeof thrown === "string" ? thrown : String(thrown));
1809
+ lastLoadedRejection = {
1810
+ name: err.name,
1811
+ message: err.message,
1812
+ stack: err.stack,
1813
+ at: Date.now()
1814
+ };
1815
+ }
1816
+ var LAZY_WRAPPER_LOG_CAP = 64;
1817
+ var lazyWrappers = [];
1818
+ function getLazyWrappers() {
1819
+ return lazyWrappers.slice();
1820
+ }
1821
+ function recordLazyWrapper(record) {
1822
+ lazyWrappers.push(record);
1823
+ if (lazyWrappers.length > LAZY_WRAPPER_LOG_CAP) {
1824
+ lazyWrappers = lazyWrappers.slice(-LAZY_WRAPPER_LOG_CAP);
1825
+ }
1826
+ }
1827
+ function peekHandleState(repo, documentId) {
1828
+ const handle = repo.handles[documentId];
1829
+ if (!handle)
1830
+ return;
1831
+ return typeof handle.state === "string" ? handle.state : String(handle.state ?? "unknown");
1832
+ }
1790
1833
  function resolveRepo(option) {
1791
1834
  meshStateEverResolved = true;
1792
1835
  const repo = option ?? defaultRepo;
@@ -1808,26 +1851,56 @@ function deriveDocumentId(key) {
1808
1851
  function buildHandleFactory(repo, key, initialDoc) {
1809
1852
  const documentId = deriveDocumentId(key);
1810
1853
  return async () => {
1811
- const cached = repo.handles[documentId];
1812
- if (cached) {
1813
- await cached.whenReady(["ready", "unavailable"]);
1814
- if (cached.state === "ready") {
1815
- return cached;
1854
+ lazyInvocations++;
1855
+ let exitReason = "threw";
1856
+ let errorMessage;
1857
+ try {
1858
+ const cached = repo.handles[documentId];
1859
+ lazyReachedRepo++;
1860
+ if (cached) {
1861
+ await cached.whenReady(["ready", "unavailable"]);
1862
+ if (cached.state === "ready") {
1863
+ exitReason = "returned-cached";
1864
+ return cached;
1865
+ }
1816
1866
  }
1867
+ const stored = await repo.storageSubsystem?.loadDoc(documentId);
1868
+ if (stored) {
1869
+ exitReason = "loaded-from-storage";
1870
+ return repo.find(documentId, { allowableStates: ["ready"] });
1871
+ }
1872
+ const seeded = Automerge.save(Automerge.from(initialDoc));
1873
+ const handle = repo.import(seeded, { docId: documentId });
1874
+ handle.doneLoading();
1875
+ exitReason = "seeded-and-imported";
1876
+ return handle;
1877
+ } catch (err) {
1878
+ errorMessage = err instanceof Error ? err.message : String(err);
1879
+ recordLoadedRejection(err);
1880
+ throw err;
1881
+ } finally {
1882
+ const handleState = peekHandleState(repo, documentId);
1883
+ recordLazyWrapper({
1884
+ key,
1885
+ docId: documentId,
1886
+ at: Date.now(),
1887
+ exitReason,
1888
+ handleRegistered: handleState !== undefined,
1889
+ handleState,
1890
+ errorMessage
1891
+ });
1817
1892
  }
1818
- const stored = await repo.storageSubsystem?.loadDoc(documentId);
1819
- if (stored) {
1820
- return repo.find(documentId, { allowableStates: ["ready"] });
1821
- }
1822
- const seeded = Automerge.save(Automerge.from(initialDoc));
1823
- const handle = repo.import(seeded, { docId: documentId });
1824
- handle.doneLoading();
1825
- return handle;
1826
1893
  };
1827
1894
  }
1895
+ function attachLoadedRejectionSink(primitive) {
1896
+ primitive.loaded.catch((err) => {
1897
+ recordLoadedRejection(err);
1898
+ });
1899
+ return primitive;
1900
+ }
1828
1901
  function $meshState(key, initialValue, options = {}) {
1829
1902
  const repo = resolveRepo(options.repo);
1830
- return $crdtState({
1903
+ return attachLoadedRejectionSink($crdtState({
1831
1904
  key,
1832
1905
  primitive: "meshState",
1833
1906
  initialValue,
@@ -1835,37 +1908,37 @@ function $meshState(key, initialValue, options = {}) {
1835
1908
  schemaVersion: options.schemaVersion,
1836
1909
  migrations: options.migrations,
1837
1910
  access: options.access
1838
- });
1911
+ }));
1839
1912
  }
1840
1913
  function $meshText(key, initialValue, options = {}) {
1841
1914
  const repo = resolveRepo(options.repo);
1842
- return $crdtText(key, initialValue, {
1915
+ return attachLoadedRejectionSink($crdtText(key, initialValue, {
1843
1916
  primitive: "meshState",
1844
1917
  getHandle: buildHandleFactory(repo, key, { text: initialValue }),
1845
1918
  schemaVersion: options.schemaVersion,
1846
1919
  migrations: options.migrations,
1847
1920
  access: options.access
1848
- });
1921
+ }));
1849
1922
  }
1850
1923
  function $meshCounter(key, initialValue, options = {}) {
1851
1924
  const repo = resolveRepo(options.repo);
1852
- return $crdtCounter(key, initialValue, {
1925
+ return attachLoadedRejectionSink($crdtCounter(key, initialValue, {
1853
1926
  primitive: "meshState",
1854
1927
  getHandle: buildHandleFactory(repo, key, {}),
1855
1928
  schemaVersion: options.schemaVersion,
1856
1929
  migrations: options.migrations,
1857
1930
  access: options.access
1858
- });
1931
+ }));
1859
1932
  }
1860
1933
  function $meshList(key, initialValue, options = {}) {
1861
1934
  const repo = resolveRepo(options.repo);
1862
- return $crdtList(key, initialValue, {
1935
+ return attachLoadedRejectionSink($crdtList(key, initialValue, {
1863
1936
  primitive: "meshState",
1864
1937
  getHandle: buildHandleFactory(repo, key, { items: initialValue }),
1865
1938
  schemaVersion: options.schemaVersion,
1866
1939
  migrations: options.migrations,
1867
1940
  access: options.access
1868
- });
1941
+ }));
1869
1942
  }
1870
1943
 
1871
1944
  // src/shared/lib/mesh-webrtc-adapter.ts
@@ -2913,6 +2986,18 @@ function getReevaluateDocumentShare(repo) {
2913
2986
  return;
2914
2987
  return () => fn.call(sync);
2915
2988
  }
2989
+ function buildMeshStateModuleDiagnostics() {
2990
+ return {
2991
+ moduleId: getMeshStateModuleId(),
2992
+ configured: isMeshStateConfigured(),
2993
+ lastConfiguredRepoPeerId: getLastConfiguredRepoPeerId(),
2994
+ wasResolved: wasMeshStateResolved(),
2995
+ lazyInvocations: getLazyInvocations(),
2996
+ lazyReachedRepo: getLazyReachedRepo(),
2997
+ lastLoadedRejection: getLastLoadedRejection(),
2998
+ lazyWrappers: getLazyWrappers()
2999
+ };
3000
+ }
2916
3001
  function installPolly107SyncReevaluation(networkAdapter, repo) {
2917
3002
  const disable = typeof process !== "undefined" && process.env?.["POLLY_107_DISABLE_FIX"] === "1";
2918
3003
  if (disable)
@@ -3022,12 +3107,7 @@ async function createMeshClient(options) {
3022
3107
  lastRunAt: undefined
3023
3108
  },
3024
3109
  peers: [],
3025
- meshStateModule: {
3026
- moduleId: getMeshStateModuleId(),
3027
- configured: isMeshStateConfigured(),
3028
- lastConfiguredRepoPeerId: getLastConfiguredRepoPeerId(),
3029
- wasResolved: wasMeshStateResolved()
3030
- },
3110
+ meshStateModule: buildMeshStateModuleDiagnostics(),
3031
3111
  repoHandleCount: Object.keys(repo.handles).length,
3032
3112
  repoHandleIds: Object.keys(repo.handles)
3033
3113
  };
@@ -3042,12 +3122,7 @@ async function createMeshClient(options) {
3042
3122
  presentPeerIds: base.presentPeerIds,
3043
3123
  sweep: base.sweep,
3044
3124
  peers: enrichedPeers,
3045
- meshStateModule: {
3046
- moduleId: getMeshStateModuleId(),
3047
- configured: isMeshStateConfigured(),
3048
- lastConfiguredRepoPeerId: getLastConfiguredRepoPeerId(),
3049
- wasResolved: wasMeshStateResolved()
3050
- },
3125
+ meshStateModule: buildMeshStateModuleDiagnostics(),
3051
3126
  repoHandleCount: knownHandleIds.length,
3052
3127
  repoHandleIds: knownHandleIds
3053
3128
  };
@@ -3433,6 +3508,10 @@ export {
3433
3508
  isMeshStateConfigured,
3434
3509
  isBlobRef,
3435
3510
  getMeshStateModuleId,
3511
+ getLazyWrappers,
3512
+ getLazyReachedRepo,
3513
+ getLazyInvocations,
3514
+ getLastLoadedRejection,
3436
3515
  getLastConfiguredRepoPeerId,
3437
3516
  generateSigningKeyPair,
3438
3517
  generateDocumentKey,
@@ -3483,4 +3562,4 @@ export {
3483
3562
  $meshCounter
3484
3563
  };
3485
3564
 
3486
- //# debugId=23078FA0DB4C154064756E2164756E21
3565
+ //# debugId=CC67EB2297E1036764756E2164756E21