Skip to content

Commit ce18c7e

Browse files
committed
RecognizeStream: cleanup events, error handling
* Renamed connect event to close * removed deprecated connection-close event * updated deprecation warnings * emit actual Error object for connection errors * emit end and close events after errors
1 parent 6a48472 commit ce18c7e

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

speech-to-text/recognize-stream.js

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,33 @@ function RecognizeStream(options) {
9494
this.listening = false;
9595
this.initialized = false;
9696
this.finished = false;
97-
var self = this;
9897

99-
/**
100-
* listening for `results` events should put the stream in flowing mode just like `data` events
101-
*
102-
* @param {String} event
103-
*/
104-
function flowForResults(event) {
105-
if (event === 'results' || event === 'result' || event === 'speaker_labels') {
106-
self.removeListener('newListener', flowForResults);
107-
process.nextTick(function() {
108-
self.resume(); // put this stream in flowing mode
109-
});
110-
if (!options.silent) {
111-
// todo: move this to the node.js wrapper
98+
this.on('newListener', function(event) {
99+
if (!options.silent) {
100+
if (event === 'results' || event === 'result' || event === 'speaker_labels') {
101+
// eslint-disable-next-line no-console
102+
console.log(new Error('Watson Speech to Text RecognizeStream: the ' + event + ' event was deprecated. ' +
103+
'Please set {objectMode: true} and listen for the \'data\' event instead. ' +
104+
'Pass {silent: true} to disable this message.'));
105+
} else if (event === 'connection-close') {
106+
// eslint-disable-next-line no-console
107+
console.log(new Error('Watson Speech to Text RecognizeStream: the ' + event + ' event was deprecated. ' +
108+
'Please listen for the \'close\' event instead. ' +
109+
'Pass {silent: true} to disable this message.'));
110+
} else if (event === 'connect') {
112111
// eslint-disable-next-line no-console
113-
console.log(new Error('Watson Speech to Text RecognizeStream: the ' + event + ' event is deprecated and will be removed from a future release. ' +
114-
'Please set {objectMode: true} and listen for the data event instead. ' +
112+
console.log(new Error('Watson Speech to Text RecognizeStream: the ' + event + ' event was deprecated. ' +
113+
'Please listen for the \'open\' event instead. ' +
115114
'Pass {silent: true} to disable this message.'));
116115
}
117116
}
118-
}
119-
this.on('newListener', flowForResults);
117+
});
120118
}
121119
util.inherits(RecognizeStream, Duplex);
122120

123121

122+
RecognizeStream.WEBSOCKET_CONNECTION_ERROR = 'WebSocket connection error';
123+
124124
RecognizeStream.prototype.initialize = function() {
125125
var options = this.options;
126126

@@ -153,39 +153,42 @@ RecognizeStream.prototype.initialize = function() {
153153
// when the input stops, let the service know that we're done
154154
self.on('finish', self.finish.bind(self));
155155

156-
socket.onerror = function(error) {
156+
/**
157+
* This can happen if the credentials are invalid - in that case, the response from DataPower doesn't include the
158+
* necessary CORS headers, so JS can't even read it :(
159+
*
160+
* @param {Event} event - event object with essentially no useful information
161+
*/
162+
socket.onerror = function(event) {
157163
self.listening = false;
158-
self.emit('error', error);
164+
var err = new Error('WebSocket connection error');
165+
err.name = RecognizeStream.WEBSOCKET_CONNECTION_ERROR;
166+
err.event = event;
167+
self.emit('error', err);
168+
self.push(null);
159169
};
160170

161171

162172
this.socket.onopen = function() {
163173
self.sendJSON(openingMessage);
164174
/**
165175
* emitted once the WebSocket connection has been established
166-
* @event RecognizeStream#connect
176+
* @event RecognizeStream#open
167177
*/
168-
self.emit('connect');
178+
self.emit('open');
169179
};
170180

171181
this.socket.onclose = function(e) {
172-
if (self.listening) {
173-
self.listening = false;
174-
self.push(null);
175-
}
182+
// if (self.listening) {
183+
self.listening = false;
184+
self.push(null);
185+
// }
176186
/**
177187
* @event RecognizeStream#close
178188
* @param {Number} reasonCode
179189
* @param {String} description
180190
*/
181191
self.emit('close', e.code, e.reason);
182-
/**
183-
* @event RecognizeStream#connection-close
184-
* @param {Number} reasonCode
185-
* @param {String} description
186-
* @deprecated
187-
*/
188-
self.emit('connection-close', e.code, e.reason);
189192
};
190193

191194
/**
@@ -232,7 +235,6 @@ RecognizeStream.prototype.initialize = function() {
232235
// this is emitted both when the server is ready for audio, and after we send the close message to indicate that it's done processing
233236
if (self.listening) {
234237
self.listening = false;
235-
self.push(null);
236238
socket.close();
237239
} else {
238240
self.listening = true;

0 commit comments

Comments
 (0)