Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 37af8ca

Browse files
committed
fix: code review
1 parent 022c650 commit 37af8ca

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ The core API is grouped into several areas:
595595
- [`ipfs.bootstrap.rm(peer, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BOOTSTRAP.md#bootstraprm)
596596
597597
- [dht](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/)
598-
- [`ipfs.dht.findpeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer)
599-
- [`ipfs.dht.findprovs(multihash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs)
598+
- [`ipfs.dht.findPeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer)
599+
- [`ipfs.dht.findProvs(multihash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs)
600600
- [`ipfs.dht.get(key, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtget)
601601
- [`ipfs.dht.provide(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtprovide)
602602
- [`ipfs.dht.put(key, value, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtput)

src/cli/commands/dht/find-providers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717

1818
handler (argv) {
1919
const opts = {
20-
numProviders: argv['num-providers']
20+
maxNumProviders: argv['num-providers']
2121
}
2222

2323
argv.ipfs.dht.findProvs(argv.key, opts, (err, result) => {

src/core/components/dht.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const PeerInfo = require('peer-info')
77
const CID = require('cids')
88
const each = require('async/each')
99
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')
1115

1216
module.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
})

src/http/api/resources/dht.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const Joi = require('joi')
4+
45
const CID = require('cids')
56

67
const debug = require('debug')
@@ -23,6 +24,13 @@ exports.findPeer = {
2324
if (err) {
2425
log.error(err)
2526

27+
if (err.code === 'ERR_LOOKUP_FAILED') {
28+
return reply({
29+
Message: err.toString(),
30+
Code: 0
31+
}).code(404)
32+
}
33+
2634
return reply({
2735
Message: err.toString(),
2836
Code: 0
@@ -44,15 +52,17 @@ exports.findProvs = {
4452
validate: {
4553
query: Joi.object().keys({
4654
arg: Joi.string().required(),
47-
'num-providers': Joi.number().integer().default(20)
55+
'num-providers': Joi.number().integer().default(20),
56+
timeout: Joi.number()
4857
}).unknown()
4958
},
5059
handler: (request, reply) => {
5160
const ipfs = request.server.app.ipfs
5261
const { arg } = request.query
53-
const cid = new CID(arg)
5462

55-
ipfs.dht.findProvs(cid, request.query, (err, res) => {
63+
request.query.maxNumProviders = request.query['num-providers']
64+
65+
ipfs.dht.findProvs(arg, request.query, (err, res) => {
5666
if (err) {
5767
log.error(err)
5868

@@ -76,7 +86,8 @@ exports.findProvs = {
7686
exports.get = {
7787
validate: {
7888
query: Joi.object().keys({
79-
arg: Joi.string().required()
89+
arg: Joi.string().required(),
90+
timeout: Joi.number()
8091
}).unknown()
8192
},
8293
handler: (request, reply) => {

test/http-api/inject/dht.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ module.exports = (http) => {
2727
})
2828
})
2929

30-
it('returns 500 if peerId is provided as there is no peers in the routing table', (done) => {
30+
it('returns 404 if peerId is provided as there is no peers in the routing table', (done) => {
3131
const peerId = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
3232

3333
api.inject({
3434
method: 'GET',
3535
url: `/api/v0/dht/findpeer?arg=${peerId}`
3636
}, (res) => {
37-
expect(res.statusCode).to.equal(500)
37+
expect(res.statusCode).to.equal(404)
3838
done()
3939
})
4040
})

0 commit comments

Comments
 (0)