HttpResponseStream doesn't work when `end`ing the stream without a `write`
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- aws, node.js, typescript
Research direction
Start by reading src/HttpResponseStream.js, especially the onBeforeFirstWrite handling, then trace the Node ClientRequest end path described in the issue. Reproduce both responseStream.end("Not Found") and responseStream.end() cases and verify that the custom status code and headers are applied in each case.
Written by the indexing model from the issue text.
Description
When using HttpResponseStream to set the status code and headers on a streaming response, I noticed that if I never call write on the stream, the custom status code and headers don't work.
Example repro:
export const handler = awslambda.streamifyResponse(
async (event, responseStream, context) => {
const metadata = {
statusCode: 404,
headers: { "Content-Type": "text/plain", "X-Foo": "Bar" }
};
responseStream = awslambda.HttpResponseStream.from(responseStream, metadata);
// This will cause a 502 with no custom response headers:
responseStream.end("Not Found");
// This will cause a 200 with no custom response headers:
responseStream.end();
}
);
I believe this is because HttpResponseStream relies on the onBeforeFirstWrite callback:
onBeforeFirstWrite is implemented by overriding http.ClientRequest stream's write:
// https://github.com/aws/aws-lambda-base-images/tree/nodejs18.x -> /var/runtime/index.mjs
req.write = function(chunk, encoding, callback) {
vvverbose("ResponseStream::write", chunk.length, "callback:", typeof callback);
if (typeof chunk !== "string" && !Buffer.isBuffer(chunk) && chunk?.constructor !== Uint8Array) {
chunk = JSON.stringify(chunk);
}
if (status === STATUS_READY && typeof this._onBeforeFirstWrite === "function") {
this._onBeforeFirstWrite((ch) => origWrite(ch));
}
const ret = origWrite(chunk, encoding, callback);
// [snip]
But turns out Node's ClientRequest doesn't call write when ending the stream with a final chunk of data, it calls an internal write_ instead:
I guess this could also be considered a Node bug, because their documentation for ClientRequest.end says:
If
datais specified, it is equivalent to callingrequest.write(data, encoding)followed byrequest.end(callback).
But even if it did implement that contract correctly, there's still the case of ending the stream with no data, i.e., responseStream.end().
- Dominant language
- TypeScript
- Stars
- 224
- Forks
- 60
- Avg merge
- 26m
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from aws/aws-lambda-nodejs-runtime-interface-client
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
aws/aws-lambda-nodejs-runtime-interface-client#198 · 3 comments · 6 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
aws/aws-lambda-nodejs-runtime-interface-client#195 · 12 reactions ·
-
bug
aws/aws-lambda-nodejs-runtime-interface-client#170 · 15 comments · 6 reactions · 1 assignee ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 38/100
aws/aws-lambda-nodejs-runtime-interface-client#159 · 2 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
aws/aws-lambda-nodejs-runtime-interface-client#145 · 1 comment · 6 reactions ·
All issues in aws/aws-lambda-nodejs-runtime-interface-client
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
bug clawsweeper:linked-pr-open clawsweeper:needs-live-repro clawsweeper:no-new-fix-pr impact:message-loss issue-rating: 🐚 platinum hermit P2 regression
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
calcite-components needs triage refactor
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Esri/calcite-design-system#15203 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
fullcalendar/fullcalendar#8106 ·