From 0cf16f4a44baeea38d1aa7f04072db65647f6900 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 29 Oct 2025 12:48:43 +0200 Subject: [PATCH] feat: expose endpoint property There can be multiple instances of the client in an application so expose a property that holds the endpoint being queried to help trace where data comes from in an application --- packages/client/src/client.ts | 12 ++++++------ packages/client/src/index.ts | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 8220de3..5f2f9ef 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -27,10 +27,10 @@ const defaultValues = { } export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV1HttpApiClient { + public endpoint: URL private started: boolean private readonly httpQueue: PQueue private readonly shutDownController: AbortController - private readonly clientUrl: URL private readonly timeout: number private readonly contentRouting: ContentRouting private readonly peerRouting: PeerRouting @@ -51,7 +51,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV concurrency: init.concurrentRequests ?? defaultValues.concurrentRequests }) this.inFlightRequests = new Map() // Tracks in-flight requests to avoid duplicate requests - this.clientUrl = url instanceof URL ? url : new URL(url) + this.endpoint = url instanceof URL ? url : new URL(url) this.timeout = init.timeout ?? defaultValues.timeout this.filterAddrs = init.filterAddrs this.filterProtocols = init.filterProtocols @@ -118,7 +118,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV await onStart.promise // https://specs.ipfs.tech/routing/http-routing-v1/ - const url = new URL(`${this.clientUrl}routing/v1/providers/${cid}`) + const url = new URL(`${this.endpoint}routing/v1/providers/${cid}`) this.#addFilterParams(url, options.filterAddrs, options.filterProtocols) const getOptions = { headers: { Accept: 'application/x-ndjson' }, signal } @@ -197,7 +197,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV await onStart.promise // https://specs.ipfs.tech/routing/http-routing-v1/ - const url = new URL(`${this.clientUrl}routing/v1/peers/${peerId.toCID().toString()}`) + const url = new URL(`${this.endpoint}routing/v1/peers/${peerId.toCID().toString()}`) this.#addFilterParams(url, options.filterAddrs, options.filterProtocols) const getOptions = { headers: { Accept: 'application/x-ndjson' }, signal } @@ -264,7 +264,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV }) // https://specs.ipfs.tech/routing/http-routing-v1/ - const resource = `${this.clientUrl}routing/v1/ipns/${libp2pKey}` + const resource = `${this.endpoint}routing/v1/ipns/${libp2pKey}` try { await onStart.promise @@ -336,7 +336,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV }) // https://specs.ipfs.tech/routing/http-routing-v1/ - const resource = `${this.clientUrl}routing/v1/ipns/${libp2pKey}` + const resource = `${this.endpoint}routing/v1/ipns/${libp2pKey}` try { await onStart.promise diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index 5659afb..979efc7 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -169,6 +169,11 @@ export type GetProvidersOptions = FilterOptions & AbortOptions export type GetPeersOptions = FilterOptions & AbortOptions export interface DelegatedRoutingV1HttpApiClient { + /** + * Where this client sends routing requests + */ + endpoint: URL + /** * Returns an async generator of {@link PeerRecord}s that can provide the * content for the passed {@link CID}