From 1c4772d56140e0f3ab82ffdabefe92c99d7919b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20B=C3=A4r?= Date: Wed, 20 Nov 2024 16:56:02 +0100 Subject: [PATCH] fix: no Brotli decompression on empty HEAD response --- src/hooks/fix-decompress.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/hooks/fix-decompress.ts b/src/hooks/fix-decompress.ts index 68245ee..9ad7292 100644 --- a/src/hooks/fix-decompress.ts +++ b/src/hooks/fix-decompress.ts @@ -56,7 +56,22 @@ const onResponse = (response: IncomingMessage, propagate: (fixedResponse: Incomi } }); } else if (encoding === 'br') { - useDecompressor(zlib.createBrotliDecompress()); + let read = false; + + response.once('data', (chunk: Buffer) => { + read = true; + + response.unshift(chunk); + + const decompressor = zlib.createBrotliDecompress(); + useDecompressor(decompressor); + }); + + response.once('end', () => { + if (!read) { + propagate(response); + } + }); } else { propagate(response); }