diff --git a/packages/google-cloud-node/src/helpers.js b/packages/google-cloud-node/src/helpers.js index f0df844bcdef..a499d7b245e0 100644 --- a/packages/google-cloud-node/src/helpers.js +++ b/packages/google-cloud-node/src/helpers.js @@ -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. @@ -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; } diff --git a/packages/google-cloud-node/test/helpers.test.js b/packages/google-cloud-node/test/helpers.test.js index 12295aae2a31..adc67639eb01 100644 --- a/packages/google-cloud-node/test/helpers.test.js +++ b/packages/google-cloud-node/test/helpers.test.js @@ -102,8 +102,6 @@ describe('Speech helper methods', () => { done(); }); - userStream.emit('writing'); - requestStream.emit('error', error); });