@@ -99,128 +99,128 @@ export interface NormalizedServerOptions extends ServerOptions {
9999 forward ?: NormalizeProxyTarget < ProxyTargetUrl > ;
100100}
101101
102- export type ErrorCallback =
102+ export type ErrorCallback < TIncomingMessage = http . IncomingMessage , TServerResponse = http . ServerResponse , TError = Error > =
103103 (
104- err : Error ,
105- req : http . IncomingMessage ,
106- res : http . ServerResponse | net . Socket ,
104+ err : TError ,
105+ req : TIncomingMessage ,
106+ res : TServerResponse | net . Socket ,
107107 target ?: ProxyTargetUrl ,
108108 ) => void ;
109109
110- type ProxyServerEventMap = {
111- error : Parameters < ErrorCallback > ;
110+ type ProxyServerEventMap < TIncomingMessage = http . IncomingMessage , TServerResponse = http . ServerResponse , TError = Error > = {
111+ error : Parameters < ErrorCallback < TIncomingMessage , TServerResponse , TError > > ;
112112 start : [
113- req : http . IncomingMessage ,
114- res : http . ServerResponse ,
113+ req : TIncomingMessage ,
114+ res : TServerResponse ,
115115 target : ProxyTargetUrl ,
116116 ] ;
117117 open : [ socket : net . Socket ] ;
118118 proxyReq : [
119119 proxyReq : http . ClientRequest ,
120- req : http . IncomingMessage ,
121- res : http . ServerResponse ,
120+ req : TIncomingMessage ,
121+ res : TServerResponse ,
122122 options : ServerOptions ,
123123 socket : net . Socket ,
124124 ] ;
125125 proxyRes : [
126- proxyRes : http . IncomingMessage ,
127- req : http . IncomingMessage ,
128- res : http . ServerResponse ,
126+ proxyRes : TIncomingMessage ,
127+ req : TIncomingMessage ,
128+ res : TServerResponse ,
129129 ] ;
130130 proxyReqWs : [
131131 proxyReq : http . ClientRequest ,
132- req : http . IncomingMessage ,
132+ req : TIncomingMessage ,
133133 socket : net . Socket ,
134134 options : ServerOptions ,
135135 head : any ,
136136 ] ;
137137 econnreset : [
138138 err : Error ,
139- req : http . IncomingMessage ,
140- res : http . ServerResponse ,
139+ req : TIncomingMessage ,
140+ res : TServerResponse ,
141141 target : ProxyTargetUrl ,
142142 ] ;
143143 end : [
144- req : http . IncomingMessage ,
145- res : http . ServerResponse ,
146- proxyRes : http . IncomingMessage ,
144+ req : TIncomingMessage ,
145+ res : TServerResponse ,
146+ proxyRes : TIncomingMessage ,
147147 ] ;
148148 close : [
149- proxyRes : http . IncomingMessage ,
149+ proxyRes : TIncomingMessage ,
150150 proxySocket : net . Socket ,
151151 proxyHead : any ,
152152 ] ;
153153}
154154
155- type ProxyMethodArgs = {
155+ type ProxyMethodArgs < TIncomingMessage = http . IncomingMessage , TServerResponse = http . ServerResponse , TError = Error > = {
156156 ws : [
157- req : http . IncomingMessage ,
157+ req : TIncomingMessage ,
158158 socket : any ,
159159 head : any ,
160160 ...args :
161- [
162- options ?: ServerOptions ,
163- callback ?: ErrorCallback ,
164- ]
161+ [
162+ options ?: ServerOptions ,
163+ callback ?: ErrorCallback < TIncomingMessage , TServerResponse , TError > ,
164+ ]
165165 | [
166- callback ?: ErrorCallback ,
167- ]
166+ callback ?: ErrorCallback < TIncomingMessage , TServerResponse , TError > ,
167+ ]
168168 ]
169169 web : [
170- req : http . IncomingMessage ,
171- res : http . ServerResponse ,
170+ req : TIncomingMessage ,
171+ res : TServerResponse ,
172172 ...args :
173- [
174- options : ServerOptions ,
175- callback ?: ErrorCallback ,
176- ]
177- | [
178- callback ?: ErrorCallback
179- ]
173+ [
174+ options : ServerOptions ,
175+ callback ?: ErrorCallback < TIncomingMessage , TServerResponse , TError > ,
176+ ]
177+ | [
178+ callback ?: ErrorCallback < TIncomingMessage , TServerResponse , TError >
179+ ]
180180 ]
181181}
182182
183- type PassFunctions = {
183+ type PassFunctions < TIncomingMessage = http . IncomingMessage , TServerResponse = http . ServerResponse , TError = Error > = {
184184 ws : (
185- req : http . IncomingMessage ,
185+ req : TIncomingMessage ,
186186 socket : net . Socket ,
187187 options : NormalizedServerOptions ,
188188 head : Buffer | undefined ,
189- server : ProxyServer ,
190- cb ?: ErrorCallback
189+ server : ProxyServer < TIncomingMessage , TServerResponse , TError > ,
190+ cb ?: ErrorCallback < TIncomingMessage , TServerResponse , TError >
191191 ) => unknown
192192 web : (
193- req : http . IncomingMessage ,
194- res : http . ServerResponse ,
193+ req : TIncomingMessage ,
194+ res : TServerResponse ,
195195 options : NormalizedServerOptions ,
196196 head : Buffer | undefined ,
197- server : ProxyServer ,
198- cb ?: ErrorCallback
197+ server : ProxyServer < TIncomingMessage , TServerResponse , TError > ,
198+ cb ?: ErrorCallback < TIncomingMessage , TServerResponse , TError >
199199 ) => unknown
200200}
201201
202- export class ProxyServer extends EventEmitter < ProxyServerEventMap > {
202+ export class ProxyServer < TIncomingMessage = http . IncomingMessage , TServerResponse = http . ServerResponse , TError = Error > extends EventEmitter < ProxyServerEventMap < TIncomingMessage , TServerResponse , TError > > {
203203 /**
204204 * Used for proxying WS(S) requests
205205 * @param req - Client request.
206206 * @param socket - Client socket.
207207 * @param head - Client head.
208208 * @param options - Additional options.
209209 */
210- public readonly ws : ( ...args : ProxyMethodArgs [ "ws" ] ) => void ;
210+ public readonly ws : ( ...args : ProxyMethodArgs < TIncomingMessage , TServerResponse , TError > [ "ws" ] ) => void ;
211211
212212 /**
213213 * Used for proxying regular HTTP(S) requests
214214 * @param req - Client request.
215215 * @param res - Client response.
216216 * @param options - Additional options.
217217 */
218- public readonly web : ( ...args : ProxyMethodArgs [ "web" ] ) => void ;
218+ public readonly web : ( ...args : ProxyMethodArgs < TIncomingMessage , TServerResponse , TError > [ "web" ] ) => void ;
219219
220220 private options : ServerOptions ;
221- private webPasses : Array < PassFunctions [ 'web' ] > ;
222- private wsPasses : Array < PassFunctions [ 'ws' ] > ;
223- private _server ?: http . Server | https . Server | null ;
221+ private webPasses : Array < PassFunctions < TIncomingMessage , TServerResponse , TError > [ 'web' ] > ;
222+ private wsPasses : Array < PassFunctions < TIncomingMessage , TServerResponse , TError > [ 'ws' ] > ;
223+ private _server ?: http . Server < TIncomingMessage , TServerResponse > | https . Server < TIncomingMessage , TServerResponse > | null ;
224224
225225 /**
226226 * Creates the proxy server with specified options.
@@ -233,8 +233,8 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
233233 this . options = options ;
234234 this . web = this . createRightProxy ( "web" ) ( options ) ;
235235 this . ws = this . createRightProxy ( "ws" ) ( options ) ;
236- this . webPasses = Object . values ( WEB_PASSES ) ;
237- this . wsPasses = Object . values ( WS_PASSES ) ;
236+ this . webPasses = Object . values ( WEB_PASSES ) as Array < PassFunctions < TIncomingMessage , TServerResponse , TError > [ 'web' ] > ;
237+ this . wsPasses = Object . values ( WS_PASSES ) as Array < PassFunctions < TIncomingMessage , TServerResponse , TError > [ 'ws' ] > ;
238238 this . on ( "error" , this . onError ) ;
239239 }
240240
@@ -243,36 +243,36 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
243243 * @param options Config object passed to the proxy
244244 * @returns Proxy object with handlers for `ws` and `web` requests
245245 */
246- static createProxyServer ( options ?: ServerOptions ) : ProxyServer {
247- return new ProxyServer ( options ) ;
246+ static createProxyServer < TIncomingMessage , TServerResponse , TError > ( options ?: ServerOptions ) : ProxyServer < TIncomingMessage , TServerResponse , TError > {
247+ return new ProxyServer < TIncomingMessage , TServerResponse , TError > ( options ) ;
248248 }
249249
250250 /**
251251 * Creates the proxy server with specified options.
252252 * @param options Config object passed to the proxy
253253 * @returns Proxy object with handlers for `ws` and `web` requests
254254 */
255- static createServer ( options ?: ServerOptions ) : ProxyServer {
256- return new ProxyServer ( options ) ;
255+ static createServer < TIncomingMessage , TServerResponse , TError > ( options ?: ServerOptions ) : ProxyServer < TIncomingMessage , TServerResponse , TError > {
256+ return new ProxyServer < TIncomingMessage , TServerResponse , TError > ( options ) ;
257257 }
258258
259259 /**
260260 * Creates the proxy server with specified options.
261261 * @param options Config object passed to the proxy
262262 * @returns Proxy object with handlers for `ws` and `web` requests
263263 */
264- static createProxy ( options ?: ServerOptions ) : ProxyServer {
265- return new ProxyServer ( options ) ;
264+ static createProxy < TIncomingMessage , TServerResponse , TError > ( options ?: ServerOptions ) : ProxyServer < TIncomingMessage , TServerResponse , TError > {
265+ return new ProxyServer < TIncomingMessage , TServerResponse , TError > ( options ) ;
266266 }
267267
268268 // createRightProxy - Returns a function that when called creates the loader for
269269 // either `ws` or `web`'s passes.
270270 createRightProxy = < PT extends ProxyType > ( type : PT ) : Function => {
271271 log ( "createRightProxy" , { type } ) ;
272272 return ( options : ServerOptions ) => {
273- return ( ...args : ProxyMethodArgs [ PT ] /* req, res, [head], [opts] */ ) => {
273+ return ( ...args : ProxyMethodArgs < TIncomingMessage , TServerResponse , TError > [ PT ] /* req, res, [head], [opts] */ ) => {
274274 const req = args [ 0 ] ;
275- log ( "proxy: " , { type, path : req . url } ) ;
275+ log ( "proxy: " , { type, path : ( req as http . IncomingMessage ) . url } ) ;
276276 const res = args [ 1 ] ;
277277 const passes = type === "ws" ? this . wsPasses : this . webPasses ;
278278 if ( type == "ws" ) {
@@ -284,13 +284,13 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
284284 // and there's no way for a user of http-proxy-3 to get ahold
285285 // of this res object and attach their own error handler until
286286 // after the passes. So we better attach one ASAP right here:
287- ( res as net . Socket ) . on ( "error" , ( err ) => {
287+ ( res as net . Socket ) . on ( "error" , ( err : TError ) => {
288288 this . emit ( "error" , err , req , res ) ;
289289 } ) ;
290290 }
291291 let counter = args . length - 1 ;
292292 let head : Buffer | undefined ;
293- let cb : ErrorCallback | undefined ;
293+ let cb : ErrorCallback < TIncomingMessage , TServerResponse , TError > | undefined ;
294294
295295 // optional args parse begin
296296 if ( typeof args [ counter ] === "function" ) {
@@ -318,7 +318,7 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
318318 }
319319
320320 if ( ! requestOptions . target && ! requestOptions . forward ) {
321- this . emit ( "error" , new Error ( "Must set target or forward" ) , req , res ) ;
321+ this . emit ( "error" , new Error ( "Must set target or forward" ) as TError , req , res ) ;
322322 return ;
323323 }
324324
@@ -340,7 +340,7 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
340340 } ;
341341 } ;
342342
343- onError = ( err : Error ) => {
343+ onError = ( err : TError ) => {
344344 // Force people to handle their own errors
345345 if ( this . listeners ( "error" ) . length === 1 ) {
346346 throw err ;
@@ -356,11 +356,11 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
356356 log ( "listen" , { port, hostname } ) ;
357357
358358 this . _server = this . options . ssl
359- ? https . createServer ( this . options . ssl , this . web )
360- : http . createServer ( this . web ) ;
359+ ? https . createServer < TIncomingMessage , TServerResponse > ( this . options . ssl , this . web )
360+ : http . createServer < TIncomingMessage , TServerResponse > ( this . web ) ;
361361
362362 if ( this . options . ws ) {
363- this . _server . on ( "upgrade" , ( req , socket , head ) => {
363+ this . _server . on ( "upgrade" , ( req : TIncomingMessage , socket , head ) => {
364364 this . ws ( req , socket , head ) ;
365365 } ) ;
366366 }
@@ -390,11 +390,11 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
390390 } ) ;
391391 } ;
392392
393- before = < PT extends ProxyType > ( type : PT , passName : string , cb : PassFunctions [ PT ] ) => {
393+ before = < PT extends ProxyType > ( type : PT , passName : string , cb : PassFunctions < TIncomingMessage , TServerResponse , TError > [ PT ] ) => {
394394 if ( type !== "ws" && type !== "web" ) {
395395 throw new Error ( "type must be `web` or `ws`" ) ;
396396 }
397- const passes = ( type === "ws" ? this . wsPasses : this . webPasses ) as PassFunctions [ PT ] [ ] ;
397+ const passes = ( type === "ws" ? this . wsPasses : this . webPasses ) as PassFunctions < TIncomingMessage , TServerResponse , TError > [ PT ] [ ] ;
398398 let i : false | number = false ;
399399
400400 passes . forEach ( ( v , idx ) => {
@@ -410,11 +410,11 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
410410 passes . splice ( i , 0 , cb ) ;
411411 } ;
412412
413- after = < PT extends ProxyType > ( type : PT , passName : string , cb : PassFunctions [ PT ] ) => {
413+ after = < PT extends ProxyType > ( type : PT , passName : string , cb : PassFunctions < TIncomingMessage , TServerResponse , TError > [ PT ] ) => {
414414 if ( type !== "ws" && type !== "web" ) {
415415 throw new Error ( "type must be `web` or `ws`" ) ;
416416 }
417- const passes = ( type === "ws" ? this . wsPasses : this . webPasses ) as PassFunctions [ PT ] [ ] ;
417+ const passes = ( type === "ws" ? this . wsPasses : this . webPasses ) as PassFunctions < TIncomingMessage , TServerResponse , TError > [ PT ] [ ] ;
418418 let i : false | number = false ;
419419
420420 passes . forEach ( ( v , idx ) => {
0 commit comments