Skip to content

Commit

Permalink
refactor(websocket methods): warn user when an incorrectly named para…
Browse files Browse the repository at this point in the history
…meter is passed
  • Loading branch information
dpopp07 committed Aug 26, 2019
1 parent b1cb4d3 commit 91ccf1d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 49 deletions.
64 changes: 32 additions & 32 deletions lib/recognize-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,38 +109,38 @@ class RecognizeStream extends Duplex {
* Note that the WebSocket connection is not established until the first chunk of data is recieved. This allows for auto-detection of content type (for wav/flac/opus audio).
*
* @param {Options} options
* @param {string} [url] - Base url for service (default='wss://stream.watsonplatform.net/speech-to-text/api')
* @param {OutgoingHttpHeaders} [headers] - Only works in Node.js, not in browsers. Allows for custom headers to be set, including an Authorization header (preventing the need for auth tokens)
* @param {boolean} [readableObjectMode] - Emit `result` objects instead of string Buffers for the `data` events. Does not affect input (which must be binary)
* @param {boolean} [objectMode] - Alias for readableObjectMode
* @param {any} [tokenManager] - Token manager for authenticating with IAM
* @param {boolean} [rejectUnauthorized] - If false, disable SSL verification for the WebSocket connection (default=true)
* @param {string} accessToken - Bearer token to put in query string
* @param {string} watsonToken - Valid Watson authentication token (for Cloud Foundry)
* @param {string} model - The identifier of the model that is to be used for all recognition requests sent over the connection
* @param {string} languageCustomizationId - The customization ID (GUID) of a custom language model that is to be used for all requests sent over the connection
* @param {string} acousticCustomizationId - The customization ID (GUID) of a custom acoustic model that is to be used for the request
* @param {string} baseModelVersion - The version of the specified base model that is to be used for all requests sent over the connection
* @param {boolean} xWatsonLearningOptOut - Indicates whether IBM can use data that is sent over the connection to improve the service for future users (default=false)
* @param {string} xWatsonMetadata - Associates a customer ID with all data that is passed over the connection. The parameter accepts the argument customer_id={id}, where {id} is a random or generic string that is to be associated with the data
* @param {string} contentType - The format (MIME type) of the audio
* @param {number} customizationWeight - Tell the service how much weight to give to words from the custom language model compared to those from the base model for the current request
* @param {number} inactivityTimeout - The time in seconds after which, if only silence (no speech) is detected in the audio, the connection is closed (default=30)
* @param {boolean} interimResults - If true, the service returns interim results as a stream of JSON SpeechRecognitionResults objects (default=false)
* @param {string[]} keywords - An array of keyword strings to spot in the audio
* @param {number} keywordsThreshold - A confidence value that is the lower bound for spotting a keyword
* @param {number} maxAlternatives - The maximum number of alternative transcripts that the service is to return (default=1)
* @param {number} wordAlternativesThreshold - A confidence value that is the lower bound for identifying a hypothesis as a possible word alternative
* @param {boolean} wordConfidence - If true, the service returns a confidence measure in the range of 0.0 to 1.0 for each word (default=false)
* @param {boolean} timestamps - If true, the service returns time alignment for each word (default=false)
* @param {boolean} profanityFilter - If true, the service filters profanity from all output except for keyword results by replacing inappropriate words with a series of asterisks (default=true)
* @param {boolean} smartFormatting - If true, the service converts dates, times, series of digits and numbers, phone numbers, currency values, and internet addresses into more readable, conventional representations (default=false)
* @param {boolean} speakerLabels - If true, the response includes labels that identify which words were spoken by which participants in a multi-person exchange (default=false)
* @param {string} grammarName - The name of a grammar that is to be used with the recognition request
* @param {boolean} redaction - If true, the service redacts, or masks, numeric data from final transcripts (default=false)
* @param {boolean} processingMetrics - If true, requests processing metrics about the service's transcription of the input audio (default=false)
* @param {number} processingMetricsInterval - Specifies the interval in seconds at which the service is to return processing metrics
* @param {boolean} audioMetrics - If true, requests detailed information about the signal characteristics of the input audio (detailed=false)
* @param {string} [options.url] - Base url for service (default='wss://stream.watsonplatform.net/speech-to-text/api')
* @param {OutgoingHttpHeaders} [options.headers] - Only works in Node.js, not in browsers. Allows for custom headers to be set, including an Authorization header (preventing the need for auth tokens)
* @param {boolean} [options.readableObjectMode] - Emit `result` objects instead of string Buffers for the `data` events. Does not affect input (which must be binary)
* @param {boolean} [options.objectMode] - Alias for readableObjectMode
* @param {any} [options.tokenManager] - Token manager for authenticating with IAM
* @param {boolean} [options.rejectUnauthorized] - If false, disable SSL verification for the WebSocket connection (default=true)
* @param {string} [options.accessToken] - Bearer token to put in query string
* @param {string} [options.watsonToken] - Valid Watson authentication token (for Cloud Foundry)
* @param {string} [options.model] - The identifier of the model that is to be used for all recognition requests sent over the connection
* @param {string} [options.languageCustomizationId] - The customization ID (GUID) of a custom language model that is to be used for all requests sent over the connection
* @param {string} [options.acousticCustomizationId] - The customization ID (GUID) of a custom acoustic model that is to be used for the request
* @param {string} [options.baseModelVersion] - The version of the specified base model that is to be used for all requests sent over the connection
* @param {boolean} [options.xWatsonLearningOptOut] - Indicates whether IBM can use data that is sent over the connection to improve the service for future users (default=false)
* @param {string} [options.xWatsonMetadata] - Associates a customer ID with all data that is passed over the connection. The parameter accepts the argument customer_id={id}, where {id} is a random or generic string that is to be associated with the data
* @param {string} [options.contentType] - The format (MIME type) of the audio
* @param {number} [options.customizationWeight] - Tell the service how much weight to give to words from the custom language model compared to those from the base model for the current request
* @param {number} [options.inactivityTimeout] - The time in seconds after which, if only silence (no speech) is detected in the audio, the connection is closed (default=30)
* @param {boolean} [options.interimResults] - If true, the service returns interim results as a stream of JSON SpeechRecognitionResults objects (default=false)
* @param {string[]} [options.keywords] - An array of keyword strings to spot in the audio
* @param {number} [options.keywordsThreshold] - A confidence value that is the lower bound for spotting a keyword
* @param {number} [options.maxAlternatives] - The maximum number of alternative transcripts that the service is to return (default=1)
* @param {number} [options.wordAlternativesThreshold] - A confidence value that is the lower bound for identifying a hypothesis as a possible word alternative
* @param {boolean} [options.wordConfidence] - If true, the service returns a confidence measure in the range of 0.0 to 1.0 for each word (default=false)
* @param {boolean} [options.timestamps] - If true, the service returns time alignment for each word (default=false)
* @param {boolean} [options.profanityFilter] - If true, the service filters profanity from all output except for keyword results by replacing inappropriate words with a series of asterisks (default=true)
* @param {boolean} [options.smartFormatting] - If true, the service converts dates, times, series of digits and numbers, phone numbers, currency values, and internet addresses into more readable, conventional representations (default=false)
* @param {boolean} [options.speakerLabels] - If true, the response includes labels that identify which words were spoken by which participants in a multi-person exchange (default=false)
* @param {string} [options.grammarName] - The name of a grammar that is to be used with the recognition request
* @param {boolean} [options.redaction] - If true, the service redacts, or masks, numeric data from final transcripts (default=false)
* @param {boolean} [options.processingMetrics] - If true, requests processing metrics about the service's transcription of the input audio (default=false)
* @param {number} [options.processingMetricsInterval] - Specifies the interval in seconds at which the service is to return processing metrics
* @param {boolean} [options.audioMetrics] - If true, requests detailed information about the signal characteristics of the input audio (detailed=false)
* @constructor
*/
constructor(options: Options) {
Expand Down
26 changes: 13 additions & 13 deletions lib/synthesize-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ class SynthesizeStream extends Readable {
* Note that the WebSocket connection is not established until the first chunk of data is recieved. This allows for IAM token request management by the SDK.
*
* @param {Options} options
* @param {string} [url] - Base url for service (default='wss://stream.watsonplatform.net/speech-to-text/api')
* @param {OutgoingHttpHeaders} [headers] - Only works in Node.js, not in browsers. Allows for custom headers to be set, including an Authorization header (preventing the need for auth tokens)
* @param {any} [tokenManager] - Token manager for authenticating with IAM
* @param {boolean} [rejectUnauthorized] - If false, disable SSL verification for the WebSocket connection (default=true)
* @param {string} text - The text that us to be synthesized
* @param {string} accept - The requested format (MIME type) of the audio
* @param {string[]} [timings] - An array that specifies whether the service is to return word timing information for all strings of the input text
* @param {string} [accessToken] - Bearer token to put in query string
* @param {string} [watsonToken] - Valid Watson authentication token (for Cloud Foundry)
* @param {string} [voice] - The voice to use for the synthesis (default='en-US_MichaelVoice')
* @param {string} [customizationId] - The customization ID (GUID) of a custom voice model that is to be used for the synthesis
* @param {boolean} [xWatsonLearningOptOut] - Indicates whether IBM can use data that is sent over the connection to improve the service for future users (default=false)
* @param {string} [xWatsonMetadata] - Associates a customer ID with all data that is passed over the connection. The parameter accepts the argument customer_id={id}, where {id} is a random or generic string that is to be associated with the data
* @param {string} [options.url] - Base url for service (default='wss://stream.watsonplatform.net/speech-to-text/api')
* @param {OutgoingHttpHeaders} [options.headers] - Only works in Node.js, not in browsers. Allows for custom headers to be set, including an Authorization header (preventing the need for auth tokens)
* @param {any} [options.tokenManager] - Token manager for authenticating with IAM
* @param {boolean} [options.rejectUnauthorized] - If false, disable SSL verification for the WebSocket connection (default=true)
* @param {string} options.text - The text that us to be synthesized
* @param {string} options.accept - The requested format (MIME type) of the audio
* @param {string[]} [options.timings] - An array that specifies whether the service is to return word timing information for all strings of the input text
* @param {string} [options.accessToken] - Bearer token to put in query string
* @param {string} [options.watsonToken] - Valid Watson authentication token (for Cloud Foundry)
* @param {string} [options.voice] - The voice to use for the synthesis (default='en-US_MichaelVoice')
* @param {string} [options.customizationId] - The customization ID (GUID) of a custom voice model that is to be used for the synthesis
* @param {boolean} [options.xWatsonLearningOptOut] - Indicates whether IBM can use data that is sent over the connection to improve the service for future users (default=false)
* @param {string} [options.xWatsonMetadata] - Associates a customer ID with all data that is passed over the connection. The parameter accepts the argument customer_id={id}, where {id} is a random or generic string that is to be associated with the data
* @constructor
*/
constructor(options: Options) {
Expand Down
3 changes: 3 additions & 0 deletions lib/websocket-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export function processUserParameters(options: any, allowedParams: string[]): an
const keyName = camelcase(param);
if (options[keyName] !== undefined) {
processedOptions[param] = options[keyName];
} else if (options[param] !== undefined) {
// if the user used the service property name, warn them and give them the name to use
console.warn(`Unrecognized parameter: "${param}". Did you mean "${keyName}"?`);
}
});

Expand Down
31 changes: 27 additions & 4 deletions test/unit/websocket-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ describe('websocket utility functions', () => {
];
const userOptions = {
kebabCase: 'some value',
snake_case: 'some value',
camelcase: 'some value',
snakeCase: 'some value',
camelCase: 'some value',
lowercase: 'some value',
mIxEdCaSe: 'some value',
notInTheList: 'some value',
Expand All @@ -41,8 +41,31 @@ describe('websocket utility functions', () => {
Object.keys(processedValues).forEach(param => {
expect(intendedValues).toContain(param);
});
expect(processedValues.notInTheList).toBeUndefined();
expect(processedValues.unused_param).toBeUndefined();
expect(processedValues).toEqual({
'kebab-case': 'some value',
snake_case: 'some value',
camelCase: 'some value',
lowercase: 'some value',
mIxED_CaSe: 'some value',
});
});

it('should warn the user if they provide the service name for a parameter', () => {
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
const intendedValues = ['kebab-case', 'snake_case'];
const userOptions = {
kebabCase: 'some value',
snake_case: 'some value',
notInTheList: 'some value',
};
const processedValues = processUserParameters(userOptions, intendedValues);
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy.mock.calls[0][0]).toBe(
'Unrecognized parameter: "snake_case". Did you mean "snakeCase"?'
);
expect(processedValues['kebab-case']).toBe('some value');
expect(Object.keys(processedValues).length).toBe(1);
consoleSpy.mockRestore();
});
});

Expand Down

0 comments on commit 91ccf1d

Please sign in to comment.