fetch-assets.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import { AbortError } from 'foxts/abort-error';
  11. const reusedCustomAbortError = new AbortError();
  12. const queue = newQueue(16);
  13. export async function fetchAssets(
  14. url: string, fallbackUrls: null | undefined | string[] | readonly string[],
  15. processLine = false, allowEmpty = false, filterAdGuardUnsupportedLines = false
  16. ) {
  17. const controller = new AbortController();
  18. const createFetchFallbackPromise = async (url: string, index: number) => {
  19. if (index >= 0) {
  20. // To avoid wasting bandwidth, we will wait for a few time before downloading from the fallback URL.
  21. try {
  22. await waitWithAbort(1800 + (index + 1) * 1200, controller.signal);
  23. } catch {
  24. throw reusedCustomAbortError;
  25. }
  26. }
  27. if (controller.signal.aborted) {
  28. throw reusedCustomAbortError;
  29. }
  30. if (index >= 0) {
  31. console.log(picocolors.yellowBright('[fetch fallback begin]'), picocolors.gray(url));
  32. }
  33. // we don't queue add here
  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. // we does queue during downloading
  45. const arr = await queue.add(() => Array.fromAsync(stream));
  46. if (arr.length < 1 && !allowEmpty) {
  47. throw new ResponseError(res, url, 'empty response w/o 304');
  48. }
  49. controller.abort();
  50. return arr;
  51. };
  52. const primaryPromise = createFetchFallbackPromise(url, -1);
  53. if (!fallbackUrls || fallbackUrls.length === 0) {
  54. return primaryPromise;
  55. }
  56. return Promise.any(
  57. appendArrayInPlace(
  58. [primaryPromise],
  59. fallbackUrls.map(createFetchFallbackPromise)
  60. )
  61. );
  62. }