Skip to content

Commit 0d6be3d

Browse files
committed
add stricter type for ProxyServer.web and ProxyServer.ws
1 parent 6cba519 commit 0d6be3d

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

lib/http-proxy/index.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as http from "http";
22
import * as https from "https";
3+
import * as net from "net";
34
import { WEB_PASSES } from "./passes/web-incoming";
45
import { WS_PASSES } from "./passes/ws-incoming";
56
import { EventEmitter } from "events";
@@ -84,6 +85,14 @@ export interface ServerOptions {
8485
buffer?: Stream;
8586
}
8687

88+
export type ErrorCallback =
89+
(
90+
err: Error,
91+
req: http.IncomingMessage,
92+
res: http.ServerResponse | net.Socket,
93+
target?: ProxyTargetUrl,
94+
) => void;
95+
8796
export class ProxyServer extends EventEmitter {
8897
/**
8998
* Used for proxying WS(S) requests
@@ -92,14 +101,43 @@ export class ProxyServer extends EventEmitter {
92101
* @param head - Client head.
93102
* @param options - Additional options.
94103
*/
95-
public readonly ws;
104+
public readonly ws: (
105+
...args:
106+
[
107+
req: http.IncomingMessage,
108+
socket: any,
109+
head: any,
110+
options?: ServerOptions,
111+
callback?: ErrorCallback,
112+
]
113+
| [
114+
req: http.IncomingMessage,
115+
socket: any,
116+
head: any,
117+
callback?: ErrorCallback,
118+
]
119+
) => void;
120+
96121
/**
97122
* Used for proxying regular HTTP(S) requests
98123
* @param req - Client request.
99124
* @param res - Client response.
100125
* @param options - Additional options.
101126
*/
102-
public readonly web;
127+
public readonly web: (
128+
...args:
129+
[
130+
req: http.IncomingMessage,
131+
res: http.ServerResponse,
132+
options: ServerOptions,
133+
callback?: ErrorCallback,
134+
]
135+
| [
136+
req: http.IncomingMessage,
137+
res: http.ServerResponse,
138+
callback?: ErrorCallback
139+
]
140+
) => void;
103141

104142
private options: ServerOptions;
105143
private webPasses;

lib/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
import { ProxyServer, type ServerOptions, type ProxyTarget, type ProxyTargetUrl } from "./http-proxy/index";
2-
export { ProxyServer, type ServerOptions, type ProxyTarget, type ProxyTargetUrl };
3-
export { numOpenSockets } from "./http-proxy/passes/ws-incoming";
1+
import {
2+
ProxyServer,
3+
type ServerOptions,
4+
type ProxyTarget,
5+
type ProxyTargetUrl,
6+
type ErrorCallback,
7+
} from './http-proxy/index';
8+
export {
9+
ProxyServer,
10+
type ServerOptions,
11+
type ProxyTarget,
12+
type ProxyTargetUrl,
13+
type ErrorCallback,
14+
};
15+
export { numOpenSockets } from './http-proxy/passes/ws-incoming';
416

517
/**
618
* Creates the proxy server.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ describe("#createProxyServer.web() using own http server", () => {
213213
function requestHandler(req, res) {
214214
proxy.web(req, res, (err) => {
215215
proxyServer.close();
216-
expect(err.code).toEqual("ECONNREFUSED");
216+
expect((err as NodeJS.ErrnoException).code).toEqual("ECONNREFUSED");
217217
done();
218218
});
219219
}

0 commit comments

Comments
 (0)