Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit bc35a9c

Browse files
committed
fix: code review
1 parent f703c17 commit bc35a9c

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ const ipfs = ipfsClient({
259259
- [`ipfs.bitswap.unwant(cid)`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BITSWAP.md#bitswapunwant)
260260

261261
- [dht](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md)
262-
- [`ipfs.dht.findpeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer)
263-
- [`ipfs.dht.findprovs(hash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs)
262+
- [`ipfs.dht.findPeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer)
263+
- [`ipfs.dht.findProvs(hash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs)
264264
- [`ipfs.dht.get(key, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtget)
265265
- [`ipfs.dht.provide(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtprovide)
266266
- [`ipfs.dht.put(key, value, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtput)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"eslint-plugin-react": "^7.11.1",
8787
"go-ipfs-dep": "~0.4.18",
8888
"gulp": "^3.9.1",
89-
"interface-ipfs-core": "ipfs/interface-ipfs-core#fix/update-dht-responses",
89+
"interface-ipfs-core": "ipfs/interface-ipfs-core#fix/dht-responses",
9090
"ipfsd-ctl": "~0.40.0",
9191
"nock": "^10.0.2",
9292
"pull-stream": "^3.6.9",

src/dht/findpeer.js

+14-25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
const promisify = require('promisify-es6')
44
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
55

6+
const multiaddr = require('multiaddr')
7+
const PeerId = require('peer-id')
8+
const PeerInfo = require('peer-info')
69
const errcode = require('err-code')
710

811
module.exports = (send) => {
@@ -25,40 +28,26 @@ module.exports = (send) => {
2528
res = res[0]
2629
}
2730

28-
// Type 2 keys (inconsistencies between go core and js core)
29-
if (res.Type !== 2 && res.type !== 2) {
31+
// Type 2 keys
32+
if (res.Type !== 2) {
3033
const errMsg = `key was not found (type 2)`
3134

3235
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND'))
3336
}
3437

35-
// inconsistencies between go core and js core
36-
let id
37-
let addrs
38+
const responses = res.Responses.map((r) => {
39+
const peerInfo = new PeerInfo(PeerId.createFromB58String(r.ID))
3840

39-
if (res.Responses) {
40-
id = res.Responses[0].ID
41-
addrs = res.Responses[0].Addrs
42-
} else {
43-
id = res.responses[0].id
44-
addrs = res.responses[0].addrs
45-
}
41+
r.Addrs.forEach((addr) => {
42+
const ma = multiaddr(addr)
4643

47-
// inconsistencies js / go - go does not add `/ipfs/{id}` to the address
48-
addrs = addrs.map((addr) => {
49-
if (addr.split('/ipfs/') > -1) {
50-
return addr
51-
} else {
52-
return `${addr}/ipfs/${id}`
53-
}
54-
})
44+
peerInfo.multiaddrs.add(ma)
45+
})
5546

56-
callback(null, {
57-
responses: [{
58-
id,
59-
addrs
60-
}]
47+
return peerInfo
6148
})
49+
50+
callback(null, responses)
6251
}
6352

6453
send({

src/dht/findprovs.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
const promisify = require('promisify-es6')
44
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
55

6+
const multiaddr = require('multiaddr')
7+
const PeerId = require('peer-id')
8+
const PeerInfo = require('peer-info')
69
const errcode = require('err-code')
710

811
module.exports = (send) => {
@@ -25,23 +28,26 @@ module.exports = (send) => {
2528
res = res[0]
2629
}
2730

28-
// Type 4 keys (inconsistencies between go core and js core)
29-
if (res.Type !== 4 && res.type !== 4) {
31+
// Type 4 keys
32+
if (res.Type !== 4) {
3033
const errMsg = `key was not found (type 4)`
3134

3235
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
3336
}
3437

35-
// inconsistencies between go core and js core
36-
const recResponses = res.Responses || res.responses
38+
const responses = res.Responses.map((r) => {
39+
const peerInfo = new PeerInfo(PeerId.createFromB58String(r.ID))
3740

38-
// providers array (handling inconsistencies)
39-
const responses = recResponses.map((r) => ({
40-
id: r.ID || r.id,
41-
addrs: r.Addrs || r.addrs
42-
}))
41+
r.Addrs.forEach((addr) => {
42+
const ma = multiaddr(addr)
4343

44-
callback(null, { responses })
44+
peerInfo.multiaddrs.add(ma)
45+
})
46+
47+
return peerInfo
48+
})
49+
50+
callback(null, responses)
4551
}
4652

4753
send({

src/dht/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module.exports = (arg) => {
88
return {
99
get: require('./get')(send),
1010
put: require('./put')(send),
11-
findprovs: require('./findprovs')(send),
12-
findpeer: require('./findpeer')(send),
11+
findProvs: require('./findprovs')(send),
12+
findPeer: require('./findpeer')(send),
1313
provide: require('./provide')(send),
1414
// find closest peerId to given peerId
1515
query: require('./query')(send)

test/sub-modules.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ describe('submodules', () => {
4949

5050
expect(dht.get).to.be.a('function')
5151
expect(dht.put).to.be.a('function')
52-
expect(dht.findprovs).to.be.a('function')
53-
expect(dht.findpeer).to.be.a('function')
52+
expect(dht.findProvs).to.be.a('function')
53+
expect(dht.findPeer).to.be.a('function')
5454
expect(dht.provide).to.be.a('function')
5555
expect(dht.query).to.be.a('function')
5656
})

0 commit comments

Comments
 (0)