3
3
const promisify = require ( 'promisify-es6' )
4
4
const every = require ( 'async/every' )
5
5
const PeerId = require ( 'peer-id' )
6
+ const PeerInfo = require ( 'peer-info' )
6
7
const CID = require ( 'cids' )
7
- const multihash = require ( 'multihashes' )
8
8
const each = require ( 'async/each' )
9
9
const setImmediate = require ( 'async/setImmediate' )
10
10
const errCode = require ( 'err-code' )
@@ -15,6 +15,8 @@ module.exports = (self) => {
15
15
* Given a key, query the DHT for its best value.
16
16
*
17
17
* @param {Buffer } key
18
+ * @param {Object } options - get options
19
+ * @param {number } options.maxTimeout - optional timeout
18
20
* @param {function(Error) } [callback]
19
21
* @returns {Promise|void }
20
22
*/
@@ -28,9 +30,9 @@ module.exports = (self) => {
28
30
29
31
if ( ! Buffer . isBuffer ( key ) ) {
30
32
try {
31
- key = multihash . fromB58String ( key )
33
+ key = ( new CID ( key ) ) . buffer ( )
32
34
} catch ( err ) {
33
- return callback ( err )
35
+ return setImmediate ( ( ) => callback ( errCode ( err , 'ERR_INVALID_CID' ) ) )
34
36
}
35
37
}
36
38
@@ -52,9 +54,9 @@ module.exports = (self) => {
52
54
put : promisify ( ( key , value , callback ) => {
53
55
if ( ! Buffer . isBuffer ( key ) ) {
54
56
try {
55
- key = multihash . fromB58String ( key )
57
+ key = ( new CID ( key ) ) . buffer ( )
56
58
} catch ( err ) {
57
- return callback ( err )
59
+ return setImmediate ( ( ) => callback ( errCode ( err , 'ERR_INVALID_CID' ) ) )
58
60
}
59
61
}
60
62
@@ -65,17 +67,21 @@ module.exports = (self) => {
65
67
* Find peers in the DHT that can provide a specific value, given a key.
66
68
*
67
69
* @param {CID } key - They key to find providers for.
70
+ * @param {Object } options - findProviders options
71
+ * @param {number } options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000)
72
+ * @param {number } options.maxNumProviders - maximum number of providers to find
68
73
* @param {function(Error, Array<PeerInfo>) } [callback]
69
74
* @returns {Promise<PeerInfo>|void }
70
75
*/
71
- findprovs : promisify ( ( key , opts , callback ) => {
72
- if ( typeof opts === 'function' ) {
73
- callback = opts
74
- opts = { }
76
+ findProvs : promisify ( ( key , options , callback ) => {
77
+ if ( typeof options === 'function' ) {
78
+ callback = options
79
+ options = { }
75
80
}
76
81
77
- opts = opts || { }
78
- opts . maxNumProviders = opts [ 'num-providers' ]
82
+ options = options || { }
83
+ options . timeout = options . maxTimeout // TODO create PR kad-dht
84
+ options . maxNumProviders = options . numProviders
79
85
80
86
if ( typeof key === 'string' ) {
81
87
try {
@@ -85,61 +91,29 @@ module.exports = (self) => {
85
91
}
86
92
}
87
93
88
- self . libp2p . contentRouting . findProviders ( key , opts , ( err , res ) => {
89
- if ( err ) {
90
- return callback ( err )
91
- }
92
-
93
- // convert to go-ipfs return value, we need to revisit
94
- // this. For now will just conform.
95
- const goResult = {
96
- responses : res . map ( ( peerInfo ) => ( {
97
- id : peerInfo . id . toB58String ( ) ,
98
- addrs : peerInfo . multiaddrs . toArray ( ) . map ( ( a ) => a . toString ( ) )
99
- } ) ) ,
100
- type : 4
101
- }
102
-
103
- callback ( null , goResult )
104
- } )
94
+ self . libp2p . contentRouting . findProviders ( key , options , callback )
105
95
} ) ,
106
96
107
97
/**
108
98
* Query the DHT for all multiaddresses associated with a `PeerId`.
109
99
*
110
100
* @param {PeerId } peer - The id of the peer to search for.
111
- * @param {function(Error, Array<Multiaddr >) } [callback]
112
- * @returns {Promise<Array<Multiaddr >>|void }
101
+ * @param {function(Error, Array<PeerInfo >) } [callback]
102
+ * @returns {Promise<Array<PeerInfo >>|void }
113
103
*/
114
- findpeer : promisify ( ( peer , callback ) => {
104
+ findPeer : promisify ( ( peer , callback ) => {
115
105
if ( typeof peer === 'string' ) {
116
106
peer = PeerId . createFromB58String ( peer )
117
107
}
118
108
119
- self . libp2p . peerRouting . findPeer ( peer , ( err , info ) => {
120
- if ( err ) {
121
- return callback ( err )
122
- }
123
-
124
- // convert to go-ipfs return value, we need to revisit
125
- // this. For now will just conform.
126
- const goResult = {
127
- responses : [ {
128
- id : info . id . toB58String ( ) ,
129
- addrs : info . multiaddrs . toArray ( ) . map ( ( a ) => a . toString ( ) )
130
- } ] ,
131
- type : 2
132
- }
133
-
134
- callback ( null , goResult )
135
- } )
109
+ self . libp2p . peerRouting . findPeer ( peer , callback )
136
110
} ) ,
137
111
138
112
/**
139
113
* Announce to the network that we are providing given values.
140
114
*
141
115
* @param {CID|Array<CID> } keys - The keys that should be announced.
142
- * @param {Object } [ options={}]
116
+ * @param {Object } options - provide options
143
117
* @param {bool } [options.recursive=false] - Provide not only the given object but also all objects linked from it.
144
118
* @param {function(Error) } [callback]
145
119
* @returns {Promise|void }
@@ -181,15 +155,15 @@ module.exports = (self) => {
181
155
* Find the closest peers to a given `PeerId`, by querying the DHT.
182
156
*
183
157
* @param {PeerId } peer - The `PeerId` to run the query agains.
184
- * @param {function(Error, Array<PeerId >) } [callback]
185
- * @returns {Promise<Array<PeerId >>|void }
158
+ * @param {function(Error, Array<PeerInfo >) } [callback]
159
+ * @returns {Promise<Array<PeerInfo >>|void }
186
160
*/
187
161
query : promisify ( ( peerId , callback ) => {
188
162
if ( typeof peerId === 'string' ) {
189
163
try {
190
164
peerId = PeerId . createFromB58String ( peerId )
191
165
} catch ( err ) {
192
- callback ( err )
166
+ return callback ( err )
193
167
}
194
168
}
195
169
@@ -198,9 +172,9 @@ module.exports = (self) => {
198
172
if ( err ) {
199
173
return callback ( err )
200
174
}
201
- callback ( null , peerIds . map ( ( id ) => {
202
- return { ID : id . toB58String ( ) }
203
- } ) )
175
+
176
+ // callback(null, peerIds)
177
+ callback ( null , peerIds . map ( ( id ) => new PeerInfo ( id ) ) )
204
178
} )
205
179
} )
206
180
}
0 commit comments