From fa2d43bd3efec57d5b17f0a2fa60e0d38ed0dad6 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 1 Apr 2018 08:52:10 +0200 Subject: [PATCH] url: make urlToOptions() handle IPv6 literals Strip the enclosing square brackets from the parsed hostname. PR-URL: https://github.com/nodejs/node/pull/19720 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/internal/url.js | 4 +++- test/parallel/test-whatwg-url-properties.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 1d5220df0b97bb..cff94e6b7d2b5b 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1314,7 +1314,9 @@ function domainToUnicode(domain) { function urlToOptions(url) { var options = { protocol: url.protocol, - hostname: url.hostname, + hostname: url.hostname.startsWith('[') ? + url.hostname.slice(1, -1) : + url.hostname, hash: url.hash, search: url.search, pathname: url.pathname, diff --git a/test/parallel/test-whatwg-url-properties.js b/test/parallel/test-whatwg-url-properties.js index d6caae511aed47..230315a70efdfc 100644 --- a/test/parallel/test-whatwg-url-properties.js +++ b/test/parallel/test-whatwg-url-properties.js @@ -143,6 +143,9 @@ assert.strictEqual(url.searchParams, oldParams); assert.strictEqual(opts.pathname, '/aaa/zzz'); assert.strictEqual(opts.search, '?l=24'); assert.strictEqual(opts.hash, '#test'); + + const { hostname } = urlToOptions(new URL('http://[::1]:21')); + assert.strictEqual(hostname, '::1'); } // Test special origins