UNPKG

7.95 kBTypeScriptView Raw
1// Inlined to avoid extra dependency
2// MIT Licensed https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/LICENSE
3
4// Type definitions for node-http-proxy 1.17
5// Project: https://github.com/nodejitsu/node-http-proxy
6// Definitions by: Maxime LUCE <https://github.com/SomaticIT>
7// Florian Oellerich <https://github.com/Raigen>
8// Daniel Schmidt <https://github.com/DanielMSchmidt>
9// Jordan Abreu <https://github.com/jabreu610>
10// Samuel Bodin <https://github.com/bodinsamuel>
11// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
12// TypeScript Version: 2.1
13
14/// <reference types="node" />
15
16import * as net from 'net'
17import * as http from 'http'
18import * as events from 'events'
19import * as url from 'url'
20import * as stream from 'stream'
21
22export namespace HttpProxy {
23 export type ProxyTarget = ProxyTargetUrl | ProxyTargetDetailed
24
25 export type ProxyTargetUrl = string | Partial<url.Url>
26
27 export interface ProxyTargetDetailed {
28 host: string
29 port: number
30 protocol?: string
31 hostname?: string
32 socketPath?: string
33 key?: string
34 passphrase?: string
35 pfx?: Buffer | string
36 cert?: string
37 ca?: string
38 ciphers?: string
39 secureProtocol?: string
40 }
41
42 export type ErrorCallback = (
43 err: Error,
44 req: http.IncomingMessage,
45 res: http.ServerResponse,
46 target?: ProxyTargetUrl
47 ) => void
48
49 export class Server extends events.EventEmitter {
50 /**
51 * Creates the proxy server with specified options.
52 * @param options - Config object passed to the proxy
53 */
54 constructor(options?: ServerOptions)
55
56 /**
57 * Used for proxying regular HTTP(S) requests
58 * @param req - Client request.
59 * @param res - Client response.
60 * @param options - Additionnal options.
61 */
62 web(
63 req: http.IncomingMessage,
64 res: http.ServerResponse,
65 options?: ServerOptions,
66 callback?: ErrorCallback
67 ): void
68
69 /**
70 * Used for proxying regular HTTP(S) requests
71 * @param req - Client request.
72 * @param socket - Client socket.
73 * @param head - Client head.
74 * @param options - Additionnal options.
75 */
76 ws(
77 req: http.IncomingMessage,
78 socket: any,
79 head: any,
80 options?: ServerOptions,
81 callback?: ErrorCallback
82 ): void
83
84 /**
85 * A function that wraps the object in a webserver, for your convenience
86 * @param port - Port to listen on
87 */
88 listen(port: number): Server
89
90 /**
91 * A function that closes the inner webserver and stops listening on given port
92 */
93 close(callback?: () => void): void
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 createProxyServer(options?: ServerOptions): Server
101
102 /**
103 * Creates the proxy server with specified options.
104 * @param options - Config object passed to the proxy
105 * @returns Proxy object with handlers for `ws` and `web` requests
106 */
107 static createServer(options?: ServerOptions): Server
108
109 /**
110 * Creates the proxy server with specified options.
111 * @param options - Config object passed to the proxy
112 * @returns Proxy object with handlers for `ws` and `web` requests
113 */
114 static createProxy(options?: ServerOptions): Server
115
116 addListener(event: string, listener: () => void): this
117 on(event: string, listener: () => void): this
118 on(event: 'error', listener: ErrorCallback): this
119 on(
120 event: 'start',
121 listener: (
122 req: http.IncomingMessage,
123 res: http.ServerResponse,
124 target: ProxyTargetUrl
125 ) => void
126 ): this
127 on(
128 event: 'proxyReq',
129 listener: (
130 proxyReq: http.ClientRequest,
131 req: http.IncomingMessage,
132 res: http.ServerResponse,
133 options: ServerOptions
134 ) => void
135 ): this
136 on(
137 event: 'proxyRes',
138 listener: (
139 proxyRes: http.IncomingMessage,
140 req: http.IncomingMessage,
141 res: http.ServerResponse
142 ) => void
143 ): this
144 on(
145 event: 'proxyReqWs',
146 listener: (
147 proxyReq: http.ClientRequest,
148 req: http.IncomingMessage,
149 socket: net.Socket,
150 options: ServerOptions,
151 head: any
152 ) => void
153 ): this
154 on(
155 event: 'econnreset',
156 listener: (
157 err: Error,
158 req: http.IncomingMessage,
159 res: http.ServerResponse,
160 target: ProxyTargetUrl
161 ) => void
162 ): this
163 on(
164 event: 'end',
165 listener: (
166 req: http.IncomingMessage,
167 res: http.ServerResponse,
168 proxyRes: http.IncomingMessage
169 ) => void
170 ): this
171 on(
172 event: 'close',
173 listener: (
174 proxyRes: http.IncomingMessage,
175 proxySocket: net.Socket,
176 proxyHead: any
177 ) => void
178 ): this
179
180 once(event: string, listener: () => void): this
181 removeListener(event: string, listener: () => void): this
182 removeAllListeners(event?: string): this
183 getMaxListeners(): number
184 setMaxListeners(n: number): this
185 listeners(event: string): Array<() => void>
186 emit(event: string, ...args: any[]): boolean
187 listenerCount(type: string): number
188 }
189
190 export interface ServerOptions {
191 /** URL string to be parsed with the url module. */
192 target?: ProxyTarget
193 /** URL string to be parsed with the url module. */
194 forward?: ProxyTargetUrl
195 /** Object to be passed to http(s).request. */
196 agent?: any
197 /** Object to be passed to https.createServer(). */
198 ssl?: any
199 /** If you want to proxy websockets. */
200 ws?: boolean
201 /** Adds x- forward headers. */
202 xfwd?: boolean
203 /** Verify SSL certificate. */
204 secure?: boolean
205 /** Explicitly specify if we are proxying to another proxy. */
206 toProxy?: boolean
207 /** Specify whether you want to prepend the target's path to the proxy path. */
208 prependPath?: boolean
209 /** Specify whether you want to ignore the proxy path of the incoming request. */
210 ignorePath?: boolean
211 /** Local interface string to bind for outgoing connections. */
212 localAddress?: string
213 /** Changes the origin of the host header to the target URL. */
214 changeOrigin?: boolean
215 /** specify whether you want to keep letter case of response header key */
216 preserveHeaderKeyCase?: boolean
217 /** Basic authentication i.e. 'user:password' to compute an Authorization header. */
218 auth?: string
219 /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */
220 hostRewrite?: string
221 /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */
222 autoRewrite?: boolean
223 /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */
224 protocolRewrite?: string
225 /** rewrites domain of set-cookie headers. */
226 cookieDomainRewrite?: false | string | { [oldDomain: string]: string }
227 /** rewrites path of set-cookie headers. Default: false */
228 cookiePathRewrite?: false | string | { [oldPath: string]: string }
229 /** object with extra headers to be added to target requests. */
230 headers?: { [header: string]: string }
231 /** Timeout (in milliseconds) when proxy receives no response from target. Default: 120000 (2 minutes) */
232 proxyTimeout?: number
233 /** Timeout (in milliseconds) for incoming requests */
234 timeout?: number
235 /** Specify whether you want to follow redirects. Default: false */
236 followRedirects?: boolean
237 /** 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 */
238 selfHandleResponse?: boolean
239 /** Buffer */
240 buffer?: stream.Stream
241 }
242}