|
| 1 | +const ip = require('ip'); |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://en.wikipedia.org/wiki/IPv6_address |
| 5 | + * Literal IPv6 addresses in resources (URLs): |
| 6 | +------------------------------------------------ |
| 7 | + * Colon (:) characters in IPv6 addresses may conflict with the established syntax of resource identifiers, |
| 8 | + * such as URIs and URLs. The colon is conventionally used to terminate the host path before a port number.[10] |
| 9 | + * To alleviate this conflict, literal IPv6 addresses are enclosed in square brackets in such resource identifiers; |
| 10 | + * When the URL doesn't conatoin the port the notation is http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/ |
| 11 | + * When the URL also contains a port number the notation is: https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/ |
| 12 | + * |
| 13 | + * @param {{ |
| 14 | +* host: string |
| 15 | +* }} param |
| 16 | +* @returns {string} |
| 17 | +*/ |
| 18 | +function escapeV6IpForURL({ host }) { |
| 19 | + /** |
| 20 | + * If the host is already URL compatible then the ip lib will return false > ip.isV6Format('[::1]') false |
| 21 | + * If the host is a proper ipv6 ip then the `new URL(host)` will fail with Uncaught TypeError: Invalid URL code: 'ERR_INVALID_URL', |
| 22 | + * !ip.isV4Format(host) check required because isV6Format returns true for ipv4 address because of backward compatibility |
| 23 | + */ |
| 24 | + if (ip.isV6Format(host) && !ip.isV4Format(host)) { |
| 25 | + return `[${host}]`; |
| 26 | + } |
| 27 | + |
| 28 | + const isUrlValid = isValidURL(host); |
| 29 | + if (isUrlValid) { |
| 30 | + return host; |
| 31 | + } |
| 32 | + |
| 33 | + const urlWithIpV6HostRegExp = new RegExp(/^http(s)?:\/\/(?<unescapedIpWithPort>([a-z0-9]{0,4}:?)+)/gim); |
| 34 | + const { unescapedIpWithPort } = urlWithIpV6HostRegExp.exec(host)?.groups ?? {}; |
| 35 | + |
| 36 | + if (!unescapedIpWithPort) { |
| 37 | + return host; |
| 38 | + } |
| 39 | + |
| 40 | + const separatedIpPortionsAndPort = unescapedIpWithPort.split(':'); |
| 41 | + const ipPortions = separatedIpPortionsAndPort.slice(0, separatedIpPortionsAndPort.length - 1); |
| 42 | + const port = separatedIpPortionsAndPort.at(-1); |
| 43 | + const escapedIpWithPort = `[${ipPortions.join(':')}]:${port}`; |
| 44 | + |
| 45 | + return host.replace(unescapedIpWithPort, escapedIpWithPort); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @param {string} url |
| 50 | + * @returns {boolean} |
| 51 | + */ |
| 52 | +function isValidURL(url) { |
| 53 | + try { |
| 54 | + new URL(url); |
| 55 | + |
| 56 | + return true; |
| 57 | + } catch { |
| 58 | + return false; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +module.exports = { |
| 63 | + escapeV6IpForURL, |
| 64 | +}; |
0 commit comments