Browse Source

Log `make-fetch-happen` status / Implement Evict

SukkaW 1 year ago
parent
commit
6a984161a4
4 changed files with 35 additions and 3 deletions
  1. 2 2
      Build/download-previous-build.ts
  2. 18 1
      Build/lib/make-fetch-happen.ts
  3. 2 0
      package.json
  4. 13 0
      pnpm-lock.yaml

+ 2 - 2
Build/download-previous-build.ts

@@ -37,8 +37,8 @@ export const downloadPreviousBuild = task(require.main === module, __filename)(a
         // https://github.com/unjs/giget/issues/97
         // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
         'sec-fetch-mode': 'same-origin'
-      }
-      // mode: 'same-origin'
+      },
+      mode: 'same-origin'
     });
 
     if (resp.status !== 200) {

+ 18 - 1
Build/lib/make-fetch-happen.ts

@@ -1,6 +1,8 @@
 import path from 'node:path';
 import fs from 'node:fs';
 import makeFetchHappen from 'make-fetch-happen';
+import type { FetchOptions } from 'make-fetch-happen';
+import cacache from 'cacache';
 import picocolors from 'picocolors';
 // eslint-disable-next-line @typescript-eslint/no-restricted-imports -- type only
 import type { Response as NodeFetchResponse } from 'node-fetch';
@@ -10,7 +12,7 @@ export type { NodeFetchResponse };
 const cachePath = path.resolve(__dirname, '../../.cache/__make_fetch_happen__');
 fs.mkdirSync(cachePath, { recursive: true });
 
-export const $fetch = makeFetchHappen.defaults({
+const _fetch = makeFetchHappen.defaults({
   cachePath,
   maxSockets: 32, /**
    * 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) {
   const status = resp.headers.get('X-Local-Cache-Status');
   if (status) {

+ 2 - 0
package.json

@@ -25,6 +25,7 @@
     "async-retry": "^1.3.3",
     "async-sema": "^3.1.1",
     "better-sqlite3": "^11.3.0",
+    "cacache": "^19.0.1",
     "cacheable-lookup": "^6.1.0",
     "ci-info": "^4.0.0",
     "cli-table3": "^0.6.5",
@@ -51,6 +52,7 @@
     "@swc/core": "^1.7.35",
     "@types/async-retry": "^1.4.9",
     "@types/better-sqlite3": "^7.6.11",
+    "@types/cacache": "^17.0.2",
     "@types/chai": "^4.3.20",
     "@types/make-fetch-happen": "^10.0.4",
     "@types/mocha": "^10.0.9",

+ 13 - 0
pnpm-lock.yaml

@@ -26,6 +26,9 @@ importers:
       better-sqlite3:
         specifier: ^11.3.0
         version: 11.3.0
+      cacache:
+        specifier: ^19.0.1
+        version: 19.0.1
       cacheable-lookup:
         specifier: ^6.1.0
         version: 6.1.0
@@ -99,6 +102,9 @@ importers:
       '@types/better-sqlite3':
         specifier: ^7.6.11
         version: 7.6.11
+      '@types/cacache':
+        specifier: ^17.0.2
+        version: 17.0.2
       '@types/chai':
         specifier: ^4.3.20
         version: 4.3.20
@@ -472,6 +478,9 @@ packages:
   '@types/better-sqlite3@7.6.11':
     resolution: {integrity: sha512-i8KcD3PgGtGBLl3+mMYA8PdKkButvPyARxA7IQAd6qeslht13qxb1zzO8dRCtE7U3IoJS782zDBAeoKiM695kg==}
 
+  '@types/cacache@17.0.2':
+    resolution: {integrity: sha512-IrqHzVX2VRMDQQKa7CtKRnuoCLdRJiLW6hWU+w7i7+AaQ0Ii5bKwJxd5uRK4zBCyrHd3tG6G8zOm2LplxbSfQg==}
+
   '@types/chai@4.3.20':
     resolution: {integrity: sha512-/pC9HAB5I/xMlc5FP77qjCnI16ChlJfW0tGa0IUcFn38VJrTV6DeZ60NU5KZBtaOZqjdpwTWohz5HU1RrhiYxQ==}
 
@@ -2194,6 +2203,10 @@ snapshots:
     dependencies:
       '@types/node': 22.7.5
 
+  '@types/cacache@17.0.2':
+    dependencies:
+      '@types/node': 22.7.5
+
   '@types/chai@4.3.20': {}
 
   '@types/chrome@0.0.270':