UNPKG

5.88 kBTypeScriptView Raw
1// Type definitions for node-http-proxy 1.12
2// Project: https://github.com/nodejitsu/node-http-proxy
3// Definitions by: Maxime LUCE <https://github.com/SomaticIT>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6/// <reference types="node" />
7
8import * as net from "net";
9import * as http from "http";
10import * as https from "https";
11import * as events from "events";
12import * as url from "url";
13
14type ProxyTargetUrl = string | url.Url;
15
16type ErrorCallback = (
17 err: Error,
18 req: http.IncomingMessage,
19 res: http.ServerResponse,
20 target?: ProxyTargetUrl
21) => void;
22
23declare class Server extends events.EventEmitter {
24 /**
25 * Creates the proxy server with specified options.
26 * @param options - Config object passed to the proxy
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
164declare namespace Server {
165 interface ServerOptions {
166 /** URL string to be parsed with the url module. */
167 target?: string;
168 /** URL string to be parsed with the url module. */
169 forward?: string;
170 /** Object to be passed to http(s).request. */
171 agent?: any;
172 /** Object to be passed to https.createServer(). */
173 ssl?: any;
174 /** If you want to proxy websockets. */
175 ws?: boolean;
176 /** Adds x- forward headers. */
177 xfwd?: boolean;
178 /** Verify SSL certificate. */
179 secure?: boolean;
180 /** Explicitly specify if we are proxying to another proxy. */
181 toProxy?: boolean;
182 /** Specify whether you want to prepend the target's path to the proxy path. */
183 prependPath?: boolean;
184 /** Specify whether you want to ignore the proxy path of the incoming request. */
185 ignorePath?: boolean;
186 /** Local interface string to bind for outgoing connections. */
187 localAddress?: boolean;
188 /** Changes the origin of the host header to the target URL. */
189 changeOrigin?: boolean;
190 /** Basic authentication i.e. 'user:password' to compute an Authorization header. */
191 auth?: string;
192 /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */
193 hostRewrite?: string;
194 /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */
195 autoRewrite?: boolean;
196 /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */
197 protocolRewrite?: string;
198 }
199}
200
201export = Server;