Skip to content

Commit

Permalink
Catch call to module.require
Browse files Browse the repository at this point in the history
This call throws an exception if module isn't defined.

Signed-off-by: Joe Richey <joerichey@google.com>
  • Loading branch information
josephlr committed Oct 6, 2022
1 parent 0503000 commit e0c93b1
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ fn getrandom_init() -> Result<RngSource, Error> {
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::<Function>() {
// If module.require isn't a valid function, we are in an ES module.
match Module::require_fn().and_then(JsCast::dyn_into::<Function>) {
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),
Expand Down Expand Up @@ -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<JsValue, JsValue>;

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

0 comments on commit e0c93b1

Please sign in to comment.