{"version":3,"sources":["src/common.speech/ConnectionFactoryBase.ts"],"names":[],"mappings":"AAMA,OAAO,EAAmC,WAAW,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACvG,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAG9E,8BAAsB,qBAAsB,YAAW,kBAAkB;WAEvD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;aAYnC,MAAM,CAClB,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,QAAQ,EAClB,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAEhD,SAAS,CAAC,kBAAkB,CACxB,MAAM,EAAE,gBAAgB,EACxB,WAAW,EAAE,iBAAiB,CAAC,MAAM,CAAC,EACtC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAyB3B,SAAS,CAAC,eAAe,CACrB,MAAM,EAAE,UAAU,EAClB,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,gBAAgB,EACxB,WAAW,EAAE,iBAAiB,CAAC,MAAM,CAAC,EACtC,QAAQ,EAAE,MAAM,GAAG,IAAI;WAWP,0BAA0B,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CA8BpF","file":"ConnectionFactoryBase.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\nimport {\n    ServicePropertiesPropertyName,\n} from \"../common.speech/Exports.js\";\nimport { ConnectionRedirectEvent, Events, IConnection, IStringDictionary } from \"../common/Exports.js\";\nimport { PropertyId } from \"../sdk/Exports.js\";\nimport { AuthInfo, IConnectionFactory, RecognizerConfig } from \"./Exports.js\";\nimport { QueryParameterNames } from \"./QueryParameterNames.js\";\n\nexport abstract class ConnectionFactoryBase implements IConnectionFactory {\n\n    public static getHostSuffix(region: string): string {\n        if (!!region) {\n            if (region.toLowerCase().startsWith(\"china\")) {\n                return \".azure.cn\";\n            }\n            if (region.toLowerCase().startsWith(\"usgov\")) {\n                return \".azure.us\";\n            }\n        }\n        return \".microsoft.com\";\n    }\n\n    public abstract create(\n        config: RecognizerConfig,\n        authInfo: AuthInfo,\n        connectionId?: string): Promise<IConnection>;\n\n    protected setCommonUrlParams(\n        config: RecognizerConfig,\n        queryParams: IStringDictionary<string>,\n        endpoint: string): void {\n\n        const propertyIdToParameterMap: Map<number, string> = new Map([\n            [PropertyId.Speech_SegmentationSilenceTimeoutMs, QueryParameterNames.SegmentationSilenceTimeoutMs],\n            [PropertyId.SpeechServiceConnection_EnableAudioLogging, QueryParameterNames.EnableAudioLogging],\n            [PropertyId.SpeechServiceConnection_EndSilenceTimeoutMs, QueryParameterNames.EndSilenceTimeoutMs],\n            [PropertyId.SpeechServiceConnection_InitialSilenceTimeoutMs, QueryParameterNames.InitialSilenceTimeoutMs],\n            [PropertyId.SpeechServiceResponse_PostProcessingOption, QueryParameterNames.Postprocessing],\n            [PropertyId.SpeechServiceResponse_ProfanityOption, QueryParameterNames.Profanity],\n            [PropertyId.SpeechServiceResponse_RequestWordLevelTimestamps, QueryParameterNames.EnableWordLevelTimestamps],\n            [PropertyId.SpeechServiceResponse_StablePartialResultThreshold, QueryParameterNames.StableIntermediateThreshold],\n        ]);\n\n        propertyIdToParameterMap.forEach((parameterName: string, propertyId: PropertyId): void => {\n            this.setUrlParameter(propertyId, parameterName, config, queryParams, endpoint);\n        });\n\n\n        const serviceProperties: IStringDictionary<string> = JSON.parse(config.parameters.getProperty(ServicePropertiesPropertyName, \"{}\")) as IStringDictionary<string>;\n\n        Object.keys(serviceProperties).forEach((value: string): void => {\n            queryParams[value] = serviceProperties[value];\n        });\n    }\n\n    protected setUrlParameter(\n        propId: PropertyId,\n        parameterName: string,\n        config: RecognizerConfig,\n        queryParams: IStringDictionary<string>,\n        endpoint: string): void {\n\n        const value: string = config.parameters.getProperty(propId, undefined);\n\n        // FIXME: The .search() check will incorrectly match parameter name anywhere in the string\n        //        including e.g. the path portion, or even as a substring of other query parameters\n        if (value && (!endpoint || endpoint.search(parameterName) === -1)) {\n            queryParams[parameterName] = value.toLocaleLowerCase();\n        }\n    }\n\n    public static async getRedirectUrlFromEndpoint(endpoint: string): Promise<string> {\n        // make a rest call to the endpoint to get the redirect url\n        const redirectUrl: URL = new URL(endpoint);\n        redirectUrl.protocol = \"https:\";\n        redirectUrl.port = \"443\";\n        const params: URLSearchParams = redirectUrl.searchParams;\n        params.append(\"GenerateRedirectResponse\", \"true\");\n\n        const redirectedUrlString: string = redirectUrl.toString();\n        Events.instance.onEvent(new ConnectionRedirectEvent(\"\", redirectedUrlString, undefined, \"ConnectionFactoryBase: redirectUrl request\"));\n\n        const redirectResponse: Response = await fetch(redirectedUrlString);\n\n        if (redirectResponse.status !== 200) {\n            return endpoint;\n        }\n\n        // Fix: properly read the response text\n        const redirectUrlString = await redirectResponse.text();\n\n        Events.instance.onEvent(new ConnectionRedirectEvent(\"\", redirectUrlString, endpoint, \"ConnectionFactoryBase: redirectUrlString\"));\n\n        try {\n            // Validate the URL before returning\n            return new URL(redirectUrlString.trim()).toString();\n        } catch (error) {\n            return endpoint; // Return original endpoint if the redirect URL is invalid\n        }\n    }\n\n}\n"]}