cached-tld-parse.ts 739 B

1234567891011121314151617
  1. import * as tldts from 'tldts';
  2. import { createCache } from './cache-apply';
  3. import type { PublicSuffixList } from 'gorhill-publicsuffixlist';
  4. const cache = createCache('cached-tld-parse', true);
  5. const sharedConfig = { allowPrivateDomains: true };
  6. export const parse = (domain: string) => cache.sync(domain, () => tldts.parse(domain, sharedConfig));
  7. let gothillGetDomainCache: ReturnType<typeof createCache> | null = null;
  8. export const createCachedGorhillGetDomain = (gorhill: PublicSuffixList) => {
  9. return (domain: string) => {
  10. gothillGetDomainCache ??= createCache('cached-gorhill-get-domain', true);
  11. return gothillGetDomainCache.sync(domain, () => gorhill.getDomain(domain[0] === '.' ? domain.slice(1) : domain));
  12. };
  13. };