Skip to content

Commit

Permalink
[https-proxy-agent] Support dynamic headers (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed May 18, 2023
1 parent 2c5ebd6 commit 8ff9faa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-jobs-refuse.md
@@ -0,0 +1,5 @@
---
'https-proxy-agent': minor
---

"headers" option can now be a function
11 changes: 7 additions & 4 deletions packages/https-proxy-agent/src/index.ts
Expand Up @@ -3,7 +3,7 @@ import * as tls from 'tls';
import * as http from 'http';
import assert from 'assert';
import createDebug from 'debug';
import { OutgoingHttpHeaders } from 'http';
import type { OutgoingHttpHeaders } from 'http';
import { Agent, AgentConnectOpts } from 'agent-base';
import { parseProxyResponse } from './parse-proxy-response';

Expand All @@ -25,7 +25,7 @@ type ConnectOpts<T> = {

export type HttpsProxyAgentOptions<T> = ConnectOpts<T> &
http.AgentOptions & {
headers?: OutgoingHttpHeaders;
headers?: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);
};

/**
Expand All @@ -44,7 +44,7 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
static protocols = ['http', 'https'] as const;

readonly proxy: URL;
proxyHeaders: OutgoingHttpHeaders;
proxyHeaders: OutgoingHttpHeaders | (() => OutgoingHttpHeaders);
connectOpts: net.TcpNetConnectOpts & tls.ConnectionOptions;

get secureProxy() {
Expand Down Expand Up @@ -101,7 +101,10 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
socket = net.connect(this.connectOpts);
}

const headers: OutgoingHttpHeaders = { ...this.proxyHeaders };
const headers: OutgoingHttpHeaders =
typeof this.proxyHeaders === 'function'
? this.proxyHeaders()
: { ...this.proxyHeaders };
const host = net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;
let payload = `CONNECT ${host}:${opts.port} HTTP/1.1\r\n`;

Expand Down
25 changes: 24 additions & 1 deletion packages/https-proxy-agent/test/test.ts
Expand Up @@ -188,7 +188,7 @@ describe('HttpsProxyAgent', () => {
assert.equal('ECONNREFUSED', err.code);
});

it('should allow custom proxy "headers"', async () => {
it('should allow custom proxy "headers" object', async () => {
const agent = new HttpsProxyAgent(serverUrl, {
headers: {
Foo: 'bar',
Expand All @@ -205,6 +205,29 @@ describe('HttpsProxyAgent', () => {
socket.destroy();
});

it('should allow custom proxy "headers" function', async () => {
let count = 1;
const agent = new HttpsProxyAgent(serverUrl, {
headers: () => ({ number: count++ }),
});

const connectPromise = once(server, 'connect');
http.get({ agent });

const [req, socket] = await connectPromise;
assert.equal('CONNECT', req.method);
assert.equal('1', req.headers.number);
socket.destroy();

const connectPromise2 = once(server, 'connect');
http.get({ agent });

const [req2, socket2] = await connectPromise2;
assert.equal('CONNECT', req2.method);
assert.equal('2', req2.headers.number);
socket2.destroy();
});

it('should work with `keepAlive: true`', async () => {
server.on('request', (req, res) => {
res.end(JSON.stringify(req.headers));
Expand Down

1 comment on commit 8ff9faa

@vercel
Copy link

@vercel vercel bot commented on 8ff9faa May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

proxy-agents – ./

proxy-agents-tootallnate.vercel.app
proxy-agents.vercel.app
proxy-agents-git-main-tootallnate.vercel.app

Please sign in to comment.