From 59f419bf410e861c49cdfa6c1b0e657cf8bcc8e3 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 19 Jan 2024 13:09:37 +0100 Subject: [PATCH] fix(node/http): remoteAddress and remotePort not being set (#21998) Ultimately, it came down to using incorrect property names in the `FakeSocket` constructor. The values are passed under `remoteAddress`, not `hostname` and `remotePort` instead of `port`. Fixes https://github.com/denoland/deno/issues/21980 --- cli/tests/unit_node/http_test.ts | 24 ++++++++++++++++++++++++ ext/node/polyfills/http.ts | 12 +++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts index 3573416f83..7b2012471a 100644 --- a/cli/tests/unit_node/http_test.ts +++ b/cli/tests/unit_node/http_test.ts @@ -184,6 +184,30 @@ Deno.test("[node/http] server can respond with 101, 204, 205, 304 status", async } }); +Deno.test("[node/http] IncomingRequest socket has remoteAddress + remotePort", async () => { + const { promise, resolve } = Promise.withResolvers(); + + let remoteAddress: string | undefined; + let remotePort: number | undefined; + const server = http.createServer((req, res) => { + remoteAddress = req.socket.remoteAddress; + remotePort = req.socket.remotePort; + res.end(); + }); + server.listen(async () => { + // deno-lint-ignore no-explicit-any + const port = (server.address() as any).port; + const res = await fetch( + `http://127.0.0.1:${port}/`, + ); + await res.arrayBuffer(); + assertEquals(remoteAddress, "127.0.0.1"); + assertEquals(typeof remotePort, "number"); + server.close(() => resolve()); + }); + await promise; +}); + Deno.test("[node/http] request default protocol", async () => { const deferred1 = Promise.withResolvers(); const deferred2 = Promise.withResolvers(); diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index 9654d3beba..2da19f7f5d 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -283,10 +283,16 @@ const kError = Symbol("kError"); const kUniqueHeaders = Symbol("kUniqueHeaders"); class FakeSocket extends EventEmitter { - constructor(opts = {}) { + constructor( + opts: { + encrypted?: boolean | undefined; + remotePort?: number | undefined; + remoteAddress?: string | undefined; + } = {}, + ) { super(); - this.remoteAddress = opts.hostname; - this.remotePort = opts.port; + this.remoteAddress = opts.remoteAddress; + this.remotePort = opts.remotePort; this.encrypted = opts.encrypted; this.writable = true; this.readable = true;