From e0c93b10d5fa11ad9983e7da3d2ca973f5554c48 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Thu, 6 Oct 2022 14:52:07 -0700 Subject: [PATCH] Catch call to module.require This call throws an exception if module isn't defined. Signed-off-by: Joe Richey --- src/js.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/js.rs b/src/js.rs index 979a28ba..59b7c0fe 100644 --- a/src/js.rs +++ b/src/js.rs @@ -68,8 +68,8 @@ fn getrandom_init() -> Result { c if c.is_object() => c, // Node.js CommonJS Crypto module _ if is_node(&global) => { - // If require isn't a valid function, we are in an ES module. - match Module::require_fn().dyn_into::() { + // If module.require isn't a valid function, we are in an ES module. + match Module::require_fn().and_then(JsCast::dyn_into::) { Ok(require_fn) => match require_fn.call1(&global, &JsValue::from_str("crypto")) { Ok(n) => return Ok(RngSource::Node(n.unchecked_into())), Err(_) => return Err(Error::NODE_CRYPTO), @@ -126,8 +126,8 @@ extern "C" { // js_name = "module.require", so that Webpack doesn't give a warning. See: // https://github.com/rust-random/getrandom/issues/224 type Module; - #[wasm_bindgen(getter, static_method_of = Module, js_class = module, js_name = require)] - fn require_fn() -> JsValue; + #[wasm_bindgen(getter, static_method_of = Module, js_class = module, js_name = require, catch)] + fn require_fn() -> Result; // Node JS process Object (https://nodejs.org/api/process.html) #[wasm_bindgen(method, getter)]