Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions api/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ import console from './console.js'

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

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

const pending = []

if (globalThis?.process?.versions?.node) {
pending.push(
import('node:crypto')
.then((module) => {
getRandomValuesFallback = module.getRandomValues
webcrypto = module.webcrypto
})
)
}
Expand Down Expand Up @@ -54,12 +58,6 @@ export const ready = Promise.all(pending)
*/
export { sodium }

/**
* WebCrypto API
* @see {https://developer.mozilla.org/en-US/docs/Web/API/Crypto}
*/
export const webcrypto = globalThis.crypto?.webcrypto ?? globalThis.crypto

/**
* Generate cryptographically strong random values into the `buffer`
* @param {TypedArray} buffer
Expand All @@ -76,8 +74,6 @@ export function getRandomValues (buffer, ...args) {
const output = toBuffer(buffer)
input.copy(output)
return buffer
} else if (typeof getRandomValuesFallback === 'function') {
return getRandomValuesFallback(buffer, ...args)
}

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