Skip to content

Commit

Permalink
fix: Register error event handler immediately. (#156)
Browse files Browse the repository at this point in the history
* fix: Register error event handler immediately.

* Lint attempt
  • Loading branch information
stephenplusplus authored Sep 11, 2018
1 parent e692b30 commit 60b2171
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
24 changes: 11 additions & 13 deletions packages/google-cloud-node/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,26 @@ module.exports = () => {
options = {};
}

var requestStream = this._innerApiCalls.streamingRecognize(options);

// Format the audio content as input request for pipeline
var recognizeStream = streamEvents(pumpify.obj());

var requestStream = this._innerApiCalls
.streamingRecognize(options)
.on('error', err => {
recognizeStream.destroy(err);
})
.on('response', response => {
recognizeStream.emit('response', response);
});

// Attach the events to the request stream, but only do so
// when the first write (of data) comes in.
//
// This also means that the sending of the initial request (with the
// config) is delayed until we get the first burst of data.
recognizeStream.once('writing', () => {
requestStream.on('error', err => {
recognizeStream.destroy(err);
});

// Responses must be explicitly forwarded.
requestStream.on('response', response => {
recognizeStream.emit('response', response);
});

// The first message should contain the streaming config.
let first_message = true;
let firstMessage = true;

// Set up appropriate piping between the stream returned by
// the underlying API method and the one that we return.
Expand All @@ -98,7 +96,7 @@ module.exports = () => {
// the appropriate request structure.
through.obj((obj, _, next) => {
let payload = {};
if (first_message && config !== undefined) {
if (firstMessage && config !== undefined) {
// Write the initial configuration to the stream.
payload.streamingConfig = config;
}
Expand Down
2 changes: 0 additions & 2 deletions packages/google-cloud-node/test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ describe('Speech helper methods', () => {
done();
});

userStream.emit('writing');

requestStream.emit('error', error);
});

Expand Down

0 comments on commit 60b2171

Please sign in to comment.