Skip to content

Commit

Permalink
fix(ext/net): fix string port number handling in listen (denoland#19921)
Browse files Browse the repository at this point in the history
While string `port` is not allowed in typing, it seems we used to
support that and now it's broken. ref:
denoland#10064 (comment)

This PR restores the support of string port number in `listen` and
`listenTls`
  • Loading branch information
kt3k committed Jul 25, 2023
1 parent e311e46 commit 3d35900
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
9 changes: 9 additions & 0 deletions cli/tests/unit/net_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,3 +1237,12 @@ Deno.test({
}, Deno.errors.AddrInUse);
listener1.close();
});

Deno.test({
permissions: { net: true },
}, function netTcpListenDoesNotThrowOnStringPort() {
// @ts-ignore String port is not allowed by typing, but it shouldn't throw
// for backwards compatibility.
const listener = Deno.listen({ hostname: "localhost", port: "0" });
listener.close();
});
14 changes: 14 additions & 0 deletions cli/tests/unit/tls_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1477,3 +1477,17 @@ Deno.test({
}, Deno.errors.AddrInUse);
listener1.close();
});

Deno.test({
permissions: { net: true },
}, function listenTlsDoesNotThrowOnStringPort() {
const listener = Deno.listenTls({
hostname: "localhost",
// @ts-ignore String port is not allowed by typing, but it shouldn't throw
// for backwards compatibility.
port: "0",
cert,
key,
});
listener.close();
});
3 changes: 2 additions & 1 deletion ext/net/01_net.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
ArrayPrototypeForEach,
ArrayPrototypePush,
Error,
Number,
ObjectPrototypeIsPrototypeOf,
PromiseResolve,
SymbolAsyncIterator,
Expand Down Expand Up @@ -420,7 +421,7 @@ function listen(args) {
case "tcp": {
const { 0: rid, 1: addr } = ops.op_net_listen_tcp({
hostname: args.hostname ?? "0.0.0.0",
port: args.port,
port: Number(args.port),
}, args.reusePort);
addr.transport = "tcp";
return new Listener(rid, addr);
Expand Down
4 changes: 2 additions & 2 deletions ext/net/02_tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const core = globalThis.Deno.core;
const ops = core.ops;
import { Conn, Listener } from "ext:deno_net/01_net.js";
const primordials = globalThis.__bootstrap.primordials;
const { TypeError } = primordials;
const { Number, TypeError } = primordials;

function opStartTls(args) {
return core.opAsync("op_tls_start", args);
Expand Down Expand Up @@ -70,7 +70,7 @@ function listenTls({
throw new TypeError(`Unsupported transport: '${transport}'`);
}
const { 0: rid, 1: localAddr } = ops.op_net_listen_tls(
{ hostname, port },
{ hostname, port: Number(port) },
{ cert, certFile, key, keyFile, alpnProtocols, reusePort },
);
return new TlsListener(rid, localAddr);
Expand Down

0 comments on commit 3d35900

Please sign in to comment.