fetch-assets.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. import { appendArrayInPlace } from 'foxts/append-array-in-place';
  9. // eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
  10. class CustomAbortError extends Error {
  11. public readonly name = 'AbortError';
  12. public readonly digest = 'AbortError';
  13. }
  14. const reusedCustomAbortError = new CustomAbortError();
  15. export async function fetchAssets(
  16. url: string, fallbackUrls: null | undefined | string[] | readonly string[],
  17. processLine = false, allowEmpty = false, filterAdGuardUnsupportedLines = false
  18. ) {
  19. const controller = new AbortController();
  20. const createFetchFallbackPromise = async (url: string, index: number) => {
  21. if (index >= 0) {
  22. // To avoid wasting bandwidth, we will wait for a few time before downloading from the fallback URL.
  23. try {
  24. await waitWithAbort(200 + (index + 1) * 400, controller.signal);
  25. } catch {
  26. console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
  27. throw reusedCustomAbortError;
  28. }
  29. }
  30. if (controller.signal.aborted) {
  31. console.log(picocolors.gray('[fetch cancelled]'), picocolors.gray(url));
  32. throw reusedCustomAbortError;
  33. }
  34. const res = await $$fetch(url, { signal: controller.signal, ...defaultRequestInit });
  35. let stream = nullthrow(res.body, url + ' has an empty body')
  36. .pipeThrough(new TextDecoderStream())
  37. .pipeThrough(new TextLineStream({ skipEmptyLines: processLine }));
  38. if (processLine) {
  39. stream = stream.pipeThrough(new ProcessLineStream());
  40. }
  41. if (filterAdGuardUnsupportedLines) {
  42. stream = stream.pipeThrough(new AdGuardFilterIgnoreUnsupportedLinesStream());
  43. }
  44. const arr = await Array.fromAsync(stream);
  45. if (arr.length < 1 && !allowEmpty) {
  46. throw new ResponseError(res, url, 'empty response w/o 304');
  47. }
  48. controller.abort();
  49. return arr;
  50. };
  51. const primaryPromise = createFetchFallbackPromise(url, -1);
  52. if (!fallbackUrls || fallbackUrls.length === 0) {
  53. return primaryPromise;
  54. }
  55. return Promise.any(
  56. appendArrayInPlace(
  57. [primaryPromise],
  58. fallbackUrls.map(createFetchFallbackPromise)
  59. )
  60. );
  61. }