Skip to content

Commit ff619d3

Browse files
peakjiMylesBorins
authored andcommitted
url: fix WHATWG host formatting error
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: #20493 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
1 parent 4c4be85 commit ff619d3

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/internal/url.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,9 @@ Object.defineProperties(URL.prototype, {
400400
ret += '@';
401401
}
402402
ret += options.unicode ?
403-
domainToUnicode(this.host) : this.host;
403+
domainToUnicode(this.hostname) : this.hostname;
404+
if (ctx.port !== null)
405+
ret += `:${ctx.port}`;
404406
} else if (ctx.scheme === 'file:') {
405407
ret += '//';
406408
}

test/parallel/test-url-format-whatwg.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,8 @@ assert.strictEqual(
111111
url.format(myURL, { unicode: 0 }),
112112
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
113113
);
114+
115+
assert.strictEqual(
116+
url.format(new URL('http://xn--0zwm56d.com:8080/path'), { unicode: true }),
117+
'http://测试.com:8080/path'
118+
);

0 commit comments

Comments
 (0)