@@ -11,7 +11,7 @@ import defer from 'p-defer'
11
11
import PQueue from 'p-queue'
12
12
import { BadResponseError , InvalidRequestError } from './errors.js'
13
13
import { 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'
15
15
import type { ContentRouting , PeerRouting , AbortOptions , PeerId } from '@libp2p/interface'
16
16
import type { Multiaddr } from '@multiformats/multiaddr'
17
17
import type { CID } from 'multiformats'
@@ -31,6 +31,8 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
31
31
private readonly timeout : number
32
32
private readonly contentRouting : ContentRouting
33
33
private readonly peerRouting : PeerRouting
34
+ private readonly filterAddrs ?: string [ ]
35
+ private readonly filterProtocols ?: string [ ]
34
36
35
37
/**
36
38
* Create a new DelegatedContentRouting instance
@@ -44,6 +46,8 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
44
46
} )
45
47
this . clientUrl = url instanceof URL ? url : new URL ( url )
46
48
this . timeout = init . timeout ?? defaultValues . timeout
49
+ this . filterAddrs = init . filterAddrs
50
+ this . filterProtocols = init . filterProtocols
47
51
this . contentRouting = new DelegatedRoutingV1HttpApiClientContentRouting ( this )
48
52
this . peerRouting = new DelegatedRoutingV1HttpApiClientPeerRouting ( this )
49
53
}
@@ -70,7 +74,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
70
74
this . started = false
71
75
}
72
76
73
- async * getProviders ( cid : CID , options : AbortOptions = { } ) : AsyncGenerator < PeerRecord > {
77
+ async * getProviders ( cid : CID , options : GetProvidersOptions = { } ) : AsyncGenerator < PeerRecord > {
74
78
log ( 'getProviders starts: %c' , cid )
75
79
76
80
const timeoutSignal = AbortSignal . timeout ( this . timeout )
@@ -88,9 +92,10 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
88
92
await onStart . promise
89
93
90
94
// 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 )
92
97
const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
93
- const res = await fetch ( resource , getOptions )
98
+ const res = await fetch ( url , getOptions )
94
99
95
100
if ( res . status === 404 ) {
96
101
// https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
@@ -135,7 +140,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
135
140
}
136
141
}
137
142
138
- async * getPeers ( peerId : PeerId , options : AbortOptions | undefined = { } ) : AsyncGenerator < PeerRecord > {
143
+ async * getPeers ( peerId : PeerId , options : GetPeersOptions = { } ) : AsyncGenerator < PeerRecord > {
139
144
log ( 'getPeers starts: %c' , peerId )
140
145
141
146
const timeoutSignal = AbortSignal . timeout ( this . timeout )
@@ -153,9 +158,11 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
153
158
await onStart . promise
154
159
155
160
// 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
+
157
164
const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
158
- const res = await fetch ( resource , getOptions )
165
+ const res = await fetch ( url , getOptions )
159
166
160
167
if ( res . status === 404 ) {
161
168
// https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
@@ -326,4 +333,20 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
326
333
log . error ( 'could not conform record to peer schema' , err )
327
334
}
328
335
}
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
+ }
329
352
}
0 commit comments