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

fix: make findprovs return all responses #1041

Merged
merged 3 commits into from
Jul 11, 2019
Merged
Changes from 1 commit
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
39 changes: 18 additions & 21 deletions src/dht/findprovs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,26 @@ module.exports = (send) => {

const handleResult = (res, callback) => {
// Inconsistent return values in the browser vs node
if (Array.isArray(res)) {
res = res.find(r => r.Type === 4)
if (!Array.isArray(res)) {
res = [res]
}

// callback with an empty array if no providers are 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) => {
const peerInfo = new PeerInfo(PeerId.createFromB58String(r.ID))

if (r.Addrs) {
r.Addrs.forEach((addr) => {
const ma = multiaddr(addr)

peerInfo.multiaddrs.add(ma)
})
}

return peerInfo
let responses = []
res.forEach(result => {
// 4 = Provider
if (result.Type !== 4) return
result.Responses.forEach(response => {
const peerInfo = new PeerInfo(PeerId.createFromB58String(response.ID))

if (response.Addrs) {
response.Addrs.forEach((addr) => {
const ma = multiaddr(addr)
peerInfo.multiaddrs.add(ma)
})
}

responses.push(peerInfo)
})
})

callback(null, responses)
Expand Down