Chore: bring up Undici Cache Store

This commit is contained in:
SukkaW 2024-12-18 00:47:43 +08:00
parent 18d55e5b39
commit 5ed7a62a2c
3 changed files with 48 additions and 25 deletions

View File

@ -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<T = unknown> = Dispatcher.ResponseData<T>;
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<typeof undici.request>[1]) {
try {
const res = await undici.request(url, opt);

View File

@ -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",

13
pnpm-lock.yaml generated
View File

@ -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: {}