Skip to content

Commit 4c4a434

Browse files
committed
add types for events
1 parent 0d6be3d commit 4c4a434

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

lib/http-proxy/index.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,51 @@ export type ErrorCallback =
9393
target?: ProxyTargetUrl,
9494
) => void;
9595

96-
export class ProxyServer extends EventEmitter {
96+
type ProxyServerEventMap = {
97+
error: Parameters<ErrorCallback>;
98+
start: [
99+
req: http.IncomingMessage,
100+
res: http.ServerResponse,
101+
target: ProxyTargetUrl,
102+
];
103+
open: [socket: net.Socket];
104+
proxyReq: [
105+
proxyReq: http.ClientRequest,
106+
req: http.IncomingMessage,
107+
res: http.ServerResponse,
108+
options: ServerOptions,
109+
];
110+
proxyRes: [
111+
proxyRes: http.IncomingMessage,
112+
req: http.IncomingMessage,
113+
res: http.ServerResponse,
114+
];
115+
proxyReqWs: [
116+
proxyReq: http.ClientRequest,
117+
req: http.IncomingMessage,
118+
socket: net.Socket,
119+
options: ServerOptions,
120+
head: any,
121+
];
122+
econnreset: [
123+
err: Error,
124+
req: http.IncomingMessage,
125+
res: http.ServerResponse,
126+
target: ProxyTargetUrl,
127+
];
128+
end: [
129+
req: http.IncomingMessage,
130+
res: http.ServerResponse,
131+
proxyRes: http.IncomingMessage,
132+
];
133+
close: [
134+
proxyRes: http.IncomingMessage,
135+
proxySocket: net.Socket,
136+
proxyHead: any,
137+
];
138+
}
139+
140+
export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
97141
/**
98142
* Used for proxying WS(S) requests
99143
* @param req - Client request.
@@ -206,8 +250,8 @@ export class ProxyServer extends EventEmitter {
206250
// and there's no way for a user of http-proxy-3 to get ahold
207251
// of this res object and attach their own error handler until
208252
// after the passes. So we better attach one ASAP right here:
209-
res.on("error", (...args) => {
210-
this.emit("error", ...args);
253+
(res as net.Socket).on("error", (err) => {
254+
this.emit("error", err, req, res);
211255
});
212256
}
213257
let counter = args.length - 1;
@@ -240,7 +284,7 @@ export class ProxyServer extends EventEmitter {
240284
}
241285

242286
if (!requestOptions.target && !requestOptions.forward) {
243-
this.emit("error", new Error("Must set target or forward"));
287+
this.emit("error", new Error("Must set target or forward"), req, res);
244288
return;
245289
}
246290

lib/test/http/custom-proxy-error.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
22
custom-proxy-error.test.ts: Example of using the custom `proxyError` event.
3-
3+
44
pnpm test ./custom-proxy-error.test.ts
55
*/
66

77
import * as httpProxy from "../..";
8+
import * as http from "http";
89
import getPort from "../get-port";
910
import fetch from "node-fetch";
1011

@@ -27,7 +28,7 @@ describe("Test proxying over HTTP with latency", () => {
2728
.listen(ports.proxy);
2829

2930
proxy.on("error", (_err, _req, res) => {
30-
res.writeHead(500, {
31+
(res as http.ServerResponse).writeHead(500, {
3132
"Content-Type": "text/plain",
3233
});
3334
res.end(CUSTOM_ERROR);

lib/test/lib/http-proxy-passes-web-incoming.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe("#createProxyServer.web() using own http server", () => {
245245
proxyServer.close();
246246
expect(errReq).toEqual(req);
247247
expect(errRes).toEqual(res);
248-
expect(err.code).toEqual("ECONNREFUSED");
248+
expect((err as NodeJS.ErrnoException).code).toEqual("ECONNREFUSED");
249249
done();
250250
});
251251

@@ -279,7 +279,7 @@ describe("#createProxyServer.web() using own http server", () => {
279279
proxyServer.close();
280280
expect(errReq).toEqual(req);
281281
expect(errRes).toEqual(res);
282-
expect(err.code).toEqual("ECONNREFUSED");
282+
expect((err as NodeJS.ErrnoException).code).toEqual("ECONNREFUSED");
283283
done();
284284
});
285285

@@ -317,7 +317,7 @@ describe("#createProxyServer.web() using own http server", () => {
317317
expect(errReq).toEqual(req);
318318
expect(errRes).toEqual(res);
319319
expect(Date.now() - started).toBeGreaterThan(99);
320-
expect(err.code).toEqual("ECONNRESET");
320+
expect((err as NodeJS.ErrnoException).code).toEqual("ECONNRESET");
321321
done();
322322
});
323323

@@ -362,7 +362,7 @@ describe("#createProxyServer.web() using own http server", () => {
362362
server.close();
363363
expect(errReq).toEqual(req);
364364
expect(errRes).toEqual(res);
365-
expect(err.code).toEqual("ECONNRESET");
365+
expect((err as NodeJS.ErrnoException).code).toEqual("ECONNRESET");
366366
doneOne();
367367
});
368368

lib/test/lib/http-proxy.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe("#createProxyServer() method with error response", () => {
168168

169169
proxy
170170
.on("error", (err) => {
171-
expect(err.code).toEqual("ECONNREFUSED");
171+
expect((err as NodeJS.ErrnoException).code).toEqual("ECONNREFUSED");
172172
proxy.close();
173173
done();
174174
})
@@ -198,7 +198,7 @@ describe("#createProxyServer setting the correct timeout value", () => {
198198
.listen(ports.proxy);
199199

200200
proxy.on("error", (e) => {
201-
expect(e.code).toEqual("ECONNRESET");
201+
expect((e as NodeJS.ErrnoException).code).toEqual("ECONNRESET");
202202
});
203203

204204
const source = http.createServer((_req, res) => {
@@ -327,7 +327,7 @@ describe("#createProxyServer using the ws-incoming passes", () => {
327327
});
328328

329329
proxy.on("error", (err) => {
330-
expect(err.code).toEqual("ECONNREFUSED");
330+
expect((err as NodeJS.ErrnoException).code).toEqual("ECONNREFUSED");
331331
proxyServer.close();
332332
maybe_done();
333333
});

lib/test/middleware/body-decoder-middleware.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ describe("connect.bodyParser() middleware in http-proxy-3", () => {
3434
});
3535

3636
// re-serialize parsed body before proxying.
37-
proxy.on("proxyReq", (proxyReq, req, _res, _options) => {
37+
proxy.on("proxyReq", (proxyReq, rawReq, _res, _options) => {
38+
const req = rawReq as http.IncomingMessage & { body?: any };
3839
if (!req.body || !Object.keys(req.body).length) {
3940
return;
4041
}

0 commit comments

Comments
 (0)