@@ -99,128 +99,128 @@ export interface NormalizedServerOptions extends ServerOptions {
99
99
forward ?: NormalizeProxyTarget < ProxyTargetUrl > ;
100
100
}
101
101
102
- export type ErrorCallback =
102
+ export type ErrorCallback < TIncomingMessage = http . IncomingMessage , TServerResponse = http . ServerResponse , TError = Error > =
103
103
(
104
- err : Error ,
105
- req : http . IncomingMessage ,
106
- res : http . ServerResponse | net . Socket ,
104
+ err : TError ,
105
+ req : TIncomingMessage ,
106
+ res : TServerResponse | net . Socket ,
107
107
target ?: ProxyTargetUrl ,
108
108
) => void ;
109
109
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 > > ;
112
112
start : [
113
- req : http . IncomingMessage ,
114
- res : http . ServerResponse ,
113
+ req : TIncomingMessage ,
114
+ res : TServerResponse ,
115
115
target : ProxyTargetUrl ,
116
116
] ;
117
117
open : [ socket : net . Socket ] ;
118
118
proxyReq : [
119
119
proxyReq : http . ClientRequest ,
120
- req : http . IncomingMessage ,
121
- res : http . ServerResponse ,
120
+ req : TIncomingMessage ,
121
+ res : TServerResponse ,
122
122
options : ServerOptions ,
123
123
socket : net . Socket ,
124
124
] ;
125
125
proxyRes : [
126
- proxyRes : http . IncomingMessage ,
127
- req : http . IncomingMessage ,
128
- res : http . ServerResponse ,
126
+ proxyRes : TIncomingMessage ,
127
+ req : TIncomingMessage ,
128
+ res : TServerResponse ,
129
129
] ;
130
130
proxyReqWs : [
131
131
proxyReq : http . ClientRequest ,
132
- req : http . IncomingMessage ,
132
+ req : TIncomingMessage ,
133
133
socket : net . Socket ,
134
134
options : ServerOptions ,
135
135
head : any ,
136
136
] ;
137
137
econnreset : [
138
138
err : Error ,
139
- req : http . IncomingMessage ,
140
- res : http . ServerResponse ,
139
+ req : TIncomingMessage ,
140
+ res : TServerResponse ,
141
141
target : ProxyTargetUrl ,
142
142
] ;
143
143
end : [
144
- req : http . IncomingMessage ,
145
- res : http . ServerResponse ,
146
- proxyRes : http . IncomingMessage ,
144
+ req : TIncomingMessage ,
145
+ res : TServerResponse ,
146
+ proxyRes : TIncomingMessage ,
147
147
] ;
148
148
close : [
149
- proxyRes : http . IncomingMessage ,
149
+ proxyRes : TIncomingMessage ,
150
150
proxySocket : net . Socket ,
151
151
proxyHead : any ,
152
152
] ;
153
153
}
154
154
155
- type ProxyMethodArgs = {
155
+ type ProxyMethodArgs < TIncomingMessage = http . IncomingMessage , TServerResponse = http . ServerResponse , TError = Error > = {
156
156
ws : [
157
- req : http . IncomingMessage ,
157
+ req : TIncomingMessage ,
158
158
socket : any ,
159
159
head : any ,
160
160
...args :
161
- [
162
- options ?: ServerOptions ,
163
- callback ?: ErrorCallback ,
164
- ]
161
+ [
162
+ options ?: ServerOptions ,
163
+ callback ?: ErrorCallback < TIncomingMessage , TServerResponse , TError > ,
164
+ ]
165
165
| [
166
- callback ?: ErrorCallback ,
167
- ]
166
+ callback ?: ErrorCallback < TIncomingMessage , TServerResponse , TError > ,
167
+ ]
168
168
]
169
169
web : [
170
- req : http . IncomingMessage ,
171
- res : http . ServerResponse ,
170
+ req : TIncomingMessage ,
171
+ res : TServerResponse ,
172
172
...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
+ ]
180
180
]
181
181
}
182
182
183
- type PassFunctions = {
183
+ type PassFunctions < TIncomingMessage = http . IncomingMessage , TServerResponse = http . ServerResponse , TError = Error > = {
184
184
ws : (
185
- req : http . IncomingMessage ,
185
+ req : TIncomingMessage ,
186
186
socket : net . Socket ,
187
187
options : NormalizedServerOptions ,
188
188
head : Buffer | undefined ,
189
- server : ProxyServer ,
190
- cb ?: ErrorCallback
189
+ server : ProxyServer < TIncomingMessage , TServerResponse , TError > ,
190
+ cb ?: ErrorCallback < TIncomingMessage , TServerResponse , TError >
191
191
) => unknown
192
192
web : (
193
- req : http . IncomingMessage ,
194
- res : http . ServerResponse ,
193
+ req : TIncomingMessage ,
194
+ res : TServerResponse ,
195
195
options : NormalizedServerOptions ,
196
196
head : Buffer | undefined ,
197
- server : ProxyServer ,
198
- cb ?: ErrorCallback
197
+ server : ProxyServer < TIncomingMessage , TServerResponse , TError > ,
198
+ cb ?: ErrorCallback < TIncomingMessage , TServerResponse , TError >
199
199
) => unknown
200
200
}
201
201
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 > > {
203
203
/**
204
204
* Used for proxying WS(S) requests
205
205
* @param req - Client request.
206
206
* @param socket - Client socket.
207
207
* @param head - Client head.
208
208
* @param options - Additional options.
209
209
*/
210
- public readonly ws : ( ...args : ProxyMethodArgs [ "ws" ] ) => void ;
210
+ public readonly ws : ( ...args : ProxyMethodArgs < TIncomingMessage , TServerResponse , TError > [ "ws" ] ) => void ;
211
211
212
212
/**
213
213
* Used for proxying regular HTTP(S) requests
214
214
* @param req - Client request.
215
215
* @param res - Client response.
216
216
* @param options - Additional options.
217
217
*/
218
- public readonly web : ( ...args : ProxyMethodArgs [ "web" ] ) => void ;
218
+ public readonly web : ( ...args : ProxyMethodArgs < TIncomingMessage , TServerResponse , TError > [ "web" ] ) => void ;
219
219
220
220
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 ;
224
224
225
225
/**
226
226
* Creates the proxy server with specified options.
@@ -233,8 +233,8 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
233
233
this . options = options ;
234
234
this . web = this . createRightProxy ( "web" ) ( options ) ;
235
235
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' ] > ;
238
238
this . on ( "error" , this . onError ) ;
239
239
}
240
240
@@ -243,36 +243,36 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
243
243
* @param options Config object passed to the proxy
244
244
* @returns Proxy object with handlers for `ws` and `web` requests
245
245
*/
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 ) ;
248
248
}
249
249
250
250
/**
251
251
* Creates the proxy server with specified options.
252
252
* @param options Config object passed to the proxy
253
253
* @returns Proxy object with handlers for `ws` and `web` requests
254
254
*/
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 ) ;
257
257
}
258
258
259
259
/**
260
260
* Creates the proxy server with specified options.
261
261
* @param options Config object passed to the proxy
262
262
* @returns Proxy object with handlers for `ws` and `web` requests
263
263
*/
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 ) ;
266
266
}
267
267
268
268
// createRightProxy - Returns a function that when called creates the loader for
269
269
// either `ws` or `web`'s passes.
270
270
createRightProxy = < PT extends ProxyType > ( type : PT ) : Function => {
271
271
log ( "createRightProxy" , { type } ) ;
272
272
return ( options : ServerOptions ) => {
273
- return ( ...args : ProxyMethodArgs [ PT ] /* req, res, [head], [opts] */ ) => {
273
+ return ( ...args : ProxyMethodArgs < TIncomingMessage , TServerResponse , TError > [ PT ] /* req, res, [head], [opts] */ ) => {
274
274
const req = args [ 0 ] ;
275
- log ( "proxy: " , { type, path : req . url } ) ;
275
+ log ( "proxy: " , { type, path : ( req as http . IncomingMessage ) . url } ) ;
276
276
const res = args [ 1 ] ;
277
277
const passes = type === "ws" ? this . wsPasses : this . webPasses ;
278
278
if ( type == "ws" ) {
@@ -284,13 +284,13 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
284
284
// and there's no way for a user of http-proxy-3 to get ahold
285
285
// of this res object and attach their own error handler until
286
286
// 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 ) => {
288
288
this . emit ( "error" , err , req , res ) ;
289
289
} ) ;
290
290
}
291
291
let counter = args . length - 1 ;
292
292
let head : Buffer | undefined ;
293
- let cb : ErrorCallback | undefined ;
293
+ let cb : ErrorCallback < TIncomingMessage , TServerResponse , TError > | undefined ;
294
294
295
295
// optional args parse begin
296
296
if ( typeof args [ counter ] === "function" ) {
@@ -318,7 +318,7 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
318
318
}
319
319
320
320
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 ) ;
322
322
return ;
323
323
}
324
324
@@ -340,7 +340,7 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
340
340
} ;
341
341
} ;
342
342
343
- onError = ( err : Error ) => {
343
+ onError = ( err : TError ) => {
344
344
// Force people to handle their own errors
345
345
if ( this . listeners ( "error" ) . length === 1 ) {
346
346
throw err ;
@@ -356,11 +356,11 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
356
356
log ( "listen" , { port, hostname } ) ;
357
357
358
358
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 ) ;
361
361
362
362
if ( this . options . ws ) {
363
- this . _server . on ( "upgrade" , ( req , socket , head ) => {
363
+ this . _server . on ( "upgrade" , ( req : TIncomingMessage , socket , head ) => {
364
364
this . ws ( req , socket , head ) ;
365
365
} ) ;
366
366
}
@@ -390,11 +390,11 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
390
390
} ) ;
391
391
} ;
392
392
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 ] ) => {
394
394
if ( type !== "ws" && type !== "web" ) {
395
395
throw new Error ( "type must be `web` or `ws`" ) ;
396
396
}
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 ] [ ] ;
398
398
let i : false | number = false ;
399
399
400
400
passes . forEach ( ( v , idx ) => {
@@ -410,11 +410,11 @@ export class ProxyServer extends EventEmitter<ProxyServerEventMap> {
410
410
passes . splice ( i , 0 , cb ) ;
411
411
} ;
412
412
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 ] ) => {
414
414
if ( type !== "ws" && type !== "web" ) {
415
415
throw new Error ( "type must be `web` or `ws`" ) ;
416
416
}
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 ] [ ] ;
418
418
let i : false | number = false ;
419
419
420
420
passes . forEach ( ( v , idx ) => {
0 commit comments