fetch-assets.ts 2.8 KB

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