cached-tld-parse.ts 1005 B

123456789101112131415161718192021
  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. const sharedConfig2 = { allowPrivateDomains: true, detectIp: false };
  7. /** { allowPrivateDomains: true } */
  8. export const parse = (domain: string) => cache.sync(domain, () => tldts.parse(domain, sharedConfig));
  9. /** { allowPrivateDomains: true, detectIp: false } */
  10. export const parse2 = (domain: string) => cache.sync(domain, () => tldts.parse(domain, sharedConfig2));
  11. let gothillGetDomainCache: ReturnType<typeof createCache> | null = null;
  12. export const createCachedGorhillGetDomain = (gorhill: PublicSuffixList) => {
  13. return (domain: string) => {
  14. gothillGetDomainCache ??= createCache('cached-gorhill-get-domain', true);
  15. return gothillGetDomainCache.sync(domain, () => gorhill.getDomain(domain[0] === '.' ? domain.slice(1) : domain));
  16. };
  17. };