mirror of
https://github.com/denoland/deno.git
synced 2025-12-23 08:48:24 +00:00
Some checks are pending
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
## Summary Aligns Deno's Node.js compatibility layer (`node:net`, `node:http`, `node:https`, `node:http2`, `node:dns`) with Node.js v18.4.0+ behavior by returning the `family` property as a string (`"IPv4"` or `"IPv6"`) rather than a number in `server.address()` and socket address methods. Node.js briefly changed `family` from string to number in v18.0.0 (nodejs/node#41431), but reverted in v18.4.0 (nodejs/node#43054) due to ecosystem breakage (nodejs/node#43014). This fix ensures compatibility with npm packages that rely on the string format, which has been the stable API since Node.js v18.4.0. ## Changes - Modified `ext/node/polyfills/http.ts` to add `family` property to `address()` return - Modified `ext/node/polyfills/internal_binding/tcp_wrap.ts` to return string `family` instead of number in `getsockname()`, `getpeername()`, and `#connect()` - Modified `ext/node/polyfills/net.ts` to fix `socket.remoteFamily` getter (no longer needs conversion since `family` is now a string) - Modified `ext/node/polyfills/dns.ts` and `ext/node/polyfills/internal/dns/promises.ts` to accept string family values (`"IPv4"`, `"IPv6"`) in `lookup()`, matching [Node.js behavior](https://nodejs.org/api/dns.html#dnslookuphostname-options-callback) - Added tests in `tests/unit_node/http_test.ts`, `tests/unit_node/http2_test.ts`, `tests/unit_node/https_test.ts`, `tests/unit_node/dns_test.ts`, and `tests/unit_node/net_test.ts` ## Node.js Compatibility Note For non-IP addresses (when `isIP()` returns 0), the `family` property is `undefined`. This matches Node.js C++ behavior in [`AddressToJS`](https://github.com/nodejs/node/blob/main/src/tcp_wrap.cc) where family is only set for `AF_INET` (`"IPv4"`) and `AF_INET6` (`"IPv6"`), and left undefined otherwise. ## Refs - nodejs/node#43054 - nodejs/node@70b516e <!-- Before submitting a PR, please read https://docs.deno.com/runtime/manual/references/contributing 1. Give the PR a descriptive title. Examples of good title: - fix(std/http): Fix race condition in server - docs(console): Update docstrings - feat(doc): Handle nested reexports Examples of bad title: - fix #7123 - update docs - fix bugs 2. Ensure there is a related issue and it is referenced in the PR text. 3. Ensure there are tests that cover the changes. 4. Ensure `cargo test` passes. 5. Ensure `./tools/format.js` passes without changing files. 6. Ensure `./tools/lint.js` passes. 7. Open as a draft PR if your work is still in progress. The CI won't run all steps, but you can add '[ci]' to a commit message to force it to. 8. If you would like to run the benchmarks on the CI, add the 'ci-bench' label. --> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Daniel Rahmanto <daniel.rahmanto@gmail.com>
502 lines
13 KiB
TypeScript
502 lines
13 KiB
TypeScript
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
|
|
// deno-lint-ignore-file no-console
|
|
|
|
import * as http2 from "node:http2";
|
|
import { Buffer } from "node:buffer";
|
|
import { readFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import * as net from "node:net";
|
|
import { assert, assertEquals } from "@std/assert";
|
|
import { curlRequest } from "../unit/test_util.ts";
|
|
import { createRequire } from "node:module";
|
|
const require = createRequire(import.meta.url);
|
|
|
|
// Increase the timeout for the auto select family to avoid flakiness
|
|
net.setDefaultAutoSelectFamilyAttemptTimeout(
|
|
net.getDefaultAutoSelectFamilyAttemptTimeout() * 30,
|
|
);
|
|
|
|
for (const url of ["http://localhost:4246", "https://localhost:4247"]) {
|
|
Deno.test(`[node/http2 client] ${url}`, {
|
|
ignore: Deno.build.os === "windows",
|
|
}, async () => {
|
|
// Create a server to respond to the HTTP2 requests
|
|
const client = http2.connect(url, {});
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
const req = client.request({ ":method": "POST", ":path": "/" }, {
|
|
waitForTrailers: true,
|
|
});
|
|
|
|
let receivedTrailers;
|
|
let receivedHeaders;
|
|
let receivedData = "";
|
|
|
|
req.on("response", (headers, _flags) => {
|
|
receivedHeaders = headers;
|
|
});
|
|
|
|
req.write("hello");
|
|
req.setEncoding("utf8");
|
|
|
|
req.on("wantTrailers", () => {
|
|
req.sendTrailers({ foo: "bar" });
|
|
});
|
|
|
|
req.on("trailers", (trailers, _flags) => {
|
|
receivedTrailers = trailers;
|
|
});
|
|
|
|
req.on("data", (chunk) => {
|
|
receivedData += chunk;
|
|
});
|
|
req.end();
|
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
|
setTimeout(() => {
|
|
try {
|
|
client.close();
|
|
} catch (_) {
|
|
// pass
|
|
}
|
|
resolve();
|
|
}, 2000);
|
|
|
|
await promise;
|
|
assertEquals(receivedHeaders, { ":status": 200 });
|
|
assertEquals(receivedData, "hello world\n");
|
|
|
|
assertEquals(receivedTrailers, {
|
|
"abc": "def",
|
|
"opr": "stv",
|
|
"foo": "bar",
|
|
"req_body_len": "5",
|
|
});
|
|
});
|
|
}
|
|
|
|
Deno.test(`[node/http2 client createConnection]`, {
|
|
ignore: Deno.build.os === "windows",
|
|
}, async () => {
|
|
const url = "http://127.0.0.1:4246";
|
|
const createConnDeferred = Promise.withResolvers<void>();
|
|
// Create a server to respond to the HTTP2 requests
|
|
const client = http2.connect(url, {
|
|
createConnection() {
|
|
const socket = net.connect({ host: "127.0.0.1", port: 4246 });
|
|
|
|
socket.on("connect", () => {
|
|
createConnDeferred.resolve();
|
|
});
|
|
|
|
return socket;
|
|
},
|
|
});
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
const req = client.request({ ":method": "POST", ":path": "/" });
|
|
|
|
let receivedData = "";
|
|
|
|
req.write("hello");
|
|
req.setEncoding("utf8");
|
|
|
|
req.on("data", (chunk) => {
|
|
receivedData += chunk;
|
|
});
|
|
req.end();
|
|
|
|
const endPromise = Promise.withResolvers<void>();
|
|
setTimeout(() => {
|
|
try {
|
|
client.close();
|
|
} catch (_) {
|
|
// pass
|
|
}
|
|
endPromise.resolve();
|
|
}, 2000);
|
|
|
|
await createConnDeferred.promise;
|
|
await endPromise.promise;
|
|
assertEquals(receivedData, "hello world\n");
|
|
});
|
|
|
|
// https://github.com/denoland/deno/issues/29956
|
|
Deno.test(`[node/http2 client body overflow]`, {
|
|
ignore: Deno.build.os === "windows",
|
|
}, async () => {
|
|
const url = "http://127.0.0.1:4246";
|
|
const createConnDeferred = Promise.withResolvers<void>();
|
|
// Create a server to respond to the HTTP2 requests
|
|
const client = http2.connect(url, {
|
|
createConnection() {
|
|
const socket = net.connect({ host: "127.0.0.1", port: 4246 });
|
|
|
|
socket.on("connect", () => {
|
|
createConnDeferred.resolve();
|
|
});
|
|
|
|
return socket;
|
|
},
|
|
});
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
const req = client.request({ ":method": "POST", ":path": "/" });
|
|
|
|
let receivedData = "";
|
|
let receivedTrailers;
|
|
|
|
const ab = new ArrayBuffer(100);
|
|
const view = new Uint8Array(ab, 0, 5);
|
|
|
|
req.write(view);
|
|
req.setEncoding("utf8");
|
|
|
|
req.on("data", (chunk) => {
|
|
receivedData += chunk;
|
|
});
|
|
|
|
req.on("trailers", (trailers, _flags) => {
|
|
receivedTrailers = trailers;
|
|
});
|
|
|
|
req.end();
|
|
|
|
const endPromise = Promise.withResolvers<void>();
|
|
setTimeout(() => {
|
|
try {
|
|
client.close();
|
|
} catch (_) {
|
|
// pass
|
|
}
|
|
endPromise.resolve();
|
|
}, 2000);
|
|
|
|
await createConnDeferred.promise;
|
|
await endPromise.promise;
|
|
assertEquals(receivedData, "hello world\n");
|
|
|
|
assertEquals(receivedTrailers?.["req_body_len"], "5");
|
|
});
|
|
|
|
Deno.test("[node/http2 client GET https://www.example.com]", async () => {
|
|
const clientSession = http2.connect("https://www.example.com");
|
|
const req = clientSession.request({
|
|
":method": "GET",
|
|
":path": "/",
|
|
});
|
|
let headers = {};
|
|
let status: number | undefined = 0;
|
|
let chunk = new Uint8Array();
|
|
const endPromise = Promise.withResolvers<void>();
|
|
req.on("response", (h) => {
|
|
status = h[":status"];
|
|
headers = h;
|
|
});
|
|
req.on("data", (c) => {
|
|
chunk = c;
|
|
});
|
|
req.on("end", () => {
|
|
clientSession.close();
|
|
req.close();
|
|
endPromise.resolve();
|
|
});
|
|
req.end();
|
|
await endPromise.promise;
|
|
assert(Object.keys(headers).length > 0);
|
|
assertEquals(status, 200);
|
|
assert(chunk.length > 0);
|
|
});
|
|
|
|
Deno.test("[node/http2.createServer()]", {
|
|
// TODO(satyarohith): enable the test on windows.
|
|
ignore: Deno.build.os === "windows",
|
|
}, async () => {
|
|
const serverListening = Promise.withResolvers<number>();
|
|
const server = http2.createServer((_req, res) => {
|
|
res.setHeader("Content-Type", "text/html");
|
|
res.setHeader("X-Foo", "bar");
|
|
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
|
|
res.write("Hello, World!");
|
|
res.end();
|
|
});
|
|
server.listen(0, () => {
|
|
serverListening.resolve((server.address() as net.AddressInfo).port);
|
|
});
|
|
const port = await serverListening.promise;
|
|
const endpoint = `http://localhost:${port}`;
|
|
|
|
const response = await curlRequest([
|
|
endpoint,
|
|
"--http2-prior-knowledge",
|
|
]);
|
|
assertEquals(response, "Hello, World!");
|
|
server.close();
|
|
// Wait to avoid leaking the timer from here
|
|
// https://github.com/denoland/deno/blob/749b6e45e58ac87188027f79fe403d130f86bd73/ext/node/polyfills/net.ts#L2396-L2402
|
|
// Issue: https://github.com/denoland/deno/issues/22764
|
|
await new Promise<void>((resolve) => server.on("close", resolve));
|
|
});
|
|
|
|
Deno.test("[node/http2 client] write image buffer on request stream works", async () => {
|
|
const url = "https://localhost:5545";
|
|
const client = http2.connect(url);
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
const imagePath = join(import.meta.dirname!, "testdata", "green.jpg");
|
|
const buffer = await readFile(imagePath);
|
|
const req = client.request({ ":method": "POST", ":path": "/echo_server" });
|
|
req.write(buffer, (err) => {
|
|
if (err) throw err;
|
|
});
|
|
|
|
let receivedData: Buffer;
|
|
req.on("data", (chunk) => {
|
|
if (!receivedData) {
|
|
receivedData = chunk;
|
|
} else {
|
|
receivedData = Buffer.concat([receivedData, chunk]);
|
|
}
|
|
});
|
|
req.end();
|
|
|
|
const endPromise = Promise.withResolvers<void>();
|
|
setTimeout(() => {
|
|
try {
|
|
client.close();
|
|
} catch (_) {
|
|
// pass
|
|
}
|
|
endPromise.resolve();
|
|
}, 2000);
|
|
|
|
await endPromise.promise;
|
|
assertEquals(receivedData!, buffer);
|
|
});
|
|
|
|
Deno.test("[node/http2 client] write 512kb buffer on request stream works", async () => {
|
|
const url = "https://localhost:5545";
|
|
const client = http2.connect(url);
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
const filePath = join(
|
|
import.meta.dirname!,
|
|
"testdata",
|
|
"lorem_ipsum_512kb.txt",
|
|
);
|
|
const buffer = await readFile(filePath);
|
|
const req = client.request({ ":method": "POST", ":path": "/echo_server" });
|
|
req.write(buffer, (err) => {
|
|
if (err) throw err;
|
|
});
|
|
|
|
let receivedData: Buffer;
|
|
req.on("data", (chunk) => {
|
|
if (!receivedData) {
|
|
receivedData = chunk;
|
|
} else {
|
|
receivedData = Buffer.concat([receivedData, chunk]);
|
|
}
|
|
});
|
|
req.end();
|
|
|
|
const endPromise = Promise.withResolvers<void>();
|
|
setTimeout(() => {
|
|
try {
|
|
client.close();
|
|
} catch (_) {
|
|
// pass
|
|
}
|
|
endPromise.resolve();
|
|
}, 2000);
|
|
|
|
await endPromise.promise;
|
|
assertEquals(receivedData!, buffer);
|
|
});
|
|
|
|
// https://github.com/denoland/deno/issues/24678
|
|
Deno.test("[node/http2 client] deno doesn't panic on uppercase headers", async () => {
|
|
const url = "http://127.0.0.1:4246";
|
|
const client = http2.connect(url);
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
// The "User-Agent" header has uppercase characters to test the panic.
|
|
const req = client.request({
|
|
":method": "POST",
|
|
":path": "/",
|
|
"User-Agent": "http2",
|
|
});
|
|
const endPromise = Promise.withResolvers<void>();
|
|
|
|
let receivedData = "";
|
|
|
|
req.write("hello");
|
|
req.setEncoding("utf8");
|
|
|
|
req.on("data", (chunk) => {
|
|
receivedData += chunk;
|
|
});
|
|
req.on("end", () => {
|
|
req.close();
|
|
client.close();
|
|
endPromise.resolve();
|
|
});
|
|
req.end();
|
|
await endPromise.promise;
|
|
assertEquals(receivedData, "hello world\n");
|
|
});
|
|
|
|
Deno.test("[node/http2 ClientHttp2Session.socket]", async () => {
|
|
const url = "http://127.0.0.1:4246";
|
|
const client = http2.connect(url);
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
const req = client.request({ ":method": "POST", ":path": "/" });
|
|
const endPromise = Promise.withResolvers<void>();
|
|
|
|
// test that we can access session.socket
|
|
client.socket.setTimeout(10000);
|
|
// nodejs allows setting arbitrary properties
|
|
// deno-lint-ignore no-explicit-any
|
|
(client.socket as any).nonExistant = 9001;
|
|
// deno-lint-ignore no-explicit-any
|
|
assertEquals((client.socket as any).nonExistant, 9001);
|
|
|
|
// regular request dance to make sure it keeps working
|
|
let receivedData = "";
|
|
req.write("hello");
|
|
req.setEncoding("utf8");
|
|
|
|
req.on("data", (chunk) => {
|
|
receivedData += chunk;
|
|
});
|
|
req.on("end", () => {
|
|
req.close();
|
|
client.close();
|
|
endPromise.resolve();
|
|
});
|
|
req.end();
|
|
await endPromise.promise;
|
|
assertEquals(client.socket.remoteAddress, "127.0.0.1");
|
|
assertEquals(client.socket.remotePort, 4246);
|
|
assertEquals(client.socket.remoteFamily, "IPv4");
|
|
client.socket.setTimeout(0);
|
|
assertEquals(receivedData, "hello world\n");
|
|
});
|
|
|
|
Deno.test("[node/http2 client] connection states", async () => {
|
|
const expected = {
|
|
beforeConnect: { connecting: true, closed: false, destroyed: false },
|
|
afterConnect: { connecting: false, closed: false, destroyed: false },
|
|
afterClose: { connecting: false, closed: true, destroyed: false },
|
|
afterDestroy: { connecting: false, closed: true, destroyed: true },
|
|
};
|
|
const actual: Partial<typeof expected> = {};
|
|
|
|
const url = "http://127.0.0.1:4246";
|
|
const connectPromise = Promise.withResolvers<void>();
|
|
const client = http2.connect(url, {}, () => {
|
|
connectPromise.resolve();
|
|
});
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
// close event happens after destory has been called
|
|
const destroyPromise = Promise.withResolvers<void>();
|
|
client.on("close", () => {
|
|
destroyPromise.resolve();
|
|
});
|
|
|
|
actual.beforeConnect = {
|
|
connecting: client.connecting,
|
|
closed: client.closed,
|
|
destroyed: client.destroyed,
|
|
};
|
|
|
|
await connectPromise.promise;
|
|
actual.afterConnect = {
|
|
connecting: client.connecting,
|
|
closed: client.closed,
|
|
destroyed: client.destroyed,
|
|
};
|
|
|
|
// leave a request open to prevent immediate destroy
|
|
const req = client.request();
|
|
req.on("data", () => {});
|
|
req.on("error", (err) => console.error(err));
|
|
const reqClosePromise = Promise.withResolvers<void>();
|
|
req.on("close", () => {
|
|
reqClosePromise.resolve();
|
|
});
|
|
|
|
client.close();
|
|
actual.afterClose = {
|
|
connecting: client.connecting,
|
|
closed: client.closed,
|
|
destroyed: client.destroyed,
|
|
};
|
|
|
|
await destroyPromise.promise;
|
|
actual.afterDestroy = {
|
|
connecting: client.connecting,
|
|
closed: client.closed,
|
|
destroyed: client.destroyed,
|
|
};
|
|
|
|
await reqClosePromise.promise;
|
|
|
|
assertEquals(actual, expected);
|
|
});
|
|
|
|
Deno.test("request and response exports", () => {
|
|
assert(http2.Http2ServerRequest);
|
|
assert(http2.Http2ServerResponse);
|
|
});
|
|
|
|
Deno.test("internal/http2/util exports", () => {
|
|
const util = require("internal/http2/util");
|
|
assert(typeof util.kAuthority === "symbol");
|
|
assert(typeof util.kSensitiveHeaders === "symbol");
|
|
assert(typeof util.kSocket === "symbol");
|
|
assert(typeof util.kProtocol === "symbol");
|
|
assert(typeof util.kProxySocket === "symbol");
|
|
assert(typeof util.kRequest === "symbol");
|
|
});
|
|
|
|
Deno.test("[node/http2] Server.address() includes family property", async () => {
|
|
// Test IPv4
|
|
{
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
|
const server = http2.createServer((_req, res) => {
|
|
res.end("ok");
|
|
});
|
|
|
|
server.listen(0, "127.0.0.1", () => {
|
|
const addr = server.address() as net.AddressInfo;
|
|
assertEquals(addr.address, "127.0.0.1");
|
|
assertEquals(addr.family, "IPv4");
|
|
assertEquals(typeof addr.port, "number");
|
|
server.close(() => resolve());
|
|
});
|
|
|
|
await promise;
|
|
}
|
|
|
|
// Test IPv6
|
|
{
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
|
const server = http2.createServer((_req, res) => {
|
|
res.end("ok");
|
|
});
|
|
|
|
server.listen(0, "::1", () => {
|
|
const addr = server.address() as net.AddressInfo;
|
|
assertEquals(addr.address, "::1");
|
|
assertEquals(addr.family, "IPv6");
|
|
assertEquals(typeof addr.port, "number");
|
|
server.close(() => resolve());
|
|
});
|
|
|
|
await promise;
|
|
}
|
|
});
|