@@ -576,7 +576,7 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
576576
577577 var onResponse = function ( res ) {
578578
579- res . _buffer = '' ;
579+ res . _buffer = null ;
580580 res . _bufferlength = 0 ;
581581
582582 // We have redirect
@@ -588,16 +588,21 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
588588
589589 res . on ( 'data' , function ( chunk ) {
590590 var self = this ;
591- self . _buffer += chunk . toString ( encoding ) ;
591+ if ( self . _buffer )
592+ self . _buffer = Buffer . concat ( [ self . _buffer , chunk ] ) ;
593+ else
594+ self . _buffer = chunk ;
592595 self . _bufferlength += chunk . length ;
593596 e . emit ( 'data' , chunk , responseLength ? ( self . _bufferlength / responseLength ) * 100 : 0 ) ;
594597 } ) ;
595598
596599 res . on ( 'end' , function ( ) {
597600 var self = this ;
598- e . emit ( 'end' , self . _buffer , self . statusCode , self . headers , uri . host ) ;
601+ var str = self . _buffer . toString ( encoding ) ;
602+ delete self . _buffer ; // Free memory
603+ e . emit ( 'end' , str , self . statusCode , self . headers , uri . host ) ;
599604 if ( callback )
600- callback ( null , self . _buffer , self . statusCode , self . headers , uri . host ) ;
605+ callback ( null , str , self . statusCode , self . headers , uri . host ) ;
601606 callback = null ;
602607 } ) ;
603608
0 commit comments