diff --git a/package.json b/package.json index 88b31c865..6b710afb5 100644 --- a/package.json +++ b/package.json @@ -107,7 +107,7 @@ "browser-process-platform": "~0.1.1", "cross-env": "^6.0.0", "go-ipfs-dep": "^0.4.22", - "interface-ipfs-core": "^0.117.0", + "interface-ipfs-core": "^0.117.2", "ipfsd-ctl": "^0.47.1", "nock": "^11.3.2", "stream-equal": "^1.1.1" diff --git a/src/block/index.js b/src/block/index.js index f346aa454..645f47df6 100644 --- a/src/block/index.js +++ b/src/block/index.js @@ -1,13 +1,24 @@ 'use strict' +const nodeify = require('promise-nodeify') const moduleConfig = require('../utils/module-config') +const { collectify } = require('../lib/converters') -module.exports = (arg) => { +module.exports = (arg, config) => { const send = moduleConfig(arg) + const rm = require('./rm-async-iterator')(config) return { get: require('./get')(send), stat: require('./stat')(send), - put: require('./put')(send) + put: require('./put')(send), + rm: (input, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + return nodeify(collectify(rm)(input, options), callback) + }, + _rmAsyncIterator: rm } } diff --git a/src/block/rm-async-iterator.js b/src/block/rm-async-iterator.js new file mode 100644 index 000000000..b934df32b --- /dev/null +++ b/src/block/rm-async-iterator.js @@ -0,0 +1,37 @@ +'use strict' + +const CID = require('cids') +const ndjson = require('iterable-ndjson') +const configure = require('../lib/configure') +const toIterable = require('../lib/stream-to-iterable') +const toCamel = require('../lib/object-to-camel') + +module.exports = configure(({ ky }) => { + return async function * removeBlock (cid, options) { + options = options || {} + + if (!Array.isArray(cid)) { + cid = [cid] + } + + const searchParams = new URLSearchParams() + searchParams.set('stream-channels', true) + searchParams.set('force', options.force || false) + searchParams.set('quiet', options.quiet || false) + + cid.forEach(cid => { + searchParams.append('arg', new CID(cid).toString()) + }) + + const res = await ky.post('block/rm', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }) + + for await (const removed of ndjson(toIterable(res.body))) { + yield toCamel(removed) + } + } +})