source: trip-planner-front/node_modules/@jridgewell/resolve-uri/dist/lib/resolve-uri.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.3 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/* istanbul ignore next */
4const Url = (typeof URL !== 'undefined' ? URL : require('url').URL);
5// Matches "..", which must be preceeded by "/" or the start of the string, and
6// must be followed by a "/". We do not eat the following "/", so that the next
7// iteration can match on it.
8const parentRegex = /(^|\/)\.\.(?=\/|$)/g;
9function isAbsoluteUrl(url) {
10 try {
11 return !!new Url(url);
12 }
13 catch (e) {
14 return false;
15 }
16}
17/**
18 * Creates a directory name that is guaranteed to not be in `str`.
19 */
20function uniqInStr(str) {
21 let uniq = String(Math.random()).slice(2);
22 while (str.indexOf(uniq) > -1) {
23 /* istanbul ignore next */
24 uniq += uniq;
25 }
26 return uniq;
27}
28/**
29 * Removes the filename from the path (everything trailing the last "/"). This
30 * is only safe to call on a path, never call with an absolute or protocol
31 * relative URL.
32 */
33function stripPathFilename(path) {
34 path = normalizePath(path);
35 const index = path.lastIndexOf('/');
36 return path.slice(0, index + 1);
37}
38/**
39 * Normalizes a protocol-relative URL, but keeps it protocol relative by
40 * stripping out the protocl before returning it.
41 */
42function normalizeProtocolRelative(input, absoluteBase) {
43 const { href, protocol } = new Url(input, absoluteBase);
44 return href.slice(protocol.length);
45}
46/**
47 * Normalizes a simple path (one that has no ".."s, or is absolute so ".."s can
48 * be normalized absolutely).
49 */
50function normalizeSimplePath(input) {
51 const { href } = new Url(input, 'https://foo.com/');
52 return href.slice('https://foo.com/'.length);
53}
54/**
55 * Normalizes a path, ensuring that excess ".."s are preserved for relative
56 * paths in the output.
57 *
58 * If the input is absolute, this will return an absolutey normalized path, but
59 * it will not have a leading "/".
60 *
61 * If the input has a leading "..", the output will have a leading "..".
62 *
63 * If the input has a leading ".", the output will not have a leading "."
64 * unless there are too many ".."s, in which case there will be a leading "..".
65 */
66function normalizePath(input) {
67 // If there are no ".."s, we can treat this as if it were an absolute path.
68 // The return won't be an absolute path, so it's easy.
69 if (!parentRegex.test(input))
70 return normalizeSimplePath(input);
71 // We already found one "..". Let's see how many there are.
72 let total = 1;
73 while (parentRegex.test(input))
74 total++;
75 // If there are ".."s, we need to prefix the the path with the same number of
76 // unique directories. This is to ensure that we "remember" how many parent
77 // directories we are accessing. Eg, "../../.." must keep 3, and "foo/../.."
78 // must keep 1.
79 const uniqDirectory = `z${uniqInStr(input)}/`;
80 // uniqDirectory is just a "z", followed by numbers, followed by a "/". So
81 // generating a runtime regex from it is safe. We'll use this search regex to
82 // strip out our uniq directory names and insert any needed ".."s.
83 const search = new RegExp(`^(?:${uniqDirectory})*`);
84 // Now we can resolve the total path. If there are excess ".."s, they will
85 // eliminate one or more of the unique directories we prefix with.
86 const relative = normalizeSimplePath(uniqDirectory.repeat(total) + input);
87 // We can now count the number of unique directories that were eliminated. If
88 // there were 3, and 1 was eliminated, we know we only need to add 1 "..". If
89 // 2 were eliminated, we need to insert 2 ".."s. If all 3 were eliminated,
90 // then we need 3, etc. This replace is guranteed to match (it may match 0 or
91 // more times), and we can count the total match to see how many were eliminated.
92 return relative.replace(search, (all) => {
93 const leftover = all.length / uniqDirectory.length;
94 return '../'.repeat(total - leftover);
95 });
96}
97/**
98 * Attempts to resolve `input` URL relative to `base`.
99 */
100function resolve(input, base) {
101 if (!base)
102 base = '';
103 // Absolute URLs are very easy to resolve right.
104 if (isAbsoluteUrl(input))
105 return new Url(input).href;
106 if (base) {
107 // Absolute URLs are easy...
108 if (isAbsoluteUrl(base))
109 return new Url(input, base).href;
110 // If base is protocol relative, we'll resolve with it but keep the result
111 // protocol relative.
112 if (base.startsWith('//'))
113 return normalizeProtocolRelative(input, `https:${base}`);
114 }
115 // Normalize input, but keep it protocol relative. We know base doesn't supply
116 // a protocol, because that would have been handled above.
117 if (input.startsWith('//'))
118 return normalizeProtocolRelative(input, 'https://foo.com/');
119 // We now know that base (if there is one) and input are paths. We've handled
120 // both absolute and protocol-relative variations above.
121 // Absolute paths don't need any special handling, because they cannot have
122 // extra "." or ".."s. That'll all be stripped away. Input takes priority here,
123 // because if input is an absolute path, base path won't affect it in any way.
124 if (input.startsWith('/'))
125 return '/' + normalizeSimplePath(input);
126 // Since input and base are paths, we need to join them to do any further
127 // processing. Paths are joined at the directory level, so we need to remove
128 // the base's filename before joining. We also know that input does not have a
129 // leading slash, and that the stripped base will have a trailing slash if
130 // there are any directories (or it'll be empty).
131 const joined = stripPathFilename(base) + input;
132 // If base is an absolute path, then input will be relative to it.
133 if (base.startsWith('/'))
134 return '/' + normalizeSimplePath(joined);
135 // We now know both base (if there is one) and input are relative paths.
136 const relative = normalizePath(joined);
137 // If base started with a leading ".", or there is no base and input started
138 // with a ".", then we need to ensure that the relative path starts with a
139 // ".". We don't know if relative starts with a "..", though, so check before
140 // prepending.
141 if ((base || input).startsWith('.') && !relative.startsWith('.')) {
142 return './' + relative;
143 }
144 return relative;
145}
146exports.default = resolve;
147//# sourceMappingURL=resolve-uri.js.map
Note: See TracBrowser for help on using the repository browser.