Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

fix: throw if asked to delete a block we don't have #88

Merged
merged 3 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"npm": ">=3.0.0"
},
"dependencies": {
"err-code": "^2.0.0",
"streaming-iterables": "^4.1.0"
},
"contributors": [
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/aborting-requests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ describe('aborting requests', () => {
putMany: abortOnSignal,
get: abortOnSignal,
delete: abortOnSignal,
deleteMany: abortOnSignal
deleteMany: abortOnSignal,
has: () => true
}
}
r = new BlockService(repo)
Expand Down
11 changes: 11 additions & 0 deletions test/block-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down