{"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/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/net/01_net.js#L635-L671","documentation":"The sibling branch of the same validator: the port converts to a non-integer through a path other than a NaN-producing string. This means the value itself is NaN, or it is fractional (8080.5), Infinity, or a string like '12.5'. The message interpolates the value without quotes, distinguishing it from the non-numeric-string case; it is a TypeError thrown before any network operation.","triggerScenarios":"Deno.connect({ port: NaN }) from an uninitialized variable; Deno.listen({ port: 80.5 }); port computed as a / b with b = 0 yielding Infinity; Deno.connectTls({ port: '12.5' }).","commonSituations":"Optional config fields defaulted to NaN; division or averaging used to compute a port; float arithmetic or unit-conversion bugs producing fractional values; JSON config typos like 8080.5.","solutions":["Initialize port variables to a concrete integer, never NaN","Reject fractional computed values: if (Math.trunc(x) !== x) the input is wrong","Guard with Number.isFinite(port) && Number.isInteger(port) before calling the API"],"exampleFix":"// before\nconst port = start + range / 2; // e.g. 8080.5 -> Invalid port: 8080.5\nDeno.connect({ port });\n\n// after\nconst port = start + Math.floor(range / 2);\nif (!Number.isInteger(port)) throw new Error(`computed port is not an integer: ${port}`);\nDeno.connect({ port });","handlingStrategy":"validation","validationCode":"function toPort(value: number | string | undefined, fallback: number): number {\n  const n = value === undefined ? fallback : Number(value);\n  if (!Number.isInteger(n)) {\n    throw new Error(`port must be an integer, received: ${String(value)}`);\n  }\n  return n;\n}","typeGuard":"function isIntegerPort(p: unknown): p is number {\n  return typeof p === 'number' && Number.isInteger(p) && Number.isFinite(p);\n}","tryCatchPattern":"try {\n  Deno.listen({ port });\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('Invalid port')) {\n    // fractional/NaN input - fix the computation, do not retry\n  }\n  throw e;\n}","preventionTips":["Never default port variables to NaN; use a concrete port or an explicit sentinel you validate","After any arithmetic on ports, assert Number.isInteger before use","Check Number.isFinite as well to catch Infinity from divide-by-zero port math"],"tags":["deno","network","port","nan","typeerror"],"backgroundTag":"invalid-port-number","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}