From 265a77a2b9f99f18ecc96ba7af501f628236f8a8 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 17 Nov 2016 13:05:07 +0100 Subject: [PATCH] feat(swarm): update swarm.peers to new api --- src/core/components/swarm.js | 32 ++++++++++++++++++++++++++++---- src/http-api/resources/swarm.js | 18 ++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/core/components/swarm.js b/src/core/components/swarm.js index d897358ebb..45313a79ce 100644 --- a/src/core/components/swarm.js +++ b/src/core/components/swarm.js @@ -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 diff --git a/src/http-api/resources/swarm.js b/src/http-api/resources/swarm.js index d557d4909d..dc4e5b04f3 100644 --- a/src/http-api/resources/swarm.js +++ b/src/http-api/resources/swarm.js @@ -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({ @@ -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 + }) }) }) }