@@ -12,6 +12,7 @@ import PQueue from 'p-queue'
1212import { DelegatedRoutingV1HttpApiClientContentRouting , DelegatedRoutingV1HttpApiClientPeerRouting } from './routings.js'
1313import type { DelegatedRoutingV1HttpApiClient , DelegatedRoutingV1HttpApiClientInit , PeerRecord } from './index.js'
1414import type { ContentRouting , PeerRouting , AbortOptions , PeerId } from '@libp2p/interface'
15+ import type { Multiaddr } from '@multiformats/multiaddr'
1516import type { CID } from 'multiformats'
1617
1718const log = logger ( 'delegated-routing-v1-http-api-client' )
@@ -68,7 +69,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
6869 this . started = false
6970 }
7071
71- async * getProviders ( cid : CID , options : AbortOptions = { } ) : AsyncGenerator < PeerRecord , any , unknown > {
72+ async * getProviders ( cid : CID , options : AbortOptions = { } ) : AsyncGenerator < PeerRecord > {
7273 log ( 'getProviders starts: %c' , cid )
7374
7475 const signal = anySignal ( [ this . shutDownController . signal , options . signal , AbortSignal . timeout ( this . timeout ) ] )
@@ -109,14 +110,14 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
109110 const body = await res . json ( )
110111
111112 for ( const provider of body . Providers ) {
112- const record = this . #handleProviderRecords ( provider )
113+ const record = this . #conformToPeerSchema ( provider )
113114 if ( record != null ) {
114115 yield record
115116 }
116117 }
117118 } else {
118119 for await ( const provider of ndjson ( toIt ( res . body ) ) ) {
119- const record = this . #handleProviderRecords ( provider )
120+ const record = this . #conformToPeerSchema ( provider )
120121 if ( record != null ) {
121122 yield record
122123 }
@@ -131,7 +132,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
131132 }
132133 }
133134
134- async * getPeers ( peerId : PeerId , options : AbortOptions | undefined = { } ) : AsyncGenerator < PeerRecord , any , unknown > {
135+ async * getPeers ( peerId : PeerId , options : AbortOptions | undefined = { } ) : AsyncGenerator < PeerRecord > {
135136 log ( 'getPeers starts: %c' , peerId )
136137
137138 const signal = anySignal ( [ this . shutDownController . signal , options . signal , AbortSignal . timeout ( this . timeout ) ] )
@@ -172,14 +173,14 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
172173 const body = await res . json ( )
173174
174175 for ( const peer of body . Peers ) {
175- const record = this . #handlePeerRecords ( peerId , peer )
176+ const record = this . #conformToPeerSchema ( peer )
176177 if ( record != null ) {
177178 yield record
178179 }
179180 }
180181 } else {
181182 for await ( const peer of ndjson ( toIt ( res . body ) ) ) {
182- const record = this . #handlePeerRecords ( peerId , peer )
183+ const record = this . #conformToPeerSchema ( peer )
183184 if ( record != null ) {
184185 yield record
185186 }
@@ -272,44 +273,25 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
272273 }
273274 }
274275
275- #handleProviderRecords ( record : any ) : PeerRecord | undefined {
276- if ( record . Schema === 'peer' ) {
277- // Peer schema can have additional, user-defined, fields.
278- record . ID = peerIdFromString ( record . ID )
279- record . Addrs = record . Addrs ?. map ( multiaddr ) ?? [ ]
280- return record
281- }
276+ #conformToPeerSchema ( record : any ) : PeerRecord | undefined {
277+ const protocols : string [ ] = [ ]
278+ const multiaddrs : Multiaddr [ ] = record . Addrs ?. map ( multiaddr ) ?? [ ]
282279
283- if ( record . Schema === 'bitswap' ) {
284- // Bitswap schema is deprecated, was incorrectly used when server had no
285- // information about actual protocols, so we convert it to peer result
286- // without protocol information
287- return {
288- Schema : 'peer' ,
289- ID : peerIdFromString ( record . ID ) ,
290- Addrs : record . Addrs ?. map ( multiaddr ) ?? [ ] ,
291- Protocol : record . Protocol
292- }
280+ if ( record . Protocols != null ) {
281+ protocols . push ( ...record . Protocols )
293282 }
294283
295- if ( record . ID != null && Array . isArray ( record . Addrs ) ) {
296- return {
297- Schema : 'peer' ,
298- ID : peerIdFromString ( record . ID ) ,
299- Addrs : record . Addrs ?. map ( multiaddr ) ?? [ ] ,
300- Protocol : record . Protocol
301- }
284+ if ( record . Protocol != null ) {
285+ protocols . push ( record . Protocol )
286+ delete record . Protocol
302287 }
303- }
304288
305- #handlePeerRecords ( peerId : PeerId , record : any ) : PeerRecord | undefined {
306- if ( record . Schema === 'peer' ) {
307- // Peer schema can have additional, user-defined, fields.
308- record . ID = peerIdFromString ( record . ID )
309- record . Addrs = record . Addrs ?. map ( multiaddr ) ?? [ ]
310- if ( peerId . equals ( record . ID ) ) {
311- return record
312- }
289+ return {
290+ ...record ,
291+ Schema : 'peer' ,
292+ ID : peerIdFromString ( record . ID ) ,
293+ Addrs : multiaddrs ,
294+ Protocols : protocols
313295 }
314296 }
315297}
0 commit comments