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

Commit

Permalink
fix: dht validate if receiving stream
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 13, 2018
1 parent 39f4733 commit 7900d27
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"debug": "^4.1.0",
"detect-node": "^2.0.4",
"end-of-stream": "^1.4.1",
"err-code": "^1.1.2",
"flatmap": "0.0.3",
"glob": "^7.1.3",
"ipfs-block": "~0.8.0",
Expand Down Expand Up @@ -85,7 +86,7 @@
"eslint-plugin-react": "^7.11.1",
"go-ipfs-dep": "~0.4.18",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.86.0",
"interface-ipfs-core": "ipfs/interface-ipfs-core#fix/update-dht-responses",
"ipfsd-ctl": "~0.40.0",
"pull-stream": "^3.6.9",
"stream-equal": "^1.1.1"
Expand Down
48 changes: 45 additions & 3 deletions src/dht/findpeer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')

const errcode = require('err-code')

module.exports = (send) => {
return promisify((peerId, opts, callback) => {
Expand All @@ -17,10 +19,50 @@ module.exports = (send) => {
opts = {}
}

send.andTransform({
const handleResult = (res, callback) => {
// Inconsistent return values in the browser
if (Array.isArray(res)) {
res = res[0]
}

// Type 2 keys
if (res.Type !== 2) {
const errMsg = `key was not found (type 2)`

return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND'))
}

const id = res.Responses[0].ID
const addresses = res.Responses[0].Addrs.map((addr) => {
// inconsistencies js / go - go does not add `/ipfs/{id}` to the address
if (addr.split('/ipfs/') > -1) {
return addr
} else {
return `${addr}/ipfs/${id}`
}
})

const response = {
...res,
Responses: [{
ID: id,
Addrs: addresses
}]
}

callback(null, response)
}

send({
path: 'dht/findpeer',
args: peerId,
qs: opts
}, streamToValue, callback)
}, (err, result) => {
if (err) {
return callback(err)
}

streamToValueWithTransformer(result, handleResult, callback)
})
})
}
30 changes: 27 additions & 3 deletions src/dht/findprovs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')

const errcode = require('err-code')

module.exports = (send) => {
return promisify((cid, opts, callback) => {
Expand All @@ -17,10 +19,32 @@ module.exports = (send) => {
opts = {}
}

send.andTransform({
const handleResult = (res, callback) => {
// Inconsistent return values in the browser vs node
if (Array.isArray(res)) {
res = res[0]
}

// Type 4 keys
if (res.Type !== 4) {
const errMsg = `key was not found (type 4)`

return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
}

callback(null, res)
}

send({
path: 'dht/findprovs',
args: cid,
qs: opts
}, streamToValue, callback)
}, (err, result) => {
if (err) {
return callback(err)
}

streamToValueWithTransformer(result, handleResult, callback)
})
})
}
14 changes: 12 additions & 2 deletions src/dht/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ module.exports = (send) => {
opts = {}
}

send.andTransform({
send({
path: 'dht/query',
args: peerId,
qs: opts
}, streamToValue, callback)
}, (err, result) => {
if (err) {
return callback(err)
}

if (typeof result.pipe === 'function') {
streamToValue(result, callback)
} else {
callback(null, result)
}
})
})
}
18 changes: 18 additions & 0 deletions src/utils/stream-to-value-with-transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const streamToValue = require('./stream-to-value')

function streamToValueWithTransformer (response, transformer, callback) {
if (typeof response.pipe === 'function') {
streamToValue(response, (err, res) => {
if (err) {
return callback(err)
}
transformer(res, callback)
})
} else {
transformer(response, callback)
}
}

module.exports = streamToValueWithTransformer

0 comments on commit 7900d27

Please sign in to comment.