normalize-domain.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. export type TldTsParsed = ReturnType<typeof tldts.parse>;
  8. /**
  9. * Skipped the input non-empty check, the `domain` should not be empty.
  10. */
  11. export function fastNormalizeDomainWithoutWww(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. if (parsed.subdomain) {
  21. if (parsed.subdomain === 'www' || parsed.subdomain === 'xml-v4') {
  22. return parsed.domain;
  23. }
  24. if (parsed.subdomain.startsWith('www.')) {
  25. return parsed.subdomain.slice(4) + '.' + parsed.domain;
  26. }
  27. }
  28. return parsed.hostname;
  29. }
  30. /**
  31. * Skipped the input non-empty check, the `domain` should not be empty.
  32. */
  33. export function fastNormalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
  34. // We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
  35. // Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
  36. if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
  37. return null;
  38. }
  39. parsed ??= tldts.parse(domain, normalizeTldtsOpt);
  40. // Private invalid domain (things like .tor, .dn42, etc)
  41. if (!parsed.isIcann && !parsed.isPrivate) return null;
  42. return parsed.hostname;
  43. }
  44. export function normalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
  45. if (domain.length === 0) return null;
  46. if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
  47. return null;
  48. }
  49. parsed ??= tldts.parse(domain, normalizeTldtsOpt);
  50. // Private invalid domain (things like .tor, .dn42, etc)
  51. if (!parsed.isIcann && !parsed.isPrivate) return null;
  52. // const h = parsed.hostname;
  53. // if (h === null) return null;
  54. return parsed.hostname;
  55. }