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