@calimero-network/mero-js 26.4.1 → 26.5.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/dist/index.cjs CHANGED
@@ -73,6 +73,7 @@ __export(index_exports, {
73
73
  RelayObserver: () => RelayObserver,
74
74
  RelayPresenceClient: () => RelayPresenceClient,
75
75
  RelayTransport: () => RelayTransport,
76
+ RelaysUnavailableError: () => RelaysUnavailableError,
76
77
  RpcClient: () => RpcClient,
77
78
  RpcError: () => RpcError,
78
79
  SEALED_CONTENT_TYPE: () => SEALED_CONTENT_TYPE,
@@ -9865,24 +9866,53 @@ function knownRelays(s) {
9865
9866
  const all = [s.relayUrl, ...Object.values(map.namespaces), ...Object.values(map.contexts)].filter((u) => typeof u === "string" && u.length > 0).map(relayOrigin);
9866
9867
  return [...new Set(all)];
9867
9868
  }
9869
+ var RelaysUnavailableError = class extends Error {
9870
+ constructor(failures) {
9871
+ super(`none of this account's relays answered: ${failures.map((f) => `${f.relayUrl} (${f.kind})`).join(", ")}`);
9872
+ this.failures = failures;
9873
+ this.name = "RelaysUnavailableError";
9874
+ this.relays = failures.map((f) => f.relayUrl);
9875
+ }
9876
+ /** Every relay refused its attestation: retrying will not help. */
9877
+ get refused() {
9878
+ return this.failures.every((f) => f.kind === "refused");
9879
+ }
9880
+ };
9881
+ async function listingClient(s, relayUrl) {
9882
+ const key = await attemptRelayNodeKey(relayUrl);
9883
+ if (key.kind !== "learned") {
9884
+ console.warn("[mero-js] could not learn the relay node key from its attestation", key.error);
9885
+ return { failure: { relayUrl, kind: key.kind, error: key.error } };
9886
+ }
9887
+ const client2 = buildDelegatedClient({ ...s, relayUrl }, null);
9888
+ if (!client2) return { failure: { relayUrl, kind: "unavailable", error: new Error(`no client for ${relayUrl}`) } };
9889
+ return { client: client2 };
9890
+ }
9868
9891
  async function listDelegatedContexts(s) {
9869
9892
  const out = [];
9870
9893
  const map = readRelayMap(s.account);
9871
- for (const relayUrl of knownRelays(s)) {
9894
+ const relays = knownRelays(s);
9895
+ const failures = [];
9896
+ for (const relayUrl of relays) {
9897
+ const got = await listingClient(s, relayUrl);
9898
+ if ("failure" in got) {
9899
+ failures.push(got.failure);
9900
+ continue;
9901
+ }
9902
+ const { client: client2 } = got;
9872
9903
  try {
9873
- if (!await resolveRelayNodeKey(relayUrl)) continue;
9874
- const client2 = buildDelegatedClient({ ...s, relayUrl }, null);
9875
- if (!client2) continue;
9876
9904
  const { contexts } = await client2.admin.getContexts();
9877
9905
  for (const c of contexts ?? []) {
9878
9906
  out.push({ ...c, relayUrl });
9879
9907
  map.contexts[c.id] = relayUrl;
9880
9908
  }
9881
9909
  } catch (e) {
9910
+ failures.push({ relayUrl, kind: "unavailable", error: e });
9882
9911
  console.warn(`[mero-js] could not list contexts on ${relayUrl}`, e);
9883
9912
  }
9884
9913
  }
9885
9914
  writeRelayMap(s.account, map);
9915
+ if (relays.length > 0 && failures.length === relays.length) throw new RelaysUnavailableError(failures);
9886
9916
  return out;
9887
9917
  }
9888
9918
  async function listDelegatedNamespaces(s) {
@@ -9890,11 +9920,16 @@ async function listDelegatedNamespaces(s) {
9890
9920
  const out = [];
9891
9921
  const seen = /* @__PURE__ */ new Set();
9892
9922
  const map = readRelayMap(s.account);
9893
- for (const relayUrl of knownRelays(s)) {
9923
+ const relays = knownRelays(s);
9924
+ const failures = [];
9925
+ for (const relayUrl of relays) {
9926
+ const got = await listingClient(s, relayUrl);
9927
+ if ("failure" in got) {
9928
+ failures.push(got.failure);
9929
+ continue;
9930
+ }
9931
+ const { client: client2 } = got;
9894
9932
  try {
9895
- if (!await resolveRelayNodeKey(relayUrl)) continue;
9896
- const client2 = buildDelegatedClient({ ...s, relayUrl }, null);
9897
- if (!client2) continue;
9898
9933
  for (const ns of await client2.admin.listNamespaces()) {
9899
9934
  if (seen.has(ns.namespaceId)) continue;
9900
9935
  seen.add(ns.namespaceId);
@@ -9902,10 +9937,12 @@ async function listDelegatedNamespaces(s) {
9902
9937
  (_a = map.namespaces)[_b = ns.namespaceId] ?? (_a[_b] = relayUrl);
9903
9938
  }
9904
9939
  } catch (e) {
9940
+ failures.push({ relayUrl, kind: "unavailable", error: e });
9905
9941
  console.warn(`[mero-js] could not list namespaces on ${relayUrl}`, e);
9906
9942
  }
9907
9943
  }
9908
9944
  writeRelayMap(s.account, map);
9945
+ if (relays.length > 0 && failures.length === relays.length) throw new RelaysUnavailableError(failures);
9909
9946
  return out;
9910
9947
  }
9911
9948