fix(cli/js/web): IPv6 hostname should be compressed (#6772)

This commit is contained in:
JohannLai 2020-07-17 00:08:29 +08:00 committed by GitHub
parent de34166891
commit d60f9c2549
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 1 deletions

View file

@ -565,7 +565,8 @@ function encodeHostname(s: string, isSpecial = true): string {
if (!s.match(/^\[[0-9A-Fa-f.:]{2,}\]$/)) {
throw new TypeError("Invalid hostname.");
}
return s.toLowerCase();
// IPv6 address compress
return s.toLowerCase().replace(/\b:?(?:0+:?){2,}/, "::");
}
let result = s;

View file

@ -44,6 +44,8 @@ unitTest(function urlHostnameParsing(): void {
assertEquals(new URL("http://[::1]").hostname, "[::1]");
assertEquals(new URL("file://[::1]").hostname, "[::1]");
assertEquals(new URL("abcd://[::1]").hostname, "[::1]");
assertEquals(new URL("http://[0:f:0:0:f:f:0:0]").hostname, "[0:f::f:f:0:0]");
assertEquals(new URL("http://[0:0:5:6:7:8]").hostname, "[::5:6:7:8]");
// Forbidden host code point.
assertThrows(() => new URL("http:// a"), TypeError, "Invalid URL.");