diff --git a/package.json b/package.json index 29f7726..89f70aa 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,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 f5dac24..b437818 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 @@ -127,7 +128,11 @@ 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} */ - delete (cid, options) { + 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, 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) 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)