Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: simplify nodejs webcrypto #405

Merged
merged 1 commit into from
May 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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