From 7b83894715ddb48d27683fdb61801644172b9ffa Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Mon, 16 Apr 2018 09:24:02 -0700 Subject: [PATCH] feat: add bitswap.unwant javascript spec --- SPEC/BITSWAP.md | 13 ++++++- js/src/bitswap.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++ js/src/index.js | 1 + 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 js/src/bitswap.js diff --git a/SPEC/BITSWAP.md b/SPEC/BITSWAP.md index 678b5995..00c4e7c6 100644 --- a/SPEC/BITSWAP.md +++ b/SPEC/BITSWAP.md @@ -3,7 +3,18 @@ Bitswap API #### `wantlist` (not spec'ed yet) -#### `unwant` (not spec'ed yet) +#### `unwant` + +> Removes a given block from your wantlist + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.bitswap.unwant(cid) + +`cid` is a [cid][cid] which can be passed as: + +- CID, a CID instance +- String, the base58 encoded version of the multihash #### `stat` diff --git a/js/src/bitswap.js b/js/src/bitswap.js new file mode 100644 index 00000000..e2929808 --- /dev/null +++ b/js/src/bitswap.js @@ -0,0 +1,99 @@ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +const statsTests = require('./utils/stats') +chai.use(dirtyChai) +const CID = require('cids') + +module.exports = (common) => { + describe('.bitswap online', () => { + let ipfs + const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'; + + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(60 * 1000) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + ipfs.block.get(new CID(key)) + .then(() => {}) + .catch(() => {}) + setTimeout(done, 800) + }) + }) + }) + + after((done) => common.teardown(done)) + + it('.stat', (done) => { + + ipfs.bitswap.stat((err, stats) => { + statsTests.expectIsBitswap(err, stats) + done() + }) + }) + + it('.wantlist', (done) => { + const wantlist = ipfs.bitswap.wantlist() + expect(wantlist[0].cid.toBaseEncodedString()).to.equal(key) + done() + }) + + it('.unwant', (done) => { + ipfs.bitswap.unwant(new CID(key)) + const wantlist = ipfs.bitswap.wantlist() + expect(wantlist).to.be.empty() + done() + }) + }) + + describe('.bitswap offline', () => { + let ipfs + + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(60 * 1000) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + ipfs.id((err, id) => { + expect(err).to.not.exist() + ipfs.stop((err) => { + // TODO: go-ipfs returns an error, https://github.com/ipfs/go-ipfs/issues/4078 + if (!id.agentVersion.startsWith('go-ipfs')) { + expect(err).to.not.exist() + } + done() + }) + }) + }) + }) + }) + + it('.stat gives error while offline', () => { + ipfs.bitswap.stat((err, stats) => { + expect(err).to.exist() + expect(stats).to.not.exist() + }) + }) + + it('.wantlist throws if offline', () => { + expect(() => ipfs.bitswap.wantlist()).to.throw(/online/) + }) + + it('.unwant throws if offline', () => { + expect(() => ipfs.bitswap.unwant('my key')).to.throw(/online/) + }) + }) +} diff --git a/js/src/index.js b/js/src/index.js index 9f527e19..90463eb9 100644 --- a/js/src/index.js +++ b/js/src/index.js @@ -16,3 +16,4 @@ exports.key = require('./key') exports.stats = require('./stats') exports.repo = require('./repo') exports.bootstrap = require('./bootstrap') +exports.bitswap = require('./bitswap')