-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle truncated compressed response (Z_BUF_ERROR) #19
Comments
I think I have used enough keywords to help others find this issue through Google in relation to Node.js and gzip. Hopefully this will save someone a productive day! |
@issuehunt has funded $40.00 to this issue.
|
I've been looking into this issue and although I understand your explanation I not able to create a test case where I can see any difference between using I created this small test code to tweak the truncated input but the result is the same for me 😅 Do you have any example or more details so we can create a better test case? Thank you!
|
compressed.slice(0, -10).push(Buffer.from('foo', 'utf-8')) should do the trick. |
I've been re-reading the issue and there is something that I don't fully understand 😅 As Node.js Zlib Documentation points out, we have two ways to decompress data. One is piping the source stream data through a zlib stream into a destination stream:
The other one is to decompress in a single step:
Changing the flushing method to Here is a little example
When we decompress a stream with pipes, all the data decompressed before the error is recovered by default.
As a conclusion, I think that we already handle correctly truncated compressed responses 💪 |
Define correctly? The current implementation returns truncated response without any warning of it being truncated. The current issue proposes to detect when this happens and throw an error:
|
You are absolutely right 😅 I've focus on I hope #20 fixes this issue, where an error is thrown with |
@sindresorhus has rewarded $36.00 to @szmarczak. See it on IssueHunt
|
Z_BUF_ERROR
can occur if a response is truncated.I am seeing this relatively a lot when making requests through proxies (mishandled stream or proxy node disconnecting mid response).
If this happens, then
gunzip
will throwZ_BUF_ERROR
error.Once you have
Z_BUF_ERROR
then you can rungunzip
again with{finishFlush: zlib.constants.Z_SYNC_FLUSH}
option. This will allow to flush all data before the error. (The reason for not usingZ_SYNC_FLUSH
on the first go is because you couldn't tell apart corrupt from partial output).See: https://github.com/nodejs/node/blob/master/doc/api/zlib.md#compressing-http-requests-and-responses
In my case, I have been seeing the "unexpected end of file" error a lot in our logs and dug into the origins. It turned out that one of the websites that we are accessing is terminating connections sockets seemingly at random (presumably when it is under high load) – therefore producing truncated output. As the output was gziped, it wasn't obvious what is going on. I have disabled decompression in Got and used
zlib.constants.Z_SYNC_FLUSH
and saw that half of the response is missing.It would be a nice DX improvement if this was detected and a more descriptive error would be thrown instead.
IssueHunt Summary
szmarczak has been rewarded.
Backers (Total: $40.00)
Submitted pull Requests
Tips
The text was updated successfully, but these errors were encountered: