Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions packages/connect-node/src/node-universal-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,23 @@ export async function universalResponseToNodeResponse(
universalResponse: UniversalServerResponse,
nodeResponse: NodeServerResponse,
): Promise<void> {
const it = universalResponse.body?.[Symbol.asyncIterator]();
let isWriteError = false;
try {
if (universalResponse.body !== undefined) {
for await (const chunk of universalResponse.body) {
// we deliberately send headers *in* this loop, not before,
// because we have to give the implementation a chance to
// set response headers
if (!nodeResponse.headersSent) {
nodeResponse.writeHead(
universalResponse.status,
webHeaderToNodeHeaders(universalResponse.header),
);
}
await write(nodeResponse, chunk);
if (it !== undefined) {
let chunk = await it.next();
isWriteError = true;
// we deliberately send headers after first read, not before,
// because we have to give the implementation a chance to
// set response headers
nodeResponse.writeHead(
universalResponse.status,
webHeaderToNodeHeaders(universalResponse.header),
);
isWriteError = false;
for (; chunk.done !== true; chunk = await it.next()) {
isWriteError = true;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do it by using a local try catch but this seemed clearer. I didn't want to rely on node semantics of having a code property.

await write(nodeResponse, chunk.value);
if (
"flush" in nodeResponse &&
typeof nodeResponse.flush == "function"
Expand All @@ -166,6 +170,7 @@ export async function universalResponseToNodeResponse(
// https://github.com/expressjs/compression/blob/ad5113b98cafe1382a0ece30bb4673707ac59ce7/index.js#L70
nodeResponse.flush();
}
isWriteError = false;
}
}
if (!nodeResponse.headersSent) {
Expand All @@ -186,7 +191,13 @@ export async function universalResponseToNodeResponse(
nodeResponse.end();
});
} catch (e) {
// Report write errors to the handler.
if (isWriteError) {
it?.throw?.(e).catch(() => {});
}
throw connectErrorFromNodeReason(e);
} finally {
it?.return?.().catch(() => {});
}
}

Expand Down