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

Commit

Permalink
style(core): cleanup libp2p commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Sep 15, 2016
1 parent 0e99029 commit d3f98fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 10 additions & 21 deletions src/core/components/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -12,35 +13,16 @@ 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
addrs: promisify((callback) => {
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) => {
Expand Down Expand Up @@ -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
})
}

0 comments on commit d3f98fe

Please sign in to comment.