Skip to content
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

Don't return empty string for .json() if Transfer-Encoding is chunked #464

Merged
merged 1 commit into from
Oct 31, 2022
Merged

Don't return empty string for .json() if Transfer-Encoding is chunked #464

merged 1 commit into from
Oct 31, 2022

Conversation

davidrunger
Copy link
Contributor

@davidrunger davidrunger commented Oct 31, 2022

fixes #463

Here is some relevant documentation about the Transfer-Encoding header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding , specifically when a chunked value is set for that header:

chunked: Data is sent in a series of chunks. The Content-Length header is omitted in this case [...] [emphasis added]

This is in contradiction to an implicit assumption seemingly made in #459 that a Content-Length header will always be set to some non-zero value if there is content in the response body.

The logic change in this PR returns a blank string for .json() only if the Content-Length header is '0' or missing (per #459) AND if there is not a Transfer-Encoding header with a value of chunked.

The test added in this PR sets a Transfer-Encoding: chunked header (and omits a Content-Length header) via the following setup, which writes to the response in chunks rather than all at once:

server.get('/', async (request, response) => {
  response.write('[');
  response.write('"one",');
  response.write('"two"');
  response.end(']');
});

@sindresorhus sindresorhus merged commit 195e0e2 into sindresorhus:main Oct 31, 2022
@sindresorhus
Copy link
Owner

Thanks for fixing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants