Skip to content

Commit 30aef59

Browse files
orgadssofisl
andauthored
fix: avoid unhandled exception on streamingRecognize (#5465)
* Execute streamingRecognize * When done, close the client * Execute streamingRecognize again, with data already in the stream * Unhandled exception occurs. TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object at _write (node:internal/streams/writable:474:13) at Writable.write (node:internal/streams/writable:502:10) at Duplexify._write (/project/node_modules/duplexify/index.js:212:22) at doWrite (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:390:139) at writeOrBuffer (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:381:5) at Writable.write (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:302:11) at Pumpify.<anonymous> (/project/node_modules/@google-cloud/speech/build/src/helpers.js:79:27) at Object.onceWrapper (node:events:633:26) at Pumpify.emit (node:events:518:28) at obj.<computed> [as _write] (/project/node_modules/stubs/index.js:28:22) at doWrite (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:390:139) at writeOrBuffer (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:381:5) at Writable.write (/project/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:302:11) at PassThrough.ondata (node:internal/streams/readable:1007:22) at PassThrough.emit (node:events:518:28) at addChunk (node:internal/streams/readable:559:12) { code: 'ERR_INVALID_ARG_TYPE' This happens because streamingRecognize writes streamingConfig on first chunk. Usually the stream is open in object mode, so it works, but when the client is terminated, PassThrough is used without object mode. Change PassThrough to object mode, so it terminates gracefully. Fixes #5464. Co-authored-by: sofisl <55454395+sofisl@users.noreply.github.com>
1 parent c89ced9 commit 30aef59

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

packages/google-cloud-speech/src/v1/speech_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export class SpeechClient {
317317
(...args: Array<{}>) => {
318318
if (this._terminated) {
319319
if (methodName in this.descriptors.stream) {
320-
const stream = new PassThrough();
320+
const stream = new PassThrough({objectMode: true});
321321
setImmediate(() => {
322322
stream.emit(
323323
'error',

packages/google-cloud-speech/src/v1p1beta1/speech_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export class SpeechClient {
317317
(...args: Array<{}>) => {
318318
if (this._terminated) {
319319
if (methodName in this.descriptors.stream) {
320-
const stream = new PassThrough();
320+
const stream = new PassThrough({objectMode: true});
321321
setImmediate(() => {
322322
stream.emit(
323323
'error',

packages/google-cloud-speech/src/v2/speech_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ export class SpeechClient {
534534
(...args: Array<{}>) => {
535535
if (this._terminated) {
536536
if (methodName in this.descriptors.stream) {
537-
const stream = new PassThrough();
537+
const stream = new PassThrough({objectMode: true});
538538
setImmediate(() => {
539539
stream.emit(
540540
'error',

0 commit comments

Comments
 (0)