@@ -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 [ ] | undefined
35+ private readonly filterProtocols : string [ ] | undefined
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,22 @@ 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+ // IPIP-484 filtering. options takes precedence over global filter
97+ if ( options . filterAddrs != null || this . filterAddrs != null ) {
98+ const filterAddrs = options . filterAddrs ?. join ( ',' ) ?? this . filterAddrs ?. join ( ',' ) ?? ''
99+ if ( filterAddrs !== '' ) {
100+ url . searchParams . set ( 'filter-addrs' , filterAddrs )
101+ }
102+ }
103+ if ( options . filterProtocols != null || this . filterProtocols != null ) {
104+ const filterProtocols = options . filterProtocols ?. join ( ',' ) ?? this . filterProtocols ?. join ( ',' ) ?? ''
105+ if ( filterProtocols !== '' ) {
106+ url . searchParams . set ( 'filter-protocols' , filterProtocols )
107+ }
108+ }
92109 const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
93- const res = await fetch ( resource , getOptions )
110+ const res = await fetch ( url , getOptions )
94111
95112 if ( res . status === 404 ) {
96113 // https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
@@ -135,7 +152,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
135152 }
136153 }
137154
138- async * getPeers ( peerId : PeerId , options : AbortOptions | undefined = { } ) : AsyncGenerator < PeerRecord > {
155+ async * getPeers ( peerId : PeerId , options : GetPeersOptions = { } ) : AsyncGenerator < PeerRecord > {
139156 log ( 'getPeers starts: %c' , peerId )
140157
141158 const timeoutSignal = AbortSignal . timeout ( this . timeout )
@@ -153,9 +170,23 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
153170 await onStart . promise
154171
155172 // https://specs.ipfs.tech/routing/http-routing-v1/
156- const resource = `${ this . clientUrl } routing/v1/peers/${ peerId . toCID ( ) . toString ( ) } `
173+ const url = new URL ( `${ this . clientUrl } routing/v1/peers/${ peerId . toCID ( ) . toString ( ) } ` )
174+
175+ // IPIP-484 filtering. local options filter precedence over global filter
176+ if ( options . filterAddrs != null || this . filterAddrs != null ) {
177+ const filterAddrs = options . filterAddrs ?. join ( ',' ) ?? this . filterAddrs ?. join ( ',' ) ?? ''
178+ if ( filterAddrs !== '' ) {
179+ url . searchParams . set ( 'filter-addrs' , filterAddrs )
180+ }
181+ }
182+ if ( options . filterProtocols != null || this . filterProtocols != null ) {
183+ const filterProtocols = options . filterProtocols ?. join ( ',' ) ?? this . filterProtocols ?. join ( ',' ) ?? ''
184+ if ( filterProtocols !== '' ) {
185+ url . searchParams . set ( 'filter-protocols' , filterProtocols )
186+ }
187+ }
157188 const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
158- const res = await fetch ( resource , getOptions )
189+ const res = await fetch ( url , getOptions )
159190
160191 if ( res . status === 404 ) {
161192 // https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
0 commit comments