get-gorhill-publicsuffix.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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 getPublicSuffixListDat = () => {
  6. if (fs.existsSync(publicSuffixPath)) {
  7. return fs.promises.readFile(publicSuffixPath, 'utf-8');
  8. }
  9. console.log('public_suffix_list.dat not found, fetch directly from remote.');
  10. return fetch('https://publicsuffix.org/list/public_suffix_list.dat').then(r => r.text());
  11. };
  12. const getGorhillPublicSuffix = async () => {
  13. const customFetch = async (url) => {
  14. const buf = await fs.promises.readFile(url);
  15. return {
  16. arrayBuffer() { return Promise.resolve(buf.buffer); }
  17. };
  18. };
  19. const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
  20. getPublicSuffixListDat(),
  21. import('gorhill-publicsuffixlist')
  22. ]);
  23. gorhill.parse(publicSuffixListDat, toASCII);
  24. await gorhill.enableWASM({ customFetch });
  25. return gorhill;
  26. };
  27. const getGorhillPublicSuffixPromise = getGorhillPublicSuffix();
  28. module.exports.getGorhillPublicSuffixPromise = getGorhillPublicSuffixPromise;