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

Commit 79e1c4a

Browse files
committed
fix: dht validate if receiving stream
1 parent 348a144 commit 79e1c4a

File tree

4 files changed

+104
-7
lines changed

4 files changed

+104
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
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",
@@ -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.84.3",
89+
"interface-ipfs-core": "ipfs/interface-ipfs-core#fix/update-dht-responses",
8990
"ipfsd-ctl": "~0.40.0",
9091
"pull-stream": "^3.6.9",
9192
"stream-equal": "^1.1.1"

src/dht/findpeer.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const promisify = require('promisify-es6')
44
const streamToValue = require('../utils/stream-to-value')
55

6+
const errcode = require('err-code')
7+
68
module.exports = (send) => {
79
return promisify((peerId, opts, callback) => {
810
if (typeof opts === 'function' && !callback) {
@@ -17,10 +19,60 @@ module.exports = (send) => {
1719
opts = {}
1820
}
1921

20-
send.andTransform({
22+
const handleResult = (res, callback) => {
23+
// TODO verify - Inconsistent return values in the browser vs node
24+
if (Array.isArray(res)) {
25+
res = res[0]
26+
}
27+
28+
// Type 2 keys
29+
if (res.Type !== 2) {
30+
const errMsg = `key was not found (type 2)`
31+
32+
log.error(errMsg)
33+
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND'))
34+
}
35+
36+
const id = res.Responses[0].ID
37+
const addresses = res.Responses[0].Addrs.map((addr) => {
38+
// TODO inconsistencies node / browser ... go?
39+
if (addr.split('/ipfs/') > - 1) {
40+
return addr
41+
} else {
42+
return `${addr}/ipfs/${id}`
43+
}
44+
})
45+
46+
const response = {
47+
...res,
48+
Responses: [{
49+
ID: id,
50+
Addrs: addresses
51+
}]
52+
}
53+
54+
callback(null, response)
55+
}
56+
57+
send({
2158
path: 'dht/findpeer',
2259
args: peerId,
2360
qs: opts
24-
}, streamToValue, callback)
61+
}, (err, result) => {
62+
if (err) {
63+
return callback(err)
64+
}
65+
66+
if (typeof result.pipe === 'function') {
67+
streamToValue(result, (err, res) => {
68+
if (err) {
69+
return callback(err)
70+
}
71+
handleResult(res, callback)
72+
})
73+
} else {
74+
handleResult(result, callback)
75+
}
76+
})
2577
})
2678
}

src/dht/findprovs.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,44 @@ module.exports = (send) => {
1717
opts = {}
1818
}
1919

20-
send.andTransform({
20+
// TODO create util for the 3?
21+
const handleResult = (res, callback) => {
22+
// TODO VERIFY - Inconsistent return values in the browser vs node
23+
if (Array.isArray(res)) {
24+
res = res[0]
25+
}
26+
27+
// Type 4 keys
28+
if (res.Type !== 4) {
29+
const errMsg = `key was not found (type 4)`
30+
31+
log.error(errMsg)
32+
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
33+
}
34+
35+
callback(null, res)
36+
}
37+
38+
send({
2139
path: 'dht/findprovs',
2240
args: cid,
2341
qs: opts
24-
}, streamToValue, callback)
42+
}, (err, result) => {
43+
// TODO utility
44+
if (err) {
45+
return callback(err)
46+
}
47+
48+
if (typeof result.pipe === 'function') {
49+
streamToValue(result, (err, res) => {
50+
if (err) {
51+
return callback(err)
52+
}
53+
handleResult(res, callback)
54+
})
55+
} else {
56+
handleResult(result, callback)
57+
}
58+
})
2559
})
2660
}

src/dht/query.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ module.exports = (send) => {
1717
opts = {}
1818
}
1919

20-
send.andTransform({
20+
send({
2121
path: 'dht/query',
2222
args: peerId,
2323
qs: opts
24-
}, streamToValue, callback)
24+
}, (err, result) => {
25+
if (err) {
26+
return callback(err)
27+
}
28+
29+
if (typeof result.pipe === 'function') {
30+
streamToValue(result, callback)
31+
} else {
32+
callback(null, result)
33+
}
34+
})
2535
})
2636
}

0 commit comments

Comments
 (0)