Skip to content

Commit d8f2926

Browse files
abhu85claude
authored andcommitted
feat(http-proxy-agent): emit 'proxyConnect' event (#390)
* 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. The event payload includes: - `socket`: The connected socket to the proxy server This allows users to access the socket connection details (remoteAddress, localPort, etc.) when the proxy connection is established. Fixes #368 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: add changeset for proxyConnect event Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 671a314 commit d8f2926

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"http-proxy-agent": minor
3+
---
4+
5+
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.

packages/http-proxy-agent/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import type { OutgoingHttpHeaders } from 'http';
99

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

12+
export interface ProxyConnect {
13+
socket: net.Socket;
14+
}
15+
1216
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1317
type Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;
1418

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

173+
// Emit the 'proxyConnect' event for parity with https-proxy-agent
174+
const connect: ProxyConnect = { socket };
175+
req.emit('proxyConnect', connect);
176+
this.emit('proxyConnect', connect, req);
177+
169178
return socket;
170179
}
171180
}

packages/http-proxy-agent/test/test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,36 @@ describe('HttpProxyAgent', () => {
207207
expect(body.host).toEqual(httpServerUrl.hostname);
208208
});
209209

210+
it('should emit "proxyConnect" event on the request', async () => {
211+
httpServer.once('request', (req, res) => {
212+
res.end(JSON.stringify(req.headers));
213+
});
214+
215+
const agent = new HttpProxyAgent(proxyUrl);
216+
217+
const r = req(httpServerUrl, { agent });
218+
const [connect] = await once(r, 'proxyConnect');
219+
expect(connect.socket).toBeDefined();
220+
expect(connect.socket.remoteAddress).toBeDefined();
221+
const res = await r;
222+
expect(res.statusCode).toEqual(200);
223+
});
224+
225+
it('should emit "proxyConnect" event on the agent', async () => {
226+
httpServer.once('request', (req, res) => {
227+
res.end(JSON.stringify(req.headers));
228+
});
229+
230+
const agent = new HttpProxyAgent(proxyUrl);
231+
232+
const r = req(httpServerUrl, { agent });
233+
const [connect] = await once(agent, 'proxyConnect');
234+
expect(connect.socket).toBeDefined();
235+
expect(connect.socket.remoteAddress).toBeDefined();
236+
const res = await r;
237+
expect(res.statusCode).toEqual(200);
238+
});
239+
210240
it('should work with `keepAlive: true`', async () => {
211241
httpServer.on('request', (req, res) => {
212242
res.end(JSON.stringify(req.headers));

0 commit comments

Comments
 (0)