Skip to content

Commit 4f1679c

Browse files
authored
fix: pass user-defined http(s) agent to websocket methods (#953)
1 parent e400240 commit 4f1679c

File tree

6 files changed

+28
-2
lines changed

6 files changed

+28
-2
lines changed

lib/recognize-stream.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class RecognizeStream extends Duplex {
126126
* Multiple versions of a base model can exist when a model is updated for internal improvements. The parameter is intended primarily for use with custom models that have been upgraded for a new base model.
127127
* The default value depends on whether the parameter is used with or without a custom model. For more information, see [Base model version](https://cloud.ibm.com/docs/services/speech-to-text?topic=speech-to-text-input#version).
128128
* @param {Boolean} [options.rejectUnauthorized] - If true, disable SSL verification for the WebSocket connection
129+
* @param {String} [options.agent] - custom http(s) agent, useful for using the sdk behind a proxy (Node only)
129130
* @param {String} [options.grammar_name] - The name of a grammar that is to be used with the recognition request. If you specify a grammar, you must also use the `language_customization_id` parameter to specify the name of the custom language model for which the grammar is defined. The service recognizes only strings that are recognized by the specified grammar; it does not recognize other custom words from the model's words resource. See [Grammars](https://cloud.ibm.com/docs/services/speech-to-text/output.html)
130131
* @param {Boolean} [options.redaction] - If `true`, the service redacts, or masks, numeric data from final transcripts. The feature redacts any number that has three or more consecutive digits by replacing each digit with an `X` character. It is intended to redact sensitive numeric data, such as credit card numbers. By default, the service performs no redaction. When you enable redaction, the service automatically enables smart formatting, regardless of whether you explicitly disable that feature. To ensure maximum security, the service also disables keyword spotting (ignores the `keywords` and `keywords_threshold` parameters) and returns only a single final transcript (forces the `max_alternatives` parameter to be `1`). **Note:** Applies to US English, Japanese, and Korean transcription only. See [Numeric redaction](https://cloud.ibm.com/docs/services/speech-to-text/output.html#redaction)
131132
*
@@ -241,12 +242,18 @@ class RecognizeStream extends Duplex {
241242
// for the last argument, `tlsOptions` gets passed to Node's `http` library,
242243
// which allows us to pass a rejectUnauthorized option
243244
// for disabling SSL verification (for ICP)
245+
246+
// add custom agent in the request options if given by user
247+
// default request options to null
248+
const { agent } = options;
249+
const requestOptions = agent ? { agent } : null;
250+
244251
const socket = (this.socket = new w3cWebSocket(
245252
url,
246253
null,
247254
null,
248255
options.headers,
249-
null,
256+
requestOptions,
250257
{ tlsOptions: { rejectUnauthorized: options.rejectUnauthorized }}
251258
));
252259

lib/synthesize-stream.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class SynthesizeStream extends Readable {
8282
* @param {String} [options.x-watson-metadata] - Associates a customer ID with data that is passed over the connection.
8383
* @param {IamTokenManagerV1} [options.token_manager] - Token manager for authenticating with IAM
8484
* @param {Boolean} [options.rejectUnauthorized] - If true, disable SSL verification for the WebSocket connection
85+
* @param {String} [options.agent] - custom http(s) agent, useful for using the sdk behind a proxy (Node only)
8586
*
8687
* @constructor
8788
*/
@@ -103,12 +104,17 @@ class SynthesizeStream extends Readable {
103104
'/v1/synthesize?' +
104105
queryString;
105106

107+
// add custom agent in the request options if given by user
108+
// default request options to null
109+
const { agent } = options;
110+
const requestOptions = agent ? { agent } : null;
111+
106112
const socket = (this.socket = new w3cWebSocket(
107113
url,
108114
null,
109115
null,
110116
options.headers,
111-
null,
117+
requestOptions,
112118
{ tlsOptions: { rejectUnauthorized: options.rejectUnauthorized }}
113119
));
114120

scripts/typedoc/generate_typedoc.sh

100644100755
File mode changed.

speech-to-text/v1.ts

+4
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ class SpeechToTextV1 extends GeneratedSpeechToTextV1 {
158158
params.token_manager = this.tokenManager;
159159
}
160160

161+
// if the user configured a custom https client, use it in the websocket method
162+
// let httpsAgent take precedence, default to null
163+
params.agent = this._options.httpsAgent || this._options.httpAgent || null;
164+
161165
// include analytics headers
162166
const sdkHeaders = getSdkHeaders('speech_to_text', 'v1', 'recognizeUsingWebSocket');
163167

test/unit/speech-helpers.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ const service = {
99
url: 'http://ibm.com:80',
1010
version: 'v1',
1111
silent: true, // hide deprecation warnings for recognizeLive and friends
12+
httpsAgent: 'fake https agent',
13+
httpAgent: 'fake http agent',
1214
};
1315

1416
const rc_service = {
1517
iam_apikey: 'abc123',
1618
url: 'http://ibm.com:80',
1719
version: 'v1',
1820
silent: true, // hide deprecation warnings for recognizeLive and friends
21+
httpAgent: 'fake http agent',
1922
};
2023

2124
const speech_to_text = new SpeechToTextV1(service);
@@ -36,13 +39,15 @@ describe('speech_to_text', () => {
3639
'service_name=speech_to_text;service_version=v1;operation_id=recognizeUsingWebSocket;async=true'
3740
);
3841
expect(stream.options.token_manager).toBeUndefined();
42+
expect(stream.options.agent).toBe(service.httpsAgent);
3943
});
4044

4145
it('should create a token manager in RecognizeStream if using IAM', () => {
4246
const stream = rc_speech_to_text.recognizeUsingWebSocket();
4347
expect(stream.options.url).toBe(service.url);
4448
expect(stream.options.headers.authorization).toBeUndefined();
4549
expect(stream.options.token_manager).toBeDefined();
50+
expect(stream.options.agent).toBe(rc_service.httpAgent);
4651
});
4752

4853
it('should override stored header with new token on refresh', done => {

text-to-speech/v1.ts

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ class TextToSpeechV1 extends GeneratedTextToSpeechV1 {
8686
params.token_manager = this.tokenManager;
8787
}
8888

89+
// if the user configured a custom https client, use it in the websocket method
90+
// let httpsAgent take precedence, default to null
91+
params.agent = this._options.httpsAgent || this._options.httpAgent || null;
92+
8993
// include analytics headers
9094
const sdkHeaders = getSdkHeaders('text_to_speech', 'v1', 'synthesizeUsingWebSocket');
9195

0 commit comments

Comments
 (0)