Skip to content

Commit 2e351b5

Browse files
committed
Merge pull request #1014 from stephenplusplus/spp--storage-1013
core: propagate write stream response events
2 parents 5547472 + 763762c commit 2e351b5

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

lib/common/util.js

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ function makeWritableStream(dup, options, onComplete) {
237237
return;
238238
}
239239

240+
dup.emit('response', resp);
240241
onComplete(data);
241242
});
242243
});

lib/storage/file.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ File.prototype.createWriteStream = function(options) {
795795
}
796796
});
797797

798+
fileWriteStream.on('response', stream.emit.bind(stream, 'response'));
799+
798800
// This is to preserve the `finish` event. We wait until the request stream
799801
// emits "complete", as that is when we do validation of the data. After that
800802
// is successful, we can allow the stream to naturally finish.
@@ -1435,10 +1437,11 @@ File.prototype.startResumableUpload_ = function(dup, metadata) {
14351437
});
14361438

14371439
uploadStream
1438-
.on('response', function(resp, metadata) {
1439-
if (metadata) {
1440-
self.metadata = metadata;
1441-
}
1440+
.on('response', function(resp) {
1441+
dup.emit('response', resp);
1442+
})
1443+
.on('metadata', function(metadata) {
1444+
self.metadata = metadata;
14421445
})
14431446
.on('finish', function() {
14441447
dup.emit('complete');

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"duplexify": "^3.2.0",
9191
"extend": "^3.0.0",
9292
"gce-images": "^0.2.0",
93-
"gcs-resumable-upload": "^0.2.1",
93+
"gcs-resumable-upload": "^0.3.0",
9494
"google-auto-auth": "^0.2.0",
9595
"hash-stream-validation": "^0.1.0",
9696
"is": "^3.0.1",

test/common/util.js

+29
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,35 @@ describe('common/util', function() {
496496
});
497497
});
498498

499+
it('should emit the response', function(done) {
500+
var dup = duplexify();
501+
var fakeStream = new stream.Writable();
502+
var fakeResponse = {};
503+
504+
fakeStream.write = function() {};
505+
506+
utilOverrides.handleResp = function(err, res, body, callback) {
507+
callback();
508+
};
509+
510+
requestOverride = function(reqOpts, callback) {
511+
callback(null, fakeResponse);
512+
};
513+
514+
var options = {
515+
makeAuthenticatedRequest: function(request, opts) {
516+
opts.onAuthenticated();
517+
}
518+
};
519+
520+
dup.on('response', function(resp) {
521+
assert.strictEqual(resp, fakeResponse);
522+
done();
523+
});
524+
525+
util.makeWritableStream(dup, options, util.noop);
526+
});
527+
499528
it('should pass back the response data to the callback', function(done) {
500529
var dup = duplexify();
501530
var fakeStream = new stream.Writable();

test/storage/file.js

+37-3
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,22 @@ describe('File', function() {
991991
writable.write('data');
992992
});
993993

994+
it('should re-emit response event', function(done) {
995+
var writable = file.createWriteStream();
996+
var resp = {};
997+
998+
file.startResumableUpload_ = function(stream) {
999+
stream.emit('response', resp);
1000+
};
1001+
1002+
writable.on('response', function(resp_) {
1003+
assert.strictEqual(resp_, resp);
1004+
done();
1005+
});
1006+
1007+
writable.write('data');
1008+
});
1009+
9941010
it('should cork data on prefinish', function(done) {
9951011
var writable = file.createWriteStream();
9961012

@@ -2020,13 +2036,32 @@ describe('File', function() {
20202036
file.startResumableUpload_(duplexify(), metadata);
20212037
});
20222038

2023-
it('should set the metadata from the response', function(done) {
2039+
it('should emit the response', function(done) {
2040+
var resp = {};
2041+
var uploadStream = through();
2042+
2043+
resumableUploadOverride = function() {
2044+
setImmediate(function() {
2045+
uploadStream.emit('response', resp);
2046+
});
2047+
return uploadStream;
2048+
};
2049+
2050+
uploadStream.on('response', function(resp_) {
2051+
assert.strictEqual(resp_, resp);
2052+
done();
2053+
});
2054+
2055+
file.startResumableUpload_(duplexify());
2056+
});
2057+
2058+
it('should set the metadata from the metadata event', function(done) {
20242059
var metadata = {};
20252060
var uploadStream = through();
20262061

20272062
resumableUploadOverride = function() {
20282063
setImmediate(function() {
2029-
uploadStream.emit('response', null, metadata);
2064+
uploadStream.emit('metadata', metadata);
20302065

20312066
setImmediate(function() {
20322067
assert.strictEqual(file.metadata, metadata);
@@ -2036,7 +2071,6 @@ describe('File', function() {
20362071
return uploadStream;
20372072
};
20382073

2039-
20402074
file.startResumableUpload_(duplexify());
20412075
});
20422076

0 commit comments

Comments
 (0)