Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat(swarm): update swarm.peers to new api
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Nov 24, 2016
1 parent db550a1 commit 265a77a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
32 changes: 28 additions & 4 deletions src/core/components/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,41 @@ const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR

module.exports = function swarm (self) {
return {
peers: promisify((callback) => {
peers: promisify((opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}

if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
}

const verbose = opts.v || opts.verbose
// TODO: return latency and streams when verbose is set
// we currently don't have this information

const peers = self._libp2pNode.peerBook.getAll()
const mas = flatMap(Object.keys(peers), (id) => {
return peers[id].multiaddrs
const keys = Object.keys(peers)

const peerList = flatMap(keys, (id) => {
const peer = peers[id]

return peer.multiaddrs.map((addr) => {
const res = {
addr: addr,
peer: peers[id]
}

if (verbose) {
res.latency = 'unknown'
}

return res
})
})

callback(null, mas)
callback(null, peerList)
}),

// all the addrs we know
Expand Down
18 changes: 16 additions & 2 deletions src/http-api/resources/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ exports.parseAddrs = (request, reply) => {

exports.peers = {
handler: (request, reply) => {
const rawVerbose = request.query.v || request.query.verbose
const verbose = rawVerbose === 'true'
const ipfs = request.server.app.ipfs
ipfs.swarm.peers((err, peers) => {

ipfs.swarm.peers({verbose: verbose}, (err, peers) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -37,7 +40,18 @@ exports.peers = {
}

return reply({
Strings: peers.map((addr) => addr.toString())
Peers: peers.map((p) => {
const res = {
Peer: p.peer.id.toB58String(),
Addr: p.addr.toString()
}

if (verbose) {
res.Latency = p.latency
}

return res
})
})
})
}
Expand Down

0 comments on commit 265a77a

Please sign in to comment.