Skip to content

Commit a2261e9

Browse files
committed
fix(core): resolve KSUID crypto source lazily so importing friendlyId never throws
1 parent a98ebac commit a2261e9

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

packages/core/src/v3/isomorphic/friendlyId.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ function loadNodeWebCrypto(): Crypto | undefined {
3939
}
4040
}
4141

42-
const getRandomValues: RandomFiller = resolveGetRandomValues();
42+
// Resolve the crypto source lazily on first use (memoized), so merely importing this
43+
// widely-used module never throws when crypto is unavailable — only minting a KSUID would.
44+
let cachedGetRandomValues: RandomFiller | undefined;
45+
const getRandomValues: RandomFiller = (array) =>
46+
(cachedGetRandomValues ??= resolveGetRandomValues())(array);
4347

4448
/** Encode raw bytes as base62 (big-endian), left-padded to the given length. */
4549
function base62Encode(bytes: Uint8Array, length: number): string {

0 commit comments

Comments
 (0)