@@ -59,7 +59,9 @@ pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
59
59
fn getrandom_init ( ) -> Result < RngSource , Error > {
60
60
let global: Global = global ( ) . unchecked_into ( ) ;
61
61
if is_node ( & global) {
62
- let crypto = require ( "crypto" ) . map_err ( |_| Error :: NODE_CRYPTO ) ?;
62
+ let crypto = NODE_MODULE
63
+ . require ( "crypto" )
64
+ . map_err ( |_| Error :: NODE_CRYPTO ) ?;
63
65
return Ok ( RngSource :: Node ( crypto) ) ;
64
66
}
65
67
@@ -102,9 +104,15 @@ extern "C" {
102
104
#[ wasm_bindgen( method, js_name = getRandomValues, catch) ]
103
105
fn get_random_values ( this : & BrowserCrypto , buf : & Uint8Array ) -> Result < ( ) , JsValue > ;
104
106
107
+ // We use a "module" object here instead of just annotating require() with
108
+ // js_name = "module.require", so that Webpack doesn't give a warning. See:
109
+ // https://github.com/rust-random/getrandom/issues/224
110
+ type NodeModule ;
111
+ #[ wasm_bindgen( js_name = module) ]
112
+ static NODE_MODULE : NodeModule ;
105
113
// Node JS crypto module (https://nodejs.org/api/crypto.html)
106
- #[ wasm_bindgen( catch , js_name = "module.require" ) ]
107
- fn require ( s : & str ) -> Result < NodeCrypto , JsValue > ;
114
+ #[ wasm_bindgen( method , catch ) ]
115
+ fn require ( this : & NodeModule , s : & str ) -> Result < NodeCrypto , JsValue > ;
108
116
type NodeCrypto ;
109
117
#[ wasm_bindgen( method, js_name = randomFillSync, catch) ]
110
118
fn random_fill_sync ( this : & NodeCrypto , buf : & mut [ u8 ] ) -> Result < ( ) , JsValue > ;
0 commit comments