From 1d08f996c585e71ee7e25363e724676fb5b56c78 Mon Sep 17 00:00:00 2001 From: haad Date: Fri, 9 May 2025 12:36:44 +0200 Subject: [PATCH] Cancel ipfs block fetches when storage is closed --- src/storage/ipfs-block.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/storage/ipfs-block.js b/src/storage/ipfs-block.js index c0b823b..44dfd68 100644 --- a/src/storage/ipfs-block.js +++ b/src/storage/ipfs-block.js @@ -28,6 +28,8 @@ const DefaultTimeout = 30000 // 30 seconds const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => { if (!ipfs) throw new Error('An instance of ipfs is required.') + const timeoutControllers = new Set() + /** * Puts data to an IPFS block. * @function @@ -56,8 +58,10 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => { */ const get = async (hash) => { const cid = CID.parse(hash, base58btc) - const { signal } = new TimeoutController(timeout || DefaultTimeout) - const block = await ipfs.blockstore.get(cid, { signal }) + const controller = new TimeoutController(timeout || DefaultTimeout) + timeoutControllers.add(controller) + const block = await ipfs.blockstore.get(cid, { signal: controller.signal }) + timeoutControllers.delete(controller) if (block) { return block } @@ -76,7 +80,12 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => { const clear = async () => {} - const close = async () => {} + const close = async () => { + for (const controller in timeoutControllers) { + controller.abort() + } + timeoutControllers.clear() + } return { put,