Skip to content

Commit 36878a4

Browse files
committed
getrandom-js: Document the custom RNG crate
The documentation can be previewed by running: cargo doc --no-deps --open --package getrandom-js --target=wasm32-unknown-unknown Signed-off-by: Joe Richey <[email protected]>
1 parent 28eaa7e commit 36878a4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

custom/js/src/lib.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
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+
141
// Necessary to work around complex macros in stdweb
242
#![cfg_attr(cargo_web, recursion_limit = "128")]
343

0 commit comments

Comments
 (0)