File tree 2 files changed +13
-1
lines changed
2 files changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,15 @@ describe(fromUrl.name, () => {
57
57
expect ( fromUrl ( "[1:2:3:4:5:6:7:8]" ) ) . toBe ( "[1:2:3:4:5:6:7:8]" ) ;
58
58
} ) ;
59
59
60
+ // https://github.com/peerigon/parse-domain/issues/114
61
+ test ( "it handles URLs with invalid IPv6" , ( ) => {
62
+ expect ( fromUrl ( "http://1:2:3:4:5:6:7:8/path?query" ) ) . toBe (
63
+ "[1:2:3:4:5:6:7:8]"
64
+ ) ;
65
+ expect ( fromUrl ( "//1:2:3:4:5:6:7:8" ) ) . toBe ( "[1:2:3:4:5:6:7:8]" ) ;
66
+ expect ( fromUrl ( "1:2:3:4:5:6:7:8" ) ) . toBe ( "[1:2:3:4:5:6:7:8]" ) ;
67
+ } ) ;
68
+
60
69
test ( "it returns the NO_HOSTNAME symbol for invalid URLs" , ( ) => {
61
70
expect ( fromUrl ( ":8080/path?query" ) ) . toBe ( NO_HOSTNAME ) ;
62
71
expect ( fromUrl ( "/path?query" ) ) . toBe ( NO_HOSTNAME ) ;
Original file line number Diff line number Diff line change 1
1
const urlPattern = / ^ [ a - z ] [ * + . a - z - ] + : \/ \/ / i;
2
+ const invalidIpv6Pattern = / ^ ( [ a - z ] [ * + . a - z - ] + : \/ \/ ) ( [ ^ [ ] .* : [ ^ / ? ] * : [ ^ / ? ] * ) ( .* ) / i;
2
3
3
4
export const NO_HOSTNAME : unique symbol = Symbol ( "NO_HOSTNAME" ) ;
4
5
@@ -16,7 +17,7 @@ export const fromUrl = (urlLike: string) => {
16
17
}
17
18
18
19
// URLs that start with // are protocol relative
19
- const url = urlLike . startsWith ( "//" )
20
+ let url = urlLike . startsWith ( "//" )
20
21
? `http:${ urlLike } `
21
22
: // URLs that start with / do not have a hostname section
22
23
urlLike . startsWith ( "/" )
@@ -25,6 +26,8 @@ export const fromUrl = (urlLike: string) => {
25
26
? urlLike
26
27
: `http://${ urlLike } ` ;
27
28
29
+ url = url . replace ( invalidIpv6Pattern , "$1[$2]$3" ) ;
30
+
28
31
try {
29
32
return new URL ( url ) . hostname ;
30
33
} catch {
You can’t perform that action at this time.
0 commit comments