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