fetch-assets.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import picocolors from 'picocolors';
  2. import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
  3. import { setTimeout } from 'node:timers/promises';
  4. class CustomAbortError extends Error {
  5. constructor() {
  6. super();
  7. this.name = 'CustomAbortError';
  8. }
  9. public readonly name = 'AbortError';
  10. public readonly digest = 'AbortError';
  11. }
  12. const sleepWithAbort = (ms: number, signal: AbortSignal) => new Promise<void>((resolve, reject) => {
  13. if (signal.aborted) {
  14. reject(signal.reason as Error);
  15. return;
  16. }
  17. function stop(this: AbortSignal) { reject(this.reason as Error); }
  18. signal.addEventListener('abort', stop, { once: true });
  19. setTimeout(ms, undefined, { ref: false }).then(resolve).catch(reject).finally(() => signal.removeEventListener('abort', stop));
  20. });
  21. export async function fetchAssets(url: string, fallbackUrls: string[] | readonly string[]) {
  22. const controller = new AbortController();
  23. const fetchMainPromise = fetchWithRetry(url, { signal: controller.signal, ...defaultRequestInit })
  24. .then(r => r.text())
  25. .then(text => {
  26. controller.abort();
  27. return text;
  28. });
  29. const createFetchFallbackPromise = async (url: string, index: number) => {
  30. // Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL.
  31. try {
  32. await sleepWithAbort(500 + (index + 1) * 20, controller.signal);
  33. } catch {
  34. console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
  35. throw new CustomAbortError();
  36. }
  37. if (controller.signal.aborted) {
  38. console.log(picocolors.gray('[fetch cancelled]'), picocolors.gray(url));
  39. throw new CustomAbortError();
  40. }
  41. const res = await fetchWithRetry(url, { signal: controller.signal, ...defaultRequestInit });
  42. const text = await res.text();
  43. controller.abort();
  44. return text;
  45. };
  46. return Promise.any([
  47. fetchMainPromise,
  48. ...fallbackUrls.map(createFetchFallbackPromise)
  49. ]).catch(e => {
  50. console.log(`Download Rule for [${url}] failed`);
  51. throw e;
  52. });
  53. }