extractSlug.tsx 879 B

123456789101112131415161718192021222324252627282930
  1. type ExtractedSlug = {
  2. domain: string;
  3. slug: string;
  4. };
  5. // XXX: If you change this also change its sibiling in:
  6. // - static/index.ejs
  7. // - webpack.config.ts
  8. const KNOWN_DOMAINS = /(?:\.?)((?:localhost|dev\.getsentry\.net|sentry\.dev)(?:\:\d*)?)$/;
  9. /**
  10. * Extract a slug from a known local development host.
  11. * If the host is not a known development host null is returned.
  12. */
  13. export function extractSlug(hostname: string): ExtractedSlug | null {
  14. const match = hostname.match(KNOWN_DOMAINS);
  15. if (!match) {
  16. return null;
  17. }
  18. const [
  19. matchedExpression, // Expression includes optional leading `.`
  20. matchedDomain, // First match group never includes optional leading `.`
  21. ] = match;
  22. const [slug, ...domainParts] = hostname.replace(matchedExpression, '').split('.');
  23. const domain = domainParts.concat(matchedDomain).join('.');
  24. return {slug, domain};
  25. }