Skip to content

Commit 944edc4

Browse files
committed
feat(net): Default to port 0 (OS assigned port) if no port was specified
Validation is also stricter for client ports, which now has to be in range 1-65535, as connecting to “port 0” is invalid.
1 parent 9cd8077 commit 944edc4

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

ext/net/01_net.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ const listenOptionApiName = Symbol("listenOptionApiName");
601601
function listen(args) {
602602
switch (args.transport ?? "tcp") {
603603
case "tcp": {
604-
const port = validatePort(args.port);
604+
const port = validatePort(args.port || 0, true);
605605
const { 0: rid, 1: addr } = op_net_listen_tcp(
606606
{
607607
hostname: args.hostname ?? "0.0.0.0",
@@ -646,7 +646,7 @@ function listen(args) {
646646
}
647647
}
648648

649-
function validatePort(maybePort) {
649+
function validatePort(maybePort, isServer = false) {
650650
if (typeof maybePort !== "number" && typeof maybePort !== "string") {
651651
throw new TypeError(`Invalid port (expected number): ${maybePort}`);
652652
}
@@ -658,6 +658,8 @@ function validatePort(maybePort) {
658658
} else {
659659
throw new TypeError(`Invalid port: ${maybePort}`);
660660
}
661+
} else if (!isServer && (port < 1 || port > 65535)) {
662+
throw new RangeError(`Invalid port (out of range): ${maybePort}`);
661663
} else if (port < 0 || port > 65535) {
662664
throw new RangeError(`Invalid port (out of range): ${maybePort}`);
663665
}
@@ -668,7 +670,7 @@ function createListenDatagram(udpOpFn, unixOpFn) {
668670
return function listenDatagram(args) {
669671
switch (args.transport) {
670672
case "udp": {
671-
const port = validatePort(args.port);
673+
const port = validatePort(args.port || 0, true);
672674
const { 0: rid, 1: addr } = udpOpFn(
673675
{
674676
hostname: args.hostname ?? "127.0.0.1",

tests/unit/net_test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {
33
assert,
44
assertEquals,
5+
assertGreaterOrEqual,
56
assertRejects,
67
assertThrows,
78
delay,
@@ -46,6 +47,24 @@ Deno.test(
4647
},
4748
);
4849

50+
Deno.test(
51+
{
52+
permissions: { net: true },
53+
},
54+
function netUdpListenEphermalClose() {
55+
const socket = Deno.listenDatagram({
56+
hostname: "127.0.0.1",
57+
transport: "udp",
58+
});
59+
assert(socket.addr.transport === "udp");
60+
assertEquals(socket.addr.hostname, "127.0.0.1");
61+
// Ephermal port range starts at 49152 according to RFC 6335 / IANA, but
62+
// many OSes use the lower number below (old OSes also started at 1024)
63+
assertGreaterOrEqual(socket.addr.port, 32768);
64+
socket.close();
65+
},
66+
);
67+
4968
Deno.test(
5069
{
5170
ignore: Deno.build.os === "windows",

tests/unit/test_util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export {
88
assert,
99
assertEquals,
1010
assertFalse,
11+
assertGreaterOrEqual,
1112
AssertionError,
1213
assertIsError,
1314
assertMatch,

0 commit comments

Comments
 (0)