Skip to content

Commit

Permalink
Do not fix the protocol string if the first part is an IPv6 host (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dezzmeister committed Feb 10, 2024
1 parent 2fceca8 commit 5bd382c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/url-join.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function normalize (strArray) {
// There must be two or three slashes in the file protocol, two slashes in anything else.
if (strArray[0].match(/^file:\/\/\//)) {
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///');
} else {
} else if (!strArray[0].match(/^\[.*:.*\]/)) {
// If the first part is not an IPv6 host, we replace the protocol.
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://');
}

Expand Down
21 changes: 21 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,24 @@ test('joins leading empty string', () => {
'/test'
);
});

test('joins a leading IPv6 hostname', () => {
assert.equal(
urlJoin('[2601:195:c381:3560::f42a]/', '/test'),
'[2601:195:c381:3560::f42a]/test'
);
});

test('joins a leading IPv6 host with an IPv4 address in the least significant 32 bits', () => {
assert.equal(
urlJoin('[2601:195:c381:3560::0.0.244.42]', '/test'),
'[2601:195:c381:3560::0.0.244.42]/test'
);
});

test('joins a protocol followed by an IPv6 host', () => {
assert.equal(
urlJoin('https://', '[2601:195:c381:3560::f42a]/', '/test'),
'https://[2601:195:c381:3560::f42a]/test'
);
});

0 comments on commit 5bd382c

Please sign in to comment.