Skip to content

Commit 61e60c2

Browse files
committed
Update documentation and error messages
This allows users to get an actionable error message about this particular problem. We also add detection for this problem. Signed-off-by: Joe Richey <joerichey@google.com>
1 parent d69e8e0 commit 61e60c2

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn internal_desc(error: Error) -> Option<&'static str> {
168168
Error::WEB_CRYPTO => Some("Web Crypto API is unavailable"),
169169
Error::WEB_GET_RANDOM_VALUES => Some("Calling Web API crypto.getRandomValues failed"),
170170
Error::VXWORKS_RAND_SECURE => Some("randSecure: VxWorks RNG module is not initialized"),
171-
Error::NODE_CRYPTO => Some("Node.js crypto CommonJS module is unavailable"),
171+
Error::NODE_CRYPTO => Some("Node.js crypto CommonJS module is unavailable, see https://docs.rs/getrandom#nodejs-es6-module-support"),
172172
Error::NODE_RANDOM_FILL_SYNC => Some("Calling Node.js API crypto.randomFillSync failed"),
173173
_ => None,
174174
}

src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! | Emscripten | `*‑emscripten` | `/dev/random` (identical to `/dev/urandom`)
3232
//! | WASI | `wasm32‑wasi` | [`random_get`]
3333
//! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues`], see [WebAssembly support]
34-
//! | Node.js | `wasm32‑*‑unknown` | [`crypto.randomBytes`], see [WebAssembly support]
34+
//! | Node.js | `wasm32‑*‑unknown` | [`crypto.randomFillSync`], see [WebAssembly support]
3535
//! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
3636
//! | Nintendo 3DS | `armv6k-nintendo-3ds` | [`getrandom`][1]
3737
//!
@@ -91,6 +91,17 @@
9191
//!
9292
//! This feature has no effect on targets other than `wasm32-unknown-unknown`.
9393
//!
94+
//! #### Node.js ES6 Module support
95+
//!
96+
//! Due to limitations in wasm-bindgen's [`module`] support, we cannot directly
97+
//! support Node.js running in an ESM context. However, on Node v15 and later,
98+
//! the module author can add a simple shim to support the Web Cryptography API:
99+
//! ```js
100+
//! import { webcrypto } from 'node:crypto'
101+
//! globalThis.crypto = webcrypto
102+
//! ```
103+
//! This crate will then use the provided `webcrypto` implementation.
104+
//!
94105
//! ### Custom implementations
95106
//!
96107
//! The [`register_custom_getrandom!`] macro allows a user to mark their own
@@ -154,11 +165,12 @@
154165
//! [`RDRAND`]: https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide
155166
//! [`SecRandomCopyBytes`]: https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc
156167
//! [`cprng_draw`]: https://fuchsia.dev/fuchsia-src/zircon/syscalls/cprng_draw
157-
//! [`crypto.randomBytes`]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
168+
//! [`crypto.randomFillSync`]: https://nodejs.org/api/crypto.html#cryptorandomfillsyncbuffer-offset-size
158169
//! [`esp_fill_random`]: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html#_CPPv415esp_fill_randomPv6size_t
159170
//! [`random_get`]: https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#-random_getbuf-pointeru8-buf_len-size---errno
160171
//! [WebAssembly support]: #webassembly-support
161172
//! [`wasm-bindgen`]: https://github.com/rustwasm/wasm-bindgen
173+
//! [`module`]: https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-js-imports/module.html
162174
163175
#![doc(
164176
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",

0 commit comments

Comments
 (0)