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

fix: response for findpeer and findprovs #1039

Merged
merged 6 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/dht/findpeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ module.exports = (send) => {
const handleResult = (res, callback) => {
// Inconsistent return values in the browser
if (Array.isArray(res)) {
res = res[0]
res = res.find(r => r.Type === 2)
}

// 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'))
// 2 = FinalPeer
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
if (!res || res.Type !== 2) {
const errMsg = `key was not found (type 4)`
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be ERR_KEY_TYPE_2_NOT_FOUND

}

const responseReceived = res.Responses[0]
Expand All @@ -49,7 +50,7 @@ module.exports = (send) => {

send({
path: 'dht/findpeer',
args: peerId,
args: peerId.toString(),
qs: opts
}, (err, result) => {
if (err) {
Expand Down
19 changes: 6 additions & 13 deletions src/dht/findprovs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const streamToValueWithTransformer = require('../utils/stream-to-value-with-tran
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const errcode = require('err-code')

module.exports = (send) => {
return promisify((cid, opts, callback) => {
Expand All @@ -25,20 +24,14 @@ module.exports = (send) => {
const handleResult = (res, callback) => {
// Inconsistent return values in the browser vs node
if (Array.isArray(res)) {
res = res[0]
res = res.find(r => r.Type === 4)
}

// callback with an empty array if no providers are found
if (!res) {
const responses = []
return callback(null, responses)
}

// 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'))
// 4 = Provider
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L20
if (!res || res.Type !== 4) {
return callback(null, [])
}

const responses = res.Responses.map((r) => {
Expand All @@ -60,7 +53,7 @@ module.exports = (send) => {

send({
path: 'dht/findprovs',
args: cid,
args: cid.toString(),
qs: opts
}, (err, result) => {
if (err) {
Expand Down
4 changes: 4 additions & 0 deletions test/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ describe('interface-ipfs-core tests', () => {
name: 'should provide from one node and find it through another node',
reason: 'FIXME go-ipfs endpoint doesn\'t conform with the others https://github.com/ipfs/go-ipfs/issues/5047'
},
{
name: 'should take options to override timeout config',
reason: 'FIXME go-ipfs does not support a timeout option'
},
// dht.get
{
name: 'should get a value after it was put on another node',
Expand Down