resolve-uri.umd.js 7.6 KB

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