Chore: minor changes

This commit is contained in:
SukkaW
2025-01-17 23:06:51 +08:00
parent 6f8d515e7c
commit 6235d9fee0
8 changed files with 20 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
import picocolors from 'picocolors';
import { $$fetch, defaultRequestInit, ResponseError } from './fetch-retry';
import { wait } from 'foxts/wait';
import { waitWithAbort } from 'foxts/wait';
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
export class CustomAbortError extends Error {
@@ -26,29 +26,14 @@ export class CustomNoETagFallbackError extends Error {
}
}
export function sleepWithAbort(ms: number, signal: AbortSignal) {
return new Promise<void>((resolve, reject) => {
if (signal.aborted) {
reject(signal.reason as Error);
return;
}
signal.addEventListener('abort', stop, { once: true });
wait(ms).then(resolve).catch(reject).finally(() => signal.removeEventListener('abort', stop));
function stop(this: AbortSignal) { reject(this.reason as Error); }
});
}
export async function fetchAssetsWithout304(url: string, fallbackUrls: null | undefined | string[] | readonly string[]) {
export async function fetchAssets(url: string, fallbackUrls: null | undefined | string[] | readonly string[]) {
const controller = new AbortController();
const createFetchFallbackPromise = async (url: string, index: number) => {
if (index >= 0) {
// Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL.
try {
await sleepWithAbort(50 + (index + 1) * 100, controller.signal);
await waitWithAbort(50 + (index + 1) * 100, controller.signal);
} catch {
console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
throw new CustomAbortError();

View File

@@ -95,10 +95,9 @@ setGlobalDispatcher(agent.compose(
: retryAfter * 1e3; // Retry-After is in seconds
}
const retryTimeout
= retryAfter > 0
? Math.min(retryAfter, maxTimeout)
: Math.min(minTimeout * (timeoutFactor ** (counter - 1)), maxTimeout);
const retryTimeout = retryAfter > 0
? Math.min(retryAfter, maxTimeout)
: Math.min(minTimeout * (timeoutFactor ** (counter - 1)), maxTimeout);
console.log('[fetch retry]', 'schedule retry', { statusCode, retryTimeout, errorCode, url: opts.origin });
// eslint-disable-next-line sukka/prefer-timer-id -- won't leak

View File

@@ -2,7 +2,7 @@ import picocolors from 'picocolors';
import { fastNormalizeDomain } from '../normalize-domain';
import { processLine } from '../process-line';
import { onBlackFound } from './shared';
import { fetchAssetsWithout304 } from '../fetch-assets';
import { fetchAssets } from '../fetch-assets';
import type { Span } from '../../trace';
function domainListLineCb(l: string, set: string[], includeAllSubDomain: boolean, meta: string) {
@@ -32,7 +32,7 @@ export function processDomainLists(
domainListsUrl: string, mirrors: string[] | null, includeAllSubDomain = false
) {
return span.traceChildAsync(`process domainlist: ${domainListsUrl}`, async (span) => {
const text = await span.traceChildAsync('download', () => fetchAssetsWithout304(
const text = await span.traceChildAsync('download', () => fetchAssets(
domainListsUrl,
mirrors
));

View File

@@ -1,6 +1,6 @@
import picocolors from 'picocolors';
import type { Span } from '../../trace';
import { fetchAssetsWithout304 } from '../fetch-assets';
import { fetchAssets } from '../fetch-assets';
import { onBlackFound, onWhiteFound } from './shared';
import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
import { fastNormalizeDomain } from '../normalize-domain';
@@ -27,7 +27,7 @@ export async function processFilterRules(
allowThirdParty = false
): Promise<{ white: string[], black: string[] }> {
const [white, black, warningMessages] = await parentSpan.traceChild(`process filter rules: ${filterRulesUrl}`).traceAsyncFn(async (span) => {
const text = await span.traceChildAsync('download', () => fetchAssetsWithout304(filterRulesUrl, fallbackUrls));
const text = await span.traceChildAsync('download', () => fetchAssets(filterRulesUrl, fallbackUrls));
const whitelistDomainSets = new Set<string>();
const blacklistDomainSets = new Set<string>();

View File

@@ -1,5 +1,5 @@
import type { Span } from '../../trace';
import { fetchAssetsWithout304 } from '../fetch-assets';
import { fetchAssets } from '../fetch-assets';
import { fastNormalizeDomain } from '../normalize-domain';
import { processLine } from '../process-line';
import { onBlackFound } from './shared';
@@ -29,7 +29,7 @@ export function processHosts(
hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false
) {
return span.traceChildAsync(`process hosts: ${hostsUrl}`, async (span) => {
const text = await span.traceChild('download').traceAsyncFn(() => fetchAssetsWithout304(hostsUrl, mirrors));
const text = await span.traceChild('download').traceAsyncFn(() => fetchAssets(hostsUrl, mirrors));
const domainSets: string[] = [];