| 1 |
// Matches the scheme of a URL, eg "http://" |
| 2 |
const schemeRegex = /^[\w+.-]+:\/\//; |
| 3 |
|
| 4 |
/** |
| 5 |
* Matches the parts of a URL: |
| 6 |
* 1. Scheme, including ":", guaranteed. |
| 7 |
* 2. User/password, including "@", optional. |
| 8 |
* 3. Host, guaranteed. |
| 9 |
* 4. Port, including ":", optional. |
| 10 |
* 5. Path, including "/", optional. |
| 11 |
* 6. Query, including "?", optional. |
| 12 |
* 7. Hash, including "#", optional. |
| 13 |
*/ |
| 14 |
const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#]*)?(#.*)?/; |
| 15 |
|
| 16 |
/** |
| 17 |
* File URLs are weird. They dont' need the regular `//` in the scheme, they may or may not start |
| 18 |
* with a leading `/`, they can have a domain (but only if they don't start with a Windows drive). |
| 19 |
* |
| 20 |
* 1. Host, optional. |
| 21 |
* 2. Path, which may include "/", guaranteed. |
| 22 |
* 3. Query, including "?", optional. |
| 23 |
* 4. Hash, including "#", optional. |
| 24 |
*/ |
| 25 |
const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i; |
| 26 |
|
| 27 |
type Url = { |
| 28 |
scheme: string; |
| 29 |
user: string; |
| 30 |
host: string; |
| 31 |
port: string; |
| 32 |
path: string; |
| 33 |
query: string; |
| 34 |
hash: string; |
| 35 |
type: UrlType; |
| 36 |
}; |
| 37 |
|
| 38 |
enum UrlType { |
| 39 |
Empty = 1, |
| 40 |
Hash = 2, |
| 41 |
Query = 3, |
| 42 |
RelativePath = 4, |
| 43 |
AbsolutePath = 5, |
| 44 |
SchemeRelative = 6, |
| 45 |
Absolute = 7, |
| 46 |
} |
| 47 |
|
| 48 |
function isAbsoluteUrl(input: string): boolean { |
| 49 |
return schemeRegex.test(input); |
| 50 |
} |
| 51 |
|
| 52 |
function isSchemeRelativeUrl(input: string): boolean { |
| 53 |
return input.startsWith('//'); |
| 54 |
} |
| 55 |
|
| 56 |
function isAbsolutePath(input: string): boolean { |
| 57 |
return input.startsWith('/'); |
| 58 |
} |
| 59 |
|
| 60 |
function isFileUrl(input: string): boolean { |
| 61 |
return input.startsWith('file:'); |
| 62 |
} |
| 63 |
|
| 64 |
function isRelative(input: string): boolean { |
| 65 |
return /^[.?#]/.test(input); |
| 66 |
} |
| 67 |
|
| 68 |
function parseAbsoluteUrl(input: string): Url { |
| 69 |
const match = urlRegex.exec(input)!; |
| 70 |
return makeUrl( |
| 71 |
match[1], |
| 72 |
match[2] || '', |
| 73 |
match[3], |
| 74 |
match[4] || '', |
| 75 |
match[5] || '/', |
| 76 |
match[6] || '', |
| 77 |
match[7] || '', |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
function parseFileUrl(input: string): Url { |
| 82 |
const match = fileRegex.exec(input)!; |
| 83 |
const path = match[2]; |
| 84 |
return makeUrl( |
| 85 |
'file:', |
| 86 |
'', |
| 87 |
match[1] || '', |
| 88 |
'', |
| 89 |
isAbsolutePath(path) ? path : '/' + path, |
| 90 |
match[3] || '', |
| 91 |
match[4] || '', |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
function makeUrl( |
| 96 |
scheme: string, |
| 97 |
user: string, |
| 98 |
host: string, |
| 99 |
port: string, |
| 100 |
path: string, |
| 101 |
query: string, |
| 102 |
hash: string, |
| 103 |
): Url { |
| 104 |
return { |
| 105 |
scheme, |
| 106 |
user, |
| 107 |
host, |
| 108 |
port, |
| 109 |
path, |
| 110 |
query, |
| 111 |
hash, |
| 112 |
type: UrlType.Absolute, |
| 113 |
}; |
| 114 |
} |
| 115 |
|
| 116 |
function parseUrl(input: string): Url { |
| 117 |
if (isSchemeRelativeUrl(input)) { |
| 118 |
const url = parseAbsoluteUrl('http:' + input); |
| 119 |
url.scheme = ''; |
| 120 |
url.type = UrlType.SchemeRelative; |
| 121 |
return url; |
| 122 |
} |
| 123 |
|
| 124 |
if (isAbsolutePath(input)) { |
| 125 |
const url = parseAbsoluteUrl('http://foo.com' + input); |
| 126 |
url.scheme = ''; |
| 127 |
url.host = ''; |
| 128 |
url.type = UrlType.AbsolutePath; |
| 129 |
return url; |
| 130 |
} |
| 131 |
|
| 132 |
if (isFileUrl(input)) return parseFileUrl(input); |
| 133 |
|
| 134 |
if (isAbsoluteUrl(input)) return parseAbsoluteUrl(input); |
| 135 |
|
| 136 |
const url = parseAbsoluteUrl('http://foo.com/' + input); |
| 137 |
url.scheme = ''; |
| 138 |
url.host = ''; |
| 139 |
url.type = input |
| 140 |
? input.startsWith('?') |
| 141 |
? UrlType.Query |
| 142 |
: input.startsWith('#') |
| 143 |
? UrlType.Hash |
| 144 |
: UrlType.RelativePath |
| 145 |
: UrlType.Empty; |
| 146 |
return url; |
| 147 |
} |
| 148 |
|
| 149 |
function stripPathFilename(path: string): string { |
| 150 |
// If a path ends with a parent directory "..", then it's a relative path with excess parent |
| 151 |
// paths. It's not a file, so we can't strip it. |
| 152 |
if (path.endsWith('/..')) return path; |
| 153 |
const index = path.lastIndexOf('/'); |
| 154 |
return path.slice(0, index + 1); |
| 155 |
} |
| 156 |
|
| 157 |
function mergePaths(url: Url, base: Url) { |
| 158 |
normalizePath(base, base.type); |
| 159 |
|
| 160 |
// If the path is just a "/", then it was an empty path to begin with (remember, we're a relative |
| 161 |
// path). |
| 162 |
if (url.path === '/') { |
| 163 |
url.path = base.path; |
| 164 |
} else { |
| 165 |
// Resolution happens relative to the base path's directory, not the file. |
| 166 |
url.path = stripPathFilename(base.path) + url.path; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* The path can have empty directories "//", unneeded parents "foo/..", or current directory |
| 172 |
* "foo/.". We need to normalize to a standard representation. |
| 173 |
*/ |
| 174 |
function normalizePath(url: Url, type: UrlType) { |
| 175 |
const rel = type <= UrlType.RelativePath; |
| 176 |
const pieces = url.path.split('/'); |
| 177 |
|
| 178 |
// We need to preserve the first piece always, so that we output a leading slash. The item at |
| 179 |
// pieces[0] is an empty string. |
| 180 |
let pointer = 1; |
| 181 |
|
| 182 |
// Positive is the number of real directories we've output, used for popping a parent directory. |
| 183 |
// Eg, "foo/bar/.." will have a positive 2, and we can decrement to be left with just "foo". |
| 184 |
let positive = 0; |
| 185 |
|
| 186 |
// We need to keep a trailing slash if we encounter an empty directory (eg, splitting "foo/" will |
| 187 |
// generate `["foo", ""]` pieces). And, if we pop a parent directory. But once we encounter a |
| 188 |
// real directory, we won't need to append, unless the other conditions happen again. |
| 189 |
let addTrailingSlash = false; |
| 190 |
|
| 191 |
for (let i = 1; i < pieces.length; i++) { |
| 192 |
const piece = pieces[i]; |
| 193 |
|
| 194 |
// An empty directory, could be a trailing slash, or just a double "//" in the path. |
| 195 |
if (!piece) { |
| 196 |
addTrailingSlash = true; |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
// If we encounter a real directory, then we don't need to append anymore. |
| 201 |
addTrailingSlash = false; |
| 202 |
|
| 203 |
// A current directory, which we can always drop. |
| 204 |
if (piece === '.') continue; |
| 205 |
|
| 206 |
// A parent directory, we need to see if there are any real directories we can pop. Else, we |
| 207 |
// have an excess of parents, and we'll need to keep the "..". |
| 208 |
if (piece === '..') { |
| 209 |
if (positive) { |
| 210 |
addTrailingSlash = true; |
| 211 |
positive--; |
| 212 |
pointer--; |
| 213 |
} else if (rel) { |
| 214 |
// If we're in a relativePath, then we need to keep the excess parents. Else, in an absolute |
| 215 |
// URL, protocol relative URL, or an absolute path, we don't need to keep excess. |
| 216 |
pieces[pointer++] = piece; |
| 217 |
} |
| 218 |
continue; |
| 219 |
} |
| 220 |
|
| 221 |
// We've encountered a real directory. Move it to the next insertion pointer, which accounts for |
| 222 |
// any popped or dropped directories. |
| 223 |
pieces[pointer++] = piece; |
| 224 |
positive++; |
| 225 |
} |
| 226 |
|
| 227 |
let path = ''; |
| 228 |
for (let i = 1; i < pointer; i++) { |
| 229 |
path += '/' + pieces[i]; |
| 230 |
} |
| 231 |
if (!path || (addTrailingSlash && !path.endsWith('/..'))) { |
| 232 |
path += '/'; |
| 233 |
} |
| 234 |
url.path = path; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Attempts to resolve `input` URL/path relative to `base`. |
| 239 |
*/ |
| 240 |
export default function resolve(input: string, base: string | undefined): string { |
| 241 |
if (!input && !base) return ''; |
| 242 |
|
| 243 |
const url = parseUrl(input); |
| 244 |
let inputType = url.type; |
| 245 |
|
| 246 |
if (base && inputType !== UrlType.Absolute) { |
| 247 |
const baseUrl = parseUrl(base); |
| 248 |
const baseType = baseUrl.type; |
| 249 |
|
| 250 |
switch (inputType) { |
| 251 |
case UrlType.Empty: |
| 252 |
url.hash = baseUrl.hash; |
| 253 |
// fall through |
| 254 |
|
| 255 |
case UrlType.Hash: |
| 256 |
url.query = baseUrl.query; |
| 257 |
// fall through |
| 258 |
|
| 259 |
case UrlType.Query: |
| 260 |
case UrlType.RelativePath: |
| 261 |
mergePaths(url, baseUrl); |
| 262 |
// fall through |
| 263 |
|
| 264 |
case UrlType.AbsolutePath: |
| 265 |
// The host, user, and port are joined, you can't copy one without the others. |
| 266 |
url.user = baseUrl.user; |
| 267 |
url.host = baseUrl.host; |
| 268 |
url.port = baseUrl.port; |
| 269 |
// fall through |
| 270 |
|
| 271 |
case UrlType.SchemeRelative: |
| 272 |
// The input doesn't have a schema at least, so we need to copy at least that over. |
| 273 |
url.scheme = baseUrl.scheme; |
| 274 |
} |
| 275 |
if (baseType > inputType) inputType = baseType; |
| 276 |
} |
| 277 |
|
| 278 |
normalizePath(url, inputType); |
| 279 |
|
| 280 |
const queryHash = url.query + url.hash; |
| 281 |
switch (inputType) { |
| 282 |
// This is impossible, because of the empty checks at the start of the function. |
| 283 |
// case UrlType.Empty: |
| 284 |
|
| 285 |
case UrlType.Hash: |
| 286 |
case UrlType.Query: |
| 287 |
return queryHash; |
| 288 |
|
| 289 |
case UrlType.RelativePath: { |
| 290 |
// The first char is always a "/", and we need it to be relative. |
| 291 |
const path = url.path.slice(1); |
| 292 |
|
| 293 |
if (!path) return queryHash || '.'; |
| 294 |
|
| 295 |
if (isRelative(base || input) && !isRelative(path)) { |
| 296 |
// If base started with a leading ".", or there is no base and input started with a ".", |
| 297 |
// then we need to ensure that the relative path starts with a ".". We don't know if |
| 298 |
// relative starts with a "..", though, so check before prepending. |
| 299 |
return './' + path + queryHash; |
| 300 |
} |
| 301 |
|
| 302 |
return path + queryHash; |
| 303 |
} |
| 304 |
|
| 305 |
case UrlType.AbsolutePath: |
| 306 |
return url.path + queryHash; |
| 307 |
|
| 308 |
default: |
| 309 |
return url.scheme + '//' + url.user + url.host + url.port + url.path + queryHash; |
| 310 |
} |
| 311 |
} |