{"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;IAwB3B,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;CAqCpF","file":"ConnectionFactoryBase.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport {\r\n    ServicePropertiesPropertyName,\r\n} from \"../common.speech/Exports.js\";\r\nimport { ConnectionRedirectEvent, Events, IConnection, IStringDictionary } from \"../common/Exports.js\";\r\nimport { PropertyId } from \"../sdk/Exports.js\";\r\nimport { AuthInfo, IConnectionFactory, RecognizerConfig } from \"./Exports.js\";\r\nimport { QueryParameterNames } from \"./QueryParameterNames.js\";\r\n\r\nexport abstract class ConnectionFactoryBase implements IConnectionFactory {\r\n\r\n    public static getHostSuffix(region: string): string {\r\n        if (!!region) {\r\n            if (region.toLowerCase().startsWith(\"china\")) {\r\n                return \".azure.cn\";\r\n            }\r\n            if (region.toLowerCase().startsWith(\"usgov\")) {\r\n                return \".azure.us\";\r\n            }\r\n        }\r\n        return \".microsoft.com\";\r\n    }\r\n\r\n    public abstract create(\r\n        config: RecognizerConfig,\r\n        authInfo: AuthInfo,\r\n        connectionId?: string): Promise<IConnection>;\r\n\r\n    protected setCommonUrlParams(\r\n        config: RecognizerConfig,\r\n        queryParams: IStringDictionary<string>,\r\n        endpoint: string): void {\r\n\r\n        const propertyIdToParameterMap: Map<number, string> = new Map([\r\n            [PropertyId.Speech_SegmentationSilenceTimeoutMs, QueryParameterNames.SegmentationSilenceTimeoutMs],\r\n            [PropertyId.SpeechServiceConnection_EnableAudioLogging, QueryParameterNames.EnableAudioLogging],\r\n            [PropertyId.SpeechServiceConnection_EndSilenceTimeoutMs, QueryParameterNames.EndSilenceTimeoutMs],\r\n            [PropertyId.SpeechServiceConnection_InitialSilenceTimeoutMs, QueryParameterNames.InitialSilenceTimeoutMs],\r\n            [PropertyId.SpeechServiceResponse_ProfanityOption, QueryParameterNames.Profanity],\r\n            [PropertyId.SpeechServiceResponse_RequestWordLevelTimestamps, QueryParameterNames.EnableWordLevelTimestamps],\r\n            [PropertyId.SpeechServiceResponse_StablePartialResultThreshold, QueryParameterNames.StableIntermediateThreshold],\r\n        ]);\r\n\r\n        propertyIdToParameterMap.forEach((parameterName: string, propertyId: PropertyId): void => {\r\n            this.setUrlParameter(propertyId, parameterName, config, queryParams, endpoint);\r\n        });\r\n\r\n\r\n        const serviceProperties: IStringDictionary<string> = JSON.parse(config.parameters.getProperty(ServicePropertiesPropertyName, \"{}\")) as IStringDictionary<string>;\r\n\r\n        Object.keys(serviceProperties).forEach((value: string): void => {\r\n            queryParams[value] = serviceProperties[value];\r\n        });\r\n    }\r\n\r\n    protected setUrlParameter(\r\n        propId: PropertyId,\r\n        parameterName: string,\r\n        config: RecognizerConfig,\r\n        queryParams: IStringDictionary<string>,\r\n        endpoint: string): void {\r\n\r\n        const value: string = config.parameters.getProperty(propId, undefined);\r\n\r\n        // FIXME: The .search() check will incorrectly match parameter name anywhere in the string\r\n        //        including e.g. the path portion, or even as a substring of other query parameters\r\n        if (value && (!endpoint || endpoint.search(parameterName) === -1)) {\r\n            queryParams[parameterName] = value.toLocaleLowerCase();\r\n        }\r\n    }\r\n\r\n    public static async getRedirectUrlFromEndpoint(endpoint: string): Promise<string> {\r\n        // make a rest call to the endpoint to get the redirect url\r\n        const redirectUrl: URL = new URL(endpoint);\r\n        redirectUrl.protocol = \"https:\";\r\n        redirectUrl.port = \"443\";\r\n        const params: URLSearchParams = redirectUrl.searchParams;\r\n        params.append(\"GenerateRedirectResponse\", \"true\");\r\n\r\n        const redirectedUrlString: string = redirectUrl.toString();\r\n        Events.instance.onEvent(new ConnectionRedirectEvent(\"\", redirectedUrlString, undefined, \"ConnectionFactoryBase: redirectUrl request\"));\r\n\r\n        const redirectResponse: Response = await fetch(redirectedUrlString);\r\n\r\n        if (redirectResponse.status !== 200) {\r\n            return endpoint;\r\n        }\r\n\r\n        // Fix: properly read the response text\r\n        const redirectUrlString = await redirectResponse.text();\r\n\r\n        Events.instance.onEvent(new ConnectionRedirectEvent(\"\", redirectUrlString, endpoint, \"ConnectionFactoryBase: redirectUrlString\"));\r\n\r\n        try {\r\n            // Validate the URL before returning\r\n            const redirectUrl = new URL(redirectUrlString.trim());\r\n            if (redirectUrl.protocol === \"https:\") {\r\n                redirectUrl.protocol = \"wss:\";\r\n            } else if (redirectUrl.protocol === \"http:\") {\r\n                redirectUrl.protocol = \"ws:\";\r\n            }\r\n\r\n            return redirectUrl.toString();\r\n        } catch (error) {\r\n            return endpoint; // Return original endpoint if the redirect URL is invalid\r\n        }\r\n    }\r\n\r\n}\r\n"]}