Skip to content

Commit

Permalink
Chore: bring up Undici Cache Store
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 17, 2024
1 parent 18d55e5 commit 5ed7a62
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
65 changes: 37 additions & 28 deletions Build/lib/fetch-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({});

Expand Down Expand Up @@ -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')
})
})
));

Expand Down Expand Up @@ -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]) {
try {
const res = await undici.request(url, opt);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ed7a62

Please sign in to comment.