@@ -601,7 +601,7 @@ const listenOptionApiName = Symbol("listenOptionApiName");
601601function 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" ,
0 commit comments