diff --git a/Build/lib/fetch-retry.ts b/Build/lib/fetch-retry.ts index 6863c81a..bcd233b0 100644 --- a/Build/lib/fetch-retry.ts +++ b/Build/lib/fetch-retry.ts @@ -7,12 +7,15 @@ import undici, { import type { Dispatcher, - Response + Response, + RequestInit } from 'undici'; +import { BetterSqlite3CacheStore } from 'undici-cache-store-better-sqlite3'; export type UndiciResponseData = Dispatcher.ResponseData; import { inspect } from 'node:util'; +import path from 'node:path'; const agent = new Agent({}); @@ -99,6 +102,11 @@ setGlobalDispatcher(agent.compose( }), interceptors.redirect({ 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); -// } +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); -// } + 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); -// } + 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; -// } -// } + 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[1]) { try { const res = await undici.request(url, opt); diff --git a/package.json b/package.json index 58bf35d6..0cba06c8 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "tldts": "^6.1.68", "tldts-experimental": "^6.1.68", "undici": "^7.1.1", + "undici-cache-store-better-sqlite3": "^0.1.1", "whoiser": "^1.18.0", "why-is-node-running": "^3.2.1", "xbits": "^0.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c0c8b2b5..ab540b2a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -77,6 +77,9 @@ importers: undici: specifier: ^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: specifier: ^1.18.0 version: 1.18.0 @@ -1788,6 +1791,11 @@ packages: engines: {node: '>=14.17'} 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: resolution: {integrity: sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==} @@ -3644,6 +3652,11 @@ snapshots: 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@7.1.1: {}