get-gorhill-publicsuffix.ts 1.2 KB

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