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

Commit 3f714d4

Browse files
committed
feat: add bitswap.unwant javascript spec
1 parent 2d227a9 commit 3f714d4

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

SPEC/BITSWAP.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44
* [bitswap.unwant](#bitswapunwant)
55
* [bitswap.stat](#bitswapstat)
66

7-
#### `bitswap.wantlist`
7+
#### `unwant`
8+
9+
> Removes a given block from your wantlist
10+
11+
##### `Go` **WIP**
12+
13+
##### `JavaScript` - ipfs.bitswap.unwant(cid)
14+
15+
`cid` is a [cid][cid] which can be passed as:
16+
17+
- CID, a CID instance
18+
- String, the base58 encoded version of the multihash
19+
20+
### `bitswap.wantlist`
821

922
(not spec'ed yet)
1023

js/src/bitswap.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict'
2+
3+
const chai = require('chai')
4+
const dirtyChai = require('dirty-chai')
5+
const expect = chai.expect
6+
const statsTests = require('./utils/stats')
7+
chai.use(dirtyChai)
8+
const CID = require('cids')
9+
10+
module.exports = (common) => {
11+
describe('.bitswap online', () => {
12+
let ipfs
13+
let withGo
14+
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
15+
16+
before(function (done) {
17+
// CI takes longer to instantiate the daemon, so we need to increase the
18+
// timeout for the before step
19+
this.timeout(60 * 1000)
20+
21+
common.setup((err, factory) => {
22+
expect(err).to.not.exist()
23+
factory.spawnNode((err, node) => {
24+
expect(err).to.not.exist()
25+
ipfs = node
26+
ipfs.block.get(key)
27+
.then(() => {})
28+
.catch(() => {})
29+
ipfs.id((err, id) => {
30+
expect(err).to.not.exist()
31+
withGo = id.agentVersion.startsWith('go-ipfs')
32+
setTimeout(done, 250) //Give block.get time to take effect
33+
})
34+
})
35+
})
36+
})
37+
38+
after((done) => common.teardown(done))
39+
40+
it('.stat', (done) => {
41+
42+
ipfs.bitswap.stat((err, stats) => {
43+
statsTests.expectIsBitswap(err, stats)
44+
done()
45+
})
46+
})
47+
48+
it('.wantlist', (done) => {
49+
ipfs.bitswap.wantlist((err, list) => {
50+
expect(err).to.not.exist()
51+
expect(list.Keys).to.have.length(1);
52+
expect(list.Keys[0]['/']).to.equal(key)
53+
done()
54+
})
55+
})
56+
57+
it('.unwant', function (done) {
58+
if (withGo) {
59+
this.skip()
60+
}
61+
ipfs.bitswap.unwant(key, (err) => {
62+
expect(err).to.not.exist();
63+
ipfs.bitswap.wantlist((err, list) => {
64+
expect(err).to.not.exist();
65+
expect(list.Keys).to.be.empty()
66+
done()
67+
})
68+
})
69+
})
70+
})
71+
72+
describe('.bitswap offline', () => {
73+
let ipfs
74+
75+
before(function (done) {
76+
// CI takes longer to instantiate the daemon, so we need to increase the
77+
// timeout for the before step
78+
this.timeout(60 * 1000)
79+
80+
common.setup((err, factory) => {
81+
expect(err).to.not.exist()
82+
factory.spawnNode((err, node) => {
83+
expect(err).to.not.exist()
84+
ipfs = node
85+
ipfs.id((err, id) => {
86+
expect(err).to.not.exist()
87+
ipfs.stop((err) => {
88+
// TODO: go-ipfs returns an error, https://github.com/ipfs/go-ipfs/issues/4078
89+
if (!id.agentVersion.startsWith('go-ipfs')) {
90+
expect(err).to.not.exist()
91+
}
92+
done()
93+
})
94+
})
95+
})
96+
})
97+
})
98+
99+
it('.stat gives error while offline', () => {
100+
ipfs.bitswap.stat((err, stats) => {
101+
expect(err).to.match(/online mode/)
102+
expect(stats).to.not.exist()
103+
})
104+
})
105+
106+
it('.wantlist gives error if offline', () => {
107+
ipfs.bitswap.wantlist((err, list) => {
108+
expect(err).to.match(/online mode/)
109+
expect(list).to.not.exist()
110+
})
111+
})
112+
113+
it('.unwant gives error if offline', () => {
114+
expect(() => ipfs.bitswap.unwant(key, (err) => {
115+
expect(err).to.match(/online mode/)
116+
}))
117+
})
118+
})
119+
}

js/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ exports.repo = require('./repo')
1919
exports.bootstrap = require('./bootstrap')
2020
exports.types = require('./types')
2121
exports.util = require('./util')
22+
exports.bitswap = require('./bitswap')

0 commit comments

Comments
 (0)