Skip to content

Commit 61b5272

Browse files
committed
expose stt recognizestream transaction id
Fixes watson-developer-cloud#412
1 parent e0e9f45 commit 61b5272

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v2.24.1
2+
* STT RecognizeStream now exposes Transaction ID
3+
14
# v2.23.1
25
* Restored support for Node.js 4.0-4.4
36

speech-to-text/recognize_stream.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,29 @@ RecognizeStream.prototype.stop = function() {
259259
this.socket.close();
260260
};
261261

262+
/**
263+
* Returns a Promise that resolves with Watson Transaction ID from the X-Transaction-ID header
264+
*
265+
* Works in Node.js but not in browsers (the W3C WebSocket API does not expose headers)
266+
*
267+
* @return Promise<String>
268+
*/
269+
RecognizeStream.prototype.getTransactionId = function() {
270+
if (this.socket &&
271+
this.socket._client &&
272+
this.socket._client.response &&
273+
this.socket._client.response.headers) {
274+
return Promise.resolve(this.socket._client.response.headers['x-global-transaction-id']);
275+
} else {
276+
return new Promise((resolve, reject) => {
277+
this.on('connect', () => {
278+
resolve(this.socket._client.response.headers['x-global-transaction-id']);
279+
});
280+
this.on('error', reject);
281+
});
282+
}
283+
};
284+
262285
// quick/dumb way to determine content type from a supported file format
263286
const headerToContentType = {
264287
fLaC: 'audio/flac',

test/integration/test.speech_to_text.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,35 +113,44 @@ describe('speech_to_text_integration', function() {
113113
speech_to_text.getModels({}, done);
114114
});
115115

116-
it('createRecognizeStream()', function(done) {
117-
const recognizeStream = speech_to_text.createRecognizeStream({
118-
content_type: 'audio/l16; rate=44100'
116+
describe('createRecognizeStream()', () => {
117+
it('transcribes audio over a websocket', function(done) {
118+
const recognizeStream = speech_to_text.createRecognizeStream();
119+
recognizeStream.setEncoding('utf8');
120+
fs.createReadStream(path.join(__dirname, '../resources/weather.flac')).pipe(recognizeStream).on('error', done).pipe(
121+
concat(function(transcription) {
122+
assert.equal(typeof transcription, 'string', 'should return a string transcription');
123+
assert.equal(transcription.trim(), 'thunderstorms could produce large hail isolated tornadoes and heavy rain');
124+
done();
125+
})
126+
);
119127
});
120-
recognizeStream.setEncoding('utf8');
121-
fs.createReadStream(path.join(__dirname, '../resources/weather.flac')).pipe(recognizeStream).on('error', done).pipe(
122-
concat(function(transcription) {
123-
assert.equal(typeof transcription, 'string', 'should return a string transcription');
124-
assert.equal(transcription.trim(), 'thunderstorms could produce large hail isolated tornadoes and heavy rain');
125-
done();
126-
})
127-
);
128-
});
129128

130-
it('createRecognizeStream() - no words', function(done) {
131-
const recognizeStream = speech_to_text.createRecognizeStream({
132-
content_type: 'audio/l16; rate=44100'
129+
it('works when stream has no words', function(done) {
130+
const recognizeStream = speech_to_text.createRecognizeStream({
131+
content_type: 'audio/l16; rate=44100'
132+
});
133+
recognizeStream.setEncoding('utf8');
134+
fs
135+
.createReadStream(path.join(__dirname, '../resources/blank.wav'))
136+
.pipe(recognizeStream)
137+
.on('error', done)
138+
.on('data', function(text) {
139+
assert(!text, 'no text expected for an audio file with no words');
140+
})
141+
.on('end', done);
133142
});
134-
recognizeStream.setEncoding('utf8');
135-
fs
136-
.createReadStream(path.join(__dirname, '../resources/blank.wav'))
137-
.pipe(recognizeStream)
138-
.on('error', done)
139-
.on('data', function(text) {
140-
assert(!text, 'no text expected for an audio file with no words');
141-
})
142-
.on('end', done);
143+
144+
it('exposes Transaction ID in node.js', () => {
145+
const recognizeStream = speech_to_text.createRecognizeStream();
146+
return fs.createReadStream(path.join(__dirname, '../resources/weather.ogg'))
147+
.pipe(recognizeStream)
148+
.getTransactionId()
149+
.then(id => assert(id));
150+
})
143151
});
144152

153+
145154
describe('customization', function() {
146155
let customization_id;
147156

0 commit comments

Comments
 (0)