Skip to content

Commit 21a5a7f

Browse files
authored
fix: add getTransactionId method to the SynthesizeStream (#1058)
this method was present on the RecognizeStream and was mistakenly left off of the SynthesizeStream
1 parent 0a2b289 commit 21a5a7f

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

lib/recognize-stream.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Authenticator, contentType, qs } from 'ibm-cloud-sdk-core';
1919
import { Duplex, DuplexOptions } from 'stream';
2020
import { w3cwebsocket as w3cWebSocket } from 'websocket';
2121
import { RecognizeWebSocketParams } from '../speech-to-text/v1';
22-
import { processUserParameters } from './websocket-utils';
22+
import { extractTransactionId, processUserParameters } from './websocket-utils';
2323

2424
interface WritableState {
2525
highWaterMark: number;
@@ -456,25 +456,7 @@ class RecognizeStream extends Duplex {
456456
* @return Promise<String>
457457
*/
458458
getTransactionId(): Promise<string> {
459-
return new Promise<string>((resolve, reject) => {
460-
if (
461-
this.socket &&
462-
this.socket._client &&
463-
this.socket._client.response &&
464-
this.socket._client.response.headers
465-
) {
466-
resolve(
467-
(this.socket._client.response.headers['x-global-transaction-id'] as string)
468-
);
469-
} else {
470-
this.on('open', () =>
471-
resolve(
472-
(this.socket._client.response.headers['x-global-transaction-id'] as string)
473-
)
474-
);
475-
this.on('error', reject);
476-
}
477-
});
459+
return extractTransactionId(this);
478460
}
479461
}
480462

lib/synthesize-stream.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Authenticator, qs } from 'ibm-cloud-sdk-core';
1919
import { Readable, ReadableOptions } from 'stream';
2020
import { w3cwebsocket as w3cWebSocket } from 'websocket';
2121
import { SynthesizeWebSocketParams } from '../text-to-speech/v1';
22-
import { processUserParameters } from './websocket-utils';
22+
import { extractTransactionId, processUserParameters } from './websocket-utils';
2323

2424
/**
2525
* pipe()-able Node.js Readable stream - accepts text in the constructor and emits binary audio data in its 'message' events
@@ -211,6 +211,17 @@ class SynthesizeStream extends Readable {
211211
}
212212
);
213213
}
214+
215+
/**
216+
* Returns a Promise that resolves with Watson Transaction ID from the X-Transaction-ID header
217+
*
218+
* Works in Node.js but not in browsers (the W3C WebSocket API does not expose headers)
219+
*
220+
* @return Promise<String>
221+
*/
222+
getTransactionId(): Promise<string> {
223+
return extractTransactionId(this);
224+
}
214225
}
215226

216227
namespace SynthesizeStream {

lib/websocket-utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,34 @@ export function processUserParameters(options: any, allowedParams: string[]): an
4242

4343
return processedOptions;
4444
}
45+
46+
/**
47+
* Pulls the transaction ID from the connection headers and returns it in a Promise.
48+
* This function is used by the RecognizeStream and the SynthesizeStream - they both expose
49+
* a method called `getTransactionId` that relies on this code to read the ID from the
50+
* connection.
51+
*
52+
* @param {object} streamContext - the context (i.e. "this") of the invoking stream class
53+
* @returns {Promise<string>} - Resolves with the transaction ID as a string
54+
*/
55+
export function extractTransactionId(streamContext: any) {
56+
return new Promise<string>((resolve, reject) => {
57+
if (
58+
streamContext.socket &&
59+
streamContext.socket._client &&
60+
streamContext.socket._client.response &&
61+
streamContext.socket._client.response.headers
62+
) {
63+
resolve(
64+
(streamContext.socket._client.response.headers['x-global-transaction-id'] as string)
65+
);
66+
} else {
67+
streamContext.on('open', () =>
68+
resolve(
69+
(streamContext.socket._client.response.headers['x-global-transaction-id'] as string)
70+
)
71+
);
72+
streamContext.on('error', reject);
73+
}
74+
});
75+
}

0 commit comments

Comments
 (0)