get-gorhill-publicsuffix.ts 832 B

123456789101112131415161718192021222324
  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. // TODO: node undfici fetch doesn't support file URL reading yet
  7. const customFetch = async (url: URL) => {
  8. const filePath = fileURLToPath(url);
  9. const file = await fsp.readFile(filePath);
  10. return new Blob([file]) as any;
  11. };
  12. export const getGorhillPublicSuffixPromise = createMemoizedPromise(async () => {
  13. const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
  14. getPublicSuffixListTextPromise(),
  15. import('@gorhill/publicsuffixlist')
  16. ]);
  17. gorhill.parse(publicSuffixListDat, toASCII);
  18. await gorhill.enableWASM({ customFetch });
  19. return gorhill;
  20. });