@@ -26,6 +26,7 @@ const { |
26 | 26 | ObjectCreate, |
27 | 27 | ObjectKeys, |
28 | 28 | SafeSet, |
| 29 | + StringPrototypeCharCodeAt, |
29 | 30 | } = primordials; |
30 | 31 |
|
31 | 32 | const { toASCII } = require('internal/idna'); |
@@ -156,6 +157,14 @@ function urlParse(url, parseQueryString, slashesDenoteHost) { |
156 | 157 | return urlObject; |
157 | 158 | } |
158 | 159 |
|
| 160 | +function isIpv6Hostname(hostname) { |
| 161 | + return ( |
| 162 | + StringPrototypeCharCodeAt(hostname, 0) === CHAR_LEFT_SQUARE_BRACKET && |
| 163 | + StringPrototypeCharCodeAt(hostname, hostname.length - 1) === |
| 164 | + CHAR_RIGHT_SQUARE_BRACKET |
| 165 | + ); |
| 166 | +} |
| 167 | + |
159 | 168 | Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) { |
160 | 169 | validateString(url, 'url'); |
161 | 170 |
|
@@ -364,8 +373,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) { |
364 | 373 |
|
365 | 374 | // If hostname begins with [ and ends with ] |
366 | 375 | // assume that it's an IPv6 address. |
367 | | - const ipv6Hostname = hostname.charCodeAt(0) === CHAR_LEFT_SQUARE_BRACKET && |
368 | | - hostname.charCodeAt(hostname.length - 1) === CHAR_RIGHT_SQUARE_BRACKET; |
| 376 | + const ipv6Hostname = isIpv6Hostname(hostname); |
369 | 377 |
|
370 | 378 | // validate a little. |
371 | 379 | if (!ipv6Hostname) { |
@@ -590,7 +598,7 @@ Url.prototype.format = function format() { |
590 | 598 | host = auth + this.host; |
591 | 599 | } else if (this.hostname) { |
592 | 600 | host = auth + ( |
593 | | - this.hostname.includes(':') ? |
| 601 | + this.hostname.includes(':') && !isIpv6Hostname(this.hostname) ? |
594 | 602 | '[' + this.hostname + ']' : |
595 | 603 | this.hostname |
596 | 604 | ); |
|