From 50a4c80713839289b2a07fd4c9668fde029b7552 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 23 Apr 2024 09:19:17 +0100 Subject: [PATCH] feat: expose allowBigBlock option (#227) To allow adding large blocks to the blockstore, expose the `--allow-big-block` flag from `ipfs block put`. --- src/block/index.ts | 7 +++++++ test/interface-tests/src/block/put.ts | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/block/index.ts b/src/block/index.ts index f56398153..26c2a9cda 100644 --- a/src/block/index.ts +++ b/src/block/index.ts @@ -26,6 +26,13 @@ export interface BlockPutOptions extends HTTPRPCOptions { * Pin this block when adding. (Defaults to `false`) */ pin?: boolean + + /** + * Allow creating blocks larger than 1MB + * + * @default false + */ + allowBigBlock?: boolean } export interface BlockRmOptions extends HTTPRPCOptions { diff --git a/test/interface-tests/src/block/put.ts b/test/interface-tests/src/block/put.ts index 1b37992df..2dca865c9 100644 --- a/test/interface-tests/src/block/put.ts +++ b/test/interface-tests/src/block/put.ts @@ -10,6 +10,8 @@ import { getDescribe, getIt, type MochaConfig } from '../utils/mocha.js' import type { KuboRPCClient } from '../../../../src/index.js' import type { KuboRPCFactory } from '../index.js' +const ONE_MEG = 1024 * 1024 + export function testPut (factory: KuboRPCFactory, options: MochaConfig): void { const describe = getDescribe(options) const it = getIt(options) @@ -64,5 +66,23 @@ export function testPut (factory: KuboRPCFactory, options: MochaConfig): void { expect(cid.multihash.bytes).to.equalBytes(expectedCID.multihash.bytes) }) + + it('should fail to put a big block', async () => { + await expect(ipfs.block.put(new Uint8Array(ONE_MEG + 1))) + .to.eventually.be.rejected() + .with.property('message') + .that.include('produced block is over 1MiB') + }) + + it('should put a big block with `allowBigBlock`', async () => { + const expectedHash = 'bafkreibmw5hnxj2uvaorehe5w2btobfi47kbpznrhunbt5fff4ah2zccmq' + const expectedCID = CID.parse(expectedHash) + + const cid = await ipfs.block.put(new Uint8Array(ONE_MEG + 1), { + allowBigBlock: true + }) + + expect(cid.toString()).to.equal(expectedCID.toString()) + }) }) }