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

refactor: use duplexify within StreamProxy #197

Merged
merged 2 commits into from
Mar 9, 2018
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
67 changes: 9 additions & 58 deletions lib/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/* This file describes the gRPC-streaming. */

var util = require('util');
var DuplexStream = require('readable-stream').Duplex;
var Duplexify = require('duplexify');

/**
* The type of gRPC streaming.
Expand Down Expand Up @@ -61,50 +61,17 @@ exports.StreamType = StreamType;
* @param {ApiCallback} callback - the callback for further API call.
*/
function StreamProxy(type, callback) {
DuplexStream.call(this, {
Duplexify.call(this, null, null, {
objectMode: true,
readable: type !== StreamType.CLIENT_STREAMING,
writable: type !== StreamType.SERVER_STREAMING,
});
this.type = type;
this._callback = callback;
this._writeQueue = [];
this._isEndCalled = false;
this._isCancelCalled = false;
var self = this;
this.on('finish', function() {
self._onFinish();
});
}

util.inherits(StreamProxy, DuplexStream);

StreamProxy.prototype._read = function() {
if (this.type === StreamType.CLIENT_STREAMING) {
this.emit('error', new Error('stream is not readable'));
}
};

StreamProxy.prototype._write = function(chunk, encoding, callback) {
if (this.type === StreamType.SERVER_STREAMING) {
this.emit('error', new Error('stream is not writable'));
return;
}
if (this.stream) {
this.stream.write(chunk);
} else {
this._writeQueue.push(chunk);
}
callback();
};

StreamProxy.prototype._onFinish = function() {
if (this.stream) {
this.stream.end();
} else {
this._isEndCalled = true;
}
};
util.inherits(StreamProxy, Duplexify);

StreamProxy.prototype.cancel = function() {
if (this.stream) {
Expand All @@ -123,7 +90,7 @@ StreamProxy.prototype.setStream = function(apiCall, argument) {
var stream = apiCall(argument, this._callback);
this.stream = stream;
var self = this;
['error', 'metadata', 'status'].forEach(function(event) {
['metadata', 'status'].forEach(function(event) {
stream.on(event, function() {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(event);
Expand All @@ -146,29 +113,13 @@ StreamProxy.prototype.setStream = function(apiCall, argument) {
});
});
if (this.type !== StreamType.CLIENT_STREAMING) {
stream.on('data', function() {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift('data');
self.emit.apply(self, args);
});
// Pushing null causes an ending process of the readable stream.
stream.on('end', function() {
self.push(null);
});
// This is required in case no 'data' handler exists.
this.resume();
this.setReadable(stream);
}
if (this.type !== StreamType.SERVER_STREAMING) {
this._writeQueue.forEach(function(data) {
stream.write(data);
});
this._writeQueue = [];
if (this._isEndCalled) {
stream.end();
}
if (this._isCancelCalled) {
stream.cancel();
}
this.setWritable(stream);
}
if (this._isCancelCalled) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

stream.cancel();
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"lib"
],
"dependencies": {
"duplexify": "^3.5.4",
"extend": "^3.0.0",
"globby": "^8.0.0",
"google-auto-auth": "^0.9.0",
Expand All @@ -15,7 +16,6 @@
"is-stream-ended": "^0.1.0",
"lodash": "^4.17.2",
"protobufjs": "^6.8.0",
"readable-stream": "^2.2.2",
"through2": "^2.0.3"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions test/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('streaming', function() {
setTimeout(function() {
s.emit('metadata', responseMetadata);
}, 10);
s.on('end', function() {
s.on('finish', function() {
s.emit('status', status);
});
return s;
Expand All @@ -165,7 +165,7 @@ describe('streaming', function() {
s.on('response', function(data) {
receivedResponse = data;
});
s.on('end', function() {
s.on('finish', function() {
expect(receivedMetadata).to.deep.eq(responseMetadata);
expect(receivedStatus).to.deep.eq(status);
expect(receivedResponse).to.deep.eq(expectedResponse);
Expand Down