Skip to content

Commit 2643590

Browse files
missinglinkjwerle
authored andcommitted
crypto: simplify nodejs webcrypto
1 parent 4e9adda commit 2643590

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

api/crypto.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ import { Buffer } from './buffer.js'
1616

1717
import * as exports from './crypto.js'
1818

19-
let getRandomValuesFallback = null
19+
/**
20+
* WebCrypto API
21+
* @see {https://developer.mozilla.org/en-US/docs/Web/API/Crypto}
22+
*/
23+
export let webcrypto = globalThis.crypto?.webcrypto ?? globalThis.crypto
2024

2125
const pending = []
2226

2327
if (globalThis?.process?.versions?.node) {
2428
pending.push(
2529
import('node:crypto')
2630
.then((module) => {
27-
getRandomValuesFallback = module.getRandomValues
31+
webcrypto = module.webcrypto
2832
})
2933
)
3034
}
@@ -54,12 +58,6 @@ export const ready = Promise.all(pending)
5458
*/
5559
export { sodium }
5660

57-
/**
58-
* WebCrypto API
59-
* @see {https://developer.mozilla.org/en-US/docs/Web/API/Crypto}
60-
*/
61-
export const webcrypto = globalThis.crypto?.webcrypto ?? globalThis.crypto
62-
6361
/**
6462
* Generate cryptographically strong random values into the `buffer`
6563
* @param {TypedArray} buffer
@@ -76,8 +74,6 @@ export function getRandomValues (buffer, ...args) {
7674
const output = toBuffer(buffer)
7775
input.copy(output)
7876
return buffer
79-
} else if (typeof getRandomValuesFallback === 'function') {
80-
return getRandomValuesFallback(buffer, ...args)
8177
}
8278

8379
console.warn('Missing implementation for globalThis.crypto.getRandomValues()')

0 commit comments

Comments
 (0)