resolve-uri.mjs 8.8 KB

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