Skip to content

Commit 3d7ea35

Browse files
committed
Migrate js/wasmJs implementations to jsWasmJs source set
1 parent 95495ec commit 3d7ea35

2 files changed

Lines changed: 12 additions & 149 deletions

File tree

library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/JsPlatform.kt

Lines changed: 0 additions & 105 deletions
This file was deleted.

library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/WasmJsPlatform.kt renamed to library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/JsWasmJsPlatform.kt

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,33 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
**/
16-
@file:Suppress("ACTUAL_ANNOTATIONS_NOT_MATCH_EXPECT", "KotlinRedundantDiagnosticSuppress", "UNUSED")
17-
1816
package org.kotlincrypto.random.internal
1917

2018
import org.kotlincrypto.random.RandomnessProcurementException
19+
import org.kotlincrypto.random.internal.js.IS_NODE_JS
20+
import org.kotlincrypto.random.internal.js.JsCrypto
21+
import org.kotlincrypto.random.internal.js.JsUint8Array
22+
import org.kotlincrypto.random.internal.js.get
23+
import org.kotlincrypto.random.internal.js.jsCryptoBrowser
24+
import org.kotlincrypto.random.internal.js.jsCryptoNode
25+
import org.kotlincrypto.random.internal.js.set
2126

2227
private const val BUFFER_SIZE = 1024 * 8
2328

24-
private external interface Crypto: JsAny {
25-
// Browser
26-
fun getRandomValues(array: Uint8Array)
27-
// Node.js
28-
fun randomFillSync(buf: Uint8Array)
29-
}
30-
31-
private open external class Uint8Array(length: Int) {
32-
fun subarray(start: Int, end: Int): Uint8Array
33-
}
34-
35-
@Suppress("UNUSED_PARAMETER")
36-
private fun uint8ArrayGet(obj: Uint8Array, index: Int): Byte = js("obj[index]")
37-
@Suppress("NOTHING_TO_INLINE")
38-
private inline operator fun Uint8Array.get(index: Int): Byte = uint8ArrayGet(this, index)
39-
40-
@Suppress("UNUSED_PARAMETER")
41-
private fun uint8ArraySet(obj: Uint8Array, index: Int, value: Byte) { js("obj[index] = value") }
42-
@Suppress("NOTHING_TO_INLINE")
43-
private inline operator fun Uint8Array.set(index: Int, value: Byte) { uint8ArraySet(this, index, value) }
44-
45-
private fun isNodeJs(): Boolean = js(
46-
"""
47-
(typeof process !== 'undefined'
48-
&& process.versions != null
49-
&& process.versions.node != null) ||
50-
(typeof window !== 'undefined'
51-
&& typeof window.process !== 'undefined'
52-
&& window.process.versions != null
53-
&& window.process.versions.node != null)
54-
"""
55-
)
56-
57-
private fun cryptoNode(): Crypto = js("eval('require')('crypto')")
58-
private fun cryptoBrowser(): Crypto = js("(window ? (window.crypto ? window.crypto : window.msCrypto) : self.crypto)")
59-
60-
private val IS_NODE_JS: Boolean by lazy { isNodeJs() }
61-
private val CRYPTO: Crypto by lazy { if (IS_NODE_JS) cryptoNode() else cryptoBrowser() }
29+
private val JS_CRYPTO: JsCrypto by lazy { if (IS_NODE_JS) jsCryptoNode() else jsCryptoBrowser() }
6230

63-
//@Throws(RandomnessProcurementException::class)
31+
@Throws(RandomnessProcurementException::class)
6432
internal actual fun ByteArray.cryptoRandFill() {
6533
try {
66-
val jsCryptoFill = if (IS_NODE_JS) CRYPTO::randomFillSync else CRYPTO::getRandomValues
34+
val jsCryptoFill = if (IS_NODE_JS) JS_CRYPTO::randomFillSync else JS_CRYPTO::getRandomValues
6735

6836
// Cannot simply use the ByteArray when calling the supplied Crypto function.
6937
// Must utilize Uint8Array and then copy over results (See Issue #8). Also,
7038
// by chunking in a size less than 65536, it avoids hitting the ceiling imposed
7139
// on JS Browser (See issue #9).
7240
if (size <= BUFFER_SIZE) {
7341
// 1 shot it
74-
val buf = Uint8Array(size)
42+
val buf = JsUint8Array(size)
7543
jsCryptoFill(buf)
7644
for (i in indices) {
7745
this[i] = buf[i]
@@ -80,7 +48,7 @@ internal actual fun ByteArray.cryptoRandFill() {
8048
return
8149
}
8250

83-
val buf = Uint8Array(BUFFER_SIZE)
51+
val buf = JsUint8Array(BUFFER_SIZE)
8452

8553
var needed = size
8654
var pos = 0

0 commit comments

Comments
 (0)