Skip to content
This repository was 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 2 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
11 changes: 4 additions & 7 deletions src/dht/findpeer.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((peerId, opts, callback) => {
Expand All @@ -25,14 +24,12 @@ 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'))
if (!res || res.Type !== 2) {
return callback()
}

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

send({
path: 'dht/findpeer',
args: peerId,
args: peerId.toString(),
qs: opts
}, (err, result) => {
if (err) {
Expand Down
17 changes: 4 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,12 @@ 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'))
if (!res || res.Type !== 4) {
return callback(null, [])
}

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

send({
path: 'dht/findprovs',
args: cid,
args: cid.toString(),
qs: opts
}, (err, result) => {
if (err) {
Expand Down