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

Commit d3f98fe

Browse files
style(core): cleanup libp2p commands
1 parent 0e99029 commit d3f98fe

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"joi": "^9.0.4",
8282
"libp2p-ipfs": "^0.14.1",
8383
"libp2p-ipfs-browser": "^0.15.1",
84+
"lodash.flatmap": "^4.5.0",
8485
"lodash.get": "^4.4.2",
8586
"lodash.has": "^4.5.2",
8687
"lodash.set": "^4.3.2",

src/core/components/swarm.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multiaddr = require('multiaddr')
44
const promisify = require('promisify-es6')
5+
const flatMap = require('lodash.flatmap')
56

67
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
78

@@ -12,35 +13,16 @@ module.exports = function swarm (self) {
1213
return callback(OFFLINE_ERROR)
1314
}
1415

15-
const peers = self._libp2pNode.peerBook.getAll()
16-
const mas = []
17-
Object
18-
.keys(peers)
19-
.forEach((b58Id) => {
20-
peers[b58Id].multiaddrs.forEach((ma) => {
21-
// TODO this should only print the addr we are using
22-
mas.push(ma)
23-
})
24-
})
25-
16+
const mas = collectAddrs(self._libp2pNode.peerBook)
2617
callback(null, mas)
2718
}),
2819
// all the addrs we know
2920
addrs: promisify((callback) => {
3021
if (!self.isOnline()) {
3122
return callback(OFFLINE_ERROR)
3223
}
33-
const peers = self._libp2pNode.peerBook.getAll()
34-
const mas = []
35-
Object
36-
.keys(peers)
37-
.forEach((b58Id) => {
38-
peers[b58Id].multiaddrs.forEach((ma) => {
39-
// TODO this should only print the addr we are using
40-
mas.push(ma)
41-
})
42-
})
4324

25+
const mas = collectAddrs(self._libp2pNode.peerBook)
4426
callback(null, mas)
4527
}),
4628
localAddrs: promisify((callback) => {
@@ -78,3 +60,10 @@ module.exports = function swarm (self) {
7860
})
7961
}
8062
}
63+
64+
function collectAddrs (book) {
65+
const peers = book.getAll()
66+
return flatMap(Object.keys(peers), (id) => {
67+
return peers[id].multiaddrs
68+
})
69+
}

0 commit comments

Comments
 (0)