@@ -7,7 +7,11 @@ const PeerInfo = require('peer-info')
77const CID = require ( 'cids' )
88const each = require ( 'async/each' )
99const setImmediate = require ( 'async/setImmediate' )
10- const errCode = require ( 'err-code' )
10+ const errcode = require ( 'err-code' )
11+
12+ const debug = require ( 'debug' )
13+ const log = debug ( 'jsipfs:dht' )
14+ log . error = debug ( 'jsipfs:dht:error' )
1115
1216module . exports = ( self ) => {
1317 return {
@@ -16,7 +20,7 @@ module.exports = (self) => {
1620 *
1721 * @param {Buffer } key
1822 * @param {Object } options - get options
19- * @param {number } options.maxTimeout - optional timeout
23+ * @param {number } options.timeout - optional timeout
2024 * @param {function(Error) } [callback]
2125 * @returns {Promise|void }
2226 */
@@ -30,9 +34,11 @@ module.exports = (self) => {
3034
3135 if ( ! Buffer . isBuffer ( key ) ) {
3236 try {
33- key = ( new CID ( key ) ) . buffer ( )
37+ key = ( new CID ( key ) ) . buffer
3438 } catch ( err ) {
35- return setImmediate ( ( ) => callback ( errCode ( err , 'ERR_INVALID_CID' ) ) )
39+ log . error ( err )
40+
41+ return setImmediate ( ( ) => callback ( errcode ( err , 'ERR_INVALID_CID' ) ) )
3642 }
3743 }
3844
@@ -54,9 +60,11 @@ module.exports = (self) => {
5460 put : promisify ( ( key , value , callback ) => {
5561 if ( ! Buffer . isBuffer ( key ) ) {
5662 try {
57- key = ( new CID ( key ) ) . buffer ( )
63+ key = ( new CID ( key ) ) . buffer
5864 } catch ( err ) {
59- return setImmediate ( ( ) => callback ( errCode ( err , 'ERR_INVALID_CID' ) ) )
65+ log . error ( err )
66+
67+ return setImmediate ( ( ) => callback ( errcode ( err , 'ERR_INVALID_CID' ) ) )
6068 }
6169 }
6270
@@ -68,7 +76,7 @@ module.exports = (self) => {
6876 *
6977 * @param {CID } key - They key to find providers for.
7078 * @param {Object } options - findProviders options
71- * @param {number } options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000)
79+ * @param {number } options.timeout - how long the query should maximally run, in milliseconds (default: 60000)
7280 * @param {number } options.maxNumProviders - maximum number of providers to find
7381 * @param {function(Error, Array<PeerInfo>) } [callback]
7482 * @returns {Promise<PeerInfo>|void }
@@ -80,14 +88,14 @@ module.exports = (self) => {
8088 }
8189
8290 options = options || { }
83- options . timeout = options . maxTimeout // TODO create PR kad-dht
84- options . maxNumProviders = options . numProviders
8591
8692 if ( typeof key === 'string' ) {
8793 try {
8894 key = new CID ( key )
8995 } catch ( err ) {
90- return setImmediate ( ( ) => callback ( errCode ( err , 'ERR_INVALID_CID' ) ) )
96+ log . error ( err )
97+
98+ return setImmediate ( ( ) => callback ( errcode ( err , 'ERR_INVALID_CID' ) ) )
9199 }
92100 }
93101
@@ -98,8 +106,8 @@ module.exports = (self) => {
98106 * Query the DHT for all multiaddresses associated with a `PeerId`.
99107 *
100108 * @param {PeerId } peer - The id of the peer to search for.
101- * @param {function(Error, Array< PeerInfo> ) } [callback]
102- * @returns {Promise<Array< PeerInfo> >|void }
109+ * @param {function(Error, PeerInfo) } [callback]
110+ * @returns {Promise<PeerInfo>|void }
103111 */
104112 findPeer : promisify ( ( peer , callback ) => {
105113 if ( typeof peer === 'string' ) {
@@ -138,11 +146,15 @@ module.exports = (self) => {
138146 }
139147
140148 if ( ! has ) {
141- return callback ( new Error ( 'block(s) not found locally, cannot provide' ) )
149+ const errMsg = 'block(s) not found locally, cannot provide'
150+
151+ log . error ( errMsg )
152+ return callback ( errcode ( errMsg , 'ERR_BLOCK_NOT_FOUND' ) )
142153 }
143154
144155 if ( options . recursive ) {
145156 // TODO: Implement recursive providing
157+ return callback ( errcode ( 'not implemented yet' , 'ERR_NOT_IMPLEMENTED_YET' ) )
146158 } else {
147159 each ( keys , ( cid , cb ) => {
148160 self . libp2p . contentRouting . provide ( cid , cb )
@@ -163,17 +175,18 @@ module.exports = (self) => {
163175 try {
164176 peerId = PeerId . createFromB58String ( peerId )
165177 } catch ( err ) {
178+ log . error ( err )
166179 return callback ( err )
167180 }
168181 }
169182
170183 // TODO expose this method in peerRouting
171184 self . libp2p . _dht . getClosestPeers ( peerId . toBytes ( ) , ( err , peerIds ) => {
172185 if ( err ) {
186+ log . error ( err )
173187 return callback ( err )
174188 }
175189
176- // callback(null, peerIds)
177190 callback ( null , peerIds . map ( ( id ) => new PeerInfo ( id ) ) )
178191 } )
179192 } )
0 commit comments