From 5d04e53355be79416e951ae081972fc99c8588bb 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 | 15 +++++- js/src/bitswap.js | 134 ++++++++++++++++++++++++++++++++++++++++++++++ js/src/index.js | 1 + 3 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 js/src/bitswap.js diff --git a/SPEC/BITSWAP.md b/SPEC/BITSWAP.md index 00bc5fc1..19ba3245 100644 --- a/SPEC/BITSWAP.md +++ b/SPEC/BITSWAP.md @@ -4,7 +4,20 @@ * [bitswap.unwant](#bitswapunwant) * [bitswap.stat](#bitswapstat) -#### `bitswap.wantlist` +#### `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 + +### `bitswap.wantlist` (not spec'ed yet) diff --git a/js/src/bitswap.js b/js/src/bitswap.js new file mode 100644 index 00000000..3c6771fb --- /dev/null +++ b/js/src/bitswap.js @@ -0,0 +1,134 @@ +'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 + let withGo + 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 * 250) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + ipfs.block.get(key) + .then(() => {}) + .catch(() => {}) + ipfs.id((err, id) => { + expect(err).to.not.exist() + withGo = id.agentVersion.startsWith('go-ipfs') + setTimeout(done, 250) //Give block.get time to take effect + }) + }) + }) + }) + + after((done) => common.teardown(done)) + + it('.stat', (done) => { + + ipfs.bitswap.stat((err, stats) => { + statsTests.expectIsBitswap(err, stats) + done() + }) + }) + + it('.wantlist', (done) => { + ipfs.bitswap.wantlist((err, list) => { + expect(err).to.not.exist() + expect(list.Keys).to.have.length(1); + expect(list.Keys[0]['/']).to.equal(key) + done() + }) + }) + + it('.unwant', function (done) { + if (withGo) { + this.skip() + } + ipfs.bitswap.unwant(key, (err) => { + expect(err).to.not.exist(); + ipfs.bitswap.wantlist((err, list) => { + expect(err).to.not.exist(); + expect(list.Keys).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() + //When run against core we get our expected error, when run + //as part of the http tests we get a connection refused + if (err.code !== 'ECONNREFUSED') { + expect(err).to.match(/online mode/) + } + expect(stats).to.not.exist() + }) + }) + + it('.wantlist gives error if offline', () => { + ipfs.bitswap.wantlist((err, list) => { + expect(err).to.exist() + //When run against core we get our expected error, when run + //as part of the http tests we get a connection refused + if (err.code !== 'ECONNREFUSED') { + expect(err).to.match(/online mode/) + } + expect(list).to.not.exist() + }) + }) + + it('.unwant gives error if offline', () => { + expect(() => ipfs.bitswap.unwant(key, (err) => { + expect(err).to.exist() + //When run against core we get our expected error, when run + //as part of the http tests we get a connection refused + if (err.code !== 'ECONNREFUSED') { + expect(err).to.match(/online mode/) + } + })) + }) + }) +} diff --git a/js/src/index.js b/js/src/index.js index dd2012e9..07904c43 100644 --- a/js/src/index.js +++ b/js/src/index.js @@ -19,3 +19,4 @@ exports.repo = require('./repo') exports.bootstrap = require('./bootstrap') exports.types = require('./types') exports.util = require('./util') +exports.bitswap = require('./bitswap')