Skip to content

url: convert port to int

I think it is both unusual and irritating that url.parse returns the port as a string. There are a lot of userland modules which seem to be unsure about this, too, and handle both strings and numbers.

Please consider:

  • RFC 3986 clearly states that a port must be a decimal integer, so there is no reason to handle it as a string. Semantically, using a correct type here guarantees that no syntactically invalid port is returned.
  • url.format still accepts both strings and numbers as the port property.
  • Checking whether the URL contains a port using typeof r.port === 'string' will not work anymore, but I don't expect a lot of packages to do this (it is a bit unintuitive).
  • For port 0, !!r.port will now return false instead of true. However, port 0 has a special meaning and usually does not occur as part of a URL.
  • Most string operations on the port property will obviously fail (but are unlikely).
  • Leading zeros will be discarded when parsing (but don't usually matter).

Refs: https://github.com/nodejs/node/issues/9550

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

url

Merge request reports

Loading