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

Commit

Permalink
fix: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Dec 10, 2018
1 parent f703c17 commit bc35a9c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ const ipfs = ipfsClient({
- [`ipfs.bitswap.unwant(cid)`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BITSWAP.md#bitswapunwant)

- [dht](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md)
- [`ipfs.dht.findpeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer)
- [`ipfs.dht.findprovs(hash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs)
- [`ipfs.dht.findPeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer)
- [`ipfs.dht.findProvs(hash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs)
- [`ipfs.dht.get(key, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtget)
- [`ipfs.dht.provide(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtprovide)
- [`ipfs.dht.put(key, value, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtput)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"eslint-plugin-react": "^7.11.1",
"go-ipfs-dep": "~0.4.18",
"gulp": "^3.9.1",
"interface-ipfs-core": "ipfs/interface-ipfs-core#fix/update-dht-responses",
"interface-ipfs-core": "ipfs/interface-ipfs-core#fix/dht-responses",
"ipfsd-ctl": "~0.40.0",
"nock": "^10.0.2",
"pull-stream": "^3.6.9",
Expand Down
39 changes: 14 additions & 25 deletions src/dht/findpeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const promisify = require('promisify-es6')
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')

const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const errcode = require('err-code')

module.exports = (send) => {
Expand All @@ -25,40 +28,26 @@ module.exports = (send) => {
res = res[0]
}

// Type 2 keys (inconsistencies between go core and js core)
if (res.Type !== 2 && res.type !== 2) {
// Type 2 keys
if (res.Type !== 2) {
const errMsg = `key was not found (type 2)`

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

// inconsistencies between go core and js core
let id
let addrs
const responses = res.Responses.map((r) => {
const peerInfo = new PeerInfo(PeerId.createFromB58String(r.ID))

if (res.Responses) {
id = res.Responses[0].ID
addrs = res.Responses[0].Addrs
} else {
id = res.responses[0].id
addrs = res.responses[0].addrs
}
r.Addrs.forEach((addr) => {
const ma = multiaddr(addr)

// inconsistencies js / go - go does not add `/ipfs/{id}` to the address
addrs = addrs.map((addr) => {
if (addr.split('/ipfs/') > -1) {
return addr
} else {
return `${addr}/ipfs/${id}`
}
})
peerInfo.multiaddrs.add(ma)
})

callback(null, {
responses: [{
id,
addrs
}]
return peerInfo
})

callback(null, responses)
}

send({
Expand Down
26 changes: 16 additions & 10 deletions src/dht/findprovs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const promisify = require('promisify-es6')
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')

const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const errcode = require('err-code')

module.exports = (send) => {
Expand All @@ -25,23 +28,26 @@ module.exports = (send) => {
res = res[0]
}

// Type 4 keys (inconsistencies between go core and js core)
if (res.Type !== 4 && res.type !== 4) {
// Type 4 keys
if (res.Type !== 4) {
const errMsg = `key was not found (type 4)`

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

// inconsistencies between go core and js core
const recResponses = res.Responses || res.responses
const responses = res.Responses.map((r) => {
const peerInfo = new PeerInfo(PeerId.createFromB58String(r.ID))

// providers array (handling inconsistencies)
const responses = recResponses.map((r) => ({
id: r.ID || r.id,
addrs: r.Addrs || r.addrs
}))
r.Addrs.forEach((addr) => {
const ma = multiaddr(addr)

callback(null, { responses })
peerInfo.multiaddrs.add(ma)
})

return peerInfo
})

callback(null, responses)
}

send({
Expand Down
4 changes: 2 additions & 2 deletions src/dht/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = (arg) => {
return {
get: require('./get')(send),
put: require('./put')(send),
findprovs: require('./findprovs')(send),
findpeer: require('./findpeer')(send),
findProvs: require('./findprovs')(send),
findPeer: require('./findpeer')(send),
provide: require('./provide')(send),
// find closest peerId to given peerId
query: require('./query')(send)
Expand Down
4 changes: 2 additions & 2 deletions test/sub-modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ describe('submodules', () => {

expect(dht.get).to.be.a('function')
expect(dht.put).to.be.a('function')
expect(dht.findprovs).to.be.a('function')
expect(dht.findpeer).to.be.a('function')
expect(dht.findProvs).to.be.a('function')
expect(dht.findPeer).to.be.a('function')
expect(dht.provide).to.be.a('function')
expect(dht.query).to.be.a('function')
})
Expand Down

0 comments on commit bc35a9c

Please sign in to comment.