1
1
'use strict'
2
2
3
3
const debug = require ( 'debug' )
4
-
5
- const dht = require ( 'ipfs-http-client/src/dht ' )
6
- const refs = require ( 'ipfs-http-client/src/refs ' )
7
- const getEndpointConfig = require ( 'ipfs-http-client/src/get-endpoint-config ' )
4
+ const PeerId = require ( 'peer-id' )
5
+ const PeerInfo = require ( 'peer-info ' )
6
+ const createFindProvs = require ( 'ipfs-http-client/src/dht/find-provs ' )
7
+ const createRefs = require ( 'ipfs-http-client/src/refs ' )
8
8
9
9
const { default : PQueue } = require ( 'p-queue' )
10
10
const all = require ( 'it-all' )
11
+ const defer = require ( 'p-defer' )
11
12
12
13
const log = debug ( 'libp2p-delegated-content-routing' )
13
14
log . error = debug ( 'libp2p-delegated-content-routing:error' )
@@ -36,9 +37,9 @@ class DelegatedContentRouting {
36
37
throw new Error ( 'missing self peerId' )
37
38
}
38
39
39
- this . api = Object . assign ( { } , getEndpointConfig ( ) ( ) , DEFAULT_IPFS_API , api )
40
- this . dht = dht ( this . api )
41
- this . refs = refs ( this . api )
40
+ this . api = Object . assign ( { } , DEFAULT_IPFS_API , api )
41
+ this . dht = { findProvs : createFindProvs ( this . api ) }
42
+ this . refs = createRefs ( this . api )
42
43
this . peerId = peerId
43
44
44
45
// limit concurrency to avoid request flood in web browser
@@ -64,18 +65,35 @@ class DelegatedContentRouting {
64
65
* @returns {AsyncIterable<PeerInfo> }
65
66
*/
66
67
async * findProviders ( key , options = { } ) {
67
- const keyString = key . toBaseEncodedString ( )
68
- log ( 'findProviders starts: ' + keyString )
68
+ const keyString = ` ${ key } `
69
+ log ( 'findProviders starts:' , keyString )
69
70
options . timeout = options . timeout || DEFAULT_TIMEOUT
70
71
71
- const results = await this . _httpQueue . add ( ( ) => this . dht . findProvs ( key , {
72
- timeout : `${ options . timeout } ms` // The api requires specification of the time unit (s/ms)
73
- } ) )
72
+ const onStart = defer ( )
73
+ const onFinish = defer ( )
74
+
75
+ this . _httpQueue . add ( ( ) => {
76
+ onStart . resolve ( )
77
+ return onFinish . promise
78
+ } )
79
+
80
+ try {
81
+ await onStart . promise
82
+
83
+ const providers = this . dht . findProvs ( key , {
84
+ numProviders : options . numProviders ,
85
+ timeout : `${ options . timeout } ms` // The api requires specification of the time unit (s/ms)
86
+ } )
74
87
75
- for ( let i = 0 ; i < results . length ; i ++ ) {
76
- yield results [ i ]
88
+ for await ( const { id, addrs } of providers ) {
89
+ const peerInfo = new PeerInfo ( PeerId . createFromCID ( id ) )
90
+ addrs . forEach ( addr => peerInfo . multiaddrs . add ( addr ) )
91
+ yield peerInfo
92
+ }
93
+ } finally {
94
+ onFinish . resolve ( )
95
+ log ( 'findProviders finished:' , keyString )
77
96
}
78
- log ( 'findProviders finished: ' + keyString )
79
97
}
80
98
81
99
/**
@@ -90,12 +108,12 @@ class DelegatedContentRouting {
90
108
* @returns {Promise<void> }
91
109
*/
92
110
async provide ( key ) {
93
- const keyString = key . toBaseEncodedString ( )
94
- log ( 'provide starts: ' + keyString )
111
+ const keyString = ` ${ key } `
112
+ log ( 'provide starts:' , keyString )
95
113
const results = await this . _httpQueueRefs . add ( ( ) =>
96
114
all ( this . refs ( keyString , { recursive : false } ) )
97
115
)
98
- log ( 'provide finished: ' , keyString , results )
116
+ log ( 'provide finished:' , keyString , results )
99
117
}
100
118
}
101
119
0 commit comments