Skip to content

Commit

Permalink
fix: send streamingConfig as a separate write before audioContent (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkwlui authored and callmehiphop committed Sep 21, 2018
1 parent f291f7e commit 1e79b40
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
24 changes: 9 additions & 15 deletions packages/google-cloud-node/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ module.exports = () => {
* // Write request objects.
* stream.write(request);
*/
methods.streamingRecognize = function(config, options) {
if (options === undefined) {
options = {};
}
methods.streamingRecognize = function(streamingConfig, options) {
options = options || {};
streamingConfig = streamingConfig || {};

// Format the audio content as input request for pipeline
const recognizeStream = streamEvents(pumpify.obj());
Expand All @@ -87,26 +86,21 @@ module.exports = () => {
// config) is delayed until we get the first burst of data.
recognizeStream.once('writing', () => {
// The first message should contain the streaming config.
const firstMessage = true;
requestStream.write({streamingConfig});

// 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.
// This entails that the user sends raw audio; it is wrapped in
// the appropriate request structure.
through.obj((obj, _, next) => {
const payload = {};
if (firstMessage && config !== undefined) {
// Write the initial configuration to the stream.
payload.streamingConfig = config;
}

if (Object.keys(obj || {}).length) {
payload.audioContent = obj;
through.obj((audioContent, _, next) => {
if (audioContent !== undefined) {
next(null, {audioContent});
return;
}

next(null, payload);
next();
}),
requestStream,
through.obj((response, enc, next) => {
Expand Down
23 changes: 20 additions & 3 deletions packages/google-cloud-node/synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,29 @@
excludes=['package.json', 'README.md', 'src/index.js',]
)

templates = common_templates.node_library(package_name="@google-cloud/speech")
s.copy(templates)
# Manual helper methods overrides the streaming API so that it
# accepts streamingConfig when calling streamingRecognize. Fix
# the gapic tests to use the overridden method signature.
s.replace( f"test/gapic-{version}.js",
"(mockBidiStreamingGrpcMethod\()request",
r"\1{ streamingConfig: {} }")

s.replace(
f"test/gapic-{version}.js",
"stream\.write\(request\)",
"stream.write()")

s.replace(
f"test/gapic-{version}.js",
"// Mock request\n\s*const request = {};",
"")

templates = common_templates.node_library()
# TODO: remove excludes once var's are converted to const/let
s.copy(templates, excludes=['.eslintrc.yml'])

#
# Node.js specific cleanup
#
subprocess.run(['npm', 'install'])
subprocess.run(['npm', 'run', 'prettier'])
subprocess.run(['npm', 'run', 'lint'])
14 changes: 4 additions & 10 deletions packages/google-cloud-node/test/gapic-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,12 @@ describe('SpeechClient', () => {
projectId: 'bogus',
});

// Mock request
const request = {};

// Mock response
const expectedResponse = {};

// Mock Grpc layer
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
request,
{streamingConfig: {}},
expectedResponse
);

Expand All @@ -246,7 +243,7 @@ describe('SpeechClient', () => {
done(err);
});

stream.write(request);
stream.write();
});

it('invokes streamingRecognize with error', done => {
Expand All @@ -255,12 +252,9 @@ describe('SpeechClient', () => {
projectId: 'bogus',
});

// Mock request
const request = {};

// Mock Grpc layer
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
request,
{streamingConfig: {}},
null,
error
);
Expand All @@ -276,7 +270,7 @@ describe('SpeechClient', () => {
done();
});

stream.write(request);
stream.write();
});
});
});
Expand Down
14 changes: 4 additions & 10 deletions packages/google-cloud-node/test/gapic-v1p1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,12 @@ describe('SpeechClient', () => {
projectId: 'bogus',
});

// Mock request
const request = {};

// Mock response
const expectedResponse = {};

// Mock Grpc layer
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
request,
{streamingConfig: {}},
expectedResponse
);

Expand All @@ -246,7 +243,7 @@ describe('SpeechClient', () => {
done(err);
});

stream.write(request);
stream.write();
});

it('invokes streamingRecognize with error', done => {
Expand All @@ -255,12 +252,9 @@ describe('SpeechClient', () => {
projectId: 'bogus',
});

// Mock request
const request = {};

// Mock Grpc layer
client._innerApiCalls.streamingRecognize = mockBidiStreamingGrpcMethod(
request,
{streamingConfig: {}},
null,
error
);
Expand All @@ -276,7 +270,7 @@ describe('SpeechClient', () => {
done();
});

stream.write(request);
stream.write();
});
});
});
Expand Down
18 changes: 13 additions & 5 deletions packages/google-cloud-node/test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,27 @@ describe('Speech helper methods', () => {
// Stub the underlying _streamingRecognize method to just return
// a bogus stream.
const requestStream = new stream.PassThrough({objectMode: true});

sandbox
.stub(client._innerApiCalls, 'streamingRecognize')
.returns(requestStream);

const userStream = client.streamingRecognize(CONFIG, OPTIONS);
const audioContent = Buffer.from('audio content');

let count = 0;
requestStream._write = (data, enc, next) => {
assert.deepStrictEqual(data, {
audioContent: audioContent,
streamingConfig: CONFIG,
});
setImmediate(done);
if (count === 0)
assert.deepStrictEqual(data, {
streamingConfig: CONFIG,
});
else if (count === 1) {
assert.deepStrictEqual(data, {
audioContent: audioContent,
});
setImmediate(done);
}
count++;
next(null, data);
};

Expand Down

0 comments on commit 1e79b40

Please sign in to comment.