get-gorhill-publicsuffix.ts 873 B

12345678910111213141516171819202122232425
  1. import fsp from 'fs/promises';
  2. import { toASCII } from 'punycode/punycode';
  3. import { createMemoizedPromise } from './memo-promise';
  4. import { getPublicSuffixListTextPromise } from './download-publicsuffixlist';
  5. import { fileURLToPath } from 'url';
  6. const customFetch = typeof Bun !== 'undefined'
  7. ? (url: string | URL) => Promise.resolve(Bun.file(url))
  8. : async (url: string | URL) => {
  9. const filePath = fileURLToPath(url);
  10. const file = await fsp.readFile(filePath);
  11. return new Blob([file]) as any;
  12. };
  13. export const getGorhillPublicSuffixPromise = createMemoizedPromise(async () => {
  14. const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
  15. getPublicSuffixListTextPromise(),
  16. import('@gorhill/publicsuffixlist')
  17. ]);
  18. gorhill.parse(publicSuffixListDat, toASCII);
  19. await gorhill.enableWASM({ customFetch });
  20. return gorhill;
  21. });