From 776006680d728b8eba340fe0695d5dca9cdfbd11 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Sat, 1 May 2021 07:13:26 +0100 Subject: [PATCH] fix: return type of deleteMany should be the cids you deleted (#311) 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. --- package.json | 1 - src/blockstore.js | 17 +++++++++++++++-- src/types.d.ts | 2 +- test/blockstore-test.js | 4 +++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index ae5eb9fb..9798fec8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/blockstore.js b/src/blockstore.js index 3bef5654..0ea1619a 100644 --- a/src/blockstore.js +++ b/src/blockstore.js @@ -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') /** @@ -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 () { diff --git a/src/types.d.ts b/src/types.d.ts index fdc53275..fd3c0db9 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -105,7 +105,7 @@ export interface Blockstore { /** * Delete a block from the store */ - deleteMany: (cids: AwaitIterable, options?: DatastoreOptions) => AsyncIterable + deleteMany: (cids: AwaitIterable, options?: DatastoreOptions) => AsyncIterable /** * Close the store diff --git a/test/blockstore-test.js b/test/blockstore-test.js index f5590f04..557434f9 100644 --- a/test/blockstore-test.js +++ b/test/blockstore-test.js @@ -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 () => {