|
| 1 | +import { type AudioConfig } from 'microsoft-cognitiveservices-speech-sdk'; |
| 2 | +import resolveFunctionOrReturnValue from './resolveFunctionOrReturnValue'; |
| 3 | + |
| 4 | +let shouldWarnOnSubscriptionKey = true; |
| 5 | + |
| 6 | +export type Credentials = Readonly< |
| 7 | + ( |
| 8 | + | { authorizationToken: string; subscriptionKey?: undefined } |
| 9 | + | { authorizationToken?: undefined; subscriptionKey: string } |
| 10 | + ) & |
| 11 | + ( |
| 12 | + | { |
| 13 | + customVoiceHostname?: undefined; |
| 14 | + region: string; |
| 15 | + speechRecognitionHostname?: undefined; |
| 16 | + speechSynthesisHostname?: undefined; |
| 17 | + } |
| 18 | + | { |
| 19 | + customVoiceHostname: string; |
| 20 | + region?: undefined; |
| 21 | + speechRecognitionHostname: string; |
| 22 | + speechSynthesisHostname: string; |
| 23 | + } |
| 24 | + ) |
| 25 | +>; |
| 26 | + |
| 27 | +export type PatchOptionsInit = { |
| 28 | + audioConfig: AudioConfig; |
| 29 | + credentials?: (() => Credentials | Promise<Credentials>) | Credentials | Promise<Credentials>; |
| 30 | + enableTelemetry: boolean; |
| 31 | + looseEvent?: boolean | undefined; |
| 32 | + looseEvents?: boolean | undefined; |
| 33 | + referenceGrammars?: readonly string[] | undefined; |
| 34 | + region?: string | undefined; |
| 35 | + speechRecognitionEndpointId: string; |
| 36 | + textNormalization: 'display' | 'itn' | 'lexical' | 'maskeditn'; |
| 37 | +} & ( |
| 38 | + | { |
| 39 | + authorizationToken: string; |
| 40 | + subscriptionKey?: undefined; |
| 41 | + } |
| 42 | + | { |
| 43 | + authorizationToken?: undefined; |
| 44 | + subscriptionKey: string; |
| 45 | + } |
| 46 | +); |
| 47 | + |
| 48 | +type PatchedOptions = Readonly<{ |
| 49 | + audioConfig: AudioConfig; |
| 50 | + enableTelemetry: boolean; |
| 51 | + fetchCredentials: () => Promise<Credentials>; |
| 52 | + looseEvents: boolean; |
| 53 | + referenceGrammars: readonly string[] | undefined; |
| 54 | + speechRecognitionEndpointId: string | undefined; |
| 55 | + textNormalization: 'display' | 'itn' | 'lexical' | 'maskeditn'; |
| 56 | +}>; |
| 57 | + |
| 58 | +export default function patchOptions(init: PatchOptionsInit): PatchedOptions { |
| 59 | + const { |
| 60 | + audioConfig, |
| 61 | + authorizationToken, |
| 62 | + enableTelemetry, |
| 63 | + looseEvent, |
| 64 | + referenceGrammars, |
| 65 | + region = 'westus', |
| 66 | + speechRecognitionEndpointId, |
| 67 | + subscriptionKey, |
| 68 | + textNormalization |
| 69 | + } = init; |
| 70 | + |
| 71 | + let { credentials, looseEvents } = init; |
| 72 | + |
| 73 | + if (typeof looseEvent !== 'undefined') { |
| 74 | + console.warn('web-speech-cognitive-services: The option "looseEvent" should be named as "looseEvents".'); |
| 75 | + |
| 76 | + looseEvents = looseEvent; |
| 77 | + } |
| 78 | + |
| 79 | + if (!credentials) { |
| 80 | + if (!authorizationToken && !subscriptionKey) { |
| 81 | + throw new Error('web-speech-cognitive-services: Credentials must be specified.'); |
| 82 | + } else { |
| 83 | + console.warn( |
| 84 | + 'web-speech-cognitive-services: We are deprecating authorizationToken, region, and subscriptionKey. Please use credentials instead. The deprecated option will be removed on or after 2020-11-14.' |
| 85 | + ); |
| 86 | + |
| 87 | + credentials = async () => |
| 88 | + typeof init.authorizationToken !== 'undefined' |
| 89 | + ? { authorizationToken: await resolveFunctionOrReturnValue<string>(init.authorizationToken), region } |
| 90 | + : { region, subscriptionKey: await resolveFunctionOrReturnValue<string>(init.subscriptionKey) }; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return Object.freeze({ |
| 95 | + audioConfig, |
| 96 | + enableTelemetry, |
| 97 | + fetchCredentials: async () => { |
| 98 | + const { |
| 99 | + authorizationToken, |
| 100 | + customVoiceHostname, |
| 101 | + region, |
| 102 | + speechRecognitionHostname, |
| 103 | + speechSynthesisHostname, |
| 104 | + subscriptionKey |
| 105 | + } = await resolveFunctionOrReturnValue<Credentials>(credentials); |
| 106 | + |
| 107 | + if ((!authorizationToken && !subscriptionKey) || (authorizationToken && subscriptionKey)) { |
| 108 | + throw new Error( |
| 109 | + 'web-speech-cognitive-services: Either "authorizationToken" or "subscriptionKey" must be provided.' |
| 110 | + ); |
| 111 | + } else if (!region && !(speechRecognitionHostname && speechSynthesisHostname)) { |
| 112 | + throw new Error( |
| 113 | + 'web-speech-cognitive-services: Either "region" or "speechRecognitionHostname" and "speechSynthesisHostname" must be set.' |
| 114 | + ); |
| 115 | + } else if (region && (customVoiceHostname || speechRecognitionHostname || speechSynthesisHostname)) { |
| 116 | + throw new Error( |
| 117 | + 'web-speech-cognitive-services: Only either "region" or "customVoiceHostname", "speechRecognitionHostname" and "speechSynthesisHostname" can be set.' |
| 118 | + ); |
| 119 | + } else if (authorizationToken) { |
| 120 | + if (typeof authorizationToken !== 'string') { |
| 121 | + throw new Error('web-speech-cognitive-services: "authorizationToken" must be a string.'); |
| 122 | + } |
| 123 | + } else if (typeof subscriptionKey !== 'string') { |
| 124 | + throw new Error('web-speech-cognitive-services: "subscriptionKey" must be a string.'); |
| 125 | + } |
| 126 | + |
| 127 | + if (shouldWarnOnSubscriptionKey && subscriptionKey) { |
| 128 | + console.warn( |
| 129 | + 'web-speech-cognitive-services: In production environment, subscription key should not be used, authorization token should be used instead.' |
| 130 | + ); |
| 131 | + |
| 132 | + shouldWarnOnSubscriptionKey = false; |
| 133 | + } |
| 134 | + |
| 135 | + return { |
| 136 | + ...(typeof authorizationToken !== 'undefined' ? { authorizationToken } : { subscriptionKey }), |
| 137 | + ...(typeof region !== 'undefined' |
| 138 | + ? { region } |
| 139 | + : { |
| 140 | + customVoiceHostname, |
| 141 | + speechRecognitionHostname, |
| 142 | + speechSynthesisHostname |
| 143 | + }) |
| 144 | + } satisfies Credentials; |
| 145 | + }, |
| 146 | + looseEvents: !!looseEvents, |
| 147 | + referenceGrammars: referenceGrammars && Object.freeze([...referenceGrammars]), |
| 148 | + speechRecognitionEndpointId, |
| 149 | + textNormalization |
| 150 | + }); |
| 151 | +} |
0 commit comments