UNPKG

8.45 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 type * as net from 'node:net'
17import type * as http from 'node:http'
18import * as events from 'node:events'
19import type * as url from 'node:url'
20import type * as stream from 'node: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 | undefined
31 hostname?: string | undefined
32 socketPath?: string | undefined
33 key?: string | undefined
34 passphrase?: string | undefined
35 pfx?: Buffer | string | undefined
36 cert?: string | undefined
37 ca?: string | undefined
38 ciphers?: string | undefined
39 secureProtocol?: string | undefined
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 - Additional 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 - Additional options.
75 */
76 ws(
77 req: http.IncomingMessage,
78 socket: unknown,
79 head: unknown,
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 | undefined
193 /** URL string to be parsed with the url module. */
194 forward?: ProxyTargetUrl | undefined
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 | undefined
201 /** Adds x- forward headers. */
202 xfwd?: boolean | undefined
203 /** Verify SSL certificate. */
204 secure?: boolean | undefined
205 /** Explicitly specify if we are proxying to another proxy. */
206 toProxy?: boolean | undefined
207 /** Specify whether you want to prepend the target's path to the proxy path. */
208 prependPath?: boolean | undefined
209 /** Specify whether you want to ignore the proxy path of the incoming request. */
210 ignorePath?: boolean | undefined
211 /** Local interface string to bind for outgoing connections. */
212 localAddress?: string | undefined
213 /** Changes the origin of the host header to the target URL. */
214 changeOrigin?: boolean | undefined
215 /** specify whether you want to keep letter case of response header key */
216 preserveHeaderKeyCase?: boolean | undefined
217 /** Basic authentication i.e. 'user:password' to compute an Authorization header. */
218 auth?: string | undefined
219 /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */
220 hostRewrite?: string | undefined
221 /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */
222 autoRewrite?: boolean | undefined
223 /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */
224 protocolRewrite?: string | undefined
225 /** rewrites domain of set-cookie headers. */
226 cookieDomainRewrite?:
227 | false
228 | string
229 | { [oldDomain: string]: string }
230 | undefined
231 /** rewrites path of set-cookie headers. Default: false */
232 cookiePathRewrite?:
233 | false
234 | string
235 | { [oldPath: string]: string }
236 | undefined
237 /** object with extra headers to be added to target requests. */
238 headers?: { [header: string]: string } | undefined
239 /** Timeout (in milliseconds) when proxy receives no response from target. Default: 120000 (2 minutes) */
240 proxyTimeout?: number | undefined
241 /** Timeout (in milliseconds) for incoming requests */
242 timeout?: number | undefined
243 /** Specify whether you want to follow redirects. Default: false */
244 followRedirects?: boolean | undefined
245 /** 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 */
246 selfHandleResponse?: boolean | undefined
247 /** Buffer */
248 buffer?: stream.Stream | undefined
249 }
250}