This repository was archived by the owner on Feb 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 10 files changed +96
-79
lines changed Expand file tree Collapse file tree 10 files changed +96
-79
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,13 @@ exports.object = {
26
26
exports . ping = require ( './ping' )
27
27
exports . start = require ( './start' )
28
28
exports . stop = require ( './stop' )
29
+ exports . swarm = {
30
+ addrs : require ( './swarm/addrs' ) ,
31
+ connect : require ( './swarm/connect' ) ,
32
+ disconnect : require ( './swarm/disconnect' ) ,
33
+ localAddrs : require ( './swarm/localAddrs' ) ,
34
+ peers : require ( './swarm/peers' )
35
+ }
29
36
exports . version = require ( './version' )
30
37
31
38
exports . legacy = { // TODO: these will be removed as the new API is completed
Original file line number Diff line number Diff line change @@ -319,6 +319,13 @@ function createApi ({
319
319
init : ( ) => { throw new AlreadyInitializedError ( ) } ,
320
320
object,
321
321
start,
322
+ swarm : {
323
+ addrs : ( ) => { throw new NotStartedError ( ) } ,
324
+ connect : ( ) => { throw new NotStartedError ( ) } ,
325
+ disconnect : ( ) => { throw new NotStartedError ( ) } ,
326
+ localAddrs : Commands . swarm . localAddrs ( { peerInfo } ) ,
327
+ peers : ( ) => { throw new NotStartedError ( ) }
328
+ } ,
322
329
version : Commands . version ( { repo } )
323
330
}
324
331
Original file line number Diff line number Diff line change @@ -159,6 +159,13 @@ function createApi ({
159
159
: ( ) => { throw new NotEnabledError ( 'pubsub not enabled' ) } ,
160
160
start : ( ) => apiManager . api ,
161
161
stop,
162
+ swarm : {
163
+ addrs : ( ) => Commands . swarm . addrs ( { libp2p } ) ,
164
+ connect : ( ) => Commands . swarm . connect ( { libp2p } ) ,
165
+ disconnect : ( ) => Commands . swarm . disconnect ( { libp2p } ) ,
166
+ localAddrs : Commands . swarm . localAddrs ( { peerInfo } ) ,
167
+ peers : ( ) => Commands . swarm . peers ( { libp2p } )
168
+ } ,
162
169
version : Commands . version ( { repo } )
163
170
}
164
171
Original file line number Diff line number Diff line change @@ -117,6 +117,13 @@ function createApi ({
117
117
init : ( ) => { throw new AlreadyInitializedError ( ) } ,
118
118
start,
119
119
stop : ( ) => apiManager . api ,
120
+ swarm : {
121
+ addrs : ( ) => { throw new NotStartedError ( ) } ,
122
+ connect : ( ) => { throw new NotStartedError ( ) } ,
123
+ disconnect : ( ) => { throw new NotStartedError ( ) } ,
124
+ localAddrs : Commands . swarm . localAddrs ( { peerInfo } ) ,
125
+ peers : ( ) => { throw new NotStartedError ( ) }
126
+ } ,
120
127
version : Commands . version ( { repo } )
121
128
}
122
129
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const CID = require ( 'cids' )
4
+
5
+ module . exports = ( { libp2p } ) => {
6
+ return async function addrs ( ) { // eslint-disable-line require-await
7
+ const peers = [ ]
8
+ for ( const [ peerId , peerInfo ] of libp2p . peerStore . entries ( ) ) {
9
+ peers . push ( { id : new CID ( peerId ) , addrs : peerInfo . multiaddrs . toArray ( ) } )
10
+ }
11
+ return peers
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ module . exports = ( { libp2p } ) => {
4
+ return function connect ( addr ) {
5
+ return libp2p . dial ( addr )
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ module . exports = ( { libp2p } ) => {
4
+ return function disconnect ( addr ) {
5
+ return libp2p . hangUp ( addr )
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ module . exports = ( { peerInfo } ) => {
4
+ return async function localAddrs ( ) { // eslint-disable-line require-await
5
+ return peerInfo . multiaddrs . toArray ( )
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const CID = require ( 'cids' )
4
+
5
+ module . exports = ( { libp2p } ) => {
6
+ return async function peers ( options ) { // eslint-disable-line require-await
7
+ options = options || { }
8
+
9
+ const verbose = options . v || options . verbose
10
+ const peers = [ ]
11
+
12
+ for ( const [ peerId , connections ] of libp2p . connections ) {
13
+ for ( const connection of connections ) {
14
+ const tupple = {
15
+ addr : connection . remoteAddr ,
16
+ peer : new CID ( peerId )
17
+ }
18
+
19
+ if ( verbose || options . direction ) {
20
+ tupple . direction = connection . stat . direction
21
+ }
22
+
23
+ if ( verbose ) {
24
+ tupple . muxer = connection . stat . multiplexer
25
+ tupple . latency = 'n/a'
26
+ }
27
+
28
+ peers . push ( tupple )
29
+ }
30
+ }
31
+
32
+ return peers
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments