fix(response-stream): fix response with no content doesn't correctly end the writable stream #206
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of change
Previously we are detecting HTTP status codes that indicate no body (
status >= 100 && status < 200 || status === 304 || status === 204 || method === 'HEAD'
). This is not great since every HTTP status code can have empty body. It's totally legit to have a response with 200 status code but empty body.Actually, the root cause of stream not being correctly ended, is that we skipped the
0\r\n\r\n
right after the HTTP header:As you can see in the screenshot (captured from AWS Lambda logs), for responses with empty body, the first write contains both headers and the chunk terminator
0\r\n\r\n
.This PR tries to detect that chunk terminator immediately after the headers, so we can close the stream correctly.
Tested working on AWS lambda with examples from https://github.com/H4ad/serverless-adapter-examples (
fastify-aws-stream
andnestjs-aws-stream
).Here is a before & after comparison.
Before (and the request just hang until Lambda timeout):
After:
Pull-Request Checklist
main
branchnpm run lint
passes with this changenpm run test
passes with this changeFixes #0000
www/docs/main
folder.index.doc.ts
.