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

Commit 1132f33

Browse files
authored
Merge pull request #600 from ipfs/new-swarm-peers
feat(swarm): update swarm.peers to new api
2 parents db550a1 + 3fa1438 commit 1132f33

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"form-data": "^2.1.2",
6060
"fs-pull-blob-store": "^0.4.1",
6161
"gulp": "^3.9.1",
62-
"interface-ipfs-core": "^0.21.0",
62+
"interface-ipfs-core": "^0.22.0",
6363
"left-pad": "^1.1.3",
6464
"lodash": "^4.17.2",
6565
"ncp": "^2.0.0",

src/core/components/swarm.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,41 @@ const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
99

1010
module.exports = function swarm (self) {
1111
return {
12-
peers: promisify((callback) => {
12+
peers: promisify((opts, callback) => {
13+
if (typeof opts === 'function') {
14+
callback = opts
15+
opts = {}
16+
}
17+
1318
if (!self.isOnline()) {
1419
return callback(OFFLINE_ERROR)
1520
}
1621

22+
const verbose = opts.v || opts.verbose
23+
// TODO: return latency and streams when verbose is set
24+
// we currently don't have this information
25+
1726
const peers = self._libp2pNode.peerBook.getAll()
18-
const mas = flatMap(Object.keys(peers), (id) => {
19-
return peers[id].multiaddrs
27+
const keys = Object.keys(peers)
28+
29+
const peerList = flatMap(keys, (id) => {
30+
const peer = peers[id]
31+
32+
return peer.multiaddrs.map((addr) => {
33+
const res = {
34+
addr: addr,
35+
peer: peers[id]
36+
}
37+
38+
if (verbose) {
39+
res.latency = 'unknown'
40+
}
41+
42+
return res
43+
})
2044
})
2145

22-
callback(null, mas)
46+
callback(null, peerList)
2347
}),
2448

2549
// all the addrs we know

src/http-api/resources/swarm.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ exports.parseAddrs = (request, reply) => {
2626

2727
exports.peers = {
2828
handler: (request, reply) => {
29+
const rawVerbose = request.query.v || request.query.verbose
30+
const verbose = rawVerbose === 'true'
2931
const ipfs = request.server.app.ipfs
30-
ipfs.swarm.peers((err, peers) => {
32+
33+
ipfs.swarm.peers({verbose: verbose}, (err, peers) => {
3134
if (err) {
3235
log.error(err)
3336
return reply({
@@ -37,7 +40,18 @@ exports.peers = {
3740
}
3841

3942
return reply({
40-
Strings: peers.map((addr) => addr.toString())
43+
Peers: peers.map((p) => {
44+
const res = {
45+
Peer: p.peer.id.toB58String(),
46+
Addr: p.addr.toString()
47+
}
48+
49+
if (verbose) {
50+
res.Latency = p.latency
51+
}
52+
53+
return res
54+
})
4155
})
4256
})
4357
}

0 commit comments

Comments
 (0)