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