…e branches) (#123)
* fix(uuid): load uniku/uuid/v4 without a secure context
`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.
* fix(uuid): guard uuidv7.timestamp against non-v7 UUIDs
`uuidv7.timestamp()` read its first six bytes through the version-agnostic byte
codec, so `uuidv7.timestamp(someV4)` returned a number that decodes to a
plausible date. `typeid.timestamp()` already refused that input with
`UUID_NOT_V7`; the two timestamp readers disagreed.
- `uuidv7.timestamp()` now requires the RFC 9562 version 7 and `10xx` variant
bits and throws `InvalidInputError` with code `UUID_NOT_V7` otherwise. NIL and
MAX still work, since RFC 9562 defines them without a version.
- Extract `isUuidVersion7` and `isNilOrMaxBytes` into `uuid/common/uuid.ts` and
use them from both `uuid/v7` and `typeid`, so the two checks cannot drift.
- Document why `toBytes()` stays version-agnostic: it is a byte codec that has to
parse arbitrary namespaces (the RFC v5 namespaces are version 1 UUIDs), while
`isValid()` remains the version guard.
`UUID_NOT_V7` now lists `uuid` and `typeid` as strategies.
* fix(ulid): unify monotonic overflow with uuidv7
UUID v7 and ULID hit the same situation with two different answers. When the
per-time-unit sequence is exhausted, `uuidv7()` advanced its timestamp by one
millisecond and carried on; `ulid()` filled the remaining random bytes with
`0xff` and threw `RANDOM_OVERFLOW`, which also left every later call in that
millisecond failing.
Both now advance virtual time by one millisecond and continue from a zeroed
counter, so `incrementBytesInPlace` leaving the buffer at zero on overflow is
exactly the state the new bucket wants.
That unification exposed a bug both generators already had: neither bounded the
advance against the end of the 48-bit timestamp field. At the maximum
millisecond, uuidv7 wrote a timestamp that wrapped to zero, ULID's string path
emitted `80000000000000000000000000` (which its own `isValid()` rejects), and
ULID's buffer path also wrapped the timestamp. Both now saturate and throw
`TIMESTAMP_OUT_OF_RANGE`, and ULID keeps the `fill(0xff)` that makes the
saturated state fail closed on every later call.
`RANDOM_OVERFLOW` is removed from `ERROR_CODES`: the random-portion overflow now
recovers instead of failing, so there is no such failure to report. Pre-1.0, this
is a breaking change to the error surface; `migration/v1.mdx` documents the code
as removed with no replacement, and drops the `ULID_RANDOM_OVERFLOW` mapping.
* chore: correct stale comments and contracts that contradicted the code
Six small statements disagreed with the implementation. None changes a valid
input's output; two tighten input handling that was already documented as strict.
- `nanoid(0)` and `nanoid({ length: 0 })` returned `''` while the JSDoc advertised
`@throws Length must be between 0 and 2048` and the CLI documented `1-256`.
Length is now one integer check over `1..2048` with a single message. An empty
string is never a usable identifier, and every other invalid length threw.
- `nanoid.isValid()` cannot check a generated length because a nanoid carries none.
JSDoc and the reference page now say that outright instead of leaving the gap
implicit for anyone reaching for it as a boundary validator.
- `nanoid` read the bare `crypto` global while every other module uses
`globalThis.crypto`, which is the convention AGENTS.md states.
- `encodeBase62` and `encodeObjectIdHex` accepted over-long inputs and encoded only
the leading bytes, while their public `fromBytes()` wrappers required the exact
length. Both now require the exact length.
- `docs/STABILITY.md` called the ObjectID and XID counters "always-incrementing".
Both wrap to zero after 2^24 values in the same second; the contract now says so.
- Corrected two comments: `incrementBytesInPlace` credited KSUID with a call it
never makes, and the `objectid/hex.ts` decode-table note reported 128 KiB for a
64 KiB allocation.
* docs(bench): report benchmark dispersion and stop overstating ratios (#120)
The performance guide reported every ratio to two significant figures with no
measure of uncertainty. The harness does not support that precision: the same row
moved between 1.2x and 1.6x across runs of the same command on one machine, and
the ULID row is 163x in the published CI snapshot versus ~85x on other hardware.
Reporting:
- `bench-summary` gains a `Spread` column built from each row's
`withinActionRme`/`crossActionRme`, and reports `parity (within spread)` when a
ratio's deviation from 1x is smaller than its own combined dispersion. A
snapshot with no multi-run data prints `—` rather than an invented number.
- `benchmarkDispersion` and `combineDispersion` live in `bench-results.ts` next to
the other statistics, prefer multi-run dispersion over a single run's RME, and
combine independent dispersions in quadrature.
- The performance page reports ranges rounded to about one significant figure and
says what each row does and does not compare: the UUID v4 row is a control that
compares two identical `crypto.randomUUID()` calls, the TypeID/KSUID/TSID rows
measure API shape as well as encoding, the ULID row is dominated by the
reference re-detecting its PRNG per call, and every number is V8-only.
The bundle-size table is unchanged: it reproduces byte-for-byte with
`pnpm bundle:summary`, so it never had a precision problem.
Co-authored-by: jkomyno <[email protected]>
---------
Co-authored-by: jkomyno <[email protected]>
Stacked on #116.
Problem
uuidv7.timestamp()read its first six bytes through the version-agnostic byte codec:So
uuidv7.timestamp(someV4)returned a number that decodes to a plausible date.typeid.timestamp()already refused that input withUUID_NOT_V7; the two timestamp readers disagreed about what a timestamp is.The adjacent surprise is that
isValidis version-strict whiletoBytesis not. That asymmetry is intentional —toBytesis a byte codec that has to parse arbitrary namespaces, and the RFC 9562 v5 namespaces are version 1 UUIDs (6ba7b810-9dad-11d1-...). MakingtoBytesversion-strict breaksuuidv5.toBytes(uuidv5.DNS), which existing tests use and which callers legitimately need. It was undocumented, though, which is what makes it look like a bug.Fix
uuidv7.timestamp()requires the RFC 9562 version 7 and10xxvariant bits and throwsInvalidInputErrorwith codeUUID_NOT_V7otherwise, matchingtypeid.timestamp(). TheNILandMAXsentinels keep working, since RFC 9562 defines them without a version.isUuidVersion7andisNilOrMaxBytesintouuid/common/uuid.tsand use them from bothuuid/v7andtypeid, so the two checks cannot drift again.toBytesstays version-agnostic, in the type declaration of all three UUID modules, plus the migration table that mapsuuid.parse()to it.UUID_NOT_V7now listsuuidandtypeidas strategies.Tests
timestamp version guardasserts UUID v4, UUID v5, and a v7-nibble/non-RFC-variant value all throwUUID_NOT_V7withstrategy: 'uuid', and thattimestamp(NIL) === 0andtimestamp(MAX) === 0xffffffffffffstill hold.Scope
One deliberate behavior change:
uuidv7.timestamp()on a non-v7 value now throws instead of returning garbage. Markedminorin the changeset.