make-fetch-happen.ts 1019 B

123456789101112131415161718192021222324252627282930313233
  1. import path from 'node:path';
  2. import fs from 'node:fs';
  3. import makeFetchHappen from 'make-fetch-happen';
  4. import picocolors from 'picocolors';
  5. // eslint-disable-next-line @typescript-eslint/no-restricted-imports -- type only
  6. import type { Response as NodeFetchResponse } from 'node-fetch';
  7. export type { NodeFetchResponse };
  8. const cachePath = path.resolve(__dirname, '../../.cache/__make_fetch_happen__');
  9. fs.mkdirSync(cachePath, { recursive: true });
  10. export const $fetch = makeFetchHappen.defaults({
  11. cachePath,
  12. maxSockets: 32, /**
  13. * They said 15 is a good default that prevents knocking out others' routers,
  14. * I disagree. 32 is a good number.
  15. */
  16. headers: {
  17. 'User-Agent': 'curl/8.9.1 (https://github.com/SukkaW/Surge)'
  18. },
  19. retry: {
  20. retries: 5,
  21. randomize: true
  22. }
  23. });
  24. export function printResponseStatus(resp: NodeFetchResponse) {
  25. const status = resp.headers.get('X-Local-Cache-Status');
  26. if (status) {
  27. console.log('[$fetch cache]', { status }, picocolors.gray(resp.url));
  28. }
  29. }