@le-space/core 0.3.12 → 0.3.14

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 (3) hide show
  1. package/index.d.ts +29 -1
  2. package/index.js +47 -16
  3. package/package.json +3 -3
package/index.d.ts CHANGED
@@ -627,7 +627,22 @@ declare function fetchUcGoPeerMetadata(args: {
627
627
  delayMs?: number;
628
628
  timeoutMs?: number;
629
629
  sleep?: (ms: number) => Promise<void>;
630
- onAttempt?: (payload: unknown, ready: boolean, attempt: number, attempts: number) => void;
630
+ isReady?: (result: {
631
+ payload: unknown;
632
+ ok: boolean;
633
+ status: number | null;
634
+ requestUrl: string;
635
+ }) => boolean;
636
+ onAttempt?: (result: {
637
+ payload: unknown;
638
+ ready: boolean;
639
+ attempt: number;
640
+ attempts: number;
641
+ requestUrl: string;
642
+ ok: boolean;
643
+ status: number | null;
644
+ error?: string | null;
645
+ }) => void;
631
646
  }): Promise<unknown>;
632
647
  declare function verifyUcGoPeerReachability(args: {
633
648
  hostIpv4?: string | null;
@@ -784,6 +799,19 @@ declare function fetchRelayMetadataForRuntime(args: {
784
799
  attempts?: number;
785
800
  delayMs?: number;
786
801
  timeoutMs?: number;
802
+ onAttempt?: (result: {
803
+ payload: unknown;
804
+ ready: boolean;
805
+ attempt: number;
806
+ attempts: number;
807
+ requestUrl: string;
808
+ ok: boolean;
809
+ status: number | null;
810
+ error?: string | null;
811
+ metadataUrl: string | null;
812
+ hostIpv4: string | null;
813
+ setupPort: number;
814
+ }) => void;
787
815
  }): Promise<RelayMetadataShape | null>;
788
816
  declare function reconcileOwnerRelayBootstrapRegistrations(args: {
789
817
  instanceOwnerAddress: string;
package/index.js CHANGED
@@ -1976,14 +1976,15 @@ async function fetchUcGoPeerMetadata(args) {
1976
1976
  const delayMs = Math.max(0, Number(args.delayMs ?? 3e3));
1977
1977
  const sleep = args.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
1978
1978
  let lastPayload = null;
1979
+ let lastError = null;
1979
1980
  for (let attempt = 0; attempt < attempts; attempt += 1) {
1980
1981
  const controller = new AbortController();
1981
1982
  const timeout = setTimeout(
1982
1983
  () => controller.abort(),
1983
1984
  Number(args.timeoutMs ?? 18e4)
1984
1985
  );
1986
+ const requestUrl = args.metadataUrl && args.metadataUrl.trim() ? args.metadataUrl : `http://${args.hostIpv4}:${args.setupPort}/metadata`;
1985
1987
  try {
1986
- const requestUrl = args.metadataUrl && args.metadataUrl.trim() ? args.metadataUrl : `http://${args.hostIpv4}:${args.setupPort}/metadata`;
1987
1988
  const response = await args.fetch(
1988
1989
  requestUrl,
1989
1990
  {
@@ -1992,22 +1993,43 @@ async function fetchUcGoPeerMetadata(args) {
1992
1993
  );
1993
1994
  const payload = await response.json().catch(() => null);
1994
1995
  lastPayload = payload;
1995
- args.onAttempt?.(
1996
- payload,
1997
- Boolean(
1998
- response.ok && payload && typeof payload === "object" && payload.status === "ready"
1999
- ),
2000
- attempt + 1,
2001
- attempts
1996
+ lastError = null;
1997
+ const ready = Boolean(
1998
+ args.isReady ? args.isReady({
1999
+ payload,
2000
+ ok: response.ok,
2001
+ status: response.status,
2002
+ requestUrl
2003
+ }) : response.ok && payload && typeof payload === "object" && payload.status === "ready"
2002
2004
  );
2003
- if (response.ok && payload && typeof payload === "object" && payload.status === "ready") {
2005
+ args.onAttempt?.({
2006
+ payload,
2007
+ ready,
2008
+ attempt: attempt + 1,
2009
+ attempts,
2010
+ requestUrl,
2011
+ ok: response.ok,
2012
+ status: response.status,
2013
+ error: null
2014
+ });
2015
+ if (ready) {
2004
2016
  return payload;
2005
2017
  }
2006
- if (response.status >= 500) {
2007
- throw new Error(
2008
- `Relay metadata request failed: ${response.status} ${typeof payload === "string" ? payload : JSON.stringify(payload ?? {})}`
2009
- );
2010
- }
2018
+ } catch (error) {
2019
+ lastError = error instanceof Error ? error.message : String(error);
2020
+ lastPayload = {
2021
+ error: lastError
2022
+ };
2023
+ args.onAttempt?.({
2024
+ payload: null,
2025
+ ready: false,
2026
+ attempt: attempt + 1,
2027
+ attempts,
2028
+ requestUrl,
2029
+ ok: false,
2030
+ status: null,
2031
+ error: lastError
2032
+ });
2011
2033
  } finally {
2012
2034
  clearTimeout(timeout);
2013
2035
  }
@@ -2016,7 +2038,7 @@ async function fetchUcGoPeerMetadata(args) {
2016
2038
  }
2017
2039
  }
2018
2040
  throw new Error(
2019
- `Relay metadata did not become ready after ${attempts} attempts: ${typeof lastPayload === "string" ? lastPayload : JSON.stringify(lastPayload ?? {})}`
2041
+ `Relay metadata did not become ready after ${attempts} attempts: ${lastError ? lastError : typeof lastPayload === "string" ? lastPayload : JSON.stringify(lastPayload ?? {})}`
2020
2042
  );
2021
2043
  }
2022
2044
  async function verifyUcGoPeerReachability(args) {
@@ -2746,7 +2768,16 @@ async function fetchRelayMetadataForRuntime(args) {
2746
2768
  fetch: args.fetch,
2747
2769
  attempts: args.attempts,
2748
2770
  delayMs: args.delayMs,
2749
- timeoutMs: args.timeoutMs
2771
+ timeoutMs: args.timeoutMs,
2772
+ isReady: (result) => extractRelayMetadata(result.payload) != null,
2773
+ onAttempt: (result) => {
2774
+ args.onAttempt?.({
2775
+ ...result,
2776
+ metadataUrl,
2777
+ hostIpv4: args.runtime.hostIpv4 ?? null,
2778
+ setupPort
2779
+ });
2780
+ }
2750
2781
  }).catch(() => null);
2751
2782
  return extractRelayMetadata(payload);
2752
2783
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@le-space/core",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
4
4
  "description": "Reusable Aleph deployment, runtime, and guest lifecycle logic.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,7 +16,7 @@
16
16
  "access": "public"
17
17
  },
18
18
  "dependencies": {
19
- "@le-space/aleph-bootstrap": "0.3.12",
20
- "@le-space/shared-types": "0.3.12"
19
+ "@le-space/aleph-bootstrap": "0.3.14",
20
+ "@le-space/shared-types": "0.3.14"
21
21
  }
22
22
  }