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

Commit b9aa7e0

Browse files
authored
Merge pull request #35 from ipfs/swarm-api
swarm API
2 parents e5e5816 + 5c145c6 commit b9aa7e0

File tree

4 files changed

+271
-2
lines changed

4 files changed

+271
-2
lines changed

API/swarm/README.md

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
Swarm API
2+
=========
3+
4+
#### `addrs`
5+
6+
> List of known addresses of each peer connected.
7+
8+
##### `Go` **WIP**
9+
10+
##### `JavaScript` - ipfs.swarm.addrs([callback])
11+
12+
`callback` must follow `function (err, addrs) {}` signature, where `err` is an error if the operation was not successful. `addrs` will be an array of multiaddrs.
13+
14+
If no `callback` is passed, a promise is returned.
15+
16+
Example:
17+
18+
```JavaScript
19+
ipfs.swarm.addrs(function (err, addrs) {})
20+
```
21+
22+
#### `connect`
23+
24+
> Open a connection to a given address.
25+
26+
##### `Go` **WIP**
27+
28+
##### `JavaScript` - ipfs.swarm.connect(addr, [callback])
29+
30+
Where `addr` is of type [multiaddr](https://github.com/multiformats/js-multiaddr)
31+
32+
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful.
33+
34+
If no `callback` is passed, a promise is returned.
35+
36+
Example:
37+
38+
```JavaScript
39+
ipfs.swarm.connect(addr, function (err) {
40+
// if no err is present, connection is now open
41+
})
42+
```
43+
44+
#### `disconnect`
45+
46+
> Close a connection on a given address.
47+
48+
##### `Go` **WIP**
49+
50+
##### `JavaScript` - ipfs.swarm.disconnect(addr, [callback])
51+
52+
Where `addr` is of type [multiaddr](https://github.com/multiformats/js-multiaddr)
53+
54+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
55+
56+
If no `callback` is passed, a promise is returned.
57+
58+
Example:
59+
60+
```JavaScript
61+
ipfs.swarm.disconnect(addr, function (err) {})
62+
```
63+
64+
#### `peers`
65+
66+
> List out the peers that we have connections with.
67+
68+
##### `Go` **WIP**
69+
70+
##### `JavaScript` - ipfs.swarm.peers([callback])
71+
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]().
73+
74+
If no `callback` is passed, a promise is returned.
75+
76+
Example:
77+
78+
```JavaScript
79+
ipfs.swarm.peers(function (err, peerInfos) {})
80+
```
81+
82+
------------------------------
83+
84+
> NOT IMPLEMENTED YET
85+
86+
#### `filters`
87+
88+
> Display current multiaddr filters. Filters are a way to set up rules for the network connections established.
89+
90+
##### `Go` **WIP**
91+
92+
##### `JavaScript` - ipfs.swarm.filters([callback])
93+
94+
`callback` must follow `function (err, filters) {}` signature, where `err` is an error if the operation was not successful. `filters` is an array of multiaddrs that represent the filters being applied.
95+
96+
If no `callback` is passed, a promise is returned.
97+
98+
Example:
99+
100+
```JavaScript
101+
ipfs.swarm.filters(function (err, filters) {})
102+
```
103+
104+
#### `filters.add`
105+
106+
> Add another filter.
107+
108+
##### `Go` **WIP**
109+
110+
##### `JavaScript` - ipfs.swarm.filters.add(filter, [callback])
111+
112+
Where `filter` is of type [multiaddr]()
113+
114+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
115+
116+
If no `callback` is passed, a promise is returned.
117+
118+
Example:
119+
120+
```JavaScript
121+
ipfs.swarm.filters.add(filter, function (err) {})
122+
```
123+
124+
#### `filters.rm`
125+
126+
> Remove a filter
127+
128+
##### `Go` **WIP**
129+
130+
##### `JavaScript` - ipfs.swarm.filters.rm(filter, [callback])
131+
132+
Where `filter` is of type [multiaddr]()
133+
134+
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` will be an array of:
135+
136+
If no `callback` is passed, a promise is returned.
137+
138+
Example:
139+
140+
```JavaScript
141+
ipfs.swarm.filters.rm(filter, function (err) {})
142+
```
143+
144+

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"concat-stream": "^1.5.1",
3535
"detect-node": "^2.0.3",
3636
"ipfs-merkle-dag": "^0.6.2",
37-
"readable-stream": "1.1.13"
37+
"readable-stream": "1.1.13",
38+
"run-series": "^1.1.4"
3839
},
3940
"devDependencies": {
4041
"aegir": "^6.0.0"
@@ -47,4 +48,4 @@
4748
"greenkeeperio-bot <support@greenkeeper.io>",
4849
"nginnever <ginneversource@gmail.com>"
4950
]
50-
}
51+
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ exports.files = require('./files')
55
exports.config = require('./config')
66
exports.pin = require('./pin')
77
exports.generic = require('./generic')
8+
exports.swarm = require('./swarm')

src/swarm.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const expect = require('chai').expect
7+
const series = require('run-series')
8+
9+
module.exports = (common) => {
10+
describe('.swarm', () => {
11+
let ipfsA
12+
let ipfsB
13+
14+
before(function (done) {
15+
// CI takes longer to instantiate the daemon,
16+
// so we need to increase the timeout for the
17+
// before step
18+
this.timeout(20 * 1000)
19+
20+
common.setup((err, factory) => {
21+
expect(err).to.not.exist
22+
series([
23+
(cb) => {
24+
factory.spawnNode((err, node) => {
25+
expect(err).to.not.exist
26+
ipfsA = node
27+
cb()
28+
})
29+
},
30+
(cb) => {
31+
factory.spawnNode((err, node) => {
32+
expect(err).to.not.exist
33+
ipfsB = node
34+
cb()
35+
})
36+
}
37+
], done)
38+
})
39+
})
40+
41+
after((done) => {
42+
common.teardown(done)
43+
})
44+
45+
describe('callback API', () => {
46+
it('.connect', (done) => {
47+
ipfsB.id((err, id) => {
48+
expect(err).to.not.exist
49+
const ipfsBAddr = id.addresses[0]
50+
ipfsA.swarm.connect(ipfsBAddr, done)
51+
})
52+
})
53+
54+
it('.peers', (done) => {
55+
ipfsA.swarm.peers((err, multiaddrs) => {
56+
expect(err).to.not.exist
57+
expect(multiaddrs).to.have.length.above(0)
58+
done()
59+
})
60+
})
61+
62+
it('.addrs', (done) => {
63+
ipfsA.swarm.addrs((err, multiaddrs) => {
64+
expect(err).to.not.exist
65+
expect(multiaddrs).to.have.length.above(0)
66+
done()
67+
})
68+
})
69+
70+
it('.localAddrs', (done) => {
71+
ipfsA.swarm.localAddrs((err, multiaddrs) => {
72+
expect(err).to.not.exist
73+
expect(multiaddrs).to.have.length.above(0)
74+
done()
75+
})
76+
})
77+
78+
it('.disconnect', (done) => {
79+
ipfsB.id((err, id) => {
80+
expect(err).to.not.exist
81+
const ipfsBAddr = id.addresses[0]
82+
ipfsA.swarm.disconnect(ipfsBAddr, done)
83+
})
84+
})
85+
})
86+
87+
describe('promise API', () => {
88+
it('.connect', () => {
89+
return ipfsB.id()
90+
.then((id) => {
91+
const ipfsBAddr = id.addresses[0]
92+
return ipfsA.swarm.connect(ipfsBAddr)
93+
})
94+
})
95+
96+
it('.peers', () => {
97+
return ipfsA.swarm.peers().then((multiaddrs) => {
98+
expect(multiaddrs).to.have.length.above(0)
99+
})
100+
})
101+
102+
it('.addrs', () => {
103+
return ipfsA.swarm.addrs().then((multiaddrs) => {
104+
expect(multiaddrs).to.have.length.above(0)
105+
})
106+
})
107+
108+
it('.localAddrs', () => {
109+
return ipfsA.swarm.localAddrs().then((multiaddrs) => {
110+
expect(multiaddrs).to.have.length.above(0)
111+
})
112+
})
113+
114+
it('.disconnect', () => {
115+
return ipfsB.id()
116+
.then((id) => {
117+
const ipfsBAddr = id.addresses[0]
118+
return ipfsA.swarm.disconnect(ipfsBAddr)
119+
})
120+
})
121+
})
122+
})
123+
}

0 commit comments

Comments
 (0)