diff --git a/package.json b/package.json index c933244bad..21b1881a24 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "joi": "^9.0.4", "libp2p-ipfs": "^0.14.1", "libp2p-ipfs-browser": "^0.15.1", + "lodash.flatmap": "^4.5.0", "lodash.get": "^4.4.2", "lodash.has": "^4.5.2", "lodash.set": "^4.3.2", diff --git a/src/core/components/swarm.js b/src/core/components/swarm.js index a4410f97b1..465db28c63 100644 --- a/src/core/components/swarm.js +++ b/src/core/components/swarm.js @@ -2,6 +2,7 @@ const multiaddr = require('multiaddr') const promisify = require('promisify-es6') +const flatMap = require('lodash.flatmap') const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR @@ -12,17 +13,7 @@ module.exports = function swarm (self) { return callback(OFFLINE_ERROR) } - const peers = self._libp2pNode.peerBook.getAll() - const mas = [] - Object - .keys(peers) - .forEach((b58Id) => { - peers[b58Id].multiaddrs.forEach((ma) => { - // TODO this should only print the addr we are using - mas.push(ma) - }) - }) - + const mas = collectAddrs(self._libp2pNode.peerBook) callback(null, mas) }), // all the addrs we know @@ -30,17 +21,8 @@ module.exports = function swarm (self) { if (!self.isOnline()) { return callback(OFFLINE_ERROR) } - const peers = self._libp2pNode.peerBook.getAll() - const mas = [] - Object - .keys(peers) - .forEach((b58Id) => { - peers[b58Id].multiaddrs.forEach((ma) => { - // TODO this should only print the addr we are using - mas.push(ma) - }) - }) + const mas = collectAddrs(self._libp2pNode.peerBook) callback(null, mas) }), localAddrs: promisify((callback) => { @@ -78,3 +60,10 @@ module.exports = function swarm (self) { }) } } + +function collectAddrs (book) { + const peers = book.getAll() + return flatMap(Object.keys(peers), (id) => { + return peers[id].multiaddrs + }) +}