33const promisify = require ( 'promisify-es6' )
44const every = require ( 'async/every' )
55const PeerId = require ( 'peer-id' )
6+ const PeerInfo = require ( 'peer-info' )
67const CID = require ( 'cids' )
7- const multihash = require ( 'multihashes' )
88const each = require ( 'async/each' )
99const setImmediate = require ( 'async/setImmediate' )
1010const errCode = require ( 'err-code' )
@@ -15,6 +15,8 @@ module.exports = (self) => {
1515 * Given a key, query the DHT for its best value.
1616 *
1717 * @param {Buffer } key
18+ * @param {Object } options - get options
19+ * @param {number } options.maxTimeout - optional timeout
1820 * @param {function(Error) } [callback]
1921 * @returns {Promise|void }
2022 */
@@ -28,9 +30,9 @@ module.exports = (self) => {
2830
2931 if ( ! Buffer . isBuffer ( key ) ) {
3032 try {
31- key = multihash . fromB58String ( key )
33+ key = ( new CID ( key ) ) . buffer ( )
3234 } catch ( err ) {
33- return callback ( err )
35+ return setImmediate ( ( ) => callback ( errCode ( err , 'ERR_INVALID_CID' ) ) )
3436 }
3537 }
3638
@@ -52,9 +54,9 @@ module.exports = (self) => {
5254 put : promisify ( ( key , value , callback ) => {
5355 if ( ! Buffer . isBuffer ( key ) ) {
5456 try {
55- key = multihash . fromB58String ( key )
57+ key = ( new CID ( key ) ) . buffer ( )
5658 } catch ( err ) {
57- return callback ( err )
59+ return setImmediate ( ( ) => callback ( errCode ( err , 'ERR_INVALID_CID' ) ) )
5860 }
5961 }
6062
@@ -65,17 +67,21 @@ module.exports = (self) => {
6567 * Find peers in the DHT that can provide a specific value, given a key.
6668 *
6769 * @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
6873 * @param {function(Error, Array<PeerInfo>) } [callback]
6974 * @returns {Promise<PeerInfo>|void }
7075 */
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 = { }
7580 }
7681
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
7985
8086 if ( typeof key === 'string' ) {
8187 try {
@@ -85,61 +91,29 @@ module.exports = (self) => {
8591 }
8692 }
8793
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 )
10595 } ) ,
10696
10797 /**
10898 * Query the DHT for all multiaddresses associated with a `PeerId`.
10999 *
110100 * @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 }
113103 */
114- findpeer : promisify ( ( peer , callback ) => {
104+ findPeer : promisify ( ( peer , callback ) => {
115105 if ( typeof peer === 'string' ) {
116106 peer = PeerId . createFromB58String ( peer )
117107 }
118108
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 )
136110 } ) ,
137111
138112 /**
139113 * Announce to the network that we are providing given values.
140114 *
141115 * @param {CID|Array<CID> } keys - The keys that should be announced.
142- * @param {Object } [ options={}]
116+ * @param {Object } options - provide options
143117 * @param {bool } [options.recursive=false] - Provide not only the given object but also all objects linked from it.
144118 * @param {function(Error) } [callback]
145119 * @returns {Promise|void }
@@ -181,15 +155,15 @@ module.exports = (self) => {
181155 * Find the closest peers to a given `PeerId`, by querying the DHT.
182156 *
183157 * @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 }
186160 */
187161 query : promisify ( ( peerId , callback ) => {
188162 if ( typeof peerId === 'string' ) {
189163 try {
190164 peerId = PeerId . createFromB58String ( peerId )
191165 } catch ( err ) {
192- callback ( err )
166+ return callback ( err )
193167 }
194168 }
195169
@@ -198,9 +172,9 @@ module.exports = (self) => {
198172 if ( err ) {
199173 return callback ( err )
200174 }
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 ) ) )
204178 } )
205179 } )
206180 }
0 commit comments