resolve-uri.umd.js 9.6 KB

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