index.mjs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import parsePath from 'parse-path';
  2. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
  3. const DATA_URL_DEFAULT_MIME_TYPE = 'text/plain';
  4. const DATA_URL_DEFAULT_CHARSET = 'us-ascii';
  5. const testParameter = (name, filters) => filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
  6. const normalizeDataURL = (urlString, {stripHash}) => {
  7. const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
  8. if (!match) {
  9. throw new Error(`Invalid URL: ${urlString}`);
  10. }
  11. let {type, data, hash} = match.groups;
  12. const mediaType = type.split(';');
  13. hash = stripHash ? '' : hash;
  14. let isBase64 = false;
  15. if (mediaType[mediaType.length - 1] === 'base64') {
  16. mediaType.pop();
  17. isBase64 = true;
  18. }
  19. // Lowercase MIME type
  20. const mimeType = (mediaType.shift() || '').toLowerCase();
  21. const attributes = mediaType
  22. .map(attribute => {
  23. let [key, value = ''] = attribute.split('=').map(string => string.trim());
  24. // Lowercase `charset`
  25. if (key === 'charset') {
  26. value = value.toLowerCase();
  27. if (value === DATA_URL_DEFAULT_CHARSET) {
  28. return '';
  29. }
  30. }
  31. return `${key}${value ? `=${value}` : ''}`;
  32. })
  33. .filter(Boolean);
  34. const normalizedMediaType = [
  35. ...attributes,
  36. ];
  37. if (isBase64) {
  38. normalizedMediaType.push('base64');
  39. }
  40. if (normalizedMediaType.length > 0 || (mimeType && mimeType !== DATA_URL_DEFAULT_MIME_TYPE)) {
  41. normalizedMediaType.unshift(mimeType);
  42. }
  43. return `data:${normalizedMediaType.join(';')},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ''}`;
  44. };
  45. function normalizeUrl(urlString, options) {
  46. options = {
  47. defaultProtocol: 'http:',
  48. normalizeProtocol: true,
  49. forceHttp: false,
  50. forceHttps: false,
  51. stripAuthentication: true,
  52. stripHash: false,
  53. stripTextFragment: true,
  54. stripWWW: true,
  55. removeQueryParameters: [/^utm_\w+/i],
  56. removeTrailingSlash: true,
  57. removeSingleSlash: true,
  58. removeDirectoryIndex: false,
  59. sortQueryParameters: true,
  60. ...options,
  61. };
  62. urlString = urlString.trim();
  63. // Data URL
  64. if (/^data:/i.test(urlString)) {
  65. return normalizeDataURL(urlString, options);
  66. }
  67. if (/^view-source:/i.test(urlString)) {
  68. throw new Error('`view-source:` is not supported as it is a non-standard protocol');
  69. }
  70. const hasRelativeProtocol = urlString.startsWith('//');
  71. const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
  72. // Prepend protocol
  73. if (!isRelativeUrl) {
  74. urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol);
  75. }
  76. const urlObject = new URL(urlString);
  77. if (options.forceHttp && options.forceHttps) {
  78. throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
  79. }
  80. if (options.forceHttp && urlObject.protocol === 'https:') {
  81. urlObject.protocol = 'http:';
  82. }
  83. if (options.forceHttps && urlObject.protocol === 'http:') {
  84. urlObject.protocol = 'https:';
  85. }
  86. // Remove auth
  87. if (options.stripAuthentication) {
  88. urlObject.username = '';
  89. urlObject.password = '';
  90. }
  91. // Remove hash
  92. if (options.stripHash) {
  93. urlObject.hash = '';
  94. } else if (options.stripTextFragment) {
  95. urlObject.hash = urlObject.hash.replace(/#?:~:text.*?$/i, '');
  96. }
  97. // Remove duplicate slashes if not preceded by a protocol
  98. // NOTE: This could be implemented using a single negative lookbehind
  99. // regex, but we avoid that to maintain compatibility with older js engines
  100. // which do not have support for that feature.
  101. if (urlObject.pathname) {
  102. // TODO: Replace everything below with `urlObject.pathname = urlObject.pathname.replace(/(?<!\b[a-z][a-z\d+\-.]{1,50}:)\/{2,}/g, '/');` when Safari supports negative lookbehind.
  103. // Split the string by occurrences of this protocol regex, and perform
  104. // duplicate-slash replacement on the strings between those occurrences
  105. // (if any).
  106. const protocolRegex = /\b[a-z][a-z\d+\-.]{1,50}:\/\//g;
  107. let lastIndex = 0;
  108. let result = '';
  109. for (;;) {
  110. const match = protocolRegex.exec(urlObject.pathname);
  111. if (!match) {
  112. break;
  113. }
  114. const protocol = match[0];
  115. const protocolAtIndex = match.index;
  116. const intermediate = urlObject.pathname.slice(lastIndex, protocolAtIndex);
  117. result += intermediate.replace(/\/{2,}/g, '/');
  118. result += protocol;
  119. lastIndex = protocolAtIndex + protocol.length;
  120. }
  121. const remnant = urlObject.pathname.slice(lastIndex, urlObject.pathname.length);
  122. result += remnant.replace(/\/{2,}/g, '/');
  123. urlObject.pathname = result;
  124. }
  125. // Decode URI octets
  126. if (urlObject.pathname) {
  127. try {
  128. urlObject.pathname = decodeURI(urlObject.pathname);
  129. } catch {}
  130. }
  131. // Remove directory index
  132. if (options.removeDirectoryIndex === true) {
  133. options.removeDirectoryIndex = [/^index\.[a-z]+$/];
  134. }
  135. if (Array.isArray(options.removeDirectoryIndex) && options.removeDirectoryIndex.length > 0) {
  136. let pathComponents = urlObject.pathname.split('/');
  137. const lastComponent = pathComponents[pathComponents.length - 1];
  138. if (testParameter(lastComponent, options.removeDirectoryIndex)) {
  139. pathComponents = pathComponents.slice(0, -1);
  140. urlObject.pathname = pathComponents.slice(1).join('/') + '/';
  141. }
  142. }
  143. if (urlObject.hostname) {
  144. // Remove trailing dot
  145. urlObject.hostname = urlObject.hostname.replace(/\.$/, '');
  146. // Remove `www.`
  147. if (options.stripWWW && /^www\.(?!www\.)[a-z\-\d]{1,63}\.[a-z.\-\d]{2,63}$/.test(urlObject.hostname)) {
  148. // Each label should be max 63 at length (min: 1).
  149. // Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
  150. // Each TLD should be up to 63 characters long (min: 2).
  151. // It is technically possible to have a single character TLD, but none currently exist.
  152. urlObject.hostname = urlObject.hostname.replace(/^www\./, '');
  153. }
  154. }
  155. // Remove query unwanted parameters
  156. if (Array.isArray(options.removeQueryParameters)) {
  157. // eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
  158. for (const key of [...urlObject.searchParams.keys()]) {
  159. if (testParameter(key, options.removeQueryParameters)) {
  160. urlObject.searchParams.delete(key);
  161. }
  162. }
  163. }
  164. if (options.removeQueryParameters === true) {
  165. urlObject.search = '';
  166. }
  167. // Sort query parameters
  168. if (options.sortQueryParameters) {
  169. urlObject.searchParams.sort();
  170. // Calling `.sort()` encodes the search parameters, so we need to decode them again.
  171. try {
  172. urlObject.search = decodeURIComponent(urlObject.search);
  173. } catch {}
  174. }
  175. if (options.removeTrailingSlash) {
  176. urlObject.pathname = urlObject.pathname.replace(/\/$/, '');
  177. }
  178. const oldUrlString = urlString;
  179. // Take advantage of many of the Node `url` normalizations
  180. urlString = urlObject.toString();
  181. if (!options.removeSingleSlash && urlObject.pathname === '/' && !oldUrlString.endsWith('/') && urlObject.hash === '') {
  182. urlString = urlString.replace(/\/$/, '');
  183. }
  184. // Remove ending `/` unless removeSingleSlash is false
  185. if ((options.removeTrailingSlash || urlObject.pathname === '/') && urlObject.hash === '' && options.removeSingleSlash) {
  186. urlString = urlString.replace(/\/$/, '');
  187. }
  188. // Restore relative protocol, if applicable
  189. if (hasRelativeProtocol && !options.normalizeProtocol) {
  190. urlString = urlString.replace(/^http:\/\//, '//');
  191. }
  192. // Remove http/https
  193. if (options.stripProtocol) {
  194. urlString = urlString.replace(/^(?:https?:)?\/\//, '');
  195. }
  196. return urlString;
  197. }
  198. // Dependencies
  199. /**
  200. * parseUrl
  201. * Parses the input url.
  202. *
  203. * **Note**: This *throws* if invalid urls are provided.
  204. *
  205. * @name parseUrl
  206. * @function
  207. * @param {String} url The input url.
  208. * @param {Boolean|Object} normalize Whether to normalize the url or not.
  209. * Default is `false`. If `true`, the url will
  210. * be normalized. If an object, it will be the
  211. * options object sent to [`normalize-url`](https://github.com/sindresorhus/normalize-url).
  212. *
  213. * For SSH urls, normalize won't work.
  214. *
  215. * @return {Object} An object containing the following fields:
  216. *
  217. * - `protocols` (Array): An array with the url protocols (usually it has one element).
  218. * - `protocol` (String): The first protocol, `"ssh"` (if the url is a ssh url) or `"file"`.
  219. * - `port` (null|Number): The domain port.
  220. * - `resource` (String): The url domain (including subdomains).
  221. * - `user` (String): The authentication user (usually for ssh urls).
  222. * - `pathname` (String): The url pathname.
  223. * - `hash` (String): The url hash.
  224. * - `search` (String): The url querystring value.
  225. * - `href` (String): The input url.
  226. * - `query` (Object): The url querystring, parsed as object.
  227. * - `parse_failed` (Boolean): Whether the parsing failed or not.
  228. */
  229. const parseUrl = (url, normalize = false) => {
  230. // Constants
  231. const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;
  232. const throwErr = msg => {
  233. const err = new Error(msg);
  234. err.subject_url = url;
  235. throw err
  236. };
  237. if (typeof url !== "string" || !url.trim()) {
  238. throwErr("Invalid url.");
  239. }
  240. if (url.length > parseUrl.MAX_INPUT_LENGTH) {
  241. throwErr("Input exceeds maximum length. If needed, change the value of parseUrl.MAX_INPUT_LENGTH.");
  242. }
  243. if (normalize) {
  244. if (typeof normalize !== "object") {
  245. normalize = {
  246. stripHash: false
  247. };
  248. }
  249. url = normalizeUrl(url, normalize);
  250. }
  251. const parsed = parsePath(url);
  252. // Potential git-ssh urls
  253. if (parsed.parse_failed) {
  254. const matched = parsed.href.match(GIT_RE);
  255. if (matched) {
  256. parsed.protocols = ["ssh"];
  257. parsed.protocol = "ssh";
  258. parsed.resource = matched[2];
  259. parsed.host = matched[2];
  260. parsed.user = matched[1];
  261. parsed.pathname = `/${matched[3]}`;
  262. parsed.parse_failed = false;
  263. } else {
  264. throwErr("URL parsing failed.");
  265. }
  266. }
  267. return parsed;
  268. };
  269. parseUrl.MAX_INPUT_LENGTH = 2048;
  270. export { parseUrl as default };