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

Commit 05a84a4

Browse files
vasco-santosAlan Shaw
authored and
Alan Shaw
committed
refactor: dht api (#890)
BREAKING CHANGE: DHT API methods renamed and return types changed * `ipfs.dht.findprovs` renamed to `ipfs.dht.findProvs` and returns an array of [PeerInfo](https://github.com/libp2p/js-peer-info) * `ipfs.dht.findpeer` renamed to `ipfs.dht.findPeer` and returns a [PeerInfo](https://github.com/libp2p/js-peer-info) * `ipfs.dht.query` now returns an array of [PeerId](https://github.com/libp2p/js-peer-id) * [More info](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md)
1 parent 0959866 commit 05a84a4

11 files changed

+132
-23
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

+6-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"debug": "^4.1.0",
3737
"detect-node": "^2.0.4",
3838
"end-of-stream": "^1.4.1",
39+
"err-code": "^1.1.2",
3940
"flatmap": "0.0.3",
4041
"glob": "^7.1.3",
4142
"ipfs-block": "~0.8.0",
@@ -47,14 +48,14 @@
4748
"is-stream": "^1.1.0",
4849
"libp2p-crypto": "~0.14.0",
4950
"lodash": "^4.17.11",
50-
"lru-cache": "^4.1.3",
51-
"multiaddr": "^5.0.2",
51+
"lru-cache": "^5.1.1",
52+
"multiaddr": "^6.0.0",
5253
"multibase": "~0.6.0",
5354
"multihashes": "~0.4.14",
5455
"ndjson": "^1.5.0",
5556
"once": "^1.4.0",
5657
"peer-id": "~0.12.0",
57-
"peer-info": "~0.14.1",
58+
"peer-info": "~0.15.0",
5859
"promisify-es6": "^1.0.3",
5960
"pull-defer": "~0.2.3",
6061
"pull-pushable": "^2.2.0",
@@ -66,7 +67,7 @@
6667
"stream-to-pull-stream": "^1.7.2",
6768
"streamifier": "~0.1.1",
6869
"tar-stream": "^1.6.2",
69-
"through2": "^2.0.3"
70+
"through2": "^3.0.0"
7071
},
7172
"engines": {
7273
"node": ">=8.0.0",
@@ -85,7 +86,7 @@
8586
"eslint-plugin-react": "^7.11.1",
8687
"go-ipfs-dep": "~0.4.18",
8788
"gulp": "^3.9.1",
88-
"interface-ipfs-core": "~0.90.0",
89+
"interface-ipfs-core": "~0.91.0",
8990
"ipfsd-ctl": "~0.40.0",
9091
"nock": "^10.0.2",
9192
"pull-stream": "^3.6.9",

src/dht/findpeer.js

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const streamToValue = require('../utils/stream-to-value')
4+
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
5+
6+
const multiaddr = require('multiaddr')
7+
const PeerId = require('peer-id')
8+
const PeerInfo = require('peer-info')
9+
const errcode = require('err-code')
510

611
module.exports = (send) => {
712
return promisify((peerId, opts, callback) => {
@@ -17,10 +22,41 @@ module.exports = (send) => {
1722
opts = {}
1823
}
1924

20-
send.andTransform({
25+
const handleResult = (res, callback) => {
26+
// Inconsistent return values in the browser
27+
if (Array.isArray(res)) {
28+
res = res[0]
29+
}
30+
31+
// Type 2 keys
32+
if (res.Type !== 2) {
33+
const errMsg = `key was not found (type 2)`
34+
35+
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND'))
36+
}
37+
38+
const responseReceived = res.Responses[0]
39+
const peerInfo = new PeerInfo(PeerId.createFromB58String(responseReceived.ID))
40+
41+
responseReceived.Addrs.forEach((addr) => {
42+
const ma = multiaddr(addr)
43+
44+
peerInfo.multiaddrs.add(ma)
45+
})
46+
47+
callback(null, peerInfo)
48+
}
49+
50+
send({
2151
path: 'dht/findpeer',
2252
args: peerId,
2353
qs: opts
24-
}, streamToValue, callback)
54+
}, (err, result) => {
55+
if (err) {
56+
return callback(err)
57+
}
58+
59+
streamToValueWithTransformer(result, handleResult, callback)
60+
})
2561
})
2662
}

src/dht/findprovs.js

+42-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const streamToValue = require('../utils/stream-to-value')
4+
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
5+
6+
const multiaddr = require('multiaddr')
7+
const PeerId = require('peer-id')
8+
const PeerInfo = require('peer-info')
9+
const errcode = require('err-code')
510

611
module.exports = (send) => {
712
return promisify((cid, opts, callback) => {
@@ -17,10 +22,44 @@ module.exports = (send) => {
1722
opts = {}
1823
}
1924

20-
send.andTransform({
25+
const handleResult = (res, callback) => {
26+
// Inconsistent return values in the browser vs node
27+
if (Array.isArray(res)) {
28+
res = res[0]
29+
}
30+
31+
// Type 4 keys
32+
if (res.Type !== 4) {
33+
const errMsg = `key was not found (type 4)`
34+
35+
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
36+
}
37+
38+
const responses = res.Responses.map((r) => {
39+
const peerInfo = new PeerInfo(PeerId.createFromB58String(r.ID))
40+
41+
r.Addrs.forEach((addr) => {
42+
const ma = multiaddr(addr)
43+
44+
peerInfo.multiaddrs.add(ma)
45+
})
46+
47+
return peerInfo
48+
})
49+
50+
callback(null, responses)
51+
}
52+
53+
send({
2154
path: 'dht/findprovs',
2255
args: cid,
2356
qs: opts
24-
}, streamToValue, callback)
57+
}, (err, result) => {
58+
if (err) {
59+
return callback(err)
60+
}
61+
62+
streamToValueWithTransformer(result, handleResult, callback)
63+
})
2564
})
2665
}

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)

src/dht/query.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const streamToValue = require('../utils/stream-to-value')
4+
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
5+
6+
const PeerId = require('peer-id')
7+
const PeerInfo = require('peer-info')
58

69
module.exports = (send) => {
710
return promisify((peerId, opts, callback) => {
@@ -17,10 +20,22 @@ module.exports = (send) => {
1720
opts = {}
1821
}
1922

20-
send.andTransform({
23+
const handleResult = (res, callback) => {
24+
const peerIds = res.map((r) => (new PeerInfo(PeerId.createFromB58String(r.ID))))
25+
26+
callback(null, peerIds)
27+
}
28+
29+
send({
2130
path: 'dht/query',
2231
args: peerId,
2332
qs: opts
24-
}, streamToValue, callback)
33+
}, (err, result) => {
34+
if (err) {
35+
return callback(err)
36+
}
37+
38+
streamToValueWithTransformer(result, handleResult, callback)
39+
})
2540
})
2641
}

src/object/data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const lruOptions = {
88
max: 128
99
}
1010

11-
const cache = LRU(lruOptions)
11+
const cache = new LRU(lruOptions)
1212

1313
module.exports = (send) => {
1414
return promisify((cid, options, callback) => {

src/object/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const lruOptions = {
1111
max: 128
1212
}
1313

14-
const cache = LRU(lruOptions)
14+
const cache = new LRU(lruOptions)
1515

1616
module.exports = (send) => {
1717
return promisify((cid, options, callback) => {

src/object/links.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const lruOptions = {
99
max: 128
1010
}
1111

12-
const cache = LRU(lruOptions)
12+
const cache = new LRU(lruOptions)
1313

1414
module.exports = (send) => {
1515
return promisify((multihash, options, callback) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const streamToValue = require('./stream-to-value')
4+
5+
function streamToValueWithTransformer (response, transformer, callback) {
6+
if (typeof response.pipe === 'function') {
7+
streamToValue(response, (err, res) => {
8+
if (err) {
9+
return callback(err)
10+
}
11+
transformer(res, callback)
12+
})
13+
} else {
14+
transformer(response, callback)
15+
}
16+
}
17+
18+
module.exports = streamToValueWithTransformer

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)