resolve-uri.umd.js 8.4 KB

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