From 2d704192ab7ca5926cf57afa1fa30d028b753380 Mon Sep 17 00:00:00 2001 From: Victor Turansky Date: Tue, 29 Oct 2024 13:13:47 +0200 Subject: [PATCH 1/2] KTOR-6158 JS. Remove redundant `require` call for `crypto` --- .../jsAndWasmShared/src/io/ktor/util/CryptoJs.kt | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt b/ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt index a18c878e8fa..6ef276bf26b 100644 --- a/ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt +++ b/ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt @@ -41,17 +41,9 @@ public actual fun Digest(name: String): Digest = object : Digest { } } -private fun requireCrypto(): Crypto = js("eval('require')('crypto')") -private fun windowCrypto(): Crypto = js("(window ? (window.crypto ? window.crypto : window.msCrypto) : self.crypto)") - // Variable is renamed to `_crypto` so it wouldn't clash with existing `crypto` variable. // JS IR backend doesn't reserve names accessed inside js("") calls -private val _crypto: Crypto by lazy { // lazy because otherwise it's untestable due to evaluation order - when { - PlatformUtils.IS_NODE -> requireCrypto() - else -> windowCrypto() - } -} +private val _crypto: Crypto = js("(globalThis ? globalThis.crypto : (window.crypto || window.msCrypto))") private external class Crypto { val subtle: SubtleCrypto From 0dae1dd5e3a3bb8bde3d46ff71ab20a598ad9a42 Mon Sep 17 00:00:00 2001 From: Victor Turansky Date: Tue, 29 Oct 2024 18:54:35 +0200 Subject: [PATCH 2/2] KTOR-6158 JS. Cleanup `Crypto` --- ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt b/ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt index 6ef276bf26b..9460e953f3e 100644 --- a/ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt +++ b/ktor-utils/jsAndWasmShared/src/io/ktor/util/CryptoJs.kt @@ -13,10 +13,7 @@ import kotlin.js.* */ public actual fun generateNonce(): String { val buffer = ByteArray(NONCE_SIZE_IN_BYTES).toJsArray() - when { - PlatformUtils.IS_NODE -> _crypto.randomFillSync(buffer) - else -> _crypto.getRandomValues(buffer) - } + _crypto.getRandomValues(buffer) return hex(buffer.toByteArray()) } @@ -49,8 +46,6 @@ private external class Crypto { val subtle: SubtleCrypto fun getRandomValues(array: Int8Array) - - fun randomFillSync(array: Int8Array) } private external class SubtleCrypto {