diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index fa3f219..4389738 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -10,7 +10,7 @@ import { ipnsValidator } from 'ipns/validator' import ndjson from 'iterable-ndjson' import defer from 'p-defer' import PQueue from 'p-queue' -import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit, Record, PeerRecord } from './index.js' +import type { DelegatedRoutingV1HttpApiClient, DelegatedRoutingV1HttpApiClientInit, PeerRecord } from './index.js' import type { AbortOptions } from '@libp2p/interface' import type { PeerId } from '@libp2p/interface/peer-id' import type { CID } from 'multiformats' @@ -56,7 +56,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV this.started = false } - async * getProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator { + async * getProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator { log('getProviders starts: %c', cid) const signal = anySignal([this.shutDownController.signal, options.signal, AbortSignal.timeout(this.timeout)]) @@ -86,14 +86,14 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV for (const provider of body.Providers) { const record = this.#handleProviderRecords(provider) - if (record !== null) { + if (record != null) { yield record } } } else { for await (const provider of ndjson(toIt(res.body))) { const record = this.#handleProviderRecords(provider) - if (record !== null) { + if (record != null) { yield record } } @@ -137,14 +137,14 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV for (const peer of body.Peers) { const record = this.#handlePeerRecords(peerId, peer) - if (record !== null) { + if (record != null) { yield record } } } else { for await (const peer of ndjson(toIt(res.body))) { const record = this.#handlePeerRecords(peerId, peer) - if (record !== null) { + if (record != null) { yield record } } @@ -223,25 +223,37 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV } } - #handleProviderRecords (record: any): Record | null { + #handleProviderRecords (record: any): PeerRecord | undefined { if (record.Schema === 'peer') { // Peer schema can have additional, user-defined, fields. record.ID = peerIdFromString(record.ID) record.Addrs = record.Addrs.map(multiaddr) return record - } else if (record.Schema === 'bitswap') { - // Bitswap schema is deprecated, was incorrectly used when server had no information about actual protocols, so we convert it to peer result without protocol information. + } + + if (record.Schema === 'bitswap') { + // Bitswap schema is deprecated, was incorrectly used when server had no + // information about actual protocols, so we convert it to peer result + // without protocol information return { Schema: 'peer', ID: peerIdFromString(record.ID), - Addrs: record.Addrs.map(multiaddr) + Addrs: record.Addrs.map(multiaddr), + Protocols: record.Protocol != null ? [record.Protocol] : [] } } - return null + if (record.ID != null && Array.isArray(record.Addrs)) { + return { + Schema: 'peer', + ID: peerIdFromString(record.ID), + Addrs: record.Addrs.map(multiaddr), + Protocols: Array.isArray(record.Protocols) ? record.Protocols : [] + } + } } - #handlePeerRecords (peerId: PeerId, record: any): PeerRecord | null { + #handlePeerRecords (peerId: PeerId, record: any): PeerRecord | undefined { if (record.Schema === 'peer') { // Peer schema can have additional, user-defined, fields. record.ID = peerIdFromString(record.ID) @@ -250,7 +262,5 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV return record } } - - return null } } diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index c9ec125..876dd96 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -31,15 +31,6 @@ export interface PeerRecord { Protocols?: string[] } -export interface BitswapRecord { - Schema: 'bitswap' - Protocol: string - ID: PeerId - Addrs: Multiaddr[] -} - -export type Record = PeerRecord | BitswapRecord - export interface DelegatedRoutingV1HttpApiClientInit { /** * A concurrency limit to avoid request flood in web browser (default: 4) @@ -59,7 +50,7 @@ export interface DelegatedRoutingV1HttpApiClient { * Returns an async generator of PeerInfos that can provide the content * for the passed CID */ - getProviders(cid: CID, options?: AbortOptions): AsyncGenerator + getProviders(cid: CID, options?: AbortOptions): AsyncGenerator /** * Returns an async generator of PeerInfos for the provided PeerId diff --git a/packages/client/test/index.spec.ts b/packages/client/test/index.spec.ts index ae58ed2..0f2dfd4 100644 --- a/packages/client/test/index.spec.ts +++ b/packages/client/test/index.spec.ts @@ -39,6 +39,9 @@ describe('routing-v1-http-api-client', () => { Metadata: 'gBI=', ID: (await createEd25519PeerId()).toString(), Addrs: ['/ip4/42.42.42.42/tcp/1234'] + }, { + ID: (await createEd25519PeerId()).toString(), + Addrs: ['/ip4/43.43.43.43/tcp/1234'] }] const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')