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

Commit

Permalink
fix: throw if asked to delete a block we don't have (#88)
Browse files Browse the repository at this point in the history
This is to be consistent with go-IPFS

BREAKING CHANGE:

`bs.delete(cid)` used to ignore mystery CIDs, now it throws.
  • Loading branch information
achingbrain authored Apr 16, 2020
1 parent 0c5f17c commit 4ffb28c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
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

0 comments on commit 4ffb28c

Please sign in to comment.