fetch-assets.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import picocolors from 'picocolors';
  2. import { $$fetch, defaultRequestInit, ResponseError } from './fetch-retry';
  3. import { waitWithAbort } from 'foxts/wait';
  4. import { nullthrow } from 'foxts/guard';
  5. import { TextLineStream } from 'foxts/text-line-stream';
  6. import { ProcessLineStream } from './process-line';
  7. import { AdGuardFilterIgnoreUnsupportedLinesStream } from './parse-filter/filters';
  8. // eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
  9. class CustomAbortError extends Error {
  10. public readonly name = 'AbortError';
  11. public readonly digest = 'AbortError';
  12. }
  13. const reusedCustomAbortError = new CustomAbortError();
  14. export async function fetchAssets(
  15. url: string, fallbackUrls: null | undefined | string[] | readonly string[],
  16. processLine = false, allowEmpty = false, filterAdGuardUnsupportedLines = false
  17. ) {
  18. const controller = new AbortController();
  19. const createFetchFallbackPromise = async (url: string, index: number) => {
  20. if (index >= 0) {
  21. // Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL.
  22. try {
  23. await waitWithAbort(50 + (index + 1) * 150, controller.signal);
  24. } catch {
  25. console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
  26. throw reusedCustomAbortError;
  27. }
  28. }
  29. if (controller.signal.aborted) {
  30. console.log(picocolors.gray('[fetch cancelled]'), picocolors.gray(url));
  31. throw reusedCustomAbortError;
  32. }
  33. const res = await $$fetch(url, { signal: controller.signal, ...defaultRequestInit });
  34. let stream = nullthrow(res.body, url + ' has an empty body').pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream({ skipEmptyLines: processLine }));
  35. if (processLine) {
  36. stream = stream.pipeThrough(new ProcessLineStream());
  37. }
  38. if (filterAdGuardUnsupportedLines) {
  39. stream = stream.pipeThrough(new AdGuardFilterIgnoreUnsupportedLinesStream());
  40. }
  41. const arr = await Array.fromAsync(stream);
  42. if (arr.length < 1 && !allowEmpty) {
  43. throw new ResponseError(res, url, 'empty response w/o 304');
  44. }
  45. controller.abort();
  46. return arr;
  47. };
  48. if (!fallbackUrls || fallbackUrls.length === 0) {
  49. return createFetchFallbackPromise(url, -1);
  50. }
  51. return Promise.any([
  52. createFetchFallbackPromise(url, -1),
  53. ...fallbackUrls.map(createFetchFallbackPromise)
  54. ]);
  55. }