1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import * as net from "net";
|
9 | import * as http from "http";
|
10 | import * as https from "https";
|
11 | import * as events from "events";
|
12 | import * as url from "url";
|
13 |
|
14 | type ProxyTargetUrl = string | url.Url;
|
15 |
|
16 | type ErrorCallback = (
|
17 | err: Error,
|
18 | req: http.IncomingMessage,
|
19 | res: http.ServerResponse,
|
20 | target?: ProxyTargetUrl
|
21 | ) => void;
|
22 |
|
23 | declare class Server extends events.EventEmitter {
|
24 | |
25 |
|
26 |
|
27 |
|
28 | constructor(options?: Server.ServerOptions);
|
29 |
|
30 | /**
|
31 | * Used for proxying regular HTTP(S) requests
|
32 | * @param req - Client request.
|
33 | * @param res - Client response.
|
34 | * @param options - Additionnal options.
|
35 | * @param
|
36 | */
|
37 | web(
|
38 | req: http.IncomingMessage,
|
39 | res: http.ServerResponse,
|
40 | options?: Server.ServerOptions,
|
41 | callback?: ErrorCallback
|
42 | ): void;
|
43 |
|
44 | /**
|
45 | * Used for proxying regular HTTP(S) requests
|
46 | * @param req - Client request.
|
47 | * @param socket - Client socket.
|
48 | * @param head - Client head.
|
49 | * @param options - Additionnal options.
|
50 | */
|
51 | ws(
|
52 | req: http.IncomingMessage,
|
53 | socket: any,
|
54 | head: any,
|
55 | options?: Server.ServerOptions
|
56 | ): void;
|
57 |
|
58 | /**
|
59 | * A function that wraps the object in a webserver, for your convenience
|
60 | * @param port - Port to listen on
|
61 | */
|
62 | listen(port: number): Server;
|
63 |
|
64 | /**
|
65 | * A function that closes the inner webserver and stops listening on given port
|
66 | */
|
67 | close(callback?: () => void): void;
|
68 |
|
69 | /**
|
70 | * Creates the proxy server with specified options.
|
71 | * @param options Config object passed to the proxy
|
72 | * @returns Proxy object with handlers for `ws` and `web` requests
|
73 | */
|
74 | static createProxyServer(options?: Server.ServerOptions): Server;
|
75 |
|
76 | /**
|
77 | * Creates the proxy server with specified options.
|
78 | * @param options Config object passed to the proxy
|
79 | * @returns Proxy object with handlers for `ws` and `web` requests
|
80 | */
|
81 | static createServer(options?: Server.ServerOptions): Server;
|
82 |
|
83 | /**
|
84 | * Creates the proxy server with specified options.
|
85 | * @param options Config object passed to the proxy
|
86 | * @returns Proxy object with handlers for `ws` and `web` requests
|
87 | */
|
88 | static createProxy(options?: Server.ServerOptions): Server;
|
89 |
|
90 | addListener(event: string, listener: () => void): this;
|
91 | on(event: string, listener: () => void): this;
|
92 | on(event: "error", listener: ErrorCallback): this;
|
93 | on(
|
94 | event: "start",
|
95 | listener: (
|
96 | req: http.IncomingMessage,
|
97 | res: http.ServerResponse,
|
98 | target: ProxyTargetUrl
|
99 | ) => void
|
100 | ): this;
|
101 | on(
|
102 | event: "proxyReq",
|
103 | listener: (
|
104 | proxyReq: http.ClientRequest,
|
105 | req: http.IncomingMessage,
|
106 | res: http.ServerResponse,
|
107 | options: Server.ServerOptions
|
108 | ) => void
|
109 | ): this;
|
110 | on(
|
111 | event: "proxyRes",
|
112 | listener: (
|
113 | proxyRes: http.IncomingMessage,
|
114 | req: http.IncomingMessage,
|
115 | res: http.ServerResponse
|
116 | ) => void
|
117 | ): this;
|
118 | on(
|
119 | event: "proxyReqWs",
|
120 | listener: (
|
121 | proxyReq: http.ClientRequest,
|
122 | req: http.IncomingMessage,
|
123 | socket: net.Socket,
|
124 | options: Server.ServerOptions,
|
125 | head: any
|
126 | ) => void
|
127 | ): this;
|
128 | on(
|
129 | event: "econnreset",
|
130 | listener: (
|
131 | err: Error,
|
132 | req: http.IncomingMessage,
|
133 | res: http.ServerResponse,
|
134 | target: ProxyTargetUrl
|
135 | ) => void
|
136 | ): this;
|
137 | on(
|
138 | event: "end",
|
139 | listener: (
|
140 | req: http.IncomingMessage,
|
141 | res: http.ServerResponse,
|
142 | proxyRes: http.IncomingMessage
|
143 | ) => void
|
144 | ): this;
|
145 | on(
|
146 | event: "close",
|
147 | listener: (
|
148 | proxyRes: http.IncomingMessage,
|
149 | proxySocket: net.Socket,
|
150 | proxyHead: any
|
151 | ) => void
|
152 | ): this;
|
153 |
|
154 | once(event: string, listener: () => void): this;
|
155 | removeListener(event: string, listener: () => void): this;
|
156 | removeAllListeners(event?: string): this;
|
157 | getMaxListeners(): number;
|
158 | setMaxListeners(n: number): this;
|
159 | listeners(event: string): Array<() => void>;
|
160 | emit(event: string, ...args: any[]): boolean;
|
161 | listenerCount(type: string): number;
|
162 | }
|
163 |
|
164 | declare namespace Server {
|
165 | interface ServerOptions {
|
166 |
|
167 | target?: string;
|
168 |
|
169 | forward?: string;
|
170 |
|
171 | agent?: any;
|
172 |
|
173 | ssl?: any;
|
174 |
|
175 | ws?: boolean;
|
176 |
|
177 | xfwd?: boolean;
|
178 |
|
179 | secure?: boolean;
|
180 |
|
181 | toProxy?: boolean;
|
182 |
|
183 | prependPath?: boolean;
|
184 |
|
185 | ignorePath?: boolean;
|
186 |
|
187 | localAddress?: boolean;
|
188 |
|
189 | changeOrigin?: boolean;
|
190 |
|
191 | auth?: string;
|
192 |
|
193 | hostRewrite?: string;
|
194 |
|
195 | autoRewrite?: boolean;
|
196 |
|
197 | protocolRewrite?: string;
|
198 | }
|
199 | }
|
200 |
|
201 | export = Server;
|