@@ -94,33 +94,33 @@ function RecognizeStream(options) {
94
94
this . listening = false ;
95
95
this . initialized = false ;
96
96
this . finished = false ;
97
- var self = this ;
98
97
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' ) {
112
111
// 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. ' +
115
114
'Pass {silent: true} to disable this message.' ) ) ;
116
115
}
117
116
}
118
- }
119
- this . on ( 'newListener' , flowForResults ) ;
117
+ } ) ;
120
118
}
121
119
util . inherits ( RecognizeStream , Duplex ) ;
122
120
123
121
122
+ RecognizeStream . WEBSOCKET_CONNECTION_ERROR = 'WebSocket connection error' ;
123
+
124
124
RecognizeStream . prototype . initialize = function ( ) {
125
125
var options = this . options ;
126
126
@@ -153,39 +153,42 @@ RecognizeStream.prototype.initialize = function() {
153
153
// when the input stops, let the service know that we're done
154
154
self . on ( 'finish' , self . finish . bind ( self ) ) ;
155
155
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 ) {
157
163
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 ) ;
159
169
} ;
160
170
161
171
162
172
this . socket . onopen = function ( ) {
163
173
self . sendJSON ( openingMessage ) ;
164
174
/**
165
175
* emitted once the WebSocket connection has been established
166
- * @event RecognizeStream#connect
176
+ * @event RecognizeStream#open
167
177
*/
168
- self . emit ( 'connect ' ) ;
178
+ self . emit ( 'open ' ) ;
169
179
} ;
170
180
171
181
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
+ // }
176
186
/**
177
187
* @event RecognizeStream#close
178
188
* @param {Number } reasonCode
179
189
* @param {String } description
180
190
*/
181
191
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 ) ;
189
192
} ;
190
193
191
194
/**
@@ -232,7 +235,6 @@ RecognizeStream.prototype.initialize = function() {
232
235
// 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
233
236
if ( self . listening ) {
234
237
self . listening = false ;
235
- self . push ( null ) ;
236
238
socket . close ( ) ;
237
239
} else {
238
240
self . listening = true ;
0 commit comments