1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import type * as net from 'net'
|
17 | import type * as http from 'http'
|
18 | import * as events from 'events'
|
19 | import type * as url from 'url'
|
20 | import type * as stream from 'stream'
|
21 |
|
22 | export 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 |
|
52 |
|
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 - 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 |
|
192 | target?: ProxyTarget
|
193 |
|
194 | forward?: ProxyTargetUrl
|
195 |
|
196 | agent?: any
|
197 |
|
198 | ssl?: any
|
199 |
|
200 | ws?: boolean
|
201 |
|
202 | xfwd?: boolean
|
203 |
|
204 | secure?: boolean
|
205 |
|
206 | toProxy?: boolean
|
207 |
|
208 | prependPath?: boolean
|
209 |
|
210 | ignorePath?: boolean
|
211 |
|
212 | localAddress?: string
|
213 |
|
214 | changeOrigin?: boolean
|
215 |
|
216 | preserveHeaderKeyCase?: boolean
|
217 |
|
218 | auth?: string
|
219 |
|
220 | hostRewrite?: string
|
221 |
|
222 | autoRewrite?: boolean
|
223 |
|
224 | protocolRewrite?: string
|
225 |
|
226 | cookieDomainRewrite?: false | string | { [oldDomain: string]: string }
|
227 |
|
228 | cookiePathRewrite?: false | string | { [oldPath: string]: string }
|
229 |
|
230 | headers?: { [header: string]: string }
|
231 |
|
232 | proxyTimeout?: number
|
233 |
|
234 | timeout?: number
|
235 |
|
236 | followRedirects?: boolean
|
237 |
|
238 | selfHandleResponse?: boolean
|
239 |
|
240 | buffer?: stream.Stream
|
241 | }
|
242 | }
|