|
@@ -1,6 +1,8 @@
|
|
|
import path from 'node:path';
|
|
import path from 'node:path';
|
|
|
import fs from 'node:fs';
|
|
import fs from 'node:fs';
|
|
|
import makeFetchHappen from 'make-fetch-happen';
|
|
import makeFetchHappen from 'make-fetch-happen';
|
|
|
|
|
+import type { FetchOptions } from 'make-fetch-happen';
|
|
|
|
|
+import cacache from 'cacache';
|
|
|
import picocolors from 'picocolors';
|
|
import picocolors from 'picocolors';
|
|
|
// eslint-disable-next-line @typescript-eslint/no-restricted-imports -- type only
|
|
// eslint-disable-next-line @typescript-eslint/no-restricted-imports -- type only
|
|
|
import type { Response as NodeFetchResponse } from 'node-fetch';
|
|
import type { Response as NodeFetchResponse } from 'node-fetch';
|
|
@@ -10,7 +12,7 @@ export type { NodeFetchResponse };
|
|
|
const cachePath = path.resolve(__dirname, '../../.cache/__make_fetch_happen__');
|
|
const cachePath = path.resolve(__dirname, '../../.cache/__make_fetch_happen__');
|
|
|
fs.mkdirSync(cachePath, { recursive: true });
|
|
fs.mkdirSync(cachePath, { recursive: true });
|
|
|
|
|
|
|
|
-export const $fetch = makeFetchHappen.defaults({
|
|
|
|
|
|
|
+const _fetch = makeFetchHappen.defaults({
|
|
|
cachePath,
|
|
cachePath,
|
|
|
maxSockets: 32, /**
|
|
maxSockets: 32, /**
|
|
|
* They said 15 is a good default that prevents knocking out others' routers,
|
|
* They said 15 is a good default that prevents knocking out others' routers,
|
|
@@ -25,6 +27,21 @@ export const $fetch = makeFetchHappen.defaults({
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+export function $fetch(uriOrRequest: string | Request, opts?: FetchOptions) {
|
|
|
|
|
+ return _fetch(uriOrRequest, opts).then((resp) => {
|
|
|
|
|
+ printResponseStatus(resp);
|
|
|
|
|
+ return resp;
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export async function $delete(resp: NodeFetchResponse) {
|
|
|
|
|
+ const cacheKey = resp.headers.get('X-Local-Cache-Key');
|
|
|
|
|
+ if (cacheKey) {
|
|
|
|
|
+ await cacache.rm.entry(cachePath, cacheKey);
|
|
|
|
|
+ await cacache.verify(cachePath, { concurrency: 64 });
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export function printResponseStatus(resp: NodeFetchResponse) {
|
|
export function printResponseStatus(resp: NodeFetchResponse) {
|
|
|
const status = resp.headers.get('X-Local-Cache-Status');
|
|
const status = resp.headers.get('X-Local-Cache-Status');
|
|
|
if (status) {
|
|
if (status) {
|