Skip to content

Commit

Permalink
crypto: simplify nodejs webcrypto
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink authored and jwerle committed May 2, 2023
1 parent 4e9adda commit 2643590
Showing 1 changed file with 6 additions and 10 deletions.
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 { Buffer } from './buffer.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

0 comments on commit 2643590

Please sign in to comment.