Skip to content

Commit e0c93b1

Browse files
committed
Catch call to module.require
This call throws an exception if module isn't defined. Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 0503000 commit e0c93b1

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/js.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ fn getrandom_init() -> Result<RngSource, Error> {
6868
c if c.is_object() => c,
6969
// Node.js CommonJS Crypto module
7070
_ if is_node(&global) => {
71-
// If require isn't a valid function, we are in an ES module.
72-
match Module::require_fn().dyn_into::<Function>() {
71+
// If module.require isn't a valid function, we are in an ES module.
72+
match Module::require_fn().and_then(JsCast::dyn_into::<Function>) {
7373
Ok(require_fn) => match require_fn.call1(&global, &JsValue::from_str("crypto")) {
7474
Ok(n) => return Ok(RngSource::Node(n.unchecked_into())),
7575
Err(_) => return Err(Error::NODE_CRYPTO),
@@ -126,8 +126,8 @@ extern "C" {
126126
// js_name = "module.require", so that Webpack doesn't give a warning. See:
127127
// https://github.com/rust-random/getrandom/issues/224
128128
type Module;
129-
#[wasm_bindgen(getter, static_method_of = Module, js_class = module, js_name = require)]
130-
fn require_fn() -> JsValue;
129+
#[wasm_bindgen(getter, static_method_of = Module, js_class = module, js_name = require, catch)]
130+
fn require_fn() -> Result<JsValue, JsValue>;
131131

132132
// Node JS process Object (https://nodejs.org/api/process.html)
133133
#[wasm_bindgen(method, getter)]

0 commit comments

Comments
 (0)