fetch-assets.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. export class Custom304NotModifiedError extends Error {
  13. public readonly name = 'Custom304NotModifiedError';
  14. public readonly digest = 'Custom304NotModifiedError';
  15. constructor(public readonly url: string, public readonly data: string) {
  16. super('304 Not Modified');
  17. }
  18. }
  19. export class CustomNoETagFallbackError extends Error {
  20. public readonly name = 'CustomNoETagFallbackError';
  21. public readonly digest = 'CustomNoETagFallbackError';
  22. constructor(public readonly data: string) {
  23. super('No ETag Fallback');
  24. }
  25. }
  26. export async function fetchAssets(url: string, fallbackUrls: null | undefined | string[] | readonly string[], processLine = false) {
  27. const controller = new AbortController();
  28. const createFetchFallbackPromise = async (url: string, index: number) => {
  29. if (index >= 0) {
  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 waitWithAbort(50 + (index + 1) * 100, controller.signal);
  33. } catch {
  34. console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
  35. throw new CustomAbortError();
  36. }
  37. }
  38. if (controller.signal.aborted) {
  39. console.log(picocolors.gray('[fetch cancelled]'), picocolors.gray(url));
  40. throw new CustomAbortError();
  41. }
  42. const res = await $$fetch(url, { signal: controller.signal, ...defaultRequestInit });
  43. let stream = nullthrow(res.body).pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream());
  44. if (processLine) {
  45. stream = stream.pipeThrough(new ProcessLineStream());
  46. }
  47. const arr = await Array.fromAsync(stream);
  48. if (arr.length < 1) {
  49. throw new ResponseError(res, url, 'empty response w/o 304');
  50. }
  51. controller.abort();
  52. return arr;
  53. };
  54. if (!fallbackUrls || fallbackUrls.length === 0) {
  55. return createFetchFallbackPromise(url, -1);
  56. }
  57. return Promise.any([
  58. createFetchFallbackPromise(url, -1),
  59. ...fallbackUrls.map(createFetchFallbackPromise)
  60. ]);
  61. }