Skip to content

chore/remove-peer-info-from-api #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2020
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const DelegatedPeerRouting = require('libp2p-delegated-peer-routing')
const routing = new DelegatedPeerRouing()

try {
const peerInfo = await routing.findPeer('peerid')
const { id, multiaddrs } = await routing.findPeer('peerid')

console.log('found peer details', peerInfo)
console.log('found peer details', id, multiaddrs)
} catch (err) {
console.error(err)
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
"debug": "^4.1.1",
"ipfs-http-client": "^44.0.0",
"p-queue": "^6.3.0",
"peer-id": "^0.13.11",
"peer-info": "^0.17.5"
"peer-id": "^0.13.11"
},
"contributors": [
"Jacob Heun <[email protected]>",
Expand Down
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const createFindPeer = require('ipfs-http-client/src/dht/find-peer')
const { default: PQueue } = require('p-queue')
const debug = require('debug')
Expand Down Expand Up @@ -36,25 +35,28 @@ class DelegatedPeerRouting {
* @param {PeerID} id
* @param {object} options
* @param {number} options.timeout How long the query can take. Defaults to 30 seconds
* @returns {Promise<PeerInfo>}
* @returns {Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>}
*/
async findPeer (id, options = {}) {
if (PeerId.isPeerId(id)) {
id = id.toB58String()
let idStr = id
if (PeerId.isPeerId(idStr)) {
idStr = id.toB58String()
}

log('findPeer starts: ' + id)

options.timeout = options.timeout || DEFAULT_TIMEOUT

try {
return await this._httpQueue.add(async () => {
const { addrs } = await this.dht.findPeer(id, {
const { addrs } = await this.dht.findPeer(idStr, {
timeout: options.timeout
})

const peerInfo = new PeerInfo(PeerId.createFromCID(id))
addrs.forEach(addr => peerInfo.multiaddrs.add(addr))
return peerInfo
return {
id,
multiaddrs: addrs
}
})
} catch (err) {
if (err.message.includes('not found')) {
Expand Down
24 changes: 15 additions & 9 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@ describe('DelegatedPeerRouting', function () {
describe('findPeers', () => {
it('should be able to find peers via the delegate with a peer id string', async () => {
const opts = delegatedNode.apiAddr.toOptions()

const router = new DelegatedPeerRouting({
protocol: 'http',
port: opts.port,
host: opts.host
})

const peer = await router.findPeer(peerIdToFind.id)
expect(peer).to.exist()
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
const { id, multiaddrs } = await router.findPeer(peerIdToFind.id)
expect(id).to.exist()
expect(multiaddrs).to.exist()
expect(id).to.eql(peerIdToFind.id)
})

it('should be able to find peers via the delegate with a peerid', async () => {
Expand All @@ -130,9 +132,11 @@ describe('DelegatedPeerRouting', function () {
host: opts.host
})

const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id))
expect(peer).to.exist()
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
const { id, multiaddrs } = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id))
expect(id).to.exist()
expect(multiaddrs).to.exist()

expect(id.toB58String()).to.eql(peerIdToFind.id)
})

it('should be able to specify a timeout', async () => {
Expand All @@ -143,9 +147,11 @@ describe('DelegatedPeerRouting', function () {
host: opts.host
})

const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { timeout: 2000 })
expect(peer).to.exist()
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
const { id, multiaddrs } = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { timeout: 2000 })
expect(id).to.exist()
expect(multiaddrs).to.exist()

expect(id.toB58String()).to.eql(peerIdToFind.id)
})

it('should not be able to find peers not on the network', async () => {
Expand Down