get-gorhill-publicsuffix.js 1.2 KB

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