Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow statusCode and statusMessage to be changed on IncomingMessage #8368

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion bench/snippets/shell-spawn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ group("ls .", () => {
});
});


await run();
12 changes: 10 additions & 2 deletions src/js/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ var isNextIncomingMessageHTTPS = false;

var typeSymbol = Symbol("type");
var reqSymbol = Symbol("req");
var statusSymbol = Symbol("status");
var statusMessageSymbol = Symbol("statusMessage");
var bodyStreamSymbol = Symbol("bodyStream");
var noBodySymbol = Symbol("noBody");
var abortedSymbol = Symbol("aborted");
Expand Down Expand Up @@ -771,13 +773,19 @@ Object.defineProperty(IncomingMessage.prototype, "connection", {

Object.defineProperty(IncomingMessage.prototype, "statusCode", {
get() {
return this[reqSymbol].status;
return this[statusSymbol] ?? this[reqSymbol].status;
},
set(value) {
this[statusSymbol] = value;
},
});

Object.defineProperty(IncomingMessage.prototype, "statusMessage", {
get() {
return STATUS_CODES[this[reqSymbol].status];
return this[statusMessageSymbol] ?? STATUS_CODES[this[reqSymbol].status];
},
set(value) {
this[statusMessageSymbol] = value;
},
Copy link
Contributor

Choose a reason for hiding this comment

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

For context:

Node http2 uses a symbol called 'state':
https://github.com/nodejs/node/blob/main/lib/internal/http2/compat.js#L71

Related to IncomingMessage however Node uses a data property (not accessor):
https://github.com/nodejs/node/blob/main/lib/_http_incoming.js#L88

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah, we should just set the property on the object and not use a getter/setter

});

Expand Down
31 changes: 31 additions & 0 deletions test/regression/issue/08258.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { test, expect, mock, afterAll } from "bun:test";
import type { AddressInfo } from "node:net";
import type { Server } from "node:http";
import { createServer } from "node:http";
import { reject } from "lodash";

let server: Server;

test("can set statusCode and statusMessage on IncomingMessage", async () => {
const fn = mock((req, res) => {
req.statusCode = 404;
expect(req.statusCode).toBe(404);
req.statusMessage = "Who dis?";
expect(req.statusMessage).toBe("Who dis?");
res.end();
});
server = createServer(fn).listen(0);
const url = await new Promise<string>(resolve => {
server.on("listening", async () => {
const { address, port, family } = server.address() as AddressInfo;
resolve(`http://${family === "IPv6" ? `[${address}]` : address}:${port}/`);
});
server.on("error", reject);
});
expect(fetch(url)).resolves.toBeInstanceOf(Response);
expect(fn).toHaveBeenCalledTimes(1);
});

afterAll(() => {
server?.close();
});
Loading