Chore: use more make-fetch-happen

This commit is contained in:
SukkaW
2024-10-21 02:09:25 +08:00
parent ed85377503
commit db7a4bc97a
6 changed files with 57 additions and 94 deletions

View File

@@ -278,10 +278,6 @@ export class Cache<S = string> {
return fn(await fetchAssetsWithout304(primaryUrl, mirrorUrls));
}
if (mirrorUrls.length === 0) {
return this.applyWithHttp304(primaryUrl, extraCacheKey, async (resp) => fn(await resp.body.text()), opt);
}
const baseKey = primaryUrl + '$' + extraCacheKey;
const getETagKey = (url: string) => baseKey + '$' + url + '$etag';
const cachedKey = baseKey + '$cached';
@@ -346,10 +342,12 @@ export class Cache<S = string> {
};
try {
const text = await Promise.any([
createFetchFallbackPromise(primaryUrl, -1),
...mirrorUrls.map(createFetchFallbackPromise)
]);
const text = mirrorUrls.length === 0
? await createFetchFallbackPromise(primaryUrl, -1)
: await Promise.any([
createFetchFallbackPromise(primaryUrl, -1),
...mirrorUrls.map(createFetchFallbackPromise)
]);
console.log(picocolors.yellow('[cache] miss'), primaryUrl);
const serializer = 'serializer' in opt ? opt.serializer : identity as any;

View File

@@ -1,15 +0,0 @@
import { deserializeArray, fsFetchCache, getFileContentHash, serializeArray } from './cache-filesystem';
import { createMemoizedPromise } from './memo-promise';
export const getPublicSuffixListTextPromise = createMemoizedPromise(() => fsFetchCache.applyWithHttp304<string[]>(
'https://publicsuffix.org/list/public_suffix_list.dat',
getFileContentHash(__filename),
(r) => r.body.text().then(text => text.split('\n')),
{
// https://github.com/publicsuffix/list/blob/master/.github/workflows/tld-update.yml
// Though the action runs every 24 hours, the IANA list is updated every 7 days.
// So a 3 day TTL should be enough.
serializer: serializeArray,
deserializer: deserializeArray
}
));

View File

@@ -7,7 +7,6 @@ import undici, {
import type {
Dispatcher,
RequestInit,
Response
} from 'undici';
@@ -143,33 +142,33 @@ 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 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);
}
// 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;
// }
// }
export async function requestWithLog(url: string, opt?: Parameters<typeof undici.request>[1]) {
try {