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

Commit 039eca7

Browse files
dignifiedquiredaviddias
authored andcommitted
feat: prepare for swarm.peers changes in 0.4.5
1 parent 1b6d452 commit 039eca7

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

API/swarm/README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,20 @@ ipfs.swarm.disconnect(addr, function (err) {})
6767
6868
##### `Go` **WIP**
6969

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

72-
`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]().
72+
If `opts.verbose` is set to `true` additional information, such as `latency` is provided.
73+
74+
`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
75+
76+
- `addr: Multiaddr`
77+
- `peer: [PeerInfo]()`
78+
- `latency: String` Only if `verbose: true` was passed
79+
80+
Starting with `go-ipfs 0.4.5` these additional properties are provided
81+
82+
- `muxer: String` - The type of stream muxer the peer is usng
83+
- `streams: []String` - Only if `verbose: true`, a list of currently open streams
7384

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

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"detect-node": "^2.0.3",
3535
"ipfs-block": "^0.5.0",
3636
"ipld-dag-pb": "^0.8.0",
37+
"multiaddr": "^2.0.3",
3738
"multihashes": "^0.2.2",
3839
"readable-stream": "2.1.5"
3940
},
@@ -49,4 +50,4 @@
4950
"Victor Bjelkholm <[email protected]>",
5051
"nginnever <[email protected]>"
5152
]
52-
}
53+
}

src/swarm.js

+47-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
const expect = require('chai').expect
77
const series = require('async/series')
8+
const multiaddr = require('multiaddr')
89

910
module.exports = (common) => {
1011
describe('.swarm', () => {
@@ -42,21 +43,61 @@ module.exports = (common) => {
4243
common.teardown(done)
4344
})
4445

46+
let ipfsBId
47+
4548
describe('callback API', () => {
4649
it('.connect', (done) => {
4750
ipfsB.id((err, id) => {
4851
expect(err).to.not.exist
49-
52+
ipfsBId = id
5053
const ipfsBAddr = id.addresses[0]
5154
ipfsA.swarm.connect(ipfsBAddr, done)
5255
})
5356
})
5457

55-
it('.peers', (done) => {
56-
ipfsA.swarm.peers((err, multiaddrs) => {
57-
expect(err).to.not.exist
58-
expect(multiaddrs).to.have.length.above(0)
59-
done()
58+
describe('.peers', () => {
59+
beforeEach((done) => {
60+
const ipfsBAddr = ipfsBId.addresses[0]
61+
ipfsA.swarm.connect(ipfsBAddr, done)
62+
})
63+
64+
it('default', (done) => {
65+
ipfsB.swarm.peers((err, peers) => {
66+
expect(err).to.not.exist
67+
expect(peers).to.have.length.above(0)
68+
69+
const peer = peers[0]
70+
71+
expect(peer).to.have.a.property('addr')
72+
expect(multiaddr.isMultiaddr(peer.addr)).to.be.true
73+
expect(peer).to.have.a.property('peer')
74+
expect(peer).to.not.have.a.property('latency')
75+
76+
// only available in 0.4.5
77+
// expect(peer).to.have.a.property('muxer')
78+
// expect(peer).to.not.have.a.property('streams')
79+
80+
done()
81+
})
82+
})
83+
84+
it('verbose', (done) => {
85+
ipfsA.swarm.peers({verbose: true}, (err, peers) => {
86+
expect(err).to.not.exist
87+
expect(peers).to.have.length.above(0)
88+
89+
const peer = peers[0]
90+
expect(peer).to.have.a.property('addr')
91+
expect(multiaddr.isMultiaddr(peer.addr)).to.be.true
92+
expect(peer).to.have.a.property('peer')
93+
expect(peer).to.have.a.property('latency')
94+
95+
// Only available in 0.4.5
96+
// expect(peer).to.have.a.property('muxer')
97+
// expect(peer).to.have.a.property('streams')
98+
99+
done()
100+
})
60101
})
61102
})
62103

0 commit comments

Comments
 (0)