fetch-assets.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. if (index >= 0) {
  35. console.log(picocolors.yellowBright('[fetch fallback begin]'), picocolors.gray(url));
  36. }
  37. const res = await $$fetch(url, { signal: controller.signal, ...defaultRequestInit });
  38. let stream = nullthrow(res.body, url + ' has an empty body')
  39. .pipeThrough(new TextDecoderStream())
  40. .pipeThrough(new TextLineStream({ skipEmptyLines: processLine }));
  41. if (processLine) {
  42. stream = stream.pipeThrough(new ProcessLineStream());
  43. }
  44. if (filterAdGuardUnsupportedLines) {
  45. stream = stream.pipeThrough(new AdGuardFilterIgnoreUnsupportedLinesStream());
  46. }
  47. const arr = await Array.fromAsync(stream);
  48. if (arr.length < 1 && !allowEmpty) {
  49. throw new ResponseError(res, url, 'empty response w/o 304');
  50. }
  51. controller.abort();
  52. return arr;
  53. };
  54. const primaryPromise = createFetchFallbackPromise(url, -1);
  55. if (!fallbackUrls || fallbackUrls.length === 0) {
  56. return primaryPromise;
  57. }
  58. return Promise.any(
  59. appendArrayInPlace(
  60. [primaryPromise],
  61. fallbackUrls.map(createFetchFallbackPromise)
  62. )
  63. );
  64. }