Skip to content

Commit

Permalink
fix: no Brotli decompression on an empty HEAD response (#150)
Browse files Browse the repository at this point in the history
Closes #149
  • Loading branch information
barjin authored Nov 20, 2024
1 parent eea94e5 commit 8d9808a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
17 changes: 16 additions & 1 deletion src/hooks/fix-decompress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 8d9808a

Please sign in to comment.