UNPKG

9.45 kBTypeScriptView Raw
1// Type definitions for node-http-proxy 1.17
2// Project: https://github.com/nodejitsu/node-http-proxy
3// Definitions by: Maxime LUCE <https://github.com/SomaticIT>
4// Florian Oellerich <https://github.com/Raigen>
5// Daniel Schmidt <https://github.com/DanielMSchmidt>
6// Jordan Abreu <https://github.com/jabreu610>
7// Samuel Bodin <https://github.com/bodinsamuel>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 2.1
10
11/// <reference types="node" />
12
13import * as net from "net";
14import * as http from "http";
15import * as https from "https";
16import * as events from "events";
17import * as url from "url";
18import * as stream from "stream";
19
20interface 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
35declare class Server extends events.EventEmitter {
36 /**
37 * Creates the proxy server with specified options.
38 * @param options - Config object passed to the proxy
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
133declare namespace Server {
134 type ProxyTarget = ProxyTargetUrl | ProxyTargetDetailed;
135 type ProxyTargetUrl = string | Partial<url.Url>;
136
137 interface ServerOptions {
138 /** URL string to be parsed with the url module. */
139 target?: ProxyTarget | undefined;
140 /** URL string to be parsed with the url module. */
141 forward?: ProxyTargetUrl | undefined;
142 /** Object to be passed to http(s).request. */
143 agent?: any;
144 /** Object to be passed to https.createServer(). */
145 ssl?: any;
146 /** If you want to proxy websockets. */
147 ws?: boolean | undefined;
148 /** Adds x- forward headers. */
149 xfwd?: boolean | undefined;
150 /** Verify SSL certificate. */
151 secure?: boolean | undefined;
152 /** Explicitly specify if we are proxying to another proxy. */
153 toProxy?: boolean | undefined;
154 /** Specify whether you want to prepend the target's path to the proxy path. */
155 prependPath?: boolean | undefined;
156 /** Specify whether you want to ignore the proxy path of the incoming request. */
157 ignorePath?: boolean | undefined;
158 /** Local interface string to bind for outgoing connections. */
159 localAddress?: string | undefined;
160 /** Changes the origin of the host header to the target URL. */
161 changeOrigin?: boolean | undefined;
162 /** specify whether you want to keep letter case of response header key */
163 preserveHeaderKeyCase?: boolean | undefined;
164 /** Basic authentication i.e. 'user:password' to compute an Authorization header. */
165 auth?: string | undefined;
166 /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */
167 hostRewrite?: string | undefined;
168 /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */
169 autoRewrite?: boolean | undefined;
170 /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */
171 protocolRewrite?: string | undefined;
172 /** rewrites domain of set-cookie headers. */
173 cookieDomainRewrite?: false | string | { [oldDomain: string]: string } | undefined;
174 /** rewrites path of set-cookie headers. Default: false */
175 cookiePathRewrite?: false | string | { [oldPath: string]: string } | undefined;
176 /** object with extra headers to be added to target requests. */
177 headers?: { [header: string]: string } | undefined;
178 /** Timeout (in milliseconds) when proxy receives no response from target. Default: 120000 (2 minutes) */
179 proxyTimeout?: number | undefined;
180 /** Timeout (in milliseconds) for incoming requests */
181 timeout?: number | undefined;
182 /** Specify whether you want to follow redirects. Default: false */
183 followRedirects?: boolean | undefined;
184 /** If set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the proxyRes event */
185 selfHandleResponse?: boolean | undefined;
186 /** Buffer */
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
226export = Server;