@@ -73,6 +73,27 @@ fn get_random_u128() -> Result<u128, getrandom::Error> {
7373
7474Pull Requests that add support for new targets to ` getrandom ` are always welcome.
7575
76+ ### WebAssembly support
77+
78+ This crate fully supports the [ WASI] and [ Emscripten] targets. However,
79+ the ` wasm32-unknown-unknown ` target (i.e. the target used by ` wasm-pack ` )
80+ is not automatically supported since, from the target name alone, we cannot deduce
81+ which JavaScript interface should be used (or if JavaScript is available at all).
82+
83+ We do not include support for this target in the default configuration because our JS backend
84+ (supporting web browsers, web workers and Node.js v19 or later) requires [ ` wasm-bindgen ` ] ,
85+ ** bloating ` Cargo.lock ` ** and ** potentially breaking builds** on non-web WASM platforms.
86+
87+ To enable ` getrandom ` 's functionality on ` wasm32-unknown-unknown ` using
88+ [ ` Crypto.getRandomValues ` ] via [ ` wasm-bindgen ` ] , enable the ` wasm_js ` crate feature.
89+
90+ WARNING: We strongly recommend against enabling this feature in libraries (except for tests)
91+ since it is known to break non-Web WASM builds and further since the usage of ` wasm-bindgen `
92+ causes significant bloat to ` Cargo.lock ` (on all targets).
93+
94+ The only exception to this rule: if your crate already unconditionally depends on ` wasm-bindgen `
95+ or ` js-sys ` on "unknown" WASM targets then it's acceptable to enable this feature unconditionally.
96+
7697### Opt-in backends
7798
7899` getrandom ` also provides optional (opt-in) backends, which allow users to customize the source
@@ -84,7 +105,6 @@ of randomness based on their specific needs:
84105| ` linux_raw ` | Linux, Android | ` *‑linux‑* ` | Same as ` linux_getrandom ` , but uses raw ` asm! ` -based syscalls instead of ` libc ` .
85106| ` rdrand ` | x86, x86-64 | ` x86_64-* ` , ` i686-* ` | [ ` RDRAND ` ] instruction
86107| ` rndr ` | AArch64 | ` aarch64-* ` | [ ` RNDR ` ] register
87- | ` wasm_js ` | Web Browser, Node.js | ` wasm32‑unknown‑unknown ` , ` wasm32v1-none ` | [ ` Crypto.getRandomValues ` ] . Enabled by the ` wasm_js ` feature ([ see below] ( #webassembly-support ) ).
88108| ` efi_rng ` | UEFI | ` *-unknown‑uefi ` | [ ` EFI_RNG_PROTOCOL ` ] with ` EFI_RNG_ALGORITHM_RAW ` (requires ` std ` and Nightly compiler)
89109| ` windows_legacy ` | Windows | ` *-windows-* ` | [ ` RtlGenRandom ` ]
90110| ` custom ` | All targets | ` * ` | User-provided custom implementation (see [ custom backend] )
@@ -94,8 +114,8 @@ Opt-in backends can be enabled using the `getrandom_backend` configuration flag.
94114The flag can be set either by specifying the ` rustflags ` field in [ ` .cargo/config.toml ` ] :
95115``` toml
96116# It's recommended to set the flag on a per-target basis:
97- [target .wasm32-unknown-unknown ]
98- rustflags = [' --cfg' , ' getrandom_backend="wasm_js "' ]
117+ [target .'cfg(target_os = "linux")' ]
118+ rustflags = [' --cfg' , ' getrandom_backend="linux_getrandom "' ]
99119```
100120
101121Or by using the ` RUSTFLAGS ` environment variable:
@@ -124,29 +144,6 @@ Note that the raw syscall backend may be slower than backends based on `libc::ge
124144e.g. it does not implement vDSO optimizations and on ` x86 ` it uses the infamously slow
125145` int 0x80 ` instruction to perform syscall.
126146
127- ### WebAssembly support
128-
129- This crate fully supports the [ WASI] and [ Emscripten] targets. However,
130- the ` wasm32-unknown-unknown ` target (i.e. the target used by ` wasm-pack ` )
131- is not automatically supported since, from the target name alone, we cannot deduce
132- which JavaScript interface should be used (or if JavaScript is available at all).
133-
134- We do not include support for this target in the default configuration because
135- our JS backend (supporting web browsers, web workers and Node.js v19 or later)
136- requires [ ` wasm-bindgen ` ] , ** bloating ` Cargo.lock ` ** and
137- ** potentially breaking builds** on non-web WASM platforms.
138-
139- To enable ` getrandom ` 's functionality on ` wasm32-unknown-unknown ` using the Web
140- Crypto methods [ described above] [ opt-in ] via [ ` wasm-bindgen ` ] , enable the
141- ` wasm_js ` feature flag. Setting ` RUSTFLAGS='--cfg getrandom_backend="wasm_js"' `
142- is allowed but is no longer required and does nothing (it was required in a
143- prior version of this crate).
144-
145- WARNING: enabling the ` wasm_js ` feature will bloat ` Cargo.lock ` on all platforms
146- (where [ ` wasm-bindgen ` ] is not an existing dependency) and is known to cause
147- build issues on some non-web WASM platforms, even when a different backend is
148- selected via ` getrandom_backend ` .
149-
150147### Custom backend
151148
152149If this crate does not support your target out of the box or you have to use
0 commit comments