fix: use undici's own fetch for proxy dispatcher support
Some checks failed
CI / Lint, Typecheck, Test & Build (push) Has been cancelled

Node's global fetch doesn't support the dispatcher option. Use
undici's exported fetch when proxy is configured, which properly
handles ProxyAgent as a dispatcher.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 12:13:09 +00:00
parent ae64e60bf0
commit 0fce6a2266

View File

@@ -18,7 +18,7 @@ import {
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as path from 'path';
import { ProxyAgent } from 'undici';
import { ProxyAgent, fetch as undiciFetch } from 'undici';
import {
EmexScraperResponse,
@@ -242,15 +242,19 @@ export class EmexService {
* without requiring authentication cookies.
*/
private async fetchEmexHtml(url: string): Promise<string> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const options: any = {
headers: { 'User-Agent': EMEX_UA, 'Accept': 'text/html,application/xhtml+xml' },
signal: AbortSignal.timeout(this.timeout),
};
const headers = { 'User-Agent': EMEX_UA, 'Accept': 'text/html,application/xhtml+xml' };
const signal = AbortSignal.timeout(this.timeout);
let res: Response;
if (this.proxyUrl) {
options.dispatcher = new ProxyAgent(this.proxyUrl);
// Use undici's fetch which supports the dispatcher option for proxy
res = await undiciFetch(url, {
headers,
signal,
dispatcher: new ProxyAgent(this.proxyUrl),
}) as unknown as Response;
} else {
res = await fetch(url, { headers, signal });
}
const res = await fetch(url, options);
if (!res.ok) {
throw new Error(`EMEX HTTP ${res.status} for ${url}`);
}