From 9937204e6989756a2597c745be5dc5b2f54a15aa Mon Sep 17 00:00:00 2001 From: Ashcon Partovi Date: Mon, 22 Jan 2024 09:06:17 -0800 Subject: [PATCH 1/2] Allow `statusCode` and `statusMessage` to be changed on IncomingMessage --- src/js/node/http.ts | 12 +++++++++-- test/regression/issue/08258.test.ts | 31 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test/regression/issue/08258.test.ts diff --git a/src/js/node/http.ts b/src/js/node/http.ts index c6401e17e91d01..0a32ddbafb25fb 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -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"); @@ -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; }, }); diff --git a/test/regression/issue/08258.test.ts b/test/regression/issue/08258.test.ts new file mode 100644 index 00000000000000..4d2a44b8040665 --- /dev/null +++ b/test/regression/issue/08258.test.ts @@ -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(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(); +}); From d57507a95c4aaa9ffc949423a3039a3754a96ced Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:10:18 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- bench/snippets/shell-spawn.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/bench/snippets/shell-spawn.mjs b/bench/snippets/shell-spawn.mjs index c7f6bb9593a0c4..aa4da66eeb0fd5 100644 --- a/bench/snippets/shell-spawn.mjs +++ b/bench/snippets/shell-spawn.mjs @@ -34,5 +34,4 @@ group("ls .", () => { }); }); - await run();