get-gorhill-publicsuffix.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const { toASCII } = require('punycode/');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const publicSuffixPath = path.resolve(__dirname, '../../node_modules/.cache/public_suffix_list_dat.txt');
  5. const getGorhillPublicSuffix = async () => {
  6. const customFetch = async (url) => {
  7. const buf = await fs.promises.readFile(url);
  8. return {
  9. arrayBuffer() { return Promise.resolve(buf.buffer); }
  10. };
  11. };
  12. const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
  13. fs.existsSync(publicSuffixPath)
  14. ? fs.promises.readFile(publicSuffixPath, 'utf-8')
  15. : fetch('https://publicsuffix.org/list/public_suffix_list.dat').then(r => {
  16. console.log('public_suffix_list.dat not found, fetch directly from remote.');
  17. return r.text();
  18. }),
  19. import('gorhill-publicsuffixlist')
  20. ]);
  21. gorhill.parse(publicSuffixListDat, toASCII);
  22. await gorhill.enableWASM({ customFetch });
  23. return gorhill;
  24. };
  25. /** @type {Promise<import('gorhill-publicsuffixlist').default> | null} */
  26. let gorhillPublicSuffixPromise = null;
  27. module.exports.getGorhillPublicSuffixPromise = () => {
  28. gorhillPublicSuffixPromise ||= getGorhillPublicSuffix();
  29. return gorhillPublicSuffixPromise;
  30. };