Skip to content

Commit

Permalink
fix: use correct ProxyAgent for global fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
d3or committed Dec 8, 2024
1 parent eb2ab3e commit b9b0dd2
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpsProxyAgent } from 'https-proxy-agent';
import { ProxyAgent } from 'undici';
import { Scraper } from './scraper';
import fs from 'fs';

Expand Down Expand Up @@ -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];
},
Expand Down

0 comments on commit b9b0dd2

Please sign in to comment.