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

Commit 3e739f0

Browse files
authored
Add missing methods to ipfs.dht and moar tests (#546)
* feat: add remaining methods to ipfs.dht and update interface-ipfs-core which has more tests for it
1 parent c188552 commit 3e739f0

File tree

3 files changed

+109
-46
lines changed

3 files changed

+109
-46
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"coverage-publish": "aegir-coverage publish"
2323
},
2424
"dependencies": {
25-
"async": "^2.2.0",
25+
"async": "^2.3.0",
2626
"bs58": "^4.0.0",
27-
"cids": "~0.4.2",
27+
"cids": "~0.5.0",
2828
"concat-stream": "^1.6.0",
2929
"detect-node": "^2.0.3",
3030
"flatmap": "0.0.3",
@@ -41,15 +41,15 @@
4141
"multipart-stream": "^2.0.1",
4242
"ndjson": "^1.5.0",
4343
"once": "^1.4.0",
44-
"peer-id": "~0.8.6",
45-
"peer-info": "~0.9.0",
44+
"peer-id": "~0.8.7",
45+
"peer-info": "~0.9.2",
4646
"promisify-es6": "^1.0.2",
4747
"pump": "^1.0.2",
4848
"qs": "^6.4.0",
49-
"readable-stream": "1.1.14",
50-
"stream-http": "^2.6.3",
49+
"stream-http": "^2.7.0",
5150
"streamifier": "^0.1.1",
52-
"tar-stream": "^1.5.2"
51+
"tar-stream": "^1.5.2",
52+
"readable-stream": "1.1.14"
5353
},
5454
"engines": {
5555
"node": ">=4.0.0",
@@ -65,8 +65,8 @@
6565
"dirty-chai": "^1.2.2",
6666
"eslint-plugin-react": "^6.10.3",
6767
"gulp": "^3.9.1",
68-
"hapi": "^16.1.0",
69-
"interface-ipfs-core": "~0.26.2",
68+
"hapi": "^16.1.1",
69+
"interface-ipfs-core": "~0.27.0",
7070
"ipfsd-ctl": "~0.20.0",
7171
"pre-commit": "^1.2.2",
7272
"socket.io": "^1.7.3",
@@ -127,4 +127,4 @@
127127
"url": "https://github.com/ipfs/js-ipfs-api/issues"
128128
},
129129
"homepage": "https://github.com/ipfs/js-ipfs-api"
130-
}
130+
}

src/api/dht.js

Lines changed: 91 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,20 @@ const streamToValue = require('../stream-to-value')
55

66
module.exports = (send) => {
77
return {
8-
findprovs: promisify((args, opts, callback) => {
9-
if (typeof opts === 'function' &&
10-
!callback) {
11-
callback = opts
12-
opts = {}
13-
}
14-
15-
// opts is the real callback --
16-
// 'callback' is being injected by promisify
17-
if (typeof opts === 'function' &&
18-
typeof callback === 'function') {
19-
callback = opts
20-
opts = {}
21-
}
22-
23-
const request = {
24-
path: 'dht/findprovs',
25-
args: args,
26-
qs: opts
27-
}
28-
29-
send.andTransform(request, streamToValue, callback)
30-
}),
318
get: promisify((key, opts, callback) => {
32-
if (typeof opts === 'function' &&
33-
!callback) {
9+
if (typeof opts === 'function' && !callback) {
3410
callback = opts
3511
opts = {}
3612
}
3713

3814
// opts is the real callback --
3915
// 'callback' is being injected by promisify
40-
if (typeof opts === 'function' &&
41-
typeof callback === 'function') {
16+
if (typeof opts === 'function' && typeof callback === 'function') {
4217
callback = opts
4318
opts = {}
4419
}
4520

46-
const handleResult = (done, err, res) => {
21+
function handleResult (done, err, res) {
4722
if (err) {
4823
return done(err)
4924
}
@@ -73,17 +48,16 @@ module.exports = (send) => {
7348
qs: opts
7449
}, handleResult.bind(null, callback))
7550
}),
51+
7652
put: promisify((key, value, opts, callback) => {
77-
if (typeof opts === 'function' &&
78-
!callback) {
53+
if (typeof opts === 'function' && !callback) {
7954
callback = opts
8055
opts = {}
8156
}
8257

8358
// opts is the real callback --
8459
// 'callback' is being injected by promisify
85-
if (typeof opts === 'function' &&
86-
typeof callback === 'function') {
60+
if (typeof opts === 'function' && typeof callback === 'function') {
8761
callback = opts
8862
opts = {}
8963
}
@@ -93,6 +67,91 @@ module.exports = (send) => {
9367
args: [key, value],
9468
qs: opts
9569
}, callback)
70+
}),
71+
72+
findprovs: promisify((cid, opts, callback) => {
73+
if (typeof opts === 'function' && !callback) {
74+
callback = opts
75+
opts = {}
76+
}
77+
78+
// opts is the real callback --
79+
// 'callback' is being injected by promisify
80+
if (typeof opts === 'function' && typeof callback === 'function') {
81+
callback = opts
82+
opts = {}
83+
}
84+
85+
send.andTransform({
86+
path: 'dht/findprovs',
87+
args: cid,
88+
qs: opts
89+
}, streamToValue, callback)
90+
}),
91+
92+
findpeer: promisify((peerId, opts, callback) => {
93+
if (typeof opts === 'function' && !callback) {
94+
callback = opts
95+
opts = {}
96+
}
97+
98+
// opts is the real callback --
99+
// 'callback' is being injected by promisify
100+
if (typeof opts === 'function' && typeof callback === 'function') {
101+
callback = opts
102+
opts = {}
103+
}
104+
105+
send.andTransform({
106+
path: 'dht/findpeer',
107+
args: peerId,
108+
qs: opts
109+
}, streamToValue, callback)
110+
}),
111+
112+
provide: promisify((cids, opts, callback) => {
113+
if (typeof opts === 'function' && !callback) {
114+
callback = opts
115+
opts = {}
116+
}
117+
118+
// opts is the real callback --
119+
// 'callback' is being injected by promisify
120+
if (typeof opts === 'function' && typeof callback === 'function') {
121+
callback = opts
122+
opts = {}
123+
}
124+
125+
if (!Array.isArray(cids)) {
126+
cids = [cids]
127+
}
128+
129+
send({
130+
path: 'dht/provide',
131+
args: cids,
132+
qs: opts
133+
}, callback)
134+
}),
135+
136+
// find closest peerId to given peerId
137+
query: promisify((peerId, opts, callback) => {
138+
if (typeof opts === 'function' && !callback) {
139+
callback = opts
140+
opts = {}
141+
}
142+
143+
// opts is the real callback --
144+
// 'callback' is being injected by promisify
145+
if (typeof opts === 'function' && typeof callback === 'function') {
146+
callback = opts
147+
opts = {}
148+
}
149+
150+
send.andTransform({
151+
path: 'dht/query',
152+
args: peerId,
153+
qs: opts
154+
}, streamToValue, callback)
96155
})
97156
}
98157
}

src/stream-to-value.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ const concat = require('concat-stream')
77
Concatenate a stream to a single value.
88
*/
99
function streamToValue (res, callback) {
10-
const done = (data) => callback(null, data)
11-
pump(res, concat(done), (err) => {
12-
if (err) callback(err)
13-
})
10+
pump(
11+
res,
12+
concat((data) => callback(null, data)),
13+
(err) => {
14+
if (err) {
15+
callback(err)
16+
}
17+
})
1418
}
1519

1620
module.exports = streamToValue

0 commit comments

Comments
 (0)