@@ -301,9 +301,10 @@ File.prototype.createReadStream = function(options) {
301
301
util . is ( options . start , 'number' ) || util . is ( options . end , 'number' ) ;
302
302
var throughStream = streamEvents ( through ( ) ) ;
303
303
304
+ var requestStream ;
305
+
304
306
var validations = [ 'crc32c' , 'md5' ] ;
305
307
var validation ;
306
- var socket ;
307
308
308
309
if ( util . is ( options . validation , 'string' ) ) {
309
310
options . validation = options . validation . toLowerCase ( ) ;
@@ -333,6 +334,22 @@ File.prototype.createReadStream = function(options) {
333
334
334
335
createAuthorizedReq ( remoteFilePath ) ;
335
336
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
+
336
353
return throughStream ;
337
354
338
355
// Authenticate the request, then pipe the remote API request to the stream
@@ -359,7 +376,7 @@ File.prototype.createReadStream = function(options) {
359
376
that . bucket . storage . makeAuthorizedRequest_ ( reqOpts , {
360
377
onAuthorized : function ( err , authorizedReqOpts ) {
361
378
if ( err ) {
362
- done ( err , null ) ;
379
+ endThroughStream ( err , null ) ;
363
380
return ;
364
381
}
365
382
@@ -368,8 +385,13 @@ File.prototype.createReadStream = function(options) {
368
385
var localCrcHash ;
369
386
var localMd5Hash = crypto . createHash ( 'md5' ) ;
370
387
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
+ } )
373
395
374
396
. on ( 'data' , function ( chunk ) {
375
397
if ( crc32c ) {
@@ -381,20 +403,16 @@ File.prototype.createReadStream = function(options) {
381
403
}
382
404
} )
383
405
384
- . on ( 'socket' , function ( s ) {
385
- socket = s ;
386
- } )
387
-
388
406
. on ( 'complete' , function ( res ) {
389
407
util . handleResp ( null , res , res . body , function ( err , resp ) {
390
408
if ( err ) {
391
- done ( err , resp ) ;
409
+ endThroughStream ( err , resp ) ;
392
410
return ;
393
411
}
394
412
395
413
if ( rangeRequest ) {
396
414
// Range requests can't receive data integrity checks.
397
- done ( null , resp ) ;
415
+ endThroughStream ( null , resp ) ;
398
416
return ;
399
417
}
400
418
@@ -434,34 +452,19 @@ File.prototype.createReadStream = function(options) {
434
452
] . join ( ' ' ) ) ;
435
453
mismatchError . code = 'CONTENT_DOWNLOAD_MISMATCH' ;
436
454
437
- done ( mismatchError , resp ) ;
455
+ endThroughStream ( mismatchError , resp ) ;
438
456
} else {
439
- done ( null , resp ) ;
457
+ endThroughStream ( null , resp ) ;
440
458
}
441
459
} ) ;
442
460
} )
443
461
444
- . pipe ( throughStream ) ;
462
+ . pipe ( throughStream )
445
463
446
- throughStream . on ( 'error' , function ( ) {
447
- if ( socket ) {
448
- socket . destroy ( ) ;
449
- }
450
- } ) ;
464
+ . on ( 'error' , endRequestStream ) ;
451
465
}
452
466
} ) ;
453
467
}
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
- }
465
468
} ;
466
469
467
470
/**
0 commit comments