cached-tld-parse.js 839 B

123456789101112131415161718192021222324
  1. const tldts = require('tldts');
  2. const { createCache } = require('./cache-apply');
  3. const cache = createCache('cached-tld-parse', true);
  4. const sharedConfig = { allowPrivateDomains: true };
  5. /**
  6. * @param {string} domain
  7. * @returns {ReturnType<import('tldts').parse>}
  8. */
  9. module.exports.parse = (domain) => cache.sync(domain, () => tldts.parse(domain, sharedConfig));
  10. let gothillGetDomainCache = null;
  11. /**
  12. * @param {import('gorhill-publicsuffixlist').default | null} gorhill
  13. */
  14. module.exports.createCachedGorhillGetDomain = (gorhill) => {
  15. gothillGetDomainCache ||= createCache('cached-gorhill-get-domain', true);
  16. /**
  17. * @param {string} domain
  18. */
  19. return (domain) => (/** @type {ReturnType<typeof createCache>} */ (gothillGetDomainCache)).sync(domain, () => gorhill.getDomain(domain[0] === '.' ? domain.slice(1) : domain));
  20. };