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

Commit 390699a

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

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
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: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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 errcode = require('err-code')
57

68
module.exports = (send) => {
79
return promisify((peerId, opts, callback) => {
@@ -17,10 +19,50 @@ module.exports = (send) => {
1719
opts = {}
1820
}
1921

20-
send.andTransform({
22+
const handleResult = (res, callback) => {
23+
// Inconsistent return values in the browser
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+
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND'))
33+
}
34+
35+
const id = res.Responses[0].ID
36+
const addresses = res.Responses[0].Addrs.map((addr) => {
37+
// inconsistencies js / go - go does not add `/ipfs/{id}` to the address
38+
if (addr.split('/ipfs/') > -1) {
39+
return addr
40+
} else {
41+
return `${addr}/ipfs/${id}`
42+
}
43+
})
44+
45+
const response = {
46+
...res,
47+
Responses: [{
48+
ID: id,
49+
Addrs: addresses
50+
}]
51+
}
52+
53+
callback(null, response)
54+
}
55+
56+
send({
2157
path: 'dht/findpeer',
2258
args: peerId,
2359
qs: opts
24-
}, streamToValue, callback)
60+
}, (err, result) => {
61+
if (err) {
62+
return callback(err)
63+
}
64+
65+
streamToValueWithTransformer(result, handleResult, callback)
66+
})
2567
})
2668
}

src/dht/findprovs.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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 errcode = require('err-code')
57

68
module.exports = (send) => {
79
return promisify((cid, opts, callback) => {
@@ -17,10 +19,32 @@ module.exports = (send) => {
1719
opts = {}
1820
}
1921

20-
send.andTransform({
22+
const handleResult = (res, callback) => {
23+
// Inconsistent return values in the browser vs node
24+
if (Array.isArray(res)) {
25+
res = res[0]
26+
}
27+
28+
// Type 4 keys
29+
if (res.Type !== 4) {
30+
const errMsg = `key was not found (type 4)`
31+
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+
if (err) {
44+
return callback(err)
45+
}
46+
47+
streamToValueWithTransformer(result, handleResult, callback)
48+
})
2549
})
2650
}

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
}
Lines changed: 18 additions & 0 deletions
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)