From 2a5496532fa57f17ae9ca13dae3f0bf7abb0fd3b Mon Sep 17 00:00:00 2001 From: Yichao 'Peak' Ji Date: Thu, 3 May 2018 15:13:25 +0800 Subject: [PATCH] url: fix WHATWG host formatting error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current url.format implementation will return an invalid URL string without the host if there is a port and unicode: true. This unexpected behavior is caused by domainToUnicode, which expects a hostname instead of a host string according to node_url.cc. Adds both a fix and a test for the issue. PR-URL: https://github.com/nodejs/node/pull/20493 Reviewed-By: Michaël Zasso Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Anatoli Papirovski Reviewed-By: Trivikram Kamat Reviewed-By: Joyee Cheung Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Ruben Bridgewater Reviewed-By: Daijiro Wachi --- lib/internal/url.js | 4 +++- test/parallel/test-url-format-whatwg.js | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index cff94e6b7d2b5b..d9daef1524787d 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -400,7 +400,9 @@ Object.defineProperties(URL.prototype, { ret += '@'; } ret += options.unicode ? - domainToUnicode(this.host) : this.host; + domainToUnicode(this.hostname) : this.hostname; + if (ctx.port !== null) + ret += `:${ctx.port}`; } else if (ctx.scheme === 'file:') { ret += '//'; } diff --git a/test/parallel/test-url-format-whatwg.js b/test/parallel/test-url-format-whatwg.js index 26cef6063c212f..e5c3e369e80390 100644 --- a/test/parallel/test-url-format-whatwg.js +++ b/test/parallel/test-url-format-whatwg.js @@ -111,3 +111,8 @@ assert.strictEqual( url.format(myURL, { unicode: 0 }), 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' ); + +assert.strictEqual( + url.format(new URL('http://xn--0zwm56d.com:8080/path'), { unicode: true }), + 'http://测试.com:8080/path' +);