@@ -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 [ ] | undefined
35
+ private readonly filterProtocols : string [ ] | undefined
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,22 @@ 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
+ // 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
+ }
92
109
const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
93
- const res = await fetch ( resource , getOptions )
110
+ const res = await fetch ( url , getOptions )
94
111
95
112
if ( res . status === 404 ) {
96
113
// https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
@@ -135,7 +152,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
135
152
}
136
153
}
137
154
138
- async * getPeers ( peerId : PeerId , options : AbortOptions | undefined = { } ) : AsyncGenerator < PeerRecord > {
155
+ async * getPeers ( peerId : PeerId , options : GetPeersOptions = { } ) : AsyncGenerator < PeerRecord > {
139
156
log ( 'getPeers starts: %c' , peerId )
140
157
141
158
const timeoutSignal = AbortSignal . timeout ( this . timeout )
@@ -153,9 +170,23 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
153
170
await onStart . promise
154
171
155
172
// 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
+ }
157
188
const getOptions = { headers : { Accept : 'application/x-ndjson' } , signal }
158
- const res = await fetch ( resource , getOptions )
189
+ const res = await fetch ( url , getOptions )
159
190
160
191
if ( res . status === 404 ) {
161
192
// https://specs.ipfs.tech/routing/http-routing-v1/#response-status-codes
0 commit comments