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

speech: add a sink to requestStream so events are not dropped #2461

Merged
Merged
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
29 changes: 19 additions & 10 deletions packages/speech/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ module.exports = () => {
* for the details.
* @returns {Stream}
* An object stream which is both readable and writable. It accepts
* [StreamingRecognizeRequest]{@link StreamingRecognizeRequest}-like
* objects for the write() method, and will emit objects representing
* raw audio for the write() method, and will emit objects representing
* [StreamingRecognizeResponse]{@link StreamingRecognizeResponse} on the
* 'data' event asynchronously.
*
Expand Down Expand Up @@ -80,29 +79,39 @@ module.exports = () => {
// Format the audio content as input request for pipeline
var recognizeStream = streamEvents(pumpify.obj());

recognizeStream.once('writing', function() {
requestStream.on('error', function(err) {
// 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', () => {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

requestStream.on('error', err => {
recognizeStream.destroy(err);
});

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

// Write the initial configuration to the stream,
// Write the initial configuration to the stream.
requestStream.write({
streamingConfig: config
});

this.setPipeline([
// Set up appropriate piping between the stream returned by
// the underlying API method and the one that we return.
recognizeStream.setPipeline([
// Format the user's input.
through.obj(function(obj, _, next) {
// This entails that the user sends raw audio; it is wrapped in
// the appropriate request structure.
through.obj((obj, _, next) => {
next(null, {
audioContent: obj
});
}),

requestStream
requestStream,
through.obj()
]);
});

Expand Down