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

Commit 5d0a823

Browse files
committed
feat(swarm): spec compliant
1 parent ba0fc67 commit 5d0a823

File tree

3 files changed

+64
-168
lines changed

3 files changed

+64
-168
lines changed

src/api/swarm.js

+37-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
const multiaddr = require('multiaddr')
5+
36
module.exports = (send) => {
47
return {
5-
peers (opts, callback) {
8+
peers: promisify((opts, callback) => {
69
if (typeof (opts) === 'function') {
710
callback = opts
811
opts = {}
912
}
10-
return send({
13+
send({
1114
path: 'swarm/peers',
1215
qs: opts
13-
}, callback)
14-
},
16+
}, (err, result) => {
17+
if (err) {
18+
return callback(err)
19+
}
20+
callback(null, result.Strings.map((addr) => {
21+
return multiaddr(addr)
22+
}))
23+
})
24+
}),
1525
connect (args, opts, callback) {
1626
if (typeof (opts) === 'function') {
1727
callback = opts
@@ -34,25 +44,41 @@ module.exports = (send) => {
3444
qs: opts
3545
}, callback)
3646
},
37-
addrs (opts, callback) {
47+
addrs: promisify((opts, callback) => {
3848
if (typeof (opts) === 'function') {
3949
callback = opts
4050
opts = {}
4151
}
42-
return send({
52+
send({
4353
path: 'swarm/addrs',
4454
qs: opts
45-
}, callback)
46-
},
47-
localAddrs (opts, callback) {
55+
}, (err, result) => {
56+
if (err) {
57+
return callback(err)
58+
}
59+
callback(null, Object.keys(result.Addrs).map((id) => {
60+
return result.Addrs[id].map((maStr) => {
61+
return multiaddr(maStr).encapsulate('/ipfs/' + id)
62+
})
63+
})[0])
64+
})
65+
}),
66+
localAddrs: promisify((opts, callback) => {
4867
if (typeof (opts) === 'function') {
4968
callback = opts
5069
opts = {}
5170
}
5271
return send({
5372
path: 'swarm/addrs/local',
5473
qs: opts
55-
}, callback)
56-
}
74+
}, (err, result) => {
75+
if (err) {
76+
return callback(err)
77+
}
78+
callback(null, result.Strings.map((addr) => {
79+
return multiaddr(addr)
80+
}))
81+
})
82+
})
5783
}
5884
}
+13-96
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,20 @@
11
/* eslint-env mocha */
2-
/* eslint max-nested-callbacks: ["error", 8] */
2+
33
'use strict'
44

5+
const test = require('interface-ipfs-core')
56
const FactoryClient = require('../factory/factory-client')
6-
const expect = require('chai').expect
77

