-
Notifications
You must be signed in to change notification settings - Fork 298
fix: dht validate if receiving stream #890
Changes from 2 commits
f4bfc2b
f703c17
c9dabf2
a750197
ca234e5
7b5ed15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) => { | ||
|
@@ -17,10 +19,58 @@ 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 (inconsistencies between go core and js core) | ||
if (res.Type !== 2 && res.type !== 2) { | ||
const errMsg = `key was not found (type 2)` | ||
|
||
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND')) | ||
} | ||
|
||
// inconsistencies between go core and js core | ||
let id | ||
let addrs | ||
|
||
if (res.Responses) { | ||
id = res.Responses[0].ID | ||
addrs = res.Responses[0].Addrs | ||
} else { | ||
id = res.responses[0].id | ||
addrs = res.responses[0].addrs | ||
} | ||
|
||
// inconsistencies js / go - go does not add `/ipfs/{id}` to the address | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to fix this here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, ipfs/interface-ipfs-core/js/src/dht/findpeer.js#L48 I can change the test in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I'm concerned about is that we're making assumptions about the format of the address. What does go-ipfs return when it's a circuit address? Would that cause a false positive? Ideally both implementations should return the same format! Do you know the reason why go-ipfs returns the addr without the peer ID? Perhaps for a smaller payload? It would be pretty easy for js-ipfs to decapsulate the peer id from the addrs wouldn't it? |
||
addrs = addrs.map((addr) => { | ||
if (addr.split('/ipfs/') > -1) { | ||
return addr | ||
} else { | ||
return `${addr}/ipfs/${id}` | ||
} | ||
}) | ||
|
||
callback(null, { | ||
responses: [{ | ||
id, | ||
addrs | ||
}] | ||
}) | ||
} | ||
|
||
send({ | ||
path: 'dht/findpeer', | ||
args: peerId, | ||
qs: opts | ||
}, streamToValue, callback) | ||
}, (err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
streamToValueWithTransformer(result, handleResult, callback) | ||
}) | ||
}) | ||
} |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies if I wasn't clear,
interface-ipfs-core
should return lowered property names but the HTTP API should be compatible with go-ipfs.Here I expect us to be converting "Uppered" property names to lowered form. In js-ipfs the HTTP API should take the lowered property names from core and "Upper" them to be compatible. I know this is a PITA but this is consistent with all our APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change it! Thanks for clarifying.