From 4ca87f0ff1789755e412f6bb3151a2c2d76d5121 Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Sun, 14 Sep 2025 13:37:08 +0000 Subject: [PATCH] revert: Revert "chore(fetcher): log proxy usage, allow to disable proxy via config" This reverts commit b67e1b85d634be413e004625b13403acc445ce72. --- src/utils/Fetcher.ts | 10 +++++----- src/utils/StreamResolver.ts | 8 ++++---- src/utils/dispatcher.ts | 22 +++++++++++++--------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/utils/Fetcher.ts b/src/utils/Fetcher.ts index 77be3a1..eb5e9c0 100644 --- a/src/utils/Fetcher.ts +++ b/src/utils/Fetcher.ts @@ -8,7 +8,7 @@ import winston from 'winston'; import { BlockedError, HttpError, NotFoundError, QueueIsFullError, TimeoutError, TooManyRequestsError, TooManyTimeoutsError } from '../error'; import { BlockedReason, Context } from '../types'; import { noCache } from './config'; -import { createProxyAgent, getProxyForUrl } from './dispatcher'; +import { createDispatcher } from './dispatcher'; import { envGet } from './env'; interface HttpCacheItem { @@ -254,9 +254,7 @@ export class Fetcher { }; protected async fetchWithTimeout(ctx: Context, url: URL, init?: CustomRequestInit, tryCount = 0): Promise { - const proxyUrl = getProxyForUrl(ctx, url); - - this.logger.info(`Fetch ${init?.method ?? 'GET'} ${url} via proxy ${proxyUrl}`, ctx); + this.logger.info(`Fetch ${init?.method ?? 'GET'} ${url}`, ctx); const isRateLimitedRaw = await this.rateLimitedCache.getRaw(url.host); /* istanbul ignore if */ @@ -284,11 +282,13 @@ export class Fetcher { finalUrl.username = ''; finalUrl.password = ''; + const dispatcher = createDispatcher(ctx, url); + const finalInit = { ...init, keepalive: true, signal: AbortSignal.timeout(init?.timeout ?? this.DEFAULT_TIMEOUT), - ...(/* istanbul ignore next */ proxyUrl && { dispatcher: createProxyAgent(proxyUrl) }), + ...(/* istanbul ignore next */ dispatcher && { dispatcher }), }; response = await fetch(finalUrl, finalInit); diff --git a/src/utils/StreamResolver.ts b/src/utils/StreamResolver.ts index 5b78976..1e4ac48 100644 --- a/src/utils/StreamResolver.ts +++ b/src/utils/StreamResolver.ts @@ -39,7 +39,7 @@ export class StreamResolver { const streams: Stream[] = []; - let sourceErrorCount = 0; + let sourceErrorOccurred = false; const urlResults: UrlResult[] = []; const sourcePromises = sources.map(async (source) => { if (!source.contentTypes.includes(type)) { @@ -55,7 +55,7 @@ export class StreamResolver { urlResults.push(...sourceUrlResults.flat()); } catch (error) { - sourceErrorCount++; + sourceErrorOccurred = true; if (showErrors(ctx.config)) { streams.push({ @@ -86,7 +86,7 @@ export class StreamResolver { return a.label.localeCompare(b.label); }); - const errorCount = urlResults.reduce((count, urlResult) => urlResult.error ? count + 1 : count, sourceErrorCount); + const errorCount = urlResults.reduce((count, urlResult) => urlResult.error ? count + 1 : count, 0); this.logger.info(`Got ${urlResults.length} url results, including ${errorCount} errors`, ctx); streams.push( @@ -107,7 +107,7 @@ export class StreamResolver { })), ); - const ttl = sourceErrorCount === 0 ? this.determineTtl(urlResults) : undefined; + const ttl = !sourceErrorOccurred ? this.determineTtl(urlResults) : undefined; return { streams, diff --git a/src/utils/dispatcher.ts b/src/utils/dispatcher.ts index 8e05c77..e15b340 100644 --- a/src/utils/dispatcher.ts +++ b/src/utils/dispatcher.ts @@ -3,7 +3,15 @@ import { minimatch } from 'minimatch'; import { Dispatcher, ProxyAgent } from 'undici'; import { Context } from '../types'; -export const getProxyForUrl = (ctx: Context, url: URL): URL | undefined => { +const createProxyAgent = (proxyUrl: URL): Dispatcher => { + if (proxyUrl.protocol === 'socks5:') { + return socksDispatcher({ type: 5, host: proxyUrl.hostname, port: parseInt(proxyUrl.port) }); + } + + return new ProxyAgent({ uri: proxyUrl.href }); +}; + +const createBasicDispatcher = (ctx: Context, url: URL): Dispatcher | undefined => { const proxyConfig = ctx.config['proxyConfig'] || process.env['PROXY_CONFIG']; if (proxyConfig) { @@ -14,20 +22,16 @@ export const getProxyForUrl = (ctx: Context, url: URL): URL | undefined => { } if (hostPattern === '*' || minimatch(url.host, hostPattern)) { - return proxy === 'false' ? undefined : new URL(proxy); + return createProxyAgent(new URL(proxy)); } } } else if (process.env['ALL_PROXY']) { - return new URL(process.env['ALL_PROXY']); + return createProxyAgent(new URL(process.env['ALL_PROXY'])); } return undefined; }; -export const createProxyAgent = (proxyUrl: URL): Dispatcher => { - if (proxyUrl.protocol === 'socks5:') { - return socksDispatcher({ type: 5, host: proxyUrl.hostname, port: parseInt(proxyUrl.port) }); - } - - return new ProxyAgent({ uri: proxyUrl.href }); +export const createDispatcher = (ctx: Context, url: URL): Dispatcher | undefined => { + return createBasicDispatcher(ctx, url); };