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