Skip to content

Commit

Permalink
[wasm] Catch error from loading "node:crypto" module (#78916) (#80249)
Browse files Browse the repository at this point in the history
* Catch error from loading node:crypto module.
* Throw error with explanation when crypto module is not available.
* Fix providing error throwing polyfill.

Co-authored-by: carlossanlop <carlossanlop@users.noreply.github.com>
  • Loading branch information
carlossanlop and carlossanlop committed Jan 5, 2023
1 parent 05202cf commit 7a88642
Showing 1 changed file with 12 additions and 2 deletions.
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

0 comments on commit 7a88642

Please sign in to comment.