Skip to content

Commit 83cfc12

Browse files
authored
[wasm] Catch error from loading "node:crypto" module (#78916)
* Catch error from loading node:crypto module. * Throw error with explanation when crypto module is not available. * Fix providing error throwing polyfill.
1 parent 7cf16f4 commit 83cfc12

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/mono/wasm/runtime/polyfills.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,18 @@ export async function init_polyfills_async(): Promise<void> {
201201
globalThis.crypto = <any>{};
202202
}
203203
if (!globalThis.crypto.getRandomValues) {
204-
const nodeCrypto = INTERNAL.require("node:crypto");
205-
if (nodeCrypto.webcrypto) {
204+
let nodeCrypto: any = undefined;
205+
try {
206+
nodeCrypto = INTERNAL.require("node:crypto");
207+
} catch (err: any) {
208+
// Noop, error throwing polyfill provided bellow
209+
}
210+
211+
if (!nodeCrypto) {
212+
globalThis.crypto.getRandomValues = () => {
213+
throw new Error("Using node without crypto support. To enable current operation, either provide polyfill for 'globalThis.crypto.getRandomValues' or enable 'node:crypto' module.");
214+
};
215+
} else if (nodeCrypto.webcrypto) {
206216
globalThis.crypto = nodeCrypto.webcrypto;
207217
} else if (nodeCrypto.randomBytes) {
208218
globalThis.crypto.getRandomValues = (buffer: TypedArray) => {

0 commit comments

Comments
 (0)