js-libp2p/src/peer-routing.js
Vasco Santos fc22c36ba7
refactor: async routing (#489)
* feat: async routing

* chore: put dht extra api commands under content routing

* chore: add default option to createPeerInfo

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* chore: address review

* chore: rm dlv
2019-12-12 10:29:02 +01:00

41 lines
1.1 KiB
JavaScript

'use strict'
const errCode = require('err-code')
const pAny = require('p-any')
module.exports = (node) => {
const routers = node._modules.peerRouting || []
// If we have the dht, make it first
if (node._dht) {
routers.unshift(node._dht)
}
return {
/**
* Iterates over all peer routers in series to find the given peer.
*
* @param {String} id The id of the peer to find
* @param {object} [options]
* @param {number} [options.timeout] How long the query should run
* @returns {Promise<PeerInfo>}
*/
findPeer: async (id, options) => { // eslint-disable-line require-await
if (!routers.length) {
throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
}
return pAny(routers.map(async (router) => {
const result = await router.findPeer(id, options)
// If we don't have a result, we need to provide an error to keep trying
if (!result || Object.keys(result).length === 0) {
throw errCode(new Error('not found'), 'NOT_FOUND')
}
return result
}))
}
}
}