Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/http-proxy-agent-proxyconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"http-proxy-agent": minor
---

feat(http-proxy-agent): emit 'proxyConnect' event

Adds `proxyConnect` event emission to `http-proxy-agent` for parity with `https-proxy-agent`. The event is emitted on both the request and the agent instance when the socket connects to the proxy server.
9 changes: 9 additions & 0 deletions packages/http-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type { OutgoingHttpHeaders } from 'http';

const debug = createDebug('http-proxy-agent');

export interface ProxyConnect {
socket: net.Socket;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;

Expand Down Expand Up @@ -166,6 +170,11 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
// connection via the `callback()` function throwing.
await once(socket, 'connect');

// Emit the 'proxyConnect' event for parity with https-proxy-agent
const connect: ProxyConnect = { socket };
req.emit('proxyConnect', connect);
this.emit('proxyConnect', connect, req);

return socket;
}
}
Expand Down
30 changes: 30 additions & 0 deletions packages/http-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,36 @@ describe('HttpProxyAgent', () => {
expect(body.host).toEqual(httpServerUrl.hostname);
});

it('should emit "proxyConnect" event on the request', async () => {
httpServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const agent = new HttpProxyAgent(proxyUrl);

const r = req(httpServerUrl, { agent });
const [connect] = await once(r, 'proxyConnect');
expect(connect.socket).toBeDefined();
expect(connect.socket.remoteAddress).toBeDefined();
const res = await r;
expect(res.statusCode).toEqual(200);
});

it('should emit "proxyConnect" event on the agent', async () => {
httpServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});

const agent = new HttpProxyAgent(proxyUrl);

const r = req(httpServerUrl, { agent });
const [connect] = await once(agent, 'proxyConnect');
expect(connect.socket).toBeDefined();
expect(connect.socket.remoteAddress).toBeDefined();
const res = await r;
expect(res.statusCode).toEqual(200);
});

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