Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass req to getProxyForUrl callback #326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-carpets-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'proxy-agent': minor
---

Include ClientRequest in getProxyForUrl parameters for additional flexibility
8 changes: 4 additions & 4 deletions packages/proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ValidProtocol = (typeof PROTOCOLS)[number];

type AgentConstructor = new (...args: never[]) => Agent;

type GetProxyForUrlCallback = (url: string) => string | Promise<string>;
type GetProxyForUrlCallback = (url: string, req: http.ClientRequest) => string | Promise<string>;

/**
* Supported proxy types.
Expand Down Expand Up @@ -69,7 +69,7 @@ export type ProxyAgentOptions = HttpProxyAgentOptions<''> &
* Defaults to standard proxy environment variables,
* see https://www.npmjs.com/package/proxy-from-env for details
*/
getProxyForUrl?: GetProxyForUrlCallback;
getProxyForUrl: GetProxyForUrlCallback;
};

/**
Expand All @@ -90,7 +90,7 @@ export class ProxyAgent extends Agent {
httpsAgent: http.Agent;
getProxyForUrl: GetProxyForUrlCallback;

constructor(opts?: ProxyAgentOptions) {
constructor(opts: ProxyAgentOptions) {
super(opts);
debug('Creating new ProxyAgent instance: %o', opts);
this.connectOpts = opts;
Expand All @@ -115,7 +115,7 @@ export class ProxyAgent extends Agent {
: 'http:';
const host = req.getHeader('host');
const url = new URL(req.path, `${protocol}//${host}`).href;
const proxy = await this.getProxyForUrl(url);
const proxy = await this.getProxyForUrl(url, req);

if (!proxy) {
debug('Proxy not enabled for URL: %o', url);
Expand Down
5 changes: 4 additions & 1 deletion packages/proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,17 @@ describe('ProxyAgent', () => {
it('should call provided function with getProxyForUrl option', async () => {
let gotCall = false;
let urlParameter = '';
let reqParameter: http.ClientRequest | undefined;
httpsServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});

const agent = new ProxyAgent({
rejectUnauthorized: false,
getProxyForUrl: (u) => {
getProxyForUrl: (u, r) => {
gotCall = true;
urlParameter = u;
reqParameter = r;
return httpsProxyServerUrl.href;
},
});
Expand All @@ -287,6 +289,7 @@ describe('ProxyAgent', () => {
assert(httpsServerUrl.host === body.host);
assert(gotCall);
assert(requestUrl.href === urlParameter);
assert(reqParameter?.constructor.name === 'ClientRequest');
});

it('should call provided function with asynchronous getProxyForUrl option', async () => {
Expand Down