From 61f15fd5569cbcd61e7db5fed8e6989f724d5462 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Tue, 27 Feb 2024 16:07:16 +0000 Subject: [PATCH] fix: Pin a block. --- src/storage/ipfs-block.js | 3 ++- test/storage/ipfs-block.test.js | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/storage/ipfs-block.js b/src/storage/ipfs-block.js index 226b1b9..a1c4acc 100644 --- a/src/storage/ipfs-block.js +++ b/src/storage/ipfs-block.js @@ -7,6 +7,7 @@ import { CID } from 'multiformats/cid' import { base58btc } from 'multiformats/bases/base58' import { TimeoutController } from 'timeout-abort-controller' +import drain from 'it-drain' const DefaultTimeout = 30000 // 30 seconds @@ -41,7 +42,7 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => { await ipfs.blockstore.put(cid, data, { signal }) if (pin && !(await ipfs.pins.isPinned(cid))) { - await ipfs.pins.add(cid) + await drain(ipfs.pins.add(cid)) } } diff --git a/test/storage/ipfs-block.test.js b/test/storage/ipfs-block.test.js index 7d405e1..03ce491 100644 --- a/test/storage/ipfs-block.test.js +++ b/test/storage/ipfs-block.test.js @@ -26,15 +26,29 @@ describe('IPFSBlockStorage', function () { it('gets a block', async () => { const expected = 'hello world' - const block = await Block.encode({ value: expected, codec, hasher }) - const cid = block.cid.toString(base58btc) + const { cid, bytes } = await Block.encode({ value: expected, codec, hasher }) - await storage.put(cid, 'hello world') - const actual = await storage.get(cid) + const hash = cid.toString(base58btc) + + await storage.put(hash, bytes) + + const data = await storage.get(hash) + const block = await Block.decode({ bytes: data, codec, hasher }) + const actual = block.value strictEqual(actual, expected) }) + + it('checks that a block is pinned', async () => { + const expected = 'hello world' + const block = await Block.encode({ value: expected, codec, hasher }) + const cid = block.cid.toString(base58btc) + await storage.put(cid, block.bytes) + + strictEqual(await ipfs.pins.isPinned(block.cid), true) + }) + it('throws an error if a block does not exist', async () => { const value = 'i don\'t exist' const block = await Block.encode({ value, codec, hasher })