1
- // Dependencies
2
1
const protocols = require ( "protocols" )
3
- , isSsh = require ( "is-ssh" )
4
- , qs = require ( "query-string" )
5
- ;
6
2
7
3
/**
8
4
* parsePath
@@ -25,110 +21,50 @@ const protocols = require("protocols")
25
21
* - `query` (Object): The url querystring, parsed as object.
26
22
*/
27
23
function parsePath ( url ) {
28
- url = ( url || "" ) . trim ( ) . replace ( / \r ? \n | \r / gm, "" )
29
- var output = {
30
- protocols : protocols ( url )
31
- , protocol : null
32
- , port : null
33
- , resource : ""
34
- , user : ""
35
- , pathname : ""
36
- , hash : ""
37
- , search : ""
38
- , href : url
39
- , query : Object . create ( null )
40
- }
41
- , protocolIndex = url . indexOf ( "://" )
42
- , resourceIndex = - 1
43
- , splits = null
44
- , parts = null
45
- ;
46
24
47
- if ( url . startsWith ( "." ) ) {
48
- if ( url . startsWith ( "./" ) ) {
49
- url = url . substring ( 2 ) ;
50
- }
51
- output . pathname = url ;
52
- output . protocol = "file" ;
25
+ const output = {
26
+ protocols : [ ]
27
+ , protocol : null
28
+ , port : null
29
+ , resource : ""
30
+ , user : ""
31
+ , password : ""
32
+ , pathname : ""
33
+ , hash : ""
34
+ , search : ""
35
+ , href : url
36
+ , query : { }
53
37
}
54
38
55
- const firstChar = url . charAt ( 1 )
56
- if ( ! output . protocol ) {
39
+ try {
40
+ const parsed = new URL ( url )
41
+ output . protocols = protocols ( parsed )
57
42
output . protocol = output . protocols [ 0 ]
58
- if ( ! output . protocol ) {
59
- if ( isSsh ( url ) ) {
60
- output . protocol = "ssh"
61
- } else if ( firstChar === "/" || firstChar === "~" ) {
62
- url = url . substring ( 2 )
63
- output . protocol = "file"
64
- } else {
65
- output . protocol = "file"
66
- }
67
- }
68
- }
69
-
70
- if ( protocolIndex !== - 1 ) {
71
- url = url . substring ( protocolIndex + 3 ) ;
72
- }
73
-
74
- parts = url . split ( / \/ | \\ / ) ;
75
- if ( output . protocol !== "file" ) {
76
- output . resource = parts . shift ( ) ;
77
- } else {
78
- output . resource = "" ;
79
- }
80
-
81
- // user@domain
82
- splits = output . resource . split ( "@" ) ;
83
- if ( splits . length === 2 ) {
84
- output . user = splits [ 0 ] ;
85
- output . resource = splits [ 1 ] ;
86
- }
87
-
88
-
89
- // domain.com:port
90
- splits = output . resource . split ( ":" ) ;
91
- if ( splits . length === 2 ) {
92
- output . resource = splits [ 0 ] ;
93
- const port = splits [ 1 ] ;
94
- if ( port ) {
95
- output . port = Number ( port ) ;
96
- if ( isNaN ( output . port ) || port . match ( / ^ \d + $ / ) === null ) {
97
- output . port = null ;
98
- parts . unshift ( port ) ;
99
- }
100
- } else {
101
- output . port = null
102
- }
103
- }
104
-
105
- // Remove empty elements
106
- parts = parts . filter ( Boolean ) ;
107
-
108
- // Stringify the pathname
109
- if ( output . protocol === "file" ) {
110
- output . pathname = output . href
111
- } else {
112
- output . pathname = output . pathname || ( ( output . protocol !== "file" || output . href [ 0 ] === "/" ? "/" : "" ) + parts . join ( "/" ) ) ;
113
- }
114
-
115
- // #some-hash
116
- splits = output . pathname . split ( "#" ) ;
117
- if ( splits . length === 2 ) {
118
- output . pathname = splits [ 0 ] ;
119
- output . hash = splits [ 1 ] ;
120
- }
121
-
122
- // ?foo=bar
123
- splits = output . pathname . split ( "?" ) ;
124
- if ( splits . length === 2 ) {
125
- output . pathname = splits [ 0 ] ;
126
- output . search = splits [ 1 ] ;
43
+ output . port = parsed . port
44
+ output . resource = parsed . host
45
+ output . user = parsed . username || ""
46
+ output . password = parsed . password || ""
47
+ output . pathname = parsed . pathname
48
+ output . hash = parsed . hash . slice ( 1 )
49
+ output . search = parsed . search . slice ( 1 )
50
+ output . href = parsed . href
51
+ output . query = Object . fromEntries ( parsed . searchParams )
52
+ } catch ( e ) {
53
+ // TODO Maybe check if it is a valid local file path
54
+ // In any case, these will be parsed by higher
55
+ // level parsers such as parse-url, git-url-parse, git-up
56
+ output . protocols = [ "file" ]
57
+ output . protocol = output . protocols [ 0 ]
58
+ output . port = ""
59
+ output . resource = ""
60
+ output . user = ""
61
+ output . pathname = ""
62
+ output . hash = ""
63
+ output . search = ""
64
+ output . href = url
65
+ output . query = { }
127
66
}
128
67
129
- output . query = qs . parse ( output . search ) ;
130
- output . href = output . href . replace ( / \/ $ / , "" )
131
- output . pathname = output . pathname . replace ( / \/ $ / , "" )
132
68
return output ;
133
69
}
134
70
0 commit comments