2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-06-29 22:44:14 -04:00
|
|
|
|
|
|
|
import * as net from "node:net";
|
2024-02-12 21:05:10 -05:00
|
|
|
import { assert, assertEquals } from "@std/assert/mod.ts";
|
|
|
|
import * as path from "@std/path/mod.ts";
|
2023-06-29 22:44:14 -04:00
|
|
|
import * as http from "node:http";
|
|
|
|
|
2024-07-24 07:33:45 -04:00
|
|
|
Deno.test("[node/net] close event emits after error event - when host is not found", async () => {
|
2023-06-29 22:44:14 -04:00
|
|
|
const socket = net.createConnection(27009, "doesnotexist");
|
|
|
|
const events: ("error" | "close")[] = [];
|
2023-11-22 06:11:20 -05:00
|
|
|
const errorEmitted = Promise.withResolvers<void>();
|
|
|
|
const closeEmitted = Promise.withResolvers<void>();
|
2023-06-29 22:44:14 -04:00
|
|
|
socket.once("error", () => {
|
|
|
|
events.push("error");
|
|
|
|
errorEmitted.resolve();
|
|
|
|
});
|
|
|
|
socket.once("close", () => {
|
|
|
|
events.push("close");
|
|
|
|
closeEmitted.resolve();
|
|
|
|
});
|
2023-11-22 06:11:20 -05:00
|
|
|
await Promise.all([errorEmitted.promise, closeEmitted.promise]);
|
2023-06-29 22:44:14 -04:00
|
|
|
|
|
|
|
// `error` happens before `close`
|
|
|
|
assertEquals(events, ["error", "close"]);
|
|
|
|
});
|
|
|
|
|
2024-07-24 07:33:45 -04:00
|
|
|
Deno.test("[node/net] close event emits after error event - when connection is refused", async () => {
|
|
|
|
const socket = net.createConnection(27009, "127.0.0.1");
|
|
|
|
const events: ("error" | "close")[] = [];
|
|
|
|
const errorEmitted = Promise.withResolvers<void>();
|
|
|
|
const closeEmitted = Promise.withResolvers<void>();
|
|
|
|
socket.once("error", () => {
|
|
|
|
events.push("error");
|
|
|
|
errorEmitted.resolve();
|
|
|
|
});
|
|
|
|
socket.once("close", () => {
|
|
|
|
events.push("close");
|
|
|
|
closeEmitted.resolve();
|
|
|
|
});
|
|
|
|
await Promise.all([errorEmitted.promise, closeEmitted.promise]);
|
|
|
|
|
|
|
|
// `error` happens before `close`
|
|
|
|
assertEquals(events, ["error", "close"]);
|
|
|
|
});
|
|
|
|
|
2023-06-29 22:44:14 -04:00
|
|
|
Deno.test("[node/net] the port is available immediately after close callback", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferred = Promise.withResolvers<void>();
|
2023-06-29 22:44:14 -04:00
|
|
|
|
|
|
|
// This simulates what get-port@5.1.1 does.
|
|
|
|
const getAvailablePort = (port: number) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const server = net.createServer();
|
|
|
|
server.on("error", reject);
|
|
|
|
server.listen({ port }, () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { port } = server.address() as any;
|
|
|
|
server.close(() => {
|
|
|
|
resolve(port);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const port = await getAvailablePort(5555);
|
|
|
|
|
|
|
|
const httpServer = http.createServer();
|
2023-11-22 06:11:20 -05:00
|
|
|
httpServer.on("error", (e) => deferred.reject(e));
|
2023-06-29 22:44:14 -04:00
|
|
|
httpServer.listen(port, () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
httpServer.close(() => deferred.resolve());
|
2023-06-29 22:44:14 -04:00
|
|
|
});
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred.promise;
|
2023-06-29 22:44:14 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[node/net] net.connect().unref() works", async () => {
|
|
|
|
const ctl = new AbortController();
|
|
|
|
const server = Deno.serve({
|
|
|
|
signal: ctl.signal,
|
2024-04-18 11:37:47 -04:00
|
|
|
port: 0, // any available port will do
|
2023-06-29 22:44:14 -04:00
|
|
|
handler: () => new Response("hello"),
|
|
|
|
onListen: async ({ port, hostname }) => {
|
|
|
|
const { stdout, stderr } = await new Deno.Command(Deno.execPath(), {
|
|
|
|
args: [
|
|
|
|
"eval",
|
|
|
|
`
|
|
|
|
import * as net from "node:net";
|
|
|
|
const socket = net.connect(${port}, "${hostname}", () => {
|
|
|
|
console.log("connected");
|
|
|
|
socket.unref();
|
|
|
|
socket.on("data", (data) => console.log(data.toString()));
|
|
|
|
socket.write("GET / HTTP/1.1\\n\\n");
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
],
|
|
|
|
cwd: path.dirname(path.fromFileUrl(import.meta.url)),
|
|
|
|
}).output();
|
|
|
|
if (stderr.length > 0) {
|
|
|
|
console.log(new TextDecoder().decode(stderr));
|
|
|
|
}
|
|
|
|
assertEquals(new TextDecoder().decode(stdout), "connected\n");
|
|
|
|
ctl.abort();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await server.finished;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "[node/net] throws permission error instead of unknown error",
|
|
|
|
permissions: "none",
|
|
|
|
fn: () => {
|
|
|
|
try {
|
|
|
|
const s = new net.Server();
|
|
|
|
s.listen(3000);
|
|
|
|
} catch (e) {
|
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-07-18 18:33:43 -04:00
|
|
|
|
|
|
|
Deno.test("[node/net] connection event has socket value", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferred = Promise.withResolvers<void>();
|
|
|
|
const deferred2 = Promise.withResolvers<void>();
|
2023-07-18 18:33:43 -04:00
|
|
|
|
|
|
|
const server = net.createServer();
|
2023-11-22 06:11:20 -05:00
|
|
|
server.on("error", deferred.reject);
|
2023-07-18 18:33:43 -04:00
|
|
|
server.on("connection", (socket) => {
|
|
|
|
assert(socket !== undefined);
|
|
|
|
socket.end();
|
|
|
|
server.close(() => {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred.resolve();
|
2023-07-18 18:33:43 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
server.listen(async () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { port } = server.address() as any;
|
|
|
|
|
|
|
|
const conn = await Deno.connect({
|
|
|
|
port,
|
|
|
|
transport: "tcp",
|
|
|
|
});
|
|
|
|
|
|
|
|
for await (const _ of conn.readable) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred2.resolve();
|
2023-07-18 18:33:43 -04:00
|
|
|
});
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
await Promise.all([deferred.promise, deferred2.promise]);
|
2023-07-18 18:33:43 -04:00
|
|
|
});
|
2023-08-22 10:45:10 -04:00
|
|
|
|
2023-08-28 17:28:39 -04:00
|
|
|
/// We need to make sure that any shared buffers are never used concurrently by two reads.
|
2023-08-22 10:45:10 -04:00
|
|
|
// https://github.com/denoland/deno/issues/20188
|
|
|
|
Deno.test("[node/net] multiple Sockets should get correct server data", async () => {
|
2023-08-28 17:28:39 -04:00
|
|
|
const socketCount = 9;
|
2023-08-22 10:45:10 -04:00
|
|
|
|
2023-08-28 17:28:39 -04:00
|
|
|
class TestSocket {
|
2023-11-22 06:11:20 -05:00
|
|
|
dataReceived: ReturnType<typeof Promise.withResolvers<void>> = Promise
|
|
|
|
.withResolvers<void>();
|
2023-08-28 17:28:39 -04:00
|
|
|
events: string[] = [];
|
|
|
|
socket: net.Socket | undefined;
|
|
|
|
}
|
2023-08-22 10:45:10 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const finished = Promise.withResolvers<void>();
|
|
|
|
const serverSocketsClosed: ReturnType<typeof Promise.withResolvers<void>>[] =
|
|
|
|
[];
|
2023-08-22 10:45:10 -04:00
|
|
|
const server = net.createServer();
|
|
|
|
server.on("connection", (socket) => {
|
|
|
|
assert(socket !== undefined);
|
2023-11-22 06:11:20 -05:00
|
|
|
const i = serverSocketsClosed.push(Promise.withResolvers<void>());
|
2023-08-22 10:45:10 -04:00
|
|
|
socket.on("data", (data) => {
|
|
|
|
socket.write(new TextDecoder().decode(data));
|
|
|
|
});
|
2023-09-16 01:48:31 -04:00
|
|
|
socket.on("close", () => {
|
|
|
|
serverSocketsClosed[i - 1].resolve();
|
|
|
|
});
|
2023-08-22 10:45:10 -04:00
|
|
|
});
|
|
|
|
|
2023-08-28 17:28:39 -04:00
|
|
|
const sockets: TestSocket[] = [];
|
|
|
|
for (let i = 0; i < socketCount; i++) {
|
|
|
|
sockets[i] = new TestSocket();
|
|
|
|
}
|
|
|
|
|
2023-08-22 10:45:10 -04:00
|
|
|
server.listen(async () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { port } = server.address() as any;
|
|
|
|
|
2023-08-28 17:28:39 -04:00
|
|
|
for (let i = 0; i < socketCount; i++) {
|
|
|
|
const socket = sockets[i].socket = net.createConnection(port);
|
|
|
|
socket.on("data", (data) => {
|
|
|
|
const count = sockets[i].events.length;
|
|
|
|
sockets[i].events.push(new TextDecoder().decode(data));
|
|
|
|
if (count === 0) {
|
|
|
|
// Trigger an immediate second write
|
|
|
|
sockets[i].socket?.write(`${i}`.repeat(3));
|
|
|
|
} else {
|
|
|
|
sockets[i].dataReceived.resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-08-22 10:45:10 -04:00
|
|
|
|
2023-08-28 17:28:39 -04:00
|
|
|
for (let i = 0; i < socketCount; i++) {
|
|
|
|
sockets[i].socket?.write(`${i}`.repeat(3));
|
|
|
|
}
|
2023-08-22 10:45:10 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
await Promise.all(sockets.map((socket) => socket.dataReceived.promise));
|
2023-08-22 10:45:10 -04:00
|
|
|
|
2023-08-28 17:28:39 -04:00
|
|
|
for (let i = 0; i < socketCount; i++) {
|
|
|
|
sockets[i].socket?.end();
|
|
|
|
}
|
2023-08-22 10:45:10 -04:00
|
|
|
|
|
|
|
server.close(() => {
|
2023-08-28 17:28:39 -04:00
|
|
|
finished.resolve();
|
2023-08-22 10:45:10 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
await finished.promise;
|
|
|
|
await Promise.all(serverSocketsClosed.map(({ promise }) => promise));
|
2023-08-22 10:45:10 -04:00
|
|
|
|
2023-08-28 17:28:39 -04:00
|
|
|
for (let i = 0; i < socketCount; i++) {
|
|
|
|
assertEquals(sockets[i].events, [`${i}`.repeat(3), `${i}`.repeat(3)]);
|
|
|
|
}
|
2023-08-22 10:45:10 -04:00
|
|
|
});
|
2024-06-18 06:46:13 -04:00
|
|
|
|
|
|
|
Deno.test("[node/net] BlockList doesn't leak resources", () => {
|
|
|
|
const blockList = new net.BlockList();
|
|
|
|
blockList.addAddress("1.1.1.1");
|
|
|
|
assert(blockList.check("1.1.1.1"));
|
|
|
|
});
|