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

[release/7.0] [wasm] Catch error from loading "node:crypto" module #80249

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
14 changes: 12 additions & 2 deletions src/mono/wasm/runtime/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,18 @@ export async function init_polyfills_async(): Promise<void> {
globalThis.crypto = <any>{};
}
if (!globalThis.crypto.getRandomValues) {
const nodeCrypto = INTERNAL.require("node:crypto");
if (nodeCrypto.webcrypto) {
let nodeCrypto: any = undefined;
try {
nodeCrypto = INTERNAL.require("node:crypto");
} catch (err: any) {
// Noop, error throwing polyfill provided bellow
}

if (!nodeCrypto) {
globalThis.crypto.getRandomValues = () => {
throw new Error("Using node without crypto support. To enable current operation, either provide polyfill for 'globalThis.crypto.getRandomValues' or enable 'node:crypto' module.");
};
} else if (nodeCrypto.webcrypto) {
globalThis.crypto = nodeCrypto.webcrypto;
} else if (nodeCrypto.randomBytes) {
globalThis.crypto.getRandomValues = (buffer: TypedArray) => {
Expand Down