|
| 1 | +//! External getrandom implementation for use with JavaScript |
| 2 | +//! |
| 3 | +//! The crate uses [`getrandom`'s custom implementation mechanism][1] to provide |
| 4 | +//! support for JavaScript environments on the `wasm32-unknown-unknown` target. |
| 5 | +//! |
| 6 | +//! This crate supports web browsers and Node.js by directly calling the |
| 7 | +//! appropriate JavaScript functions. Specifically: |
| 8 | +//! - On web browsers, [`Crypto.getRandomValues`][2] is used. |
| 9 | +//! - On Node.js [`crypto.randomBytes`][3] is used. |
| 10 | +//! |
| 11 | +//! This crate works with either the [`wasm-bindgen`][4] or [`stdweb`][5] |
| 12 | +//! JavaScript shims. The correct shim will be automatically selected at compile |
| 13 | +//! time. Note that using `stdweb` requires [`cargo web`][6] v0.6.24 or higher. |
| 14 | +//! |
| 15 | +//! ## Using this crate |
| 16 | +//! |
| 17 | +//! First, depend on this crate by adding the following to your crate's `Cargo.toml`: |
| 18 | +//! ```toml |
| 19 | +//! [target.wasm32-unknown-unknown.dependencies] |
| 20 | +//! getrandom-js = 0.1 |
| 21 | +//! ``` |
| 22 | +//! Next, use `getrandom-js` somewhere in your crate. Explicit usage is |
| 23 | +//! needed to avoid linker errors. This can be done by adding the following to |
| 24 | +//! your crate's `src/lib.rs` or `src/main.rs`. |
| 25 | +//! ```rust |
| 26 | +//! #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 27 | +//! use getrandom_js as _; |
| 28 | +//! ``` |
| 29 | +//! |
| 30 | +//! Now, any use of `getrandom` (either as a direct or indirect dependency of |
| 31 | +//! your crate) on the `wasm32-unknown-unknown` target will call into the |
| 32 | +//! implementation defined by this crate. |
| 33 | +//! |
| 34 | +//! [1]: https://docs.rs/getrandom/#use-a-custom-implementation |
| 35 | +//! [2]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues |
| 36 | +//! [3]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback |
| 37 | +//! [4]: https://github.com/rustwasm/wasm-bindgen |
| 38 | +//! [5]: https://github.com/koute/stdweb |
| 39 | +//! [6]: https://github.com/koute/cargo-web |
| 40 | +
|
1 | 41 | // Necessary to work around complex macros in stdweb
|
2 | 42 | #![cfg_attr(cargo_web, recursion_limit = "128")]
|
3 | 43 |
|
|
0 commit comments