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

Commit ff1cc7e

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

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

SPEC/BITSWAP.md

+12-1
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

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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, 250)
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+
ipfs.bitswap.wantlist((err, list) => {
45+
expect(err).to.not.exist()
46+
expect(list[0].cid.toBaseEncodedString()).to.equal(key)
47+
done()
48+
})
49+
})
50+
51+
it('.unwant', (done) => {
52+
ipfs.bitswap.unwant(new CID(key), (err) => {
53+
ipfs.bitswap.wantlist((err, list) => {
54+
expect(list).to.be.empty()
55+
done()
56+
})
57+
})
58+
})
59+
})
60+
61+
describe('.bitswap offline', () => {
62+
let ipfs
63+
64+
before(function (done) {
65+
// CI takes longer to instantiate the daemon, so we need to increase the
66+
// timeout for the before step
67+
this.timeout(60 * 1000)
68+
69+
common.setup((err, factory) => {
70+
expect(err).to.not.exist()
71+
factory.spawnNode((err, node) => {
72+
expect(err).to.not.exist()
73+
ipfs = node
74+
ipfs.id((err, id) => {
75+
expect(err).to.not.exist()
76+
ipfs.stop((err) => {
77+
// TODO: go-ipfs returns an error, https://github.com/ipfs/go-ipfs/issues/4078
78+
if (!id.agentVersion.startsWith('go-ipfs')) {
79+
expect(err).to.not.exist()
80+
}
81+
done()
82+
})
83+
})
84+
})
85+
})
86+
})
87+
88+
it('.stat gives error while offline', () => {
89+
ipfs.bitswap.stat((err, stats) => {
90+
expect(err).to.exist()
91+
expect(stats).to.not.exist()
92+
})
93+
})
94+
95+
it('.wantlist gives error if offline', () => {
96+
ipfs.bitswap.wantlist((err, list) => {
97+
expect(err).to.exist()
98+
expect(list).to.not.exist()
99+
})
100+
})
101+
102+
it('.unwant gives error if offline', () => {
103+
expect(() => ipfs.bitswap.unwant('my key'), (err) => {
104+
expect(err).to.exist()
105+
})
106+
})
107+
})
108+
}

js/src/index.js

+1
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)