From bb0f3d2ca9268e2ca8e66d2750b39cc8d231e17f Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 18 Jan 2024 19:12:44 +0530 Subject: [PATCH 1/3] fix(node): update socket held by request on WS upgrade --- ext/node/polyfills/http.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index fe589653447244..9356dde0ad5864 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -1637,6 +1637,8 @@ export class ServerImpl extends EventEmitter { const socket = new Socket({ handle: new TCP(constants.SERVER, conn), }); + // Update socket held by `req`. + req.socket = socket; this.emit("upgrade", req, socket, Buffer.from([])); return response; } else { From c42ad22cde846f5035ad7efa0821c2b41919712e Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 18 Jan 2024 19:22:46 +0530 Subject: [PATCH 2/3] Add test --- cli/tests/unit_node/http_test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts index 91e2f57030edc5..0842befaa4d29e 100644 --- a/cli/tests/unit_node/http_test.ts +++ b/cli/tests/unit_node/http_test.ts @@ -716,7 +716,9 @@ Deno.test( }); // @ts-ignore it's a socket for real let serverSocket; - server.on("upgrade", (_req, socket, _head) => { + server.on("upgrade", (req, socket, _head) => { + // https://github.com/denoland/deno/issues/21979 + assert(req.socket?.write); socket.write( "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" + "Upgrade: WebSocket\r\n" + @@ -741,7 +743,7 @@ Deno.test( const req = http.request(options); req.end(); - req.on("upgrade", (_res, socket, _upgradeHead) => { + req.on("upgrade", (res, socket, _upgradeHead) => { socket.end(); // @ts-ignore it's a socket for real serverSocket!.end(); From 7d5911c853ddbf5f0f891b75dd3fdf32d9992183 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 18 Jan 2024 19:23:10 +0530 Subject: [PATCH 3/3] x --- cli/tests/unit_node/http_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts index 0842befaa4d29e..3573416f83b5a7 100644 --- a/cli/tests/unit_node/http_test.ts +++ b/cli/tests/unit_node/http_test.ts @@ -743,7 +743,7 @@ Deno.test( const req = http.request(options); req.end(); - req.on("upgrade", (res, socket, _upgradeHead) => { + req.on("upgrade", (_res, socket, _upgradeHead) => { socket.end(); // @ts-ignore it's a socket for real serverSocket!.end();