From cf048575972d1dc98392092db65ce450b21464e4 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 4 Oct 2019 17:51:43 +0100 Subject: [PATCH 1/2] fix: throw if asked to delete a block we don't have This is to be consistent with go-IPFS BREAKING CHANGE: `bs.delete(cid)` used to ignore mystery CIDs, now it throws. --- package.json | 1 + src/index.js | 7 ++++++- test/block-service-test.js | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8e984fb..eb73c7b 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "npm": ">=3.0.0" }, "dependencies": { + "err-code": "^2.0.0", "streaming-iterables": "^4.1.0" }, "contributors": [ diff --git a/src/index.js b/src/index.js index d29beaf..2d8a9ec 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ 'use strict' const { map } = require('streaming-iterables') +const errcode = require('err-code') /** * BlockService is a hybrid block datastore. It stores data in a local @@ -117,7 +118,11 @@ class BlockService { * @param {CID} cid * @returns {Promise} */ - delete (cid) { + async delete (cid) { + if (!await this._repo.blocks.has(cid)) { + throw errcode(new Error('blockstore: block not found'), 'ERR_BLOCK_NOT_FOUND') + } + return this._repo.blocks.delete(cid) } } diff --git a/test/block-service-test.js b/test/block-service-test.js index be55cc5..3f16bba 100644 --- a/test/block-service-test.js +++ b/test/block-service-test.js @@ -80,6 +80,17 @@ module.exports = (repo) => { expect(res).to.be.eql(false) }) + it('does not delete a block it does not have', async () => { + const data = Buffer.from('Will not live that much ' + Date.now()) + const cid = new CID(await multihashing(data, 'sha2-256')) + + await bs.delete(cid) + .then( + () => expect.fail('Should have thrown'), + (err) => expect(err).to.have.property('code', 'ERR_BLOCK_NOT_FOUND') + ) + }) + it('stores and gets lots of blocks', async function () { this.timeout(8 * 1000) From f2a4636c66b777843eb330c9c56d0c358df08ec1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 16 Apr 2020 15:48:37 +0100 Subject: [PATCH 2/2] fix: fix tests --- src/index.js | 4 ++-- test/aborting-requests.spec.js | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 23d050e..b437818 100644 --- a/src/index.js +++ b/src/index.js @@ -128,12 +128,12 @@ class BlockService { * @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation * @returns {Promise} */ - async delete (cid) { + async delete (cid, options) { if (!await this._repo.blocks.has(cid)) { throw errcode(new Error('blockstore: block not found'), 'ERR_BLOCK_NOT_FOUND') } - return this._repo.blocks.delete(cid) + return this._repo.blocks.delete(cid, options) } } diff --git a/test/aborting-requests.spec.js b/test/aborting-requests.spec.js index 524af6e..40b3d68 100644 --- a/test/aborting-requests.spec.js +++ b/test/aborting-requests.spec.js @@ -33,7 +33,8 @@ describe('aborting requests', () => { putMany: abortOnSignal, get: abortOnSignal, delete: abortOnSignal, - deleteMany: abortOnSignal + deleteMany: abortOnSignal, + has: () => true } } r = new BlockService(repo)