From 34c15a6fad6c93705b6192f47bd9cfbc8937486c Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Tue, 3 Dec 2024 21:51:17 -0500 Subject: [PATCH] fix: Prevent gzip decompressor from hanging --- src/files/nifti.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/files/nifti.ts b/src/files/nifti.ts index 58a05895..edea1537 100644 --- a/src/files/nifti.ts +++ b/src/files/nifti.ts @@ -11,20 +11,24 @@ async function extract(buffer: Uint8Array, nbytes: number): Promise const stream = new ReadableStream({ start(controller) { controller.enqueue(buffer) + controller.close() }, }) const reader = stream.pipeThrough(new DecompressionStream('gzip')).getReader() let offset = 0 - while (offset < nbytes) { - const { value, done } = await reader.read() - if (done) { - break + try { + while (offset < nbytes) { + const { value, done } = await reader.read() + if (done || !value) { + break + } + result.set(value.subarray(0, Math.min(value.length, nbytes - offset)), offset) + offset += value.length } - result.set(value.subarray(0, Math.min(value.length, nbytes - offset)), offset) - offset += value.length + } finally { + await reader.cancel() } - await reader.cancel() - return result + return result.subarray(0, offset) } export async function loadHeader(file: BIDSFile): Promise {