Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 690a77c

Browse files
feat(swarm): support for new swarm.peers in 0.4.5
1 parent 0080f20 commit 690a77c

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/api/swarm.js

+30-15
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,44 @@ module.exports = (send) => {
1212
callback = opts
1313
opts = {}
1414
}
15-
if (opts['v']) {
16-
throw "I don't know how to handle errors in javascript, but the -v option needs to be handled"
17-
}
15+
1816
send({
1917
path: 'swarm/peers',
2018
qs: opts
2119
}, (err, result) => {
2220
if (err) {
2321
return callback(err)
2422
}
25-
if (result.Strings) {
26-
callback(null, result.Strings.map((addr) => {
27-
return multiaddr(addr)
23+
24+
console.log(JSON.stringify(result, null, 2))
25+
26+
if (result.Strings) {
27+
// go-ipfs <= 0.4.4
28+
callback(null, result.Strings.map((p) => {
29+
// splitting on whitespace as verbose mode
30+
// returns the latency appended
31+
return multiaddr(p.split(' ')[0])
32+
}))
33+
} else if (result.Peers) {
34+
// go-ipfs >= 0.4.5
35+
callback(null, result.Peers.map((p) => {
36+
const res = {
37+
addr: multiaddr(p.Addr),
38+
peer: PeerId.createFromB58String(p.Peer),
39+
muxer: p.Muxer
40+
}
41+
42+
if (p.Latency) {
43+
res.latency = p.Latency
44+
}
45+
46+
if (p.Streams) {
47+
res.streams = p.Streams
48+
}
49+
50+
return res
2851
}))
29-
} else if (result.Peers) {
30-
let out = result.Peers.map((p) => {
31-
return {
32-
addr: new multiaddr(p.Addr),
33-
peer: new PeerId(p.Peer),
34-
latency: p.Latency,
35-
streams: p.Streams,
36-
}
37-
}
52+
}
3853
})
3954
}),
4055
connect: promisify((args, opts, callback) => {

0 commit comments

Comments
 (0)