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

Commit bc9f037

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

File tree

5 files changed

+103
-7
lines changed

5 files changed

+103
-7
lines changed

package.json

+2-1
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

+46-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
const promisify = require('promisify-es6')
44
const streamToValue = require('../utils/stream-to-value')
5+
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
6+
7+
const errcode = require('err-code')
58

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

20-
send.andTransform({
23+
const handleResult = (res, callback) => {
24+
// Inconsistent return values in the browser
25+
if (Array.isArray(res)) {
26+
res = res[0]
27+
}
28+
29+
// Type 2 keys
30+
if (res.Type !== 2) {
31+
const errMsg = `key was not found (type 2)`
32+
33+
log.error(errMsg)
34+
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND'))
35+
}
36+
37+
const id = res.Responses[0].ID
38+
const addresses = res.Responses[0].Addrs.map((addr) => {
39+
// inconsistencies js / go - go does not add `/ipfs/{id}` to the address
40+
if (addr.split('/ipfs/') > - 1) {
41+
return addr
42+
} else {
43+
return `${addr}/ipfs/${id}`
44+
}
45+
})
46+
47+
const response = {
48+
...res,
49+
Responses: [{
50+
ID: id,
51+
Addrs: addresses
52+
}]
53+
}
54+
55+
callback(null, response)
56+
}
57+
58+
send({
2159
path: 'dht/findpeer',
2260
args: peerId,
2361
qs: opts
24-
}, streamToValue, callback)
62+
}, (err, result) => {
63+
if (err) {
64+
return callback(err)
65+
}
66+
67+
streamToValueWithTransformer(result, handleResult, callback)
68+
})
2569
})
2670
}

src/dht/findprovs.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,33 @@ module.exports = (send) => {
1717
opts = {}
1818
}
1919

20-
send.andTransform({
20+
const handleResult = (res, callback) => {
21+
// Inconsistent return values in the browser vs node
22+
if (Array.isArray(res)) {
23+
res = res[0]
24+
}
25+
26+
// Type 4 keys
27+
if (res.Type !== 4) {
28+
const errMsg = `key was not found (type 4)`
29+
30+
log.error(errMsg)
31+
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
32+
}
33+
34+
callback(null, res)
35+
}
36+
37+
send({
2138
path: 'dht/findprovs',
2239
args: cid,
2340
qs: opts
24-
}, streamToValue, callback)
41+
}, (err, result) => {
42+
if (err) {
43+
return callback(err)
44+
}
45+
46+
streamToValueWithTransformer(result, handleResult, callback)
47+
})
2548
})
2649
}

src/dht/query.js

+12-2
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
}
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

0 commit comments

Comments
 (0)