@@ -218,46 +218,24 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
218
218
return ;
219
219
}
220
220
221
- //
222
- // For each data `chunk` received from the `reverseProxy`
223
- // `response` write it to the outgoing `res`.
224
- // If the res socket has been killed already, then write()
225
- // will throw. Nevertheless, try our best to end it nicely.
226
- //
227
- var paused = false ;
228
- response . on ( 'data' , function ( chunk ) {
229
- if ( req . method !== 'HEAD' && res . writable ) {
230
- try {
231
- var flushed = res . write ( chunk ) ;
232
- }
233
- catch ( ex ) {
234
- console . error ( "res.write error: %s" , ex . message ) ;
235
-
236
- try { res . end ( ) }
237
- catch ( ex ) { console . error ( "res.end error: %s" , ex . message ) }
238
-
239
- return ;
240
- }
241
-
242
- if ( ! flushed && ! paused ) {
243
- paused = true ;
244
- response . pause ( ) ;
245
- res . once ( 'drain' , function ( ) {
246
- paused = false ;
247
- try { response . resume ( ) }
248
- catch ( er ) { console . error ( "response.resume error: %s" , er . message ) }
249
- } ) ;
250
-
251
- //
252
- // Force the `drain` event in 100ms if it hasn't
253
- // happened on its own.
254
- //
255
- setTimeout ( function ( ) {
256
- res . emit ( 'drain' ) ;
257
- } , 100 ) ;
221
+
222
+ function ondata ( chunk ) {
223
+ if ( res . writable ) {
224
+ if ( false === res . write ( chunk ) && response . pause ) {
225
+ response . pause ( ) ;
226
+ }
258
227
}
259
228
}
260
- } ) ;
229
+
230
+ response . on ( 'data' , ondata ) ;
231
+
232
+ function ondrain ( ) {
233
+ if ( response . readable && response . resume ) {
234
+ response . resume ( ) ;
235
+ }
236
+ }
237
+
238
+ res . on ( 'drain' , ondrain ) ;
261
239
262
240
//
263
241
// When the `reverseProxy` `response` ends, end the
0 commit comments