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

Commit 7b83894

Browse files
committed
feat: add bitswap.unwant javascript spec
1 parent 6e68a26 commit 7b83894

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

SPEC/BITSWAP.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ Bitswap API
33

44
#### `wantlist` (not spec'ed yet)
55

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

819
#### `stat`
920

js/src/bitswap.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR';
14+
15+
before(function (done) {
16+
// CI takes longer to instantiate the daemon, so we need to increase the
17+
// timeout for the before step
18+
this.timeout(60 * 1000)
19+
20+
common.setup((err, factory) => {
21+
expect(err).to.not.exist()
22+
factory.spawnNode((err, node) => {
23+
expect(err).to.not.exist()
24+
ipfs = node
25+
ipfs.block.get(new CID(key))
26+
.then(() => {})
27+
.catch(() => {})
28+
setTimeout(done, 800)
29+
})
30+
})
31+
})
32+
33+
after((done) => common.teardown(done))
34+
35+
it('.stat', (done) => {
36+
37+
ipfs.bitswap.stat((err, stats) => {
38+
statsTests.expectIsBitswap(err, stats)
39+
done()
40+
})
41+
})
42+
43+
it('.wantlist', (done) => {
44+
const wantlist = ipfs.bitswap.wantlist()
45+
expect(wantlist[0].cid.toBaseEncodedString()).to.equal(key)
46+
done()
47+
})
48+
49+
it('.unwant', (done) => {
50+
ipfs.bitswap.unwant(new CID(key))
51+
const wantlist = ipfs.bitswap.wantlist()
52+
expect(wantlist).to.be.empty()
53+
done()
54+
})
55+
})
56+
57+
describe('.bitswap offline', () => {
58+
let ipfs
59+
60+
before(function (done) {
61+
// CI takes longer to instantiate the daemon, so we need to increase the
62+
// timeout for the before step
63+
this.timeout(60 * 1000)
64+
65+
common.setup((err, factory) => {
66+
expect(err).to.not.exist()
67+
factory.spawnNode((err, node) => {
68+
expect(err).to.not.exist()
69+
ipfs = node
70+
ipfs.id((err, id) => {
71+
expect(err).to.not.exist()
72+
ipfs.stop((err) => {
73+
// TODO: go-ipfs returns an error, https://github.com/ipfs/go-ipfs/issues/4078
74+
if (!id.agentVersion.startsWith('go-ipfs')) {
75+
expect(err).to.not.exist()
76+
}
77+
done()
78+
})
79+
})
80+
})
81+
})
82+
})
83+
84+
it('.stat gives error while offline', () => {
85+
ipfs.bitswap.stat((err, stats) => {
86+
expect(err).to.exist()
87+
expect(stats).to.not.exist()
88+
})
89+
})
90+
91+
it('.wantlist throws if offline', () => {
92+
expect(() => ipfs.bitswap.wantlist()).to.throw(/online/)
93+
})
94+
95+
it('.unwant throws if offline', () => {
96+
expect(() => ipfs.bitswap.unwant('my key')).to.throw(/online/)
97+
})
98+
})
99+
}

js/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ exports.key = require('./key')
1616
exports.stats = require('./stats')
1717
exports.repo = require('./repo')
1818
exports.bootstrap = require('./bootstrap')
19+
exports.bitswap = require('./bitswap')

0 commit comments

Comments
 (0)