From 69e8ec02a5dd6baf4488666050edb3ff5b9308c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Mon, 28 Nov 2022 16:17:39 +0100 Subject: [PATCH 1/3] Catch error from loading node:crypto module. --- src/mono/wasm/runtime/polyfills.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/mono/wasm/runtime/polyfills.ts b/src/mono/wasm/runtime/polyfills.ts index b93eb1c3f211d0..eda79609f069b3 100644 --- a/src/mono/wasm/runtime/polyfills.ts +++ b/src/mono/wasm/runtime/polyfills.ts @@ -201,15 +201,19 @@ export async function init_polyfills_async(): Promise { globalThis.crypto = {}; } if (!globalThis.crypto.getRandomValues) { - const nodeCrypto = INTERNAL.require("node:crypto"); - if (nodeCrypto.webcrypto) { - globalThis.crypto = nodeCrypto.webcrypto; - } else if (nodeCrypto.randomBytes) { - globalThis.crypto.getRandomValues = (buffer: TypedArray) => { - if (buffer) { - buffer.set(nodeCrypto.randomBytes(buffer.length)); - } - }; + try { + const nodeCrypto = INTERNAL.require("node:crypto"); + if (nodeCrypto.webcrypto) { + globalThis.crypto = nodeCrypto.webcrypto; + } else if (nodeCrypto.randomBytes) { + globalThis.crypto.getRandomValues = (buffer: TypedArray) => { + if (buffer) { + buffer.set(nodeCrypto.randomBytes(buffer.length)); + } + }; + } + } catch (err: any) { + // Noop as node can be delivered without "node:crypto", but it's possible that we won't need it. } } } From d10bc630e6b00e1a09ffbf1de4105996ad37c79e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 29 Nov 2022 09:45:52 +0100 Subject: [PATCH 2/3] Throw error with explanation when crypto module is not available. --- src/mono/wasm/runtime/polyfills.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/mono/wasm/runtime/polyfills.ts b/src/mono/wasm/runtime/polyfills.ts index eda79609f069b3..82818ec3d7897e 100644 --- a/src/mono/wasm/runtime/polyfills.ts +++ b/src/mono/wasm/runtime/polyfills.ts @@ -201,19 +201,23 @@ export async function init_polyfills_async(): Promise { globalThis.crypto = {}; } if (!globalThis.crypto.getRandomValues) { + let nodeCrypto: any = undefined; try { - const nodeCrypto = INTERNAL.require("node:crypto"); - if (nodeCrypto.webcrypto) { - globalThis.crypto = nodeCrypto.webcrypto; - } else if (nodeCrypto.randomBytes) { - globalThis.crypto.getRandomValues = (buffer: TypedArray) => { - if (buffer) { - buffer.set(nodeCrypto.randomBytes(buffer.length)); - } - }; - } + nodeCrypto = INTERNAL.require("node:crypto"); } catch (err: any) { - // Noop as node can be delivered without "node:crypto", but it's possible that we won't need it. + // Noop, error throwing polyfill provided bellow + } + + if (!nodeCrypto) { + nodeCrypto.randomBytes = () => { 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) => { + if (buffer) { + buffer.set(nodeCrypto.randomBytes(buffer.length)); + } + }; } } } From d1f55d8949325bbba340fae54b73265a12a3aedd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Tue, 29 Nov 2022 12:33:39 +0100 Subject: [PATCH 3/3] Fix providing error throwing polyfill. --- src/mono/wasm/runtime/polyfills.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mono/wasm/runtime/polyfills.ts b/src/mono/wasm/runtime/polyfills.ts index 82818ec3d7897e..58f1bdd13eec06 100644 --- a/src/mono/wasm/runtime/polyfills.ts +++ b/src/mono/wasm/runtime/polyfills.ts @@ -209,7 +209,9 @@ export async function init_polyfills_async(): Promise { } if (!nodeCrypto) { - nodeCrypto.randomBytes = () => { throw new Error("Using node without crypto support. To enable current operation, either provide polyfill for 'globalThis.crypto.getRandomValues' or enable 'node:crypto' module."); }; + 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) {