Skip to content

Commit

Permalink
fix: pass user-defined http(s) agent to websocket methods (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpopp07 committed Sep 19, 2019
1 parent e400240 commit 4f1679c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/recognize-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class RecognizeStream extends Duplex {
* 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.
* 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).
* @param {Boolean} [options.rejectUnauthorized] - If true, disable SSL verification for the WebSocket connection
* @param {String} [options.agent] - custom http(s) agent, useful for using the sdk behind a proxy (Node only)
* @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)
* @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)
*
Expand Down Expand Up @@ -241,12 +242,18 @@ class RecognizeStream extends Duplex {
// for the last argument, `tlsOptions` gets passed to Node's `http` library,
// which allows us to pass a rejectUnauthorized option
// for disabling SSL verification (for ICP)

// add custom agent in the request options if given by user
// default request options to null
const { agent } = options;
const requestOptions = agent ? { agent } : null;

const socket = (this.socket = new w3cWebSocket(
url,
null,
null,
options.headers,
null,
requestOptions,
{ tlsOptions: { rejectUnauthorized: options.rejectUnauthorized }}
));

Expand Down
8 changes: 7 additions & 1 deletion lib/synthesize-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class SynthesizeStream extends Readable {
* @param {String} [options.x-watson-metadata] - Associates a customer ID with data that is passed over the connection.
* @param {IamTokenManagerV1} [options.token_manager] - Token manager for authenticating with IAM
* @param {Boolean} [options.rejectUnauthorized] - If true, disable SSL verification for the WebSocket connection
* @param {String} [options.agent] - custom http(s) agent, useful for using the sdk behind a proxy (Node only)
*
* @constructor
*/
Expand All @@ -103,12 +104,17 @@ class SynthesizeStream extends Readable {
'/v1/synthesize?' +
queryString;

// add custom agent in the request options if given by user
// default request options to null
const { agent } = options;
const requestOptions = agent ? { agent } : null;

const socket = (this.socket = new w3cWebSocket(
url,
null,
null,
options.headers,
null,
requestOptions,
{ tlsOptions: { rejectUnauthorized: options.rejectUnauthorized }}
));

Expand Down
Empty file modified scripts/typedoc/generate_typedoc.sh
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions speech-to-text/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ class SpeechToTextV1 extends GeneratedSpeechToTextV1 {
params.token_manager = this.tokenManager;
}

// if the user configured a custom https client, use it in the websocket method
// let httpsAgent take precedence, default to null
params.agent = this._options.httpsAgent || this._options.httpAgent || null;

// include analytics headers
const sdkHeaders = getSdkHeaders('speech_to_text', 'v1', 'recognizeUsingWebSocket');

Expand Down
5 changes: 5 additions & 0 deletions test/unit/speech-helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ const service = {
url: 'http://ibm.com:80',
version: 'v1',
silent: true, // hide deprecation warnings for recognizeLive and friends
httpsAgent: 'fake https agent',
httpAgent: 'fake http agent',
};

const rc_service = {
iam_apikey: 'abc123',
url: 'http://ibm.com:80',
version: 'v1',
silent: true, // hide deprecation warnings for recognizeLive and friends
httpAgent: 'fake http agent',
};

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

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

it('should override stored header with new token on refresh', done => {
Expand Down
4 changes: 4 additions & 0 deletions text-to-speech/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class TextToSpeechV1 extends GeneratedTextToSpeechV1 {
params.token_manager = this.tokenManager;
}

// if the user configured a custom https client, use it in the websocket method
// let httpsAgent take precedence, default to null
params.agent = this._options.httpsAgent || this._options.httpAgent || null;

// include analytics headers
const sdkHeaders = getSdkHeaders('text_to_speech', 'v1', 'synthesizeUsingWebSocket');

Expand Down

0 comments on commit 4f1679c

Please sign in to comment.