get-gorhill-publicsuffix.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { toASCII } from 'punycode';
  2. import path from 'path';
  3. import { traceAsync } from './trace-runner';
  4. import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
  5. import type { PublicSuffixList } from 'gorhill-publicsuffixlist';
  6. const publicSuffixPath = path.resolve(import.meta.dir, '../../node_modules/.cache/public_suffix_list_dat.txt');
  7. const getGorhillPublicSuffix = () => traceAsync('create gorhill public suffix instance', async () => {
  8. const customFetch = (url: string | URL) => Promise.resolve(Bun.file(url));
  9. const publicSuffixFile = Bun.file(publicSuffixPath);
  10. const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
  11. await publicSuffixFile.exists()
  12. ? publicSuffixFile.text()
  13. : fetchWithRetry('https://publicsuffix.org/list/public_suffix_list.dat', defaultRequestInit).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. let gorhillPublicSuffixPromise: Promise<PublicSuffixList> | null = null;
  24. export const getGorhillPublicSuffixPromise = () => {
  25. gorhillPublicSuffixPromise ||= getGorhillPublicSuffix();
  26. return gorhillPublicSuffixPromise;
  27. };