Skip to content

Commit

Permalink
fix: Prevent gzip decompressor from hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Dec 4, 2024
1 parent 07efb46 commit 34c15a6
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/files/nifti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ async function extract(buffer: Uint8Array, nbytes: number): Promise<Uint8Array>
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<NiftiHeader> {
Expand Down

0 comments on commit 34c15a6

Please sign in to comment.