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

Commit 7e932b1

Browse files
author
Alan Shaw
authored
refactor: convert swarm API to async/await (#2681)
* refactor: convert swarm API to async/await * refactor: use libp2p.connections
1 parent 920e399 commit 7e932b1

File tree

10 files changed

+96
-79
lines changed

10 files changed

+96
-79
lines changed

src/core/components/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ exports.object = {
2626
exports.ping = require('./ping')
2727
exports.start = require('./start')
2828
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+
}
2936
exports.version = require('./version')
3037

3138
exports.legacy = { // TODO: these will be removed as the new API is completed

src/core/components/init.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ function createApi ({
319319
init: () => { throw new AlreadyInitializedError() },
320320
object,
321321
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+
},
322329
version: Commands.version({ repo })
323330
}
324331

src/core/components/start.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ function createApi ({
159159
: () => { throw new NotEnabledError('pubsub not enabled') },
160160
start: () => apiManager.api,
161161
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+
},
162169
version: Commands.version({ repo })
163170
}
164171

src/core/components/stop.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ function createApi ({
117117
init: () => { throw new AlreadyInitializedError() },
118118
start,
119119
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+
},
120127
version: Commands.version({ repo })
121128
}
122129

src/core/components/swarm.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/core/components/swarm/addrs.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

src/core/components/swarm/connect.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = ({ libp2p }) => {
4+
return function connect (addr) {
5+
return libp2p.dial(addr)
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = ({ libp2p }) => {
4+
return function disconnect (addr) {
5+
return libp2p.hangUp(addr)
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

src/core/components/swarm/peers.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)