resolve-uri.umd.js 10 KB

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