From b9b0dd29878b344b62995f6fe087b1718c01da43 Mon Sep 17 00:00:00 2001 From: d3or Date: Sat, 7 Dec 2024 23:12:00 -0500 Subject: [PATCH] fix: use correct ProxyAgent for global fetch --- src/test-utils.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/test-utils.ts b/src/test-utils.ts index 071e83d..630f950 100644 --- a/src/test-utils.ts +++ b/src/test-utils.ts @@ -1,4 +1,4 @@ -import { HttpsProxyAgent } from 'https-proxy-agent'; +import { ProxyAgent } from 'undici'; import { Scraper } from './scraper'; import fs from 'fs'; @@ -72,16 +72,37 @@ export async function getScraper( } if (proxyUrl) { - agent = new HttpsProxyAgent(proxyUrl, { - rejectUnauthorized: false, - }); + // Parse the proxy URL + const url = new URL(proxyUrl); + const username = url.username; + const password = url.password; + + // Strip auth from URL if present + url.username = ''; + url.password = ''; + + const agentOptions: any = { + uri: url.toString(), + requestTls: { + rejectUnauthorized: false, + }, + }; + + // Add Basic auth if credentials exist + if (username && password) { + agentOptions.token = `Basic ${Buffer.from( + `${username}:${password}`, + ).toString('base64')}`; + } + + agent = new ProxyAgent(agentOptions); } const scraper = new Scraper({ transform: { request: (input, init) => { if (agent) { - return [input, { ...init, agent }]; + return [input, { ...init, dispatcher: agent }]; } return [input, init]; },