From 8d9808acd5c37fe1f0050091f82b9a836299ffd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20B=C3=A4r?= Date: Wed, 20 Nov 2024 17:01:28 +0100 Subject: [PATCH] fix: no Brotli decompression on an empty `HEAD` response (#150) Closes #149 --- package-lock.json | 4 ++-- package.json | 2 +- src/hooks/fix-decompress.ts | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index d95e88c..2eb5592 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "got-scraping", - "version": "4.0.7", + "version": "4.0.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "got-scraping", - "version": "4.0.7", + "version": "4.0.8", "license": "Apache-2.0", "dependencies": { "got": "^14.2.1", diff --git a/package.json b/package.json index 754096e..99da930 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "got-scraping", - "version": "4.0.7", + "version": "4.0.8", "description": "HTTP client made for scraping based on got.", "engines": { "node": ">=16" 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); }