`Crypto.randomUUID` is secure-context-only while `Crypto.getRandomValues` is
not, so an insecure browser origin exposes `crypto` with `randomUUID` missing.
The module captured `globalThis.crypto.randomUUID.bind(globalThis.crypto)` at
module scope, which threw during module evaluation there:
TypeError: Cannot read properties of undefined (reading 'bind')
That took down the whole entry point, including `toBytes`, `fromBytes`, and
`isValid`, none of which touch randomness.
Resolve `randomUUID` once and guard it. The no-argument generator keeps using
`crypto.randomUUID()` where available and otherwise falls back to the shared
CSPRNG pool plus the local formatter, which is the path `_v4` already used.
Problem
Crypto.randomUUIDis available only in secure contexts, whileCrypto.getRandomValuesis not. On an insecure browser origin (plainhttp://on a LAN address, an embedded panel, some webviews)cryptoexists butrandomUUIDdoes not.src/uuid/v4.tscaptured the method at module scope:So the import itself threw:
That took down the entire entry point, including
toBytes,fromBytes, andisValid, none of which touch randomness. It contradicts the README claim that the library runs in browsers.Fix
Resolve
randomUUIDonce and guard it. The no-argument generator keeps usingcrypto.randomUUID()where available and otherwise falls back to the shared CSPRNG pool plus the local formatter — the path_v4already used foruuidv4({}).uuid@14guards the same call (if (!buf && !options && crypto.randomUUID)), so this also aligns the two implementations.Tests
New
without crypto.randomUUID (insecure origin)block stubscryptodown togetRandomValues, resets modules, and asserts that the entry point imports, generates valid v4 strings, keeps the version and variant bits, returns distinct IDs from the fallback buffer, and still exposes the byte helpers.Scope
No change to any existing output. On a secure context the code path is unchanged.
Stacked: this is PR 1 of 5.