8-
describe('.block', () => {
9-
let ipfs
10-
let fc
8+
let fc
119

12-
before(function (done) {
13-
this.timeout(20 * 1000) // slow CI
10+
const common = {
11+
setup: function (callback) {
1412
fc = new FactoryClient()
15-
fc.spawnNode((err, node) => {
16-
expect(err).to.not.exist
17-
ipfs = node
18-
done()
19-
})
20-
})
21-
22-
after((done) => {
23-
fc.dismantle(done)
24-
})
25-
26-
const blorbKey = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
27-
const blorb = Buffer('blorb')
28-
29-
it('returns an error when putting an array of files', () => {
30-
return ipfs.block.put([blorb, blorb], (err) => {
31-
expect(err).to.be.an.instanceof(Error)
32-
})
33-
})
34-
35-
it('block.put', (done) => {
36-
ipfs.block.put(blorb, (err, res) => {
37-
expect(err).to.not.exist
38-
expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ')
39-
done()
40-
})
41-
})
42-
43-
it('block.get', (done) => {
44-
ipfs.block.get(blorbKey, (err, res) => {
45-
expect(err).to.not.exist
46-
47-
let buf = ''
48-
res
49-
.on('data', function (data) { buf += data })
50-
.on('end', function () {
51-
expect(buf).to.be.equal('blorb')
52-
done()
53-
})
54-
})
55-
})
56-
57-
it('block.stat', (done) => {
58-
ipfs.block.stat(blorbKey, (err, res) => {
59-
expect(err).to.not.exist
60-
expect(res).to.have.property('Key')
61-
expect(res).to.have.property('Size')
62-
done()
63-
})
64-
})
65-
66-
describe('promise', () => {
67-
it('returns an error when putting an array of files', () => {
68-
return ipfs.block.put([blorb, blorb])
69-
.catch((err) => {
70-
expect(err).to.be.an.instanceof(Error)
71-
})
72-
})
73-
74-
it('block.put', () => {
75-
return ipfs.block.put(blorb)
76-
.then((res) => {
77-
expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ')
78-
})
79-
})
80-
81-
it('block.get', (done) => {
82-
ipfs.block.get(blorbKey)
83-
.then((res) => {
84-
let buf = ''
85-
res
86-
.on('data', function (data) { buf += data })
87-
.on('end', function () {
88-
expect(buf).to.be.equal('blorb')
89-
done()
90-
})
91-
})
92-
.catch(done)
93-
})
94-
95-
it('block.stat', () => {
96-
return ipfs.block.stat(blorbKey)
97-
.then((res) => {
98-
expect(res).to.have.property('Key')
99-
expect(res).to.have.property('Size')
100-
})
101-
})
102-
})
103-
})
13+
callback(null, fc)
14+
},
15+
teardown: function (callback) {
16+
fc.dismantle(callback)
17+
}
18+
}
19+
20+
// test.block(common)
+14-61
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,20 @@
11
/* eslint-env mocha */
2-
/* globals apiClients */
2+
/* eslint max-nested-callbacks: ["error", 8] */
33
'use strict'
44

5-
const expect = require('chai').expect
5+
const test = require('interface-ipfs-core')
6+
const FactoryClient = require('../factory/factory-client')
67

7-
describe('.swarm', () => {
8-
it('.swarm.peers', (done) => {
9-
apiClients.a.swarm.peers((err, res) => {
10-
expect(err).to.not.exist
8+
let fc
119

12-
expect(res.Strings).to.have.length.above(1)
13-
done()
14-
})
15-
})
10+
const common = {
11+
setup: function (callback) {
12+
fc = new FactoryClient()
13+
callback(null, fc)
14+
},
15+
teardown: function (callback) {
16+
fc.dismantle(callback)
17+
}
18+
}
1619

17-
it('.swarm.connect', (done) => {
18-
// Done in the 'before' segment
19-
done()
20-
})
21-
22-
it('.swarm.disconnect', (done) => {
23-
// Done in the 'after' segment
24-
done()
25-
})
26-
27-
it('.swarm.addrs', (done) => {
28-
apiClients.a.swarm.addrs((err, res) => {
29-
expect(err).to.not.exist
30-
31-
expect(Object.keys(res.Addrs)).to.have.length.above(1)
32-
done()
33-
})
34-
})
35-
36-
it('.swarm.localAddrs', (done) => {
37-
apiClients.a.swarm.localAddrs((err, res) => {
38-
expect(err).to.not.exist
39-
40-
expect(res.Strings).to.have.length.above(1)
41-
done()
42-
})
43-
})
44-
45-
describe('promise', () => {
46-
it('.swarm.peers', () => {
47-
return apiClients.a.swarm.peers()
48-
.then((res) => {
49-
expect(res.Strings).to.have.length.above(1)
50-
})
51-
})
52-
53-
it('.swarm.addrs', () => {
54-
return apiClients.a.swarm.addrs()
55-
.then((res) => {
56-
expect(Object.keys(res.Addrs)).to.have.length.above(1)
57-
})
58-
})
59-
60-
it('.swarm.localAddrs', () => {
61-
return apiClients.a.swarm.localAddrs()
62-
.then((res) => {
63-
expect(res.Strings).to.have.length.above(1)
64-
})
65-
})
66-
})
67-
})
20+
test.swarm(common)

0 commit comments

Comments
 (0)