url.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. /**
  3. * Parses string form of URL into an object
  4. * // borrowed from https://tools.ietf.org/html/rfc3986#appendix-B
  5. * // intentionally using regex and not <a/> href parsing trick because React Native and other
  6. * // environments where DOM might not be available
  7. * @returns parsed URL object
  8. */
  9. function parseUrl(url) {
  10. if (!url) {
  11. return {};
  12. }
  13. const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
  14. if (!match) {
  15. return {};
  16. }
  17. // coerce to undefined values to empty string so we don't get 'undefined'
  18. const query = match[6] || '';
  19. const fragment = match[8] || '';
  20. return {
  21. host: match[4],
  22. path: match[5],
  23. protocol: match[2],
  24. search: query,
  25. hash: fragment,
  26. relative: match[5] + query + fragment, // everything minus origin
  27. };
  28. }
  29. /**
  30. * Strip the query string and fragment off of a given URL or path (if present)
  31. *
  32. * @param urlPath Full URL or path, including possible query string and/or fragment
  33. * @returns URL or path without query string or fragment
  34. */
  35. function stripUrlQueryAndFragment(urlPath) {
  36. // eslint-disable-next-line no-useless-escape
  37. return urlPath.split(/[\?#]/, 1)[0];
  38. }
  39. /**
  40. * Returns number of URL segments of a passed string URL.
  41. */
  42. function getNumberOfUrlSegments(url) {
  43. // split at '/' or at '\/' to split regex urls correctly
  44. return url.split(/\\?\//).filter(s => s.length > 0 && s !== ',').length;
  45. }
  46. /**
  47. * Takes a URL object and returns a sanitized string which is safe to use as span description
  48. * see: https://develop.sentry.dev/sdk/data-handling/#structuring-data
  49. */
  50. function getSanitizedUrlString(url) {
  51. const { protocol, host, path } = url;
  52. const filteredHost =
  53. (host &&
  54. host
  55. // Always filter out authority
  56. .replace(/^.*@/, '[filtered]:[filtered]@')
  57. // Don't show standard :80 (http) and :443 (https) ports to reduce the noise
  58. // TODO: Use new URL global if it exists
  59. .replace(/(:80)$/, '')
  60. .replace(/(:443)$/, '')) ||
  61. '';
  62. return `${protocol ? `${protocol}://` : ''}${filteredHost}${path}`;
  63. }
  64. exports.getNumberOfUrlSegments = getNumberOfUrlSegments;
  65. exports.getSanitizedUrlString = getSanitizedUrlString;
  66. exports.parseUrl = parseUrl;
  67. exports.stripUrlQueryAndFragment = stripUrlQueryAndFragment;
  68. //# sourceMappingURL=url.js.map