Skip to content

Commit

Permalink
fix(node): set default http server response code 200 (denoland#23977)
Browse files Browse the repository at this point in the history
Node sets the default HTTP response status code to 200 on the
`ServerResponse`. We initialised it as `undefined` before which caused a
problem with 11ty's dev server.

Thanks to @vrugtehagel for reporting this issue and finding the correct
fix as well 🎉

Fixes denoland#23970
  • Loading branch information
marvinhagemeister committed May 26, 2024
1 parent f8975a8 commit 8806eac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 3 additions & 4 deletions ext/node/polyfills/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ function onError(self, error, cb) {
}

export class ServerResponse extends NodeWritable {
statusCode?: number = undefined;
statusCode = 200;
statusMessage?: string = undefined;
#headers = new Headers({});
#readable: ReadableStream;
Expand Down Expand Up @@ -1444,8 +1444,7 @@ export class ServerResponse extends NodeWritable {
}

#ensureHeaders(singleChunk?: Chunk) {
if (this.statusCode === undefined) {
this.statusCode = 200;
if (this.statusCode === 200 && this.statusMessage === undefined) {
this.statusMessage = "OK";
}
if (
Expand All @@ -1460,7 +1459,7 @@ export class ServerResponse extends NodeWritable {
this.headersSent = true;
this.#ensureHeaders(singleChunk);
let body = singleChunk ?? (final ? null : this.#readable);
if (ServerResponse.#bodyShouldBeNull(this.statusCode!)) {
if (ServerResponse.#bodyShouldBeNull(this.statusCode)) {
body = null;
}
this.#resolve(
Expand Down
6 changes: 6 additions & 0 deletions tests/unit_node/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,12 @@ Deno.test("[node/http] ServerResponse getHeaders", () => {
assertEquals(res.getHeaders(), { "bar": "baz", "foo": "bar" });
});

Deno.test("[node/http] ServerResponse default status code 200", () => {
const req = new http.IncomingMessage(new net.Socket());
const res = new http.ServerResponse(req);
assertEquals(res.statusCode, 200);
});

Deno.test("[node/http] maxHeaderSize is defined", () => {
assertEquals(http.maxHeaderSize, 16_384);
});

0 comments on commit 8806eac

Please sign in to comment.