From d59599fc187c559ee231882773e1c5a2b932fc3d Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 16 Oct 2024 20:58:44 +0900 Subject: fix(ext/node): fix dns.lookup result ordering (#26264) partially unblocks #25470 This PR aligns the resolution of `localhost` hostname to Node.js behavior. In Node.js `dns.lookup("localhost", (_, addr) => console.log(addr))` prints ipv6 address `::1`, but it prints ipv4 address `127.0.0.1` in Deno. That difference causes some errors in the work of enabling `createConnection` option in `http.request` (#25470). This PR fixes the issue by aligning `dns.lookup` behavior to Node.js. This PR also changes the following behaviors (resolving TODOs): - `http.createServer` now listens on ipv6 address `[::]` by default on linux/mac - `net.createServer` now listens on ipv6 address `[::]` by default on linux/mac These changes are also alignments to Node.js behaviors. --- tests/unit_node/http_test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'tests/unit_node/http_test.ts') diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index f85b1466b..f1ff77bb3 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -318,10 +318,14 @@ Deno.test("[node/http] IncomingRequest socket has remoteAddress + remotePort", a // deno-lint-ignore no-explicit-any const port = (server.address() as any).port; const res = await fetch( - `http://127.0.0.1:${port}/`, + `http://localhost:${port}/`, ); await res.arrayBuffer(); - assertEquals(remoteAddress, "127.0.0.1"); + if (Deno.build.os === "windows") { + assertEquals(remoteAddress, "127.0.0.1"); + } else { + assertEquals(remoteAddress, "::1"); + } assertEquals(typeof remotePort, "number"); server.close(() => resolve()); }); -- cgit v1.2.3 From 458d6278d2835896018086da773fd72b7af8ed66 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:42:15 -0700 Subject: fix(node/http): normalize header names in `ServerResponse` (#26339) Fixes https://github.com/denoland/deno/issues/26115. We weren't normalizing the headers to lower case, so code that attempted to delete the `Content-Length` header (but used a different case) wasn't actually removing the header. --- tests/unit_node/http_test.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests/unit_node/http_test.ts') diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index f1ff77bb3..0faf7fb34 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -1147,6 +1147,34 @@ Deno.test("[node/http] ServerResponse appendHeader set-cookie", async () => { await promise; }); +Deno.test("[node/http] ServerResponse header names case insensitive", async () => { + const { promise, resolve } = Promise.withResolvers(); + const server = http.createServer((_req, res) => { + res.setHeader("Content-Length", "12345"); + res.removeHeader("content-length"); + assertEquals(res.getHeader("Content-Length"), undefined); + assert(!res.hasHeader("Content-Length")); + res.appendHeader("content-length", "12345"); + res.removeHeader("Content-Length"); + assertEquals(res.getHeader("content-length"), undefined); + assert(!res.hasHeader("content-length")); + res.end("Hello World"); + }); + + server.listen(async () => { + const { port } = server.address() as { port: number }; + const res = await fetch(`http://localhost:${port}`); + assertEquals(res.headers.get("Content-Length"), null); + assertEquals(res.headers.get("content-length"), null); + assertEquals(await res.text(), "Hello World"); + server.close(() => { + resolve(); + }); + }); + + await promise; +}); + Deno.test("[node/http] IncomingMessage override", () => { const req = new http.IncomingMessage(new net.Socket()); // https://github.com/dougmoscrop/serverless-http/blob/3aaa6d0fe241109a8752efb011c242d249f32368/lib/request.js#L20-L30 -- cgit v1.2.3 From 8dd6177c624649d75ffcacca77e7c4f48cea07a2 Mon Sep 17 00:00:00 2001 From: Nicola Bovolato <61934734+nicolabovolato@users.noreply.github.com> Date: Fri, 25 Oct 2024 00:02:26 +0200 Subject: fix(ext/node): refactor http.ServerResponse into function class (#26210) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While testing, I found out that light-my-request relies on `ServerResponse.connection`, which is deprecated, so I added that and `socket`, the non deprecated property. It also relies on an undocumented `_header` property, apparently for [raw header processing](https://github.com/fastify/light-my-request/blob/v6.1.0/lib/response.js#L180-L186). I added it as an empty string, feel free to provide other approaches. Fixes #19901 Co-authored-by: Bartek IwaƄczuk --- tests/unit_node/http_test.ts | 80 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) (limited to 'tests/unit_node/http_test.ts') diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index 0faf7fb34..84d6f5727 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -3,10 +3,14 @@ // deno-lint-ignore-file no-console import EventEmitter from "node:events"; -import http, { type RequestOptions, type ServerResponse } from "node:http"; +import http, { + IncomingMessage, + type RequestOptions, + ServerResponse, +} from "node:http"; import url from "node:url"; import https from "node:https"; -import net from "node:net"; +import net, { Socket } from "node:net"; import fs from "node:fs"; import { text } from "node:stream/consumers"; @@ -1704,3 +1708,75 @@ Deno.test("[node/http] upgraded socket closes when the server closed without clo await clientSocketClosed.promise; await serverProcessClosed.promise; }); + +// deno-lint-ignore require-await +Deno.test("[node/http] ServerResponse.call()", async () => { + function Wrapper(this: unknown, req: IncomingMessage) { + ServerResponse.call(this, req); + } + Object.setPrototypeOf(Wrapper.prototype, ServerResponse.prototype); + + // deno-lint-ignore no-explicit-any + const wrapper = new (Wrapper as any)(new IncomingMessage(new Socket())); + + assert(wrapper instanceof ServerResponse); +}); + +Deno.test("[node/http] ServerResponse _header", async () => { + const { promise, resolve } = Promise.withResolvers(); + const server = http.createServer((_req, res) => { + assert(Object.hasOwn(res, "_header")); + res.end(); + }); + + server.listen(async () => { + const { port } = server.address() as { port: number }; + const res = await fetch(`http://localhost:${port}`); + await res.body?.cancel(); + server.close(() => { + resolve(); + }); + }); + + await promise; +}); + +Deno.test("[node/http] ServerResponse connection", async () => { + const { promise, resolve } = Promise.withResolvers(); + const server = http.createServer((_req, res) => { + assert(Object.hasOwn(res, "connection")); + assert(res.connection instanceof Socket); + res.end(); + }); + + server.listen(async () => { + const { port } = server.address() as { port: number }; + const res = await fetch(`http://localhost:${port}`); + await res.body?.cancel(); + server.close(() => { + resolve(); + }); + }); + + await promise; +}); + +Deno.test("[node/http] ServerResponse socket", async () => { + const { promise, resolve } = Promise.withResolvers(); + const server = http.createServer((_req, res) => { + assert(Object.hasOwn(res, "socket")); + assert(res.socket instanceof Socket); + res.end(); + }); + + server.listen(async () => { + const { port } = server.address() as { port: number }; + const res = await fetch(`http://localhost:${port}`); + await res.body?.cancel(); + server.close(() => { + resolve(); + }); + }); + + await promise; +}); -- cgit v1.2.3 From a69224ea5bd02f08108aac867c492754095f2d34 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 30 Oct 2024 02:41:16 +0900 Subject: Revert "fix(ext/node): fix dns.lookup result ordering (#26264)" (#26621) This reverts commit d59599fc187c559ee231882773e1c5a2b932fc3d. Closes #26588 --- tests/unit_node/http_test.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'tests/unit_node/http_test.ts') diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index 84d6f5727..2b3b8f509 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -322,14 +322,10 @@ Deno.test("[node/http] IncomingRequest socket has remoteAddress + remotePort", a // deno-lint-ignore no-explicit-any const port = (server.address() as any).port; const res = await fetch( - `http://localhost:${port}/`, + `http://127.0.0.1:${port}/`, ); await res.arrayBuffer(); - if (Deno.build.os === "windows") { - assertEquals(remoteAddress, "127.0.0.1"); - } else { - assertEquals(remoteAddress, "::1"); - } + assertEquals(remoteAddress, "127.0.0.1"); assertEquals(typeof remotePort, "number"); server.close(() => resolve()); }); -- cgit v1.2.3