Browse Source

Chore: bring up Undici Cache Store

SukkaW 1 year ago
parent
commit
5ed7a62a2c
3 changed files with 51 additions and 28 deletions
  1. 37 28
      Build/lib/fetch-retry.ts
  2. 1 0
      package.json
  3. 13 0
      pnpm-lock.yaml

+ 37 - 28
Build/lib/fetch-retry.ts

@@ -7,12 +7,15 @@ import undici, {
 
 
 import type {
 import type {
   Dispatcher,
   Dispatcher,
-  Response
+  Response,
+  RequestInit
 } from 'undici';
 } from 'undici';
+import { BetterSqlite3CacheStore } from 'undici-cache-store-better-sqlite3';
 
 
 export type UndiciResponseData<T = unknown> = Dispatcher.ResponseData<T>;
 export type UndiciResponseData<T = unknown> = Dispatcher.ResponseData<T>;
 
 
 import { inspect } from 'node:util';
 import { inspect } from 'node:util';
+import path from 'node:path';
 
 
 const agent = new Agent({});
 const agent = new Agent({});
 
 
@@ -99,6 +102,11 @@ setGlobalDispatcher(agent.compose(
   }),
   }),
   interceptors.redirect({
   interceptors.redirect({
     maxRedirections: 5
     maxRedirections: 5
+  }),
+  interceptors.cache({
+    store: new BetterSqlite3CacheStore({
+      location: path.resolve(__dirname, '../../.cache/undici-better-sqlite3-cache-store.db')
+    })
   })
   })
 ));
 ));
 
 
@@ -133,34 +141,35 @@ export const defaultRequestInit = {
   }
   }
 };
 };
 
 
-// export async function fetchWithLog(url: string, init?: RequestInit) {
-//   try {
-//     const res = await undici.fetch(url, init);
-//     if (res.status >= 400) {
-//       throw new ResponseError(res, url);
-//     }
-
-//     if (!(res.status >= 200 && res.status <= 299) && res.status !== 304) {
-//       throw new ResponseError(res, url);
-//     }
-
-//     return res;
-//   } catch (err: unknown) {
-//     if (typeof err === 'object' && err !== null && 'name' in err) {
-//       if ((
-//         err.name === 'AbortError'
-//         || ('digest' in err && err.digest === 'AbortError')
-//       )) {
-//         console.log(picocolors.gray('[fetch abort]'), url);
-//       }
-//     } else {
-//       console.log(picocolors.gray('[fetch fail]'), url, { name: (err as any).name }, err);
-//     }
-
-//     throw err;
-//   }
-// }
+export async function $$fetch(url: string, init?: RequestInit) {
+  try {
+    const res = await undici.fetch(url, init);
+    if (res.status >= 400) {
+      throw new ResponseError(res, url);
+    }
+
+    if (!(res.status >= 200 && res.status <= 299) && res.status !== 304) {
+      throw new ResponseError(res, url);
+    }
+
+    return res;
+  } catch (err: unknown) {
+    if (typeof err === 'object' && err !== null && 'name' in err) {
+      if ((
+        err.name === 'AbortError'
+        || ('digest' in err && err.digest === 'AbortError')
+      )) {
+        console.log(picocolors.gray('[fetch abort]'), url);
+      }
+    } else {
+      console.log(picocolors.gray('[fetch fail]'), url, { name: (err as any).name }, err);
+    }
+
+    throw err;
+  }
+}
 
 
+/** @deprecated -- undici.requests doesn't support gzip/br/deflate, and has difficulty w/ undidi cache */
 export async function requestWithLog(url: string, opt?: Parameters<typeof undici.request>[1]) {
 export async function requestWithLog(url: string, opt?: Parameters<typeof undici.request>[1]) {
   try {
   try {
     const res = await undici.request(url, opt);
     const res = await undici.request(url, opt);

+ 1 - 0
package.json

@@ -42,6 +42,7 @@
     "tldts": "^6.1.68",
     "tldts": "^6.1.68",
     "tldts-experimental": "^6.1.68",
     "tldts-experimental": "^6.1.68",
     "undici": "^7.1.1",
     "undici": "^7.1.1",
+    "undici-cache-store-better-sqlite3": "^0.1.1",
     "whoiser": "^1.18.0",
     "whoiser": "^1.18.0",
     "why-is-node-running": "^3.2.1",
     "why-is-node-running": "^3.2.1",
     "xbits": "^0.2.0",
     "xbits": "^0.2.0",

+ 13 - 0
pnpm-lock.yaml

@@ -77,6 +77,9 @@ importers:
       undici:
       undici:
         specifier: ^7.1.1
         specifier: ^7.1.1
         version: 7.1.1
         version: 7.1.1
+      undici-cache-store-better-sqlite3:
+        specifier: ^0.1.1
+        version: 0.1.1(undici@7.1.1)
       whoiser:
       whoiser:
         specifier: ^1.18.0
         specifier: ^1.18.0
         version: 1.18.0
         version: 1.18.0
@@ -1788,6 +1791,11 @@ packages:
     engines: {node: '>=14.17'}
     engines: {node: '>=14.17'}
     hasBin: true
     hasBin: true
 
 
+  undici-cache-store-better-sqlite3@0.1.1:
+    resolution: {integrity: sha512-F/PxGx+QY3oScnFQ/YGbN7AYtqKLTzps0izFi8LddDaobso3SYXyQORuPcnUd2JbqpdxmLLkvS2zSvG6VPPMpw==}
+    peerDependencies:
+      undici: '>=7.0.0'
+
   undici-types@6.20.0:
   undici-types@6.20.0:
     resolution: {integrity: sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==}
     resolution: {integrity: sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==}
 
 
@@ -3644,6 +3652,11 @@ snapshots:
 
 
   typescript@5.7.2: {}
   typescript@5.7.2: {}
 
 
+  undici-cache-store-better-sqlite3@0.1.1(undici@7.1.1):
+    dependencies:
+      better-sqlite3: 11.7.0
+      undici: 7.1.1
+
   undici-types@6.20.0: {}
   undici-types@6.20.0: {}
 
 
   undici@7.1.1: {}
   undici@7.1.1: {}