This repository was archived by the owner on Feb 12, 2024. It is now read-only.
File tree 2 files changed +11
-21
lines changed 2 files changed +11
-21
lines changed Original file line number Diff line number Diff line change 81
81
"joi" : " ^9.0.4" ,
82
82
"libp2p-ipfs" : " ^0.14.1" ,
83
83
"libp2p-ipfs-browser" : " ^0.15.1" ,
84
+ "lodash.flatmap" : " ^4.5.0" ,
84
85
"lodash.get" : " ^4.4.2" ,
85
86
"lodash.has" : " ^4.5.2" ,
86
87
"lodash.set" : " ^4.3.2" ,
Original file line number Diff line number Diff line change 2
2
3
3
const multiaddr = require ( 'multiaddr' )
4
4
const promisify = require ( 'promisify-es6' )
5
+ const flatMap = require ( 'lodash.flatmap' )
5
6
6
7
const OFFLINE_ERROR = require ( '../utils' ) . OFFLINE_ERROR
7
8
@@ -12,35 +13,16 @@ module.exports = function swarm (self) {
12
13
return callback ( OFFLINE_ERROR )
13
14
}
14
15
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 )
26
17
callback ( null , mas )
27
18
} ) ,
28
19
// all the addrs we know
29
20
addrs : promisify ( ( callback ) => {
30
21
if ( ! self . isOnline ( ) ) {
31
22
return callback ( OFFLINE_ERROR )
32
23
}
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
- } )
43
24
25
+ const mas = collectAddrs ( self . _libp2pNode . peerBook )
44
26
callback ( null , mas )
45
27
} ) ,
46
28
localAddrs : promisify ( ( callback ) => {
@@ -78,3 +60,10 @@ module.exports = function swarm (self) {
78
60
} )
79
61
}
80
62
}
63
+
64
+ function collectAddrs ( book ) {
65
+ const peers = book . getAll ( )
66
+ return flatMap ( Object . keys ( peers ) , ( id ) => {
67
+ return peers [ id ] . multiaddrs
68
+ } )
69
+ }
You can’t perform that action at this time.
0 commit comments