GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — url: fix url.format with ipv6 hostname · nodejs/node@5cb8b16

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • url.js
  • test/parallel
    • test-url-format.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/url.js‎

Copy file name to clipboardExpand all lines: lib/url.js
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
ObjectCreate,
2727
ObjectKeys,
2828
SafeSet,
29+
StringPrototypeCharCodeAt,
2930
} = primordials;
3031

3132
const { toASCII } = require('internal/idna');
@@ -156,6 +157,14 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
156157
return urlObject;
157158
}
158159

160+
function isIpv6Hostname(hostname) {
161+
return (
162+
StringPrototypeCharCodeAt(hostname, 0) === CHAR_LEFT_SQUARE_BRACKET &&
163+
StringPrototypeCharCodeAt(hostname, hostname.length - 1) ===
164+
CHAR_RIGHT_SQUARE_BRACKET
165+
);
166+
}
167+
159168
Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
160169
validateString(url, 'url');
161170

@@ -364,8 +373,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
364373

365374
// If hostname begins with [ and ends with ]
366375
// assume that it's an IPv6 address.
367-
const ipv6Hostname = hostname.charCodeAt(0) === CHAR_LEFT_SQUARE_BRACKET &&
368-
hostname.charCodeAt(hostname.length - 1) === CHAR_RIGHT_SQUARE_BRACKET;
376+
const ipv6Hostname = isIpv6Hostname(hostname);
369377

370378
// validate a little.
371379
if (!ipv6Hostname) {
@@ -590,7 +598,7 @@ Url.prototype.format = function format() {
590598
host = auth + this.host;
591599
} else if (this.hostname) {
592600
host = auth + (
593-
this.hostname.includes(':') ?
601+
this.hostname.includes(':') && !isIpv6Hostname(this.hostname) ?
594602
'[' + this.hostname + ']' :
595603
this.hostname
596604
);
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-url-format.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ const formatTests = {
148148
host: '[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:61616',
149149
pathname: '/s/stopButton'
150150
},
151+
'http://[::]/': {
152+
href: 'http://[::]/',
153+
protocol: 'http:',
154+
hostname: '[::]',
155+
pathname: '/'
156+
},
151157

152158
// Encode context-specific delimiters in path and query, but do not touch
153159
// other non-delimiter chars like `%`.

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.