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

Commit

Permalink
feat: prepare for swarm.peers changes in 0.4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Nov 17, 2016
1 parent 1b6d452 commit 039eca7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
15 changes: 13 additions & 2 deletions API/swarm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,20 @@ ipfs.swarm.disconnect(addr, function (err) {})
##### `Go` **WIP**

##### `JavaScript` - ipfs.swarm.peers([callback])
##### `JavaScript` - ipfs.swarm.peers([opts] [, callback])

`callback` must follow `function (err, peerInfos) {}` signature, where `err` is an error if the operation was not successful. `peerInfos` will be an array of [PeerInfo]().
If `opts.verbose` is set to `true` additional information, such as `latency` is provided.

`callback` must follow `function (err, peerInfos) {}` signature, where `err` is an error if the operation was not successful. `peerInfos` will be an array of the form

- `addr: Multiaddr`
- `peer: [PeerInfo]()`
- `latency: String` Only if `verbose: true` was passed

Starting with `go-ipfs 0.4.5` these additional properties are provided

- `muxer: String` - The type of stream muxer the peer is usng
- `streams: []String` - Only if `verbose: true`, a list of currently open streams

If no `callback` is passed, a promise is returned.

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"detect-node": "^2.0.3",
"ipfs-block": "^0.5.0",
"ipld-dag-pb": "^0.8.0",
"multiaddr": "^2.0.3",
"multihashes": "^0.2.2",
"readable-stream": "2.1.5"
},
Expand All @@ -49,4 +50,4 @@
"Victor Bjelkholm <victorbjelkholm@gmail.com>",
"nginnever <ginneversource@gmail.com>"
]
}
}
53 changes: 47 additions & 6 deletions src/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const expect = require('chai').expect
const series = require('async/series')
const multiaddr = require('multiaddr')

module.exports = (common) => {
describe('.swarm', () => {
Expand Down Expand Up @@ -42,21 +43,61 @@ module.exports = (common) => {
common.teardown(done)
})

let ipfsBId

describe('callback API', () => {
it('.connect', (done) => {
ipfsB.id((err, id) => {
expect(err).to.not.exist

ipfsBId = id
const ipfsBAddr = id.addresses[0]
ipfsA.swarm.connect(ipfsBAddr, done)
})
})

it('.peers', (done) => {
ipfsA.swarm.peers((err, multiaddrs) => {
expect(err).to.not.exist
expect(multiaddrs).to.have.length.above(0)
done()
describe('.peers', () => {
beforeEach((done) => {
const ipfsBAddr = ipfsBId.addresses[0]
ipfsA.swarm.connect(ipfsBAddr, done)
})

it('default', (done) => {
ipfsB.swarm.peers((err, peers) => {
expect(err).to.not.exist
expect(peers).to.have.length.above(0)

const peer = peers[0]

expect(peer).to.have.a.property('addr')
expect(multiaddr.isMultiaddr(peer.addr)).to.be.true
expect(peer).to.have.a.property('peer')
expect(peer).to.not.have.a.property('latency')

// only available in 0.4.5
// expect(peer).to.have.a.property('muxer')
// expect(peer).to.not.have.a.property('streams')

done()
})
})

it('verbose', (done) => {
ipfsA.swarm.peers({verbose: true}, (err, peers) => {
expect(err).to.not.exist
expect(peers).to.have.length.above(0)

const peer = peers[0]
expect(peer).to.have.a.property('addr')
expect(multiaddr.isMultiaddr(peer.addr)).to.be.true
expect(peer).to.have.a.property('peer')
expect(peer).to.have.a.property('latency')

// Only available in 0.4.5
// expect(peer).to.have.a.property('muxer')
// expect(peer).to.have.a.property('streams')

done()
})
})
})

Expand Down

0 comments on commit 039eca7

Please sign in to comment.