Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: propagate write stream response events #1014

Merged
merged 1 commit into from
Dec 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ function makeWritableStream(dup, options, onComplete) {
return;
}

dup.emit('response', resp);
onComplete(data);
});
});
Expand Down
11 changes: 7 additions & 4 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
40 changes: 37 additions & 3 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -2036,7 +2071,6 @@ describe('File', function() {
return uploadStream;
};


file.startResumableUpload_(duplexify());
});

Expand Down