normalize-domain.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // https://github.com/remusao/tldts/issues/2121
  2. // In short, single label domain suffix is ignored due to the size optimization, so no isIcann
  3. // import tldts from 'tldts-experimental';
  4. import tldts from 'tldts';
  5. import { normalizeTldtsOpt } from '../constants/loose-tldts-opt';
  6. import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
  7. type TldTsParsed = ReturnType<typeof tldts.parse>;
  8. /**
  9. * Skipped the input non-empty check, the `domain` should not be empty.
  10. */
  11. export function fastNormalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
  12. // We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
  13. // Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
  14. if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
  15. return null;
  16. }
  17. parsed ??= tldts.parse(domain, normalizeTldtsOpt);
  18. // Private invalid domain (things like .tor, .dn42, etc)
  19. if (!parsed.isIcann && !parsed.isPrivate) return null;
  20. return parsed.hostname;
  21. }
  22. export function fastNormalizeDomainIgnoreWww(domain: string, parsed: TldTsParsed | null = null) {
  23. // We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
  24. // Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
  25. if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
  26. return null;
  27. }
  28. parsed ??= tldts.parse(domain, normalizeTldtsOpt);
  29. // Private invalid domain (things like .tor, .dn42, etc)
  30. if (!parsed.isIcann && !parsed.isPrivate) return null;
  31. if (parsed.subdomain === 'www') {
  32. return parsed.domain;
  33. }
  34. return parsed.hostname;
  35. }
  36. export function normalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
  37. if (domain.length === 0) return null;
  38. if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
  39. return null;
  40. }
  41. parsed ??= tldts.parse(domain, normalizeTldtsOpt);
  42. // Private invalid domain (things like .tor, .dn42, etc)
  43. if (!parsed.isIcann && !parsed.isPrivate) return null;
  44. // const h = parsed.hostname;
  45. // if (h === null) return null;
  46. return parsed.hostname;
  47. }