@@ -7,7 +7,11 @@ const PeerInfo = require('peer-info')
7
7
const CID = require ( 'cids' )
8
8
const each = require ( 'async/each' )
9
9
const 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' )
11
15
12
16
module . exports = ( self ) => {
13
17
return {
@@ -16,7 +20,7 @@ module.exports = (self) => {
16
20
*
17
21
* @param {Buffer } key
18
22
* @param {Object } options - get options
19
- * @param {number } options.maxTimeout - optional timeout
23
+ * @param {number } options.timeout - optional timeout
20
24
* @param {function(Error) } [callback]
21
25
* @returns {Promise|void }
22
26
*/
@@ -30,9 +34,11 @@ module.exports = (self) => {
30
34
31
35
if ( ! Buffer . isBuffer ( key ) ) {
32
36
try {
33
- key = ( new CID ( key ) ) . buffer ( )
37
+ key = ( new CID ( key ) ) . buffer
34
38
} 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' ) ) )
36
42
}
37
43
}
38
44
@@ -54,9 +60,11 @@ module.exports = (self) => {
54
60
put : promisify ( ( key , value , callback ) => {
55
61
if ( ! Buffer . isBuffer ( key ) ) {
56
62
try {
57
- key = ( new CID ( key ) ) . buffer ( )
63
+ key = ( new CID ( key ) ) . buffer
58
64
} 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' ) ) )
60
68
}
61
69
}
62
70
@@ -68,7 +76,7 @@ module.exports = (self) => {
68
76
*
69
77
* @param {CID } key - They key to find providers for.
70
78
* @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)
72
80
* @param {number } options.maxNumProviders - maximum number of providers to find
73
81
* @param {function(Error, Array<PeerInfo>) } [callback]
74
82
* @returns {Promise<PeerInfo>|void }
@@ -80,14 +88,14 @@ module.exports = (self) => {
80
88
}
81
89
82
90
options = options || { }
83
- options . timeout = options . maxTimeout // TODO create PR kad-dht
84
- options . maxNumProviders = options . numProviders
85
91
86
92
if ( typeof key === 'string' ) {
87
93
try {
88
94
key = new CID ( key )
89
95
} 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' ) ) )
91
99
}
92
100
}
93
101
@@ -98,8 +106,8 @@ module.exports = (self) => {
98
106
* Query the DHT for all multiaddresses associated with a `PeerId`.
99
107
*
100
108
* @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 }
103
111
*/
104
112
findPeer : promisify ( ( peer , callback ) => {
105
113
if ( typeof peer === 'string' ) {
@@ -138,11 +146,15 @@ module.exports = (self) => {
138
146
}
139
147
140
148
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' ) )
142
153
}
143
154
144
155
if ( options . recursive ) {
145
156
// TODO: Implement recursive providing
157
+ return callback ( errcode ( 'not implemented yet' , 'ERR_NOT_IMPLEMENTED_YET' ) )
146
158
} else {
147
159
each ( keys , ( cid , cb ) => {
148
160
self . libp2p . contentRouting . provide ( cid , cb )
@@ -163,17 +175,18 @@ module.exports = (self) => {
163
175
try {
164
176
peerId = PeerId . createFromB58String ( peerId )
165
177
} catch ( err ) {
178
+ log . error ( err )
166
179
return callback ( err )
167
180
}
168
181
}
169
182
170
183
// TODO expose this method in peerRouting
171
184
self . libp2p . _dht . getClosestPeers ( peerId . toBytes ( ) , ( err , peerIds ) => {
172
185
if ( err ) {
186
+ log . error ( err )
173
187
return callback ( err )
174
188
}
175
189
176
- // callback(null, peerIds)
177
190
callback ( null , peerIds . map ( ( id ) => new PeerInfo ( id ) ) )
178
191
} )
179
192
} )
0 commit comments