Skip to content

Commit

Permalink
enhance(server): cork the response once for the status code, headers …
Browse files Browse the repository at this point in the history
…and the response(if static)
  • Loading branch information
ardatan committed Jul 17, 2023
1 parent 12eff3a commit 633655d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-socks-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/server': patch
---

Cork the response once for status codes and headers with the static response in uWS handler
36 changes: 17 additions & 19 deletions packages/server/src/uwebsockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,30 @@ export async function sendResponseToUwsOpts({ res, response }: SendResponseToUWS
res.onAborted(function () {
resAborted = true;
});
const isStringOrBuffer =
(response as any).bodyType === 'String' || (response as any).bodyType === 'Uint8Array';
res.cork(() => {
res.writeStatus(`${response.status} ${response.statusText}`);
});
response.headers.forEach((value, key) => {
// content-length causes an error with Node.js's fetch
if (key !== 'content-length') {
if (key === 'set-cookie') {
const setCookies = response.headers.getSetCookie?.();
if (setCookies) {
setCookies.forEach(setCookie => {
res.cork(() => {
for (const [key, value] of response.headers) {
// content-length causes an error with Node.js's fetch
if (key !== 'content-length') {
if (key === 'set-cookie') {
const setCookies = response.headers.getSetCookie?.();
if (setCookies) {
for (const setCookie of setCookies) {
res.writeHeader(key, setCookie);
});
});
return;
}
continue;
}
}
}
res.cork(() => {
res.writeHeader(key, value);
});
}
}
});
if ((response as any).bodyType === 'String' || (response as any).bodyType === 'Uint8Array') {
res.cork(() => {
if (isStringOrBuffer) {
res.end((response as any).bodyInit);
});
}
});
if (isStringOrBuffer) {
return;
}
if (!response.body) {
Expand Down

0 comments on commit 633655d

Please sign in to comment.