UNPKG

7.21 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// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9/// <reference types="node" />
10
11import * as net from "net";
12import * as http from "http";
13import * as https from "https";
14import * as events from "events";
15import * as url from "url";
16import * as stream from "stream";
17
18type ProxyTargetUrl = string | url.Url;
19
20type ErrorCallback = (
21 err: Error,
22 req: http.IncomingMessage,
23 res: http.ServerResponse,
24 target?: ProxyTargetUrl
25) => void;
26
27declare class Server extends events.EventEmitter {
28 /**
29 * Creates the proxy server with specified options.
30 * @param options - Config object passed to the proxy
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
168declare namespace Server {
169 interface ServerOptions {
170 /** URL string to be parsed with the url module. */
171 target?: ProxyTargetUrl;
172 /** URL string to be parsed with the url module. */
173 forward?: ProxyTargetUrl;
174 /** Object to be passed to http(s).request. */
175 agent?: any;
176 /** Object to be passed to https.createServer(). */
177 ssl?: any;
178 /** If you want to proxy websockets. */
179 ws?: boolean;
180 /** Adds x- forward headers. */
181 xfwd?: boolean;
182 /** Verify SSL certificate. */
183 secure?: boolean;
184 /** Explicitly specify if we are proxying to another proxy. */
185 toProxy?: boolean;
186 /** Specify whether you want to prepend the target's path to the proxy path. */
187 prependPath?: boolean;
188 /** Specify whether you want to ignore the proxy path of the incoming request. */
189 ignorePath?: boolean;
190 /** Local interface string to bind for outgoing connections. */
191 localAddress?: boolean;
192 /** Changes the origin of the host header to the target URL. */
193 changeOrigin?: boolean;
194 /** specify whether you want to keep letter case of response header key */
195 preserveHeaderKeyCase?: boolean;
196 /** Basic authentication i.e. 'user:password' to compute an Authorization header. */
197 auth?: string;
198 /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */
199 hostRewrite?: string;
200 /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */
201 autoRewrite?: boolean;
202 /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */
203 protocolRewrite?: string;
204 /** rewrites domain of set-cookie headers. */
205 cookieDomainRewrite?: false | string | {[oldDomain: string]: string};
206 /** rewrites path of set-cookie headers. Default: false */
207 cookiePathRewrite?: false | string | {[oldPath: string]: string};
208 /** object with extra headers to be added to target requests. */
209 headers?: {[header: string]: string};
210 /** Timeout (in milliseconds) when proxy receives no response from target. Default: 120000 (2 minutes) */
211 proxyTimeout?: number;
212 /** Timeout (in milliseconds) for incoming requests */
213 timeout?: number;
214 /** Specify whether you want to follow redirects. Default: false */
215 followRedirects?: boolean;
216 /** 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 */
217 selfHandleResponse?: boolean;
218 /** Buffer */
219 buffer?: stream.Stream;
220 }
221}
222
223export = Server;