diff --git a/src/js.rs b/src/js.rs index 6b4c8cc2..e910f2bc 100644 --- a/src/js.rs +++ b/src/js.rs @@ -59,7 +59,9 @@ pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { fn getrandom_init() -> Result { let global: Global = global().unchecked_into(); if is_node(&global) { - let crypto = require("crypto").map_err(|_| Error::NODE_CRYPTO)?; + let crypto = NODE_MODULE + .require("crypto") + .map_err(|_| Error::NODE_CRYPTO)?; return Ok(RngSource::Node(crypto)); } @@ -102,9 +104,15 @@ extern "C" { #[wasm_bindgen(method, js_name = getRandomValues, catch)] fn get_random_values(this: &BrowserCrypto, buf: &Uint8Array) -> Result<(), JsValue>; + // We use a "module" object here instead of just annotating require() with + // js_name = "module.require", so that Webpack doesn't give a warning. See: + // https://github.com/rust-random/getrandom/issues/224 + type NodeModule; + #[wasm_bindgen(js_name = module)] + static NODE_MODULE: NodeModule; // Node JS crypto module (https://nodejs.org/api/crypto.html) - #[wasm_bindgen(catch, js_name = "module.require")] - fn require(s: &str) -> Result; + #[wasm_bindgen(method, catch)] + fn require(this: &NodeModule, s: &str) -> Result; type NodeCrypto; #[wasm_bindgen(method, js_name = randomFillSync, catch)] fn random_fill_sync(this: &NodeCrypto, buf: &mut [u8]) -> Result<(), JsValue>;