Skip to content

Commit 1af2510

Browse files
authored
Implement #675: let wasm_js enable the backend by default (#730)
Implements #675. Closes #726. Now, when the `wasm_js` feature is enabled and `getrandom_backend` is not specified, this crate will use the `wasm_js` implementation by default (instead of failing to compile). The backend my still be overridden via `getrandom_backend` as with any other platform.
1 parent 18d8984 commit 1af2510

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ exclude = [".*"]
1717
std = []
1818

1919
# Optional backend: wasm_js
20-
# This flag enables the backend but does not select it. To use the backend, use
21-
# this flag *and* set getrandom_backend=wasm_js (see README).
20+
# This flag enables the wasm_js backend and uses it by default on wasm32 where
21+
# the target_os is unknown. The getrandom_backend cfg may override this.
2222
# WARNING: It is highly recommended to enable this feature only for binary crates and tests,
2323
# i.e. avoid unconditionally enabling it in library crates.
2424
wasm_js = ["dep:wasm-bindgen", "dep:js-sys"]

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,20 @@ the `wasm32-unknown-unknown` target (i.e. the target used by `wasm-pack`)
131131
is not automatically supported since, from the target name alone, we cannot deduce
132132
which JavaScript interface should be used (or if JavaScript is available at all).
133133

134-
To enable `getrandom`'s functionality on `wasm32-unknown-unknown` using the Web
135-
Crypto methods [described above][opt-in] via [`wasm-bindgen`], do
136-
*both* of the following:
137-
138-
- Use the `wasm_js` feature flag, i.e.
139-
`getrandom = { version = "0.3", features = ["wasm_js"] }`.
140-
On its own, this only makes the backend available. (As a side effect this
141-
will make your `Cargo.lock` significantly larger if you are not already
142-
using [`wasm-bindgen`], but otherwise enabling this feature is harmless.)
143-
- Set `RUSTFLAGS='--cfg getrandom_backend="wasm_js"'` ([see above][opt-in]).
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.
144138

145-
This backend supports both web browsers (main window and Web Workers)
146-
and Node.js (v19 or later) environments.
147-
148-
WARNING: It is highly recommended to enable the `wasm_js` feature only for
149-
binary crates and tests, i.e. avoid unconditionally enabling it in library crates.
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. Optionally, one can also set
142+
`RUSTFLAGS='--cfg getrandom_backend="wasm_js"'`.
143+
144+
WARNING: enabling the `wasm_js` feature will bloat `Cargo.lock` on all platforms
145+
(where [`wasm-bindgen`] is not an existing dependency) and is known to cause
146+
build issues on some non-web WASM platforms, even when a different backend is
147+
selected via `getrandom_backend`.
150148

151149
### Custom backend
152150

src/backends.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cfg_if! {
3131
} else if #[cfg(getrandom_backend = "windows_legacy")] {
3232
mod windows_legacy;
3333
pub use windows_legacy::*;
34-
} else if #[cfg(all(getrandom_backend = "wasm_js"))] {
34+
} else if #[cfg(getrandom_backend = "wasm_js")] {
3535
cfg_if! {
3636
if #[cfg(feature = "wasm_js")] {
3737
mod wasm_js;
@@ -186,13 +186,20 @@ cfg_if! {
186186
mod rdrand;
187187
pub use rdrand::*;
188188
} else if #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] {
189-
compile_error!(concat!(
190-
"The wasm32-unknown-unknown targets are not supported by default; \
191-
you may need to enable the \"wasm_js\" configuration flag. Note \
192-
that enabling the `wasm_js` feature flag alone is insufficient. \
193-
For more information see: \
194-
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
195-
));
189+
cfg_if! {
190+
if #[cfg(feature = "wasm_js")] {
191+
mod wasm_js;
192+
pub use wasm_js::*;
193+
} else {
194+
compile_error!(concat!(
195+
"The wasm32-unknown-unknown targets are not supported by default; \
196+
you may need to enable the \"wasm_js\" configuration flag. Note \
197+
that enabling the `wasm_js` feature flag alone is insufficient. \
198+
For more information see: \
199+
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
200+
));
201+
}
202+
}
196203
} else {
197204
compile_error!(concat!(
198205
"target is not supported. You may need to define a custom backend see: \

0 commit comments

Comments
 (0)