From 763762c9ec352dcae1edc3f2c5c90a99f639aea7 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Thu, 10 Dec 2015 14:03:38 -0500 Subject: [PATCH] coe: propagate write stream response events --- lib/common/util.js | 1 + lib/storage/file.js | 11 +++++++---- package.json | 2 +- test/common/util.js | 29 +++++++++++++++++++++++++++++ test/storage/file.js | 40 +++++++++++++++++++++++++++++++++++++--- 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/lib/common/util.js b/lib/common/util.js index 32746627669..37a7ce00018 100644 --- a/lib/common/util.js +++ b/lib/common/util.js @@ -237,6 +237,7 @@ function makeWritableStream(dup, options, onComplete) { return; } + dup.emit('response', resp); onComplete(data); }); }); diff --git a/lib/storage/file.js b/lib/storage/file.js index bd288e29df4..6a519ff4b6d 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -795,6 +795,8 @@ File.prototype.createWriteStream = function(options) { } }); + fileWriteStream.on('response', stream.emit.bind(stream, 'response')); + // This is to preserve the `finish` event. We wait until the request stream // emits "complete", as that is when we do validation of the data. After that // is successful, we can allow the stream to naturally finish. @@ -1435,10 +1437,11 @@ File.prototype.startResumableUpload_ = function(dup, metadata) { }); uploadStream - .on('response', function(resp, metadata) { - if (metadata) { - self.metadata = metadata; - } + .on('response', function(resp) { + dup.emit('response', resp); + }) + .on('metadata', function(metadata) { + self.metadata = metadata; }) .on('finish', function() { dup.emit('complete'); diff --git a/package.json b/package.json index 6ea324e7d4f..5e80f17cb4e 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "duplexify": "^3.2.0", "extend": "^3.0.0", "gce-images": "^0.2.0", - "gcs-resumable-upload": "^0.2.1", + "gcs-resumable-upload": "^0.3.0", "google-auto-auth": "^0.2.0", "hash-stream-validation": "^0.1.0", "is": "^3.0.1", diff --git a/test/common/util.js b/test/common/util.js index 83ad03cf2b7..04c23e277ac 100644 --- a/test/common/util.js +++ b/test/common/util.js @@ -496,6 +496,35 @@ describe('common/util', function() { }); }); + it('should emit the response', function(done) { + var dup = duplexify(); + var fakeStream = new stream.Writable(); + var fakeResponse = {}; + + fakeStream.write = function() {}; + + utilOverrides.handleResp = function(err, res, body, callback) { + callback(); + }; + + requestOverride = function(reqOpts, callback) { + callback(null, fakeResponse); + }; + + var options = { + makeAuthenticatedRequest: function(request, opts) { + opts.onAuthenticated(); + } + }; + + dup.on('response', function(resp) { + assert.strictEqual(resp, fakeResponse); + done(); + }); + + util.makeWritableStream(dup, options, util.noop); + }); + it('should pass back the response data to the callback', function(done) { var dup = duplexify(); var fakeStream = new stream.Writable(); diff --git a/test/storage/file.js b/test/storage/file.js index 10d1f91a7ba..600849916c7 100644 --- a/test/storage/file.js +++ b/test/storage/file.js @@ -991,6 +991,22 @@ describe('File', function() { writable.write('data'); }); + it('should re-emit response event', function(done) { + var writable = file.createWriteStream(); + var resp = {}; + + file.startResumableUpload_ = function(stream) { + stream.emit('response', resp); + }; + + writable.on('response', function(resp_) { + assert.strictEqual(resp_, resp); + done(); + }); + + writable.write('data'); + }); + it('should cork data on prefinish', function(done) { var writable = file.createWriteStream(); @@ -2020,13 +2036,32 @@ describe('File', function() { file.startResumableUpload_(duplexify(), metadata); }); - it('should set the metadata from the response', function(done) { + it('should emit the response', function(done) { + var resp = {}; + var uploadStream = through(); + + resumableUploadOverride = function() { + setImmediate(function() { + uploadStream.emit('response', resp); + }); + return uploadStream; + }; + + uploadStream.on('response', function(resp_) { + assert.strictEqual(resp_, resp); + done(); + }); + + file.startResumableUpload_(duplexify()); + }); + + it('should set the metadata from the metadata event', function(done) { var metadata = {}; var uploadStream = through(); resumableUploadOverride = function() { setImmediate(function() { - uploadStream.emit('response', null, metadata); + uploadStream.emit('metadata', metadata); setImmediate(function() { assert.strictEqual(file.metadata, metadata); @@ -2036,7 +2071,6 @@ describe('File', function() { return uploadStream; }; - file.startResumableUpload_(duplexify()); });