fetch-assets.ts 2.1 KB

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