{"record":{"id":"a21db3cebc7de9de","repo":"denoland/deno","slug":"invalid-port-out-of-range-maybeport","errorCode":null,"errorMessage":"Invalid port (out of range): ${maybePort}","messagePattern":"Invalid port \\(out of range\\): (.+?)","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/net/01_net.js","lineNumber":657,"sourceCode":"  // must always specify a port to connect to, so a missing port is left as-is\n  // and rejected below.\n  if (isServer && (maybePort === null || maybePort === undefined)) {\n    maybePort = 0;\n  }\n  if (typeof maybePort !== \"number\" && typeof maybePort !== \"string\") {\n    throw new TypeError(`Invalid port (expected number): ${maybePort}`);\n  }\n  if (maybePort === \"\") throw new TypeError(\"Invalid port: ''\");\n  const port = Number(maybePort);\n  if (!NumberIsInteger(port)) {\n    if (NumberIsNaN(port) && !NumberIsNaN(maybePort)) {\n      throw new TypeError(`Invalid port: '${maybePort}'`);\n    } else {\n      throw new TypeError(`Invalid port: ${maybePort}`);\n    }\n  } else if (port < (isServer ? 0 : 1) || port > 65535) {\n    // Servers may bind to port 0 (OS-assigned), clients may not connect to it.\n    throw new RangeError(`Invalid port (out of range): ${maybePort}`);\n  }\n  return port;\n}\n\nfunction createListenDatagram(udpOpFn, unixOpFn) {\n  return function listenDatagram(args) {\n    switch (args.transport) {\n      case \"udp\": {\n        const port = validatePort(args.port, true);\n        const { 0: rid, 1: addr } = udpOpFn(\n          {\n            hostname: args.hostname ?? \"0.0.0.0\",\n            port,\n          },\n          args.reuseAddress ?? false,\n          args.loopback ?? false,\n        );\n        addr.transport = \"udp\";","sourceCodeStart":639,"sourceCodeEnd":675,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/net/01_net.js#L639-L675","documentation":"RangeError from validatePort(): the port is a valid integer but outside the allowed range. Client APIs (Deno.connect, Deno.connectTls) require 1-65535; server APIs (Deno.listen, Deno.listenTls, Deno.listenDatagram) additionally accept 0, which asks the OS to assign a port.","triggerScenarios":"Deno.connect({ port: 0 }) (0 is only valid for listeners); port: 65536; port: -1; port: 70000 from a misparsed config; Deno.listen({ port: -1 }).","commonSituations":"Off-by-one at the 65535/65536 boundary; using 0 to request an ephemeral source port on a client connect (unsupported); ports above 65535 copied from 'ephemeral range' docs; signed arithmetic overflow on 16-bit math.","solutions":["Validate to 1-65535 for connects and 0-65535 for listens before the call","If you wanted an OS-assigned port, remember only listeners accept 0 - clients simply connect to the server's port","Re-check config parsing for typos such as 80800 or 6553 missing a digit"],"exampleFix":"// before\nawait Deno.connect({ hostname, port: config.port }); // config.port = 70000\n\n// after\nconst { port } = config;\nif (!(Number.isInteger(port) && port >= 1 && port <= 65535)) {\n  throw new Error(`Port out of range: ${port}`);\n}\nawait Deno.connect({ hostname, port });","handlingStrategy":"validation","validationCode":"function portInRange(port: number, isServer: boolean): boolean {\n  const min = isServer ? 0 : 1;\n  return Number.isInteger(port) && port >= min && port <= 65535;\n}\n\nif (!portInRange(port, false)) throw new Error(`connect port out of range: ${port}`);\nawait Deno.connect({ hostname, port });","typeGuard":"function isClientPort(p: unknown): p is number {\n  return typeof p === \"number\" && Number.isInteger(p) && p >= 1 && p <= 65535;\n}","tryCatchPattern":null,"preventionTips":["Remember port 0 means OS-assigned and is only valid on listen/listenTls/listenDatagram","Range-check ports at the config boundary with a single shared helper","Write boundary tests for 0, 1, 65535, and 65536"],"tags":["network","port","range","validation","deno"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}