From d60f9c2549c7dfb0fbd6c9c8984c215f5dd75e76 Mon Sep 17 00:00:00 2001 From: JohannLai Date: Fri, 17 Jul 2020 00:08:29 +0800 Subject: [PATCH] fix(cli/js/web): IPv6 hostname should be compressed (#6772) --- cli/js/web/url.ts | 3 ++- cli/tests/unit/url_test.ts | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cli/js/web/url.ts b/cli/js/web/url.ts index f466f9583391c4..fabef3329168ab 100644 --- a/cli/js/web/url.ts +++ b/cli/js/web/url.ts @@ -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; diff --git a/cli/tests/unit/url_test.ts b/cli/tests/unit/url_test.ts index 15833633ae50ed..b50f6a25bd39c5 100644 --- a/cli/tests/unit/url_test.ts +++ b/cli/tests/unit/url_test.ts @@ -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.");