@@ -11,7 +11,7 @@ import defer from 'p-defer'
1111import PQueue from 'p-queue'
1212import { BadResponseError , InvalidRequestError } from './errors.js'
1313import { DelegatedRoutingV1HttpApiClientContentRouting , DelegatedRoutingV1HttpApiClientPeerRouting } from './routings.js'
14- import type { DelegatedRoutingV1HttpApiClient , DelegatedRoutingV1HttpApiClientInit , GetIPNSOptions , PeerRecord } from './index.js'
14+ import type { DelegatedRoutingV1HttpApiClient , DelegatedRoutingV1HttpApiClientInit , GetProvidersOptions , GetPeersOptions , GetIPNSOptions , PeerRecord } from './index.js'
1515import type { ContentRouting , PeerRouting , AbortOptions , PeerId } from '@libp2p/interface'
1616import type { Multiaddr } from '@multiformats/multiaddr'
1717import type { CID } from 'multiformats'
@@ -31,6 +31,8 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
3131 private readonly timeout : number
3232 private readonly contentRouting : ContentRouting
3333 private readonly peerRouting : PeerRouting
34+ private readonly filterAddrs ?: string [ ]
35+ private readonly filterProtocols ?: string [ ]
3436
3537 /**
3638 * Create a new DelegatedContentRouting instance
@@ -44,6 +46,8 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
4446 } )
4547 this . clientUrl = url instanceof URL ? url : new URL ( url )
4648 this . timeout = init . timeout ?? defaultValues . timeout
49+ this . filterAddrs = init . filterAddrs
50+ this . filterProtocols = init . filterProtocols
4751 this . contentRouting = new DelegatedRoutingV1HttpApiClientContentRouting ( this )
4852 this . peerRouting = new DelegatedRoutingV1HttpApiClientPeerRouting ( this )
4953 }
@@ -70,7 +74,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
7074 this . started = false
7175 }
7276
73- async * getProviders ( cid : CID , options : AbortOptions = { } ) : AsyncGenerator < PeerRecord > {
77+ async * getProviders ( cid : CID , options : GetProvidersOptions = { } ) : AsyncGenerator < PeerRecord > {
7478 log ( 'getProviders starts: %c' , cid )
7579
7680 const timeoutSignal = AbortSignal . timeout ( this . timeout )
@@ -88,9 +92,10 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
8892 await onStart . promise
8993
9094 // https://specs.ipfs.tech/routing/http-routing-v1/
91- const resource = `${ this . clientUrl } routing/v1/providers/${ cid . toString ( ) } `
95+ const url = new URL ( `${ this . clientUrl } routing/v1/providers/${ cid . toString ( ) } ` )
96+ this . #addFilterParams( url , options . filterAddrs , options . filterProtocols )
9297 const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
93- const res = await fetch ( resource , getOptions )
98+ const res = await fetch ( url , getOptions )
9499
95100 if ( res . status === 404 ) {
96101 // https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
@@ -135,7 +140,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
135140 }
136141 }
137142
138- async * getPeers ( peerId : PeerId , options : AbortOptions | undefined = { } ) : AsyncGenerator < PeerRecord > {
143+ async * getPeers ( peerId : PeerId , options : GetPeersOptions = { } ) : AsyncGenerator < PeerRecord > {
139144 log ( 'getPeers starts: %c' , peerId )
140145
141146 const timeoutSignal = AbortSignal . timeout ( this . timeout )
@@ -153,9 +158,11 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
153158 await onStart . promise
154159
155160 // https://specs.ipfs.tech/routing/http-routing-v1/
156- const resource = `${ this . clientUrl } routing/v1/peers/${ peerId . toCID ( ) . toString ( ) } `
161+ const url = new URL ( `${ this . clientUrl } routing/v1/peers/${ peerId . toCID ( ) . toString ( ) } ` )
162+ this . #addFilterParams( url , options . filterAddrs , options . filterProtocols )
163+
157164 const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
158- const res = await fetch ( resource , getOptions )
165+ const res = await fetch ( url , getOptions )
159166
160167 if ( res . status === 404 ) {
161168 // https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
@@ -326,4 +333,20 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
326333 log . error ( 'could not conform record to peer schema' , err )
327334 }
328335 }
336+
337+ #addFilterParams ( url : URL , filterAddrs ?: string [ ] , filterProtocols ?: string [ ] ) : void {
338+ // IPIP-484 filtering. local options filter precedence over global filter
339+ if ( filterAddrs != null || this . filterAddrs != null ) {
340+ const adressFilter = filterAddrs ?. join ( ',' ) ?? this . filterAddrs ?. join ( ',' ) ?? ''
341+ if ( adressFilter !== '' ) {
342+ url . searchParams . set ( 'filter-addrs' , adressFilter )
343+ }
344+ }
345+ if ( filterProtocols != null || this . filterProtocols != null ) {
346+ const protocolFilter = filterProtocols ?. join ( ',' ) ?? this . filterProtocols ?. join ( ',' ) ?? ''
347+ if ( protocolFilter !== '' ) {
348+ url . searchParams . set ( 'filter-protocols' , protocolFilter )
349+ }
350+ }
351+ }
329352}
0 commit comments