normalize-domain.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 normalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
  23. if (domain.length === 0) return null;
  24. if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
  25. return null;
  26. }
  27. parsed ??= tldts.parse(domain, normalizeTldtsOpt);
  28. // Private invalid domain (things like .tor, .dn42, etc)
  29. if (!parsed.isIcann && !parsed.isPrivate) return null;
  30. // const h = parsed.hostname;
  31. // if (h === null) return null;
  32. return parsed.hostname;
  33. }