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

Commit

Permalink
feat(swarm): support for new swarm.peers in 0.4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 17, 2016
1 parent 0080f20 commit 690a77c
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/api/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,44 @@ module.exports = (send) => {
callback = opts
opts = {}
}
if (opts['v']) {
throw "I don't know how to handle errors in javascript, but the -v option needs to be handled"
}

send({
path: 'swarm/peers',
qs: opts
}, (err, result) => {
if (err) {
return callback(err)
}
if (result.Strings) {
callback(null, result.Strings.map((addr) => {
return multiaddr(addr)

console.log(JSON.stringify(result, null, 2))

if (result.Strings) {
// go-ipfs <= 0.4.4
callback(null, result.Strings.map((p) => {
// splitting on whitespace as verbose mode
// returns the latency appended
return multiaddr(p.split(' ')[0])
}))
} else if (result.Peers) {
// go-ipfs >= 0.4.5
callback(null, result.Peers.map((p) => {
const res = {
addr: multiaddr(p.Addr),
peer: PeerId.createFromB58String(p.Peer),
muxer: p.Muxer
}

if (p.Latency) {
res.latency = p.Latency
}

if (p.Streams) {
res.streams = p.Streams
}

return res
}))
} else if (result.Peers) {
let out = result.Peers.map((p) => {
return {
addr: new multiaddr(p.Addr),
peer: new PeerId(p.Peer),
latency: p.Latency,
streams: p.Streams,
}
}
}
})
}),
connect: promisify((args, opts, callback) => {
Expand Down

0 comments on commit 690a77c

Please sign in to comment.