@@ -301,9 +301,10 @@ File.prototype.createReadStream = function(options) {
301301 util . is ( options . start , 'number' ) || util . is ( options . end , 'number' ) ;
302302 var throughStream = streamEvents ( through ( ) ) ;
303303
304+ var requestStream ;
305+
304306 var validations = [ 'crc32c' , 'md5' ] ;
305307 var validation ;
306- var socket ;
307308
308309 if ( util . is ( options . validation , 'string' ) ) {
309310 options . validation = options . validation . toLowerCase ( ) ;
@@ -333,6 +334,22 @@ File.prototype.createReadStream = function(options) {
333334
334335 createAuthorizedReq ( remoteFilePath ) ;
335336
337+ // End the stream, first emitting an error or complete event.
338+ var endThroughStream = once ( function ( err , resp ) {
339+ if ( err ) {
340+ throughStream . emit ( 'error' , err , resp ) ;
341+ } else {
342+ throughStream . emit ( 'complete' , resp ) ;
343+ }
344+
345+ throughStream . destroy ( ) ;
346+ } ) ;
347+
348+ var endRequestStream = once ( function ( ) {
349+ requestStream . abort ( ) ;
350+ requestStream . destroy ( ) ;
351+ } ) ;
352+
336353 return throughStream ;
337354
338355 // Authenticate the request, then pipe the remote API request to the stream
@@ -359,7 +376,7 @@ File.prototype.createReadStream = function(options) {
359376 that . bucket . storage . makeAuthorizedRequest_ ( reqOpts , {
360377 onAuthorized : function ( err , authorizedReqOpts ) {
361378 if ( err ) {
362- done ( err , null ) ;
379+ endThroughStream ( err , null ) ;
363380 return ;
364381 }
365382
@@ -368,8 +385,13 @@ File.prototype.createReadStream = function(options) {
368385 var localCrcHash ;
369386 var localMd5Hash = crypto . createHash ( 'md5' ) ;
370387
371- request ( authorizedReqOpts )
372- . on ( 'error' , done )
388+ requestStream = request ( authorizedReqOpts ) ;
389+
390+ requestStream
391+ . on ( 'error' , function ( err ) {
392+ endRequestStream ( ) ;
393+ endThroughStream ( err ) ;
394+ } )
373395
374396 . on ( 'data' , function ( chunk ) {
375397 if ( crc32c ) {
@@ -381,20 +403,16 @@ File.prototype.createReadStream = function(options) {
381403 }
382404 } )
383405
384- . on ( 'socket' , function ( s ) {
385- socket = s ;
386- } )
387-
388406 . on ( 'complete' , function ( res ) {
389407 util . handleResp ( null , res , res . body , function ( err , resp ) {
390408 if ( err ) {
391- done ( err , resp ) ;
409+ endThroughStream ( err , resp ) ;
392410 return ;
393411 }
394412
395413 if ( rangeRequest ) {
396414 // Range requests can't receive data integrity checks.
397- done ( null , resp ) ;
415+ endThroughStream ( null , resp ) ;
398416 return ;
399417 }
400418
@@ -434,34 +452,19 @@ File.prototype.createReadStream = function(options) {
434452 ] . join ( ' ' ) ) ;
435453 mismatchError . code = 'CONTENT_DOWNLOAD_MISMATCH' ;
436454
437- done ( mismatchError , resp ) ;
455+ endThroughStream ( mismatchError , resp ) ;
438456 } else {
439- done ( null , resp ) ;
457+ endThroughStream ( null , resp ) ;
440458 }
441459 } ) ;
442460 } )
443461
444- . pipe ( throughStream ) ;
462+ . pipe ( throughStream )
445463
446- throughStream . on ( 'error' , function ( ) {
447- if ( socket ) {
448- socket . destroy ( ) ;
449- }
450- } ) ;
464+ . on ( 'error' , endRequestStream ) ;
451465 }
452466 } ) ;
453467 }
454-
455- // End the stream, first emitting an error or complete event.
456- function done ( err ) {
457- if ( err ) {
458- throughStream . emit ( 'error' , err ) ;
459- } else {
460- throughStream . emit ( 'complete' ) ;
461- }
462-
463- throughStream . end ( ) ;
464- }
465468} ;
466469
467470/**
0 commit comments