Skip to content

Commit

Permalink
[wasm] Catch error from loading "node:crypto" module (dotnet#78916)
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.
  • Loading branch information
maraf committed Dec 7, 2022
1 parent 4ebb4a9 commit 0ec98b0
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 0ec98b0

Please sign in to comment.