Skip to content

Commit

Permalink
Merge pull request #17 from d3or/fix-incorrect-proxy-agent
Browse files Browse the repository at this point in the history
fix: use correct ProxyAgent
  • Loading branch information
lalalune authored Dec 9, 2024
2 parents 6210993 + f3bef5c commit 915e790
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 35 deletions.
121 changes: 92 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"gh-pages": "^5.0.0",
"https-proxy-agent": "^7.0.2",
"jest": "^29.7.0",
"lint-staged": "^13.2.2",
"prettier": "^2.8.8",
Expand Down
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 915e790

Please sign in to comment.