cached-tld-parse.js 803 B

1234567891011121314151617181920212223242526
  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) => {
  10. return cache.sync(domain, () => tldts.parse(domain, sharedConfig));
  11. };
  12. let gothillGetDomainCache = null;
  13. /**
  14. * @param {import('gorhill-publicsuffixlist').default | null} gorhill
  15. */
  16. module.exports.createCachedGorhillGetDomain = (gorhill) => {
  17. gothillGetDomainCache ||= createCache('cached-gorhill-get-domain', true);
  18. /**
  19. * @param {string} domain
  20. */
  21. return (domain) => gothillGetDomainCache.sync(domain, () => gorhill.getDomain(domain[0] === '.' ? domain.slice(1) : domain));
  22. };