{"record":{"id":"4dc18f9b914b4f08","repo":"denoland/deno","slug":"invalid-port-maybeport-4dc18f","errorCode":null,"errorMessage":"Invalid port: ${maybePort}","messagePattern":"Invalid port: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/net/01_net.js","lineNumber":653,"sourceCode":"}\n\nfunction validatePort(maybePort, isServer = false) {\n  // A missing port means \"any available port\" (port 0) for servers. Clients\n  // 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          },","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/net/01_net.js#L635-L671","documentation":"The sibling branch of validatePort(): thrown when the numeric conversion of the port is not an integer but the input is not an unparseable string - i.e. the literal NaN value or a fractional number/string such as 8080.5. The unquoted message form distinguishes these from garbage strings.","triggerScenarios":"Deno.connect({ port: NaN }) after a calculation on undefined; port: 8080.5 from fractional arithmetic; port: '63.5'; expressions like Math.random() * 65535 passed without Math.floor.","commonSituations":"NaN propagation from failed parseInt or optional-chain arithmetic on undefined; computing ports with offsets/halving; random port selection missing a floor/round step.","solutions":["Trace the NaN or fraction to its source (failed parse, arithmetic on undefined) and fix it there","Round computed ports with Math.floor() or Math.round() before passing them","For random ports use 1 + Math.floor(Math.random() * 65535)"],"exampleFix":"// before\nconst port = basePort + Math.random() * 100;\nawait Deno.connect({ hostname, port }); // TypeError: Invalid port: 8080.47...\n\n// after\nconst port = Math.floor(basePort + Math.random() * 100);\nawait Deno.connect({ hostname, port });","handlingStrategy":"validation","validationCode":"function assertIntegerPort(port: unknown): asserts port is number {\n  const n = Number(port);\n  if (!Number.isInteger(n)) {\n    throw new Error(\n      `Port must be an integer, got: ${typeof port === \"string\" ? JSON.stringify(port) : String(port)}`,\n    );\n  }\n}","typeGuard":"function isIntegerPort(p: unknown): p is number {\n  return typeof p === \"number\" && Number.isInteger(p);\n}","tryCatchPattern":null,"preventionTips":["Wrap derived or random port math in Math.floor explicitly","Fail fast near the computation: if (Number.isNaN(port)) throw ...","Enable strict TS options (strict, noUncheckedIndexedAccess) to catch undefined arithmetic"],"tags":["network","port","nan","validation","deno"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}