From 9a41d888d1882fb7672259ee49a354ced00e3bcf Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 31 Jul 2015 16:00:06 -0400 Subject: [PATCH] storage: file.createReadStream: only send data when non-error --- lib/storage/file.js | 10 +++++++++- system-test/storage.js | 15 +++++++++++++++ test/storage/file.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/storage/file.js b/lib/storage/file.js index 833ff6dc0aa..8f2cdd8da3d 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -536,7 +536,15 @@ File.prototype.createReadStream = function(options) { requestStream .on('error', endThroughStream) - .on('response', throughStream.emit.bind(throughStream, 'response')) + .on('response', function(incomingMessage) { + throughStream.emit('response', incomingMessage); + + util.handleResp(null, incomingMessage, null, function(err) { + if (err) { + requestStream.unpipe(throughStream); + } + }); + }) .on('complete', function(res) { util.handleResp(null, res, null, function(err) { diff --git a/system-test/storage.js b/system-test/storage.js index 9ef6b3e82f3..3c49df281eb 100644 --- a/system-test/storage.js +++ b/system-test/storage.js @@ -491,6 +491,21 @@ describe('storage', function() { }); }); + it('should not push data when a file cannot be read', function(done) { + var file = bucket.file('non-existing-file'); + var dataEmitted = false; + + file.createReadStream() + .on('data', function() { + dataEmitted = true; + }) + .on('error', function(err) { + assert.strictEqual(dataEmitted, false); + assert.strictEqual(err.code, 404); + done(); + }); + }); + it('should read a byte range from a file', function(done) { bucket.upload(files.big.path, function(err, file) { assert.ifError(err); diff --git a/test/storage/file.js b/test/storage/file.js index 7067f4feb30..01131170508 100644 --- a/test/storage/file.js +++ b/test/storage/file.js @@ -585,6 +585,35 @@ describe('File', function() { }); }); + it('should unpipe stream from an error on the response', function(done) { + var requestStream = through(); + + file.bucket.storage.makeAuthorizedRequest_ = function() { + setImmediate(function() { + // Must be a stream. Doesn't matter for the tests, though. + requestStream.emit('response', through()); + }); + + return requestStream; + }; + + handleRespOverride = function(err, resp, body, callback) { + assert.strictEqual(requestStream._readableState.pipesCount, 1); + assert.strictEqual(requestStream._readableState.pipes, readStream); + + // Triggers the unpipe. + callback(new Error()); + + setImmediate(function() { + assert.strictEqual(requestStream._readableState.pipesCount, 0); + assert.strictEqual(requestStream._readableState.pipes, null); + done(); + }); + }; + + var readStream = file.createReadStream(); + }); + it('should let util.handleResp handle the response', function(done) { var response = { a: 'b', c: 'd' };