diff --git a/sdk/cosmosdb/cosmos/src/request/request.ts b/sdk/cosmosdb/cosmos/src/request/request.ts index 65a1e42daf74..f9726c3bb964 100644 --- a/sdk/cosmosdb/cosmos/src/request/request.ts +++ b/sdk/cosmosdb/cosmos/src/request/request.ts @@ -2,6 +2,7 @@ // Licensed under the MIT license. import { setAuthorizationHeader } from "../auth"; import { Constants, HTTPMethod, jsonStringifyAndEscapeNonASCII, ResourceType } from "../common"; +import { defaultLogger } from "../common/logger"; import { CosmosClientOptions } from "../CosmosClientOptions"; import { PartitionKey } from "../documents"; import { CosmosHeaders } from "../queryExecutionContext"; diff --git a/sdk/cosmosdb/cosmos/src/utils/digest.browser.ts b/sdk/cosmosdb/cosmos/src/utils/digest.browser.ts index b5a7cfa27218..84a240a1565b 100644 --- a/sdk/cosmosdb/cosmos/src/utils/digest.browser.ts +++ b/sdk/cosmosdb/cosmos/src/utils/digest.browser.ts @@ -2,11 +2,11 @@ // Licensed under the MIT license. import { encodeUTF8 } from "./encode"; -import { globalCrypto } from "./globalCrypto"; +import { getGlobalCrypto } from "./globalCrypto"; export async function digest(str: string): Promise { const data = encodeUTF8(str); - const hash = await globalCrypto.subtle.digest("SHA-256", data); + const hash = await getGlobalCrypto().subtle.digest("SHA-256", data); return bufferToHex(hash); } diff --git a/sdk/cosmosdb/cosmos/src/utils/globalCrypto.native.ts b/sdk/cosmosdb/cosmos/src/utils/globalCrypto.native.ts deleted file mode 100644 index db9582b8fbf1..000000000000 --- a/sdk/cosmosdb/cosmos/src/utils/globalCrypto.native.ts +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -// isomorphic-webcrypto is not listed as a dependency in package.json because -// doing so requires adding a bunch of react packages as peer dependencies. So, -// it is being loaded dynamically here to not cause compiler error. -const globalCrypto = require("isomorphic-webcrypto"); // eslint-disable-line import/no-extraneous-dependencies, @typescript-eslint/no-require-imports -export { globalCrypto }; diff --git a/sdk/cosmosdb/cosmos/src/utils/globalCrypto.ts b/sdk/cosmosdb/cosmos/src/utils/globalCrypto.ts index 551298e93f01..90078718a9fd 100644 --- a/sdk/cosmosdb/cosmos/src/utils/globalCrypto.ts +++ b/sdk/cosmosdb/cosmos/src/utils/globalCrypto.ts @@ -1,17 +1,24 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -// eslint-disable-next-line @azure/azure-sdk/ts-no-window -const globalRef: any = typeof self === "undefined" ? window : self; +let globalCrypto: Crypto | undefined; -if (!globalRef) { - throw new Error("Could not find global"); -} +export function getGlobalCrypto(): Crypto { + if (globalCrypto) { + return globalCrypto; + } + // eslint-disable-next-line @azure/azure-sdk/ts-no-window + const globalRef: any = typeof self === "undefined" ? window : self; -const globalCrypto: Crypto = globalRef.crypto || globalRef.msCrypto; + if (!globalRef) { + throw new Error("Could not find global"); + } -if (!globalCrypto || !globalCrypto.subtle) { - throw new Error("Browser does not support cryptography functions"); -} + globalCrypto = globalRef.crypto || globalRef.msCrypto; -export { globalCrypto }; + if (!globalCrypto || !globalCrypto.subtle) { + throw new Error("Environment does not support required cryptography functions"); + } + + return globalCrypto; +} diff --git a/sdk/cosmosdb/cosmos/src/utils/hmac.browser.ts b/sdk/cosmosdb/cosmos/src/utils/hmac.browser.ts index b9d0312d2add..d4363abf203d 100644 --- a/sdk/cosmosdb/cosmos/src/utils/hmac.browser.ts +++ b/sdk/cosmosdb/cosmos/src/utils/hmac.browser.ts @@ -3,7 +3,7 @@ import { encodeUTF8, encodeBase64 } from "./encode"; import atob from "./atob"; -import { globalCrypto } from "./globalCrypto"; +import { getGlobalCrypto } from "./globalCrypto"; export async function hmac(key: string, message: string): Promise { const importParams: HmacImportParams = { name: "HMAC", hash: { name: "SHA-256" } }; @@ -11,10 +11,14 @@ export async function hmac(key: string, message: string): Promise { [...unescape(encodeURIComponent(message))].map((c) => c.charCodeAt(0)) ); const encodedKey = encodeUTF8(atob(key)); - const cryptoKey = await globalCrypto.subtle.importKey("raw", encodedKey, importParams, false, [ - "sign", - ]); - const signature = await globalCrypto.subtle.sign(importParams, cryptoKey, encodedMessage); + const cryptoKey = await getGlobalCrypto().subtle.importKey( + "raw", + encodedKey, + importParams, + false, + ["sign"] + ); + const signature = await getGlobalCrypto().subtle.sign(importParams, cryptoKey, encodedMessage); return encodeBase64(signature); }