make-fetch-happen.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import path from 'node:path';
  2. import fsp from 'node:fs/promises';
  3. // import makeFetchHappen from 'make-fetch-happen';
  4. // import type { FetchOptions } from 'make-fetch-happen';
  5. // import cacache from 'cacache';
  6. // import picocolors from 'picocolors';
  7. import { task } from '../trace';
  8. import { ROOT_DIR } from '../constants/dir';
  9. // import { bytes } from 'xbits';
  10. const cachePath = path.join(ROOT_DIR, '.cache/__make_fetch_happen__');
  11. // fs.mkdirSync(cachePath, { recursive: true });
  12. // interface CacacheVerifyStats {
  13. // startTime: Date,
  14. // endTime: Date,
  15. // runTime: {
  16. // markStartTime: 0,
  17. // fixPerms: number,
  18. // garbageCollect: number,
  19. // rebuildIndex: number,
  20. // cleanTmp: number,
  21. // writeVerifile: number,
  22. // markEndTime: number,
  23. // total: number
  24. // },
  25. // verifiedContent: number,
  26. // reclaimedCount: number,
  27. // reclaimedSize: number,
  28. // badContentCount: number,
  29. // keptSize: number,
  30. // missingContent: number,
  31. // rejectedEntries: number,
  32. // totalEntries: number
  33. // }
  34. export const cacheGc = task(require.main === module, __filename)(
  35. () => fsp.rm(cachePath, { recursive: true, force: true })
  36. // span
  37. // .traceChildAsync('cacache gc', () => cacache.verify(cachePath, { concurrency: 64 }))
  38. // .then((stats: CacacheVerifyStats) => {
  39. // // console.log({ stats });
  40. // console.log(picocolors.green('[cacheGc] running gc on cache:'), cachePath);
  41. // console.log(picocolors.green('[cacheGc] content verified:'), stats.verifiedContent, '(' + bytes(stats.keptSize) + ')');
  42. // console.log(picocolors.green('[cacheGc] reclaimed:'), stats.reclaimedCount, '(' + bytes(stats.reclaimedSize) + ')');
  43. // });
  44. );
  45. // const _fetch = makeFetchHappen.defaults({
  46. // cachePath,
  47. // maxSockets: 32, /**
  48. // * They said 15 is a good default that prevents knocking out others' routers,
  49. // * I disagree. 32 is a good number.
  50. // */
  51. // headers: {
  52. // 'User-Agent': 'curl/8.9.1 (https://github.com/SukkaW/Surge)'
  53. // },
  54. // retry: {
  55. // retries: 5,
  56. // randomize: true
  57. // }
  58. // });
  59. // export function $fetch(uriOrRequest: string | Request, opts?: FetchOptions) {
  60. // return _fetch(uriOrRequest, opts).then((resp) => {
  61. // printResponseStatus(resp);
  62. // return resp;
  63. // });
  64. // }
  65. // export async function $delete(resp: NodeFetchResponse) {
  66. // const cacheKey = resp.headers.get('X-Local-Cache-Key');
  67. // if (cacheKey) {
  68. // await cacache.rm.entry(cachePath, cacheKey);
  69. // await cacache.verify(cachePath, { concurrency: 64 });
  70. // }
  71. // }
  72. // export function printResponseStatus(resp: NodeFetchResponse) {
  73. // const status = resp.headers.get('X-Local-Cache-Status');
  74. // if (status) {
  75. // console.log('[$fetch cache]', { status }, picocolors.gray(resp.url));
  76. // }
  77. // }
  78. // export { type Response as NodeFetchResponse } from 'node-fetch';