From 66ff28c21eac7f0cca0d3ccf141b1dc4cafa4888 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 18 Jan 2024 22:24:02 +0530 Subject: [PATCH] fix(node): update `req.socket` on WS upgrade (#21984) Update the `req.socket` to be a `net.Socket` from `FakeSocket` Fixes #21979 --- cli/tests/unit_node/http_test.ts | 4 +++- ext/node/polyfills/http.ts | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts index 91e2f57030..3573416f83 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" + diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index fe58965344..9356dde0ad 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 {