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

Commit 4ffb28c

Browse files
authored
fix: throw if asked to delete a block we don't have (#88)
This is to be consistent with go-IPFS BREAKING CHANGE: `bs.delete(cid)` used to ignore mystery CIDs, now it throws.
1 parent 0c5f17c commit 4ffb28c

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"npm": ">=3.0.0"
4848
},
4949
"dependencies": {
50+
"err-code": "^2.0.0",
5051
"streaming-iterables": "^4.1.0"
5152
},
5253
"contributors": [

src/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const { map } = require('streaming-iterables')
4+
const errcode = require('err-code')
45

56
/**
67
* BlockService is a hybrid block datastore. It stores data in a local
@@ -127,7 +128,11 @@ class BlockService {
127128
* @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
128129
* @returns {Promise}
129130
*/
130-
delete (cid, options) {
131+
async delete (cid, options) {
132+
if (!await this._repo.blocks.has(cid)) {
133+
throw errcode(new Error('blockstore: block not found'), 'ERR_BLOCK_NOT_FOUND')
134+
}
135+
131136
return this._repo.blocks.delete(cid, options)
132137
}
133138
}

test/aborting-requests.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe('aborting requests', () => {
3333
putMany: abortOnSignal,
3434
get: abortOnSignal,
3535
delete: abortOnSignal,
36-
deleteMany: abortOnSignal
36+
deleteMany: abortOnSignal,
37+
has: () => true
3738
}
3839
}
3940
r = new BlockService(repo)

test/block-service-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ module.exports = (repo) => {
8080
expect(res).to.be.eql(false)
8181
})
8282

83+
it('does not delete a block it does not have', async () => {
84+
const data = Buffer.from('Will not live that much ' + Date.now())
85+
const cid = new CID(await multihashing(data, 'sha2-256'))
86+
87+
await bs.delete(cid)
88+
.then(
89+
() => expect.fail('Should have thrown'),
90+
(err) => expect(err).to.have.property('code', 'ERR_BLOCK_NOT_FOUND')
91+
)
92+
})
93+
8394
it('stores and gets lots of blocks', async function () {
8495
this.timeout(8 * 1000)
8596

0 commit comments

Comments
 (0)