Skip to content

Commit

Permalink
fix: return type of deleteMany should be the cids you deleted (#311)
Browse files Browse the repository at this point in the history
It should not leak datastore keys, instead return the cid you deleted.

Lets us not depend on ipfs-repo or interface-datastore in the types of
ipfs-blockstore, then ipld, then ipfs-http-client.
  • Loading branch information
achingbrain authored May 1, 2021
1 parent e8784a8 commit 7760066
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"ipfs-utils": "^6.0.0",
"ipld-block": "^0.11.0",
"it-filter": "^1.0.2",
"it-map": "^1.0.2",
"it-pushable": "^1.4.0",
"just-safe-get": "^2.0.0",
"just-safe-set": "^2.1.0",
Expand Down
17 changes: 15 additions & 2 deletions src/blockstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const { shard, ShardingDatastore } = require('datastore-core')
const Block = require('ipld-block')
const { cidToKey, keyToCid } = require('./blockstore-utils')
const map = require('it-map')
const drain = require('it-drain')
const pushable = require('it-pushable')
/**
Expand Down Expand Up @@ -135,7 +134,21 @@ function createBaseStore (store) {
},

deleteMany (cids, options) {
return store.deleteMany(map(cids, cid => cidToKey(cid)), options)
const out = pushable()

drain(store.deleteMany((async function * () {
for await (const cid of cids) {
yield cidToKey(cid)

out.push(cid)
}

out.end()
}()), options)).catch(err => {
out.end(err)
})

return out
},

close () {
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export interface Blockstore {
/**
* Delete a block from the store
*/
deleteMany: (cids: AwaitIterable<any>, options?: DatastoreOptions) => AsyncIterable<Key>
deleteMany: (cids: AwaitIterable<any>, options?: DatastoreOptions) => AsyncIterable<CID>

/**
* Close the store
Expand Down
4 changes: 3 additions & 1 deletion test/blockstore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,11 @@ module.exports = (repo) => {

describe('.deleteMany', () => {
it('simple', async () => {
await drain(repo.blocks.deleteMany([b.cid]))
const deleted = await all(repo.blocks.deleteMany([b.cid]))
const exists = await repo.blocks.has(b.cid)
expect(exists).to.equal(false)
expect(deleted).to.have.lengthOf(1)
expect(deleted[0]).to.deep.equal(b.cid)
})

it('including identity cid', async () => {
Expand Down

0 comments on commit 7760066

Please sign in to comment.