From 010ab1ddb9ac40ed6f8982795a8bbd83b8128446 Mon Sep 17 00:00:00 2001 From: David LeGare Date: Wed, 2 Mar 2022 16:21:18 +0000 Subject: Update getrandom to 0.2.5 Test: cd external/rust/crates && atest --host -c Change-Id: I1e16cc05e960f5b53d01f5d034f14ae10929f885 --- src/js.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/js.rs') diff --git a/src/js.rs b/src/js.rs index 657d2ac..e910f2b 100644 --- a/src/js.rs +++ b/src/js.rs @@ -10,8 +10,8 @@ use crate::Error; extern crate std; use std::thread_local; -use js_sys::Uint8Array; -use wasm_bindgen::{prelude::*, JsCast}; +use js_sys::{global, Uint8Array}; +use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue}; // Maximum is 65536 bytes see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues const BROWSER_CRYPTO_BUFFER_SIZE: usize = 256; @@ -57,9 +57,11 @@ pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { } fn getrandom_init() -> Result { - let global: Global = js_sys::global().unchecked_into(); + 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>; -- cgit v1.2.3