Repository navigation
chore: land PRs #117-#120 on main (stacked merges went into their base branches) - #123
Merged
Merged
Conversation
`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.
`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.
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.
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.
…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]>
Contributor
|
Preview removed because the pull request was closed. |
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
mainonly contains #116All five PRs show as MERGED, but four of them were merged into their own base branch, not into
main. GitHub's merge button targets the PR's base, and for a stack each base is the PR below it:main?main128402conmainfix/uuid-v4-randomuuid-guarde90172don that branchfix/uuid-versioned-tobytes75ce468on that branchfix/unify-monotonic-overflowcc6551aon that branchchore/correct-stale-contracts997d020on that branchSo
mainhas the secure-context fix foruniku/uuid/v4and nothing else from the stack. Still missing:uuidv7.timestamp()rejecting non-v7 UUIDs (UUID_NOT_V7), and the doc correction thattoBytes()is a codec, not a version guard;80000000000000000000000000and stopped uuidv7 silently wrapping timestamps to zero;nanoid(0)/nanoid.isValid/STABILITY.mdcounter / stale-comment corrections;This PR is that reconciliation: 31 files, +589/−90, no new changes — just the four merged PRs' content, which is already reviewed and green. I verified
docs/benchmark-honestycontributes nothing beyondchore/correct-stale-contracts, so nothing is stranded on the bottom branch either.Please also hold #121
changeset-release/main(chore(release): version packages) is open againstmainand already consumed the only changeset there — theuuid/v4patch from #116. Merging it now would publish0.8.1with one of five fixes, then produce a second release PR when this lands and the remaining four changesets appear. Merging this first folds all five changesets into one release.If you would rather not use this PR
Equivalent outcomes: re-target #120/#119/#118/#117 to
mainbefore merging them (GitHub keeps them MERGED, so they cannot be re-opened — the branches are the only handle), or merge the branches directly. Deleting the stack branches after this lands is safe; deleting them before it loses four PRs' worth of work.