cached-tld-parse.ts 1.0 KB

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