Skip to content

chore: correct stale comments and contracts that contradicted the code - #119

Merged
jkomyno merged 1 commit into
fix/unify-monotonic-overflowfrom
chore/correct-stale-contracts
Nov 16, 2026
Merged

jkomyno merged 1 commit into
fix/unify-monotonic-overflowfrom
chore/correct-stale-contracts

Conversation

@jkomyno

@jkomyno jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Stacked on #118.

Six statements disagreed with the implementation. None changes a valid input's output; two tighten input handling that was already documented as strict.

1. nanoid(0) returned an empty string

if (size === 0) return ''

while the JSDoc advertised @throws Length must be between 0 and 2048 and the CLI documents Length of ID (1-256). Zero is now rejected like every other invalid length, with one check over 1..2048 and one message.

2. nanoid.isValid() was documented as if it checked anything useful

It cannot check the length, because a nanoid carries none — nanoid.isValid('a') and nanoid.isValid('a'.repeat(4000)) are both true. That is a property of the format, not a bug, but the docs sold it as boundary validation. JSDoc and the reference page now say plainly that it only checks the default alphabet.

3. nanoid used the bare crypto global

Every other module uses globalThis.crypto, which is the convention AGENTS.md states. Now it does too.

4. Two internal encoders silently truncated

if (bytes.length < KSUID_BYTES) { throw ... }   // then loops over exactly 20

Passing 32 bytes encoded only the first 20. The public fromBytes() wrappers already required the exact length, so this was unreachable, but it is a trap for the next caller. Both now require the exact length.

5. docs/STABILITY.md called the ObjectID and XID counters "always-incrementing"

They wrap: (state.counter + 1) & 0xffffff. After 2^24 values in the same second the counter returns to a previous value and the ID repeats. Spec-faithful for MongoDB, but the contract document said the opposite of the code.

6. Two wrong comments

  • incrementBytesInPlace credited KSUID with a call KSUID never makes (KSUID has no monotonic sequencing).
  • objectid/hex.ts reported its decoding table as "128 KiB"; new Uint8Array(65536) is 64 KiB.

Tests

nanoid length 0 / 2049 rejection, the 1 and 2048 message, nanoid(2048) succeeding, and exact-length rejection for ksuid.fromBytes / objectid.fromBytes at 0, 11/19, 13/21, and 16/32 bytes.

Changeset is minor because nanoid(0) changes from returning an empty string to throwing.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Preview removed because the pull request was closed.

@pkg-pr-new

pkg-pr-new Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

pnpm add https://pkg.pr.new/@uniku/cli@119
pnpm add https://pkg.pr.new/uniku@119

commit: d04a92c

@jkomyno
jkomyno force-pushed the fix/unify-monotonic-overflow branch from 6880ddb to 59e495b Compare November 15, 2026 22:49
@jkomyno
jkomyno force-pushed the chore/correct-stale-contracts branch 2 times, most recently from c5afbf7 to e2903cb Compare November 15, 2026 22:53
@jkomyno

jkomyno commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Codex review

Reviewed with codex exec (read-only sandbox) against fix/unify-monotonic-overflow. Two findings, both about wording I got wrong. Both fixed in e2903cb.

1. "Rejects IDs generated with a custom alphabet" was false

I wrote that in two places. It is wrong: isValid tests membership in the default URL-safe alphabet, so an ID built from a hex alphabet passes — 000000000000 is true. The validator cannot know which alphabet produced a value, so "rejects custom alphabets" overstates the guarantee in exactly the direction that matters for a boundary check.

Now: it checks a non-empty run of the default alphabet, cannot verify length, and an ID from a custom alphabet passes whenever that alphabet is a subset of the default one.

2. The stability contract implied a per-second counter reset

I wrote that the ObjectID and XID counters "wrap to zero after 2^24 values in the same second", which reads as though the counter resets each second. It does not — it is a plain modulo counter that increments on every call regardless of the timestamp. Codex reproduced the real behaviour:

objectid: counters ["ffffff","000000"], timestampSeconds [1700000000, 1700000001]
xid:      counters ["ffffff","000000"], timestampSeconds [1700000000, 1700000001]

docs/STABILITY.md now separates the two facts that matter: the counter wraps modulo 2^24 and keeps incrementing across seconds, and a full cycle that returns to a value emitted in the same second — with the same per-process identity — produces a duplicate. Stated as the format's own design limit, requiring 2^24 IDs from one process inside one second.

Also verified by the review, no action needed

  • No caller of encodeBase62/encodeObjectIdHex relied on over-long input; the stricter check does not touch generation paths, NIL/MAX, or the CLI.
  • No documented behaviour, CLI path, or example depended on nanoid(0) returning ''; the CLI already documents 1-256.
  • globalThis.crypto is a no-op change across Deno, Bun, workers, and browsers, and matches the convention the rest of the package uses.

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.
@jkomyno
jkomyno force-pushed the fix/unify-monotonic-overflow branch from 59e495b to c7feebf Compare November 15, 2026 23:03
@jkomyno
jkomyno force-pushed the chore/correct-stale-contracts branch from e2903cb to d04a92c Compare November 15, 2026 23:03
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Results

Generation

ID uniku npm/regex Comparison
ULID 3.7M ops/s 28.2K ops/s 130.3x faster ⚠️ ±14.7%
UUID v7 3.5M ops/s 471K ops/s 7.3x faster
CUID2 44.3K ops/s 6.6K ops/s 6.7x faster ⚠️ ±4.6%
TypeID 2.1M ops/s 574.5K ops/s 3.6x faster
UUID v5 1.1M ops/s 356.9K ops/s 3.1x faster ⚠️ ±9.2%
TSID 6.9M ops/s 2.6M ops/s 2.7x faster
UUID v5 (256-char name) 408.7K ops/s 199.4K ops/s 2x faster ⚠️ ±14.8%
KSUID 370K ops/s 213.8K ops/s 1.7x faster ⚠️ ±3.9%
XID 7.4M ops/s 5.5M ops/s 1.3x faster
UUID v4 10.4M ops/s 8.8M ops/s 1.2x faster
NanoID 10M ops/s 14.8M ops/s npm 1.5x faster
NanoID (10) 11.8M ops/s 18.2M ops/s npm 1.5x faster
ObjectID 4.8M ops/s 7.8M ops/s npm 1.6x faster

Validation

ID uniku npm/regex Comparison
KSUID 8.6M ops/s 697.8K ops/s 12.3x faster
TypeID 4.3M ops/s 1.2M ops/s 3.7x faster
TSID 4.3M ops/s 1.4M ops/s 3x faster ⚠️ ±5.5%
XID 9.9M ops/s 3.5M ops/s 2.8x faster ⚠️ ±14.6%
ULID 9.3M ops/s 3.6M ops/s 2.6x faster
UUID v5 9M ops/s 4.9M ops/s 1.8x faster
UUID v7 8.9M ops/s 5M ops/s 1.8x faster
UUID v4 8.8M ops/s 4.9M ops/s 1.8x faster
CUID2 12.8M ops/s 14.1M ops/s npm 1.1x faster
NanoID 12.6M ops/s 14.2M ops/s regex 1.1x faster
ObjectID 9.5M ops/s 11.5M ops/s npm 1.2x faster

Cold Start (Node)

Generator Process to exit (median / p95) Import + first ID (median / p95)
uuid/v4 29.9 ms / 33.1 ms 5.3 ms / 5.6 ms
uuid/v5 27.9 ms / 29.5 ms 3.9 ms / 4.0 ms
uuid/v7 30.9 ms / 32.3 ms 5.8 ms / 6.2 ms
ulid 30.4 ms / 33.7 ms 5.7 ms / 5.9 ms
typeid 31.3 ms / 34.8 ms 6.5 ms / 6.7 ms
cuid2 38.7 ms / 40.7 ms 13.7 ms / 14.8 ms
cuid/v2 38.4 ms / 40.0 ms 13.7 ms / 14.4 ms
nanoid 29.5 ms / 32.3 ms 5.0 ms / 5.2 ms
ksuid 31.2 ms / 34.4 ms 6.2 ms / 6.5 ms
objectid 31.5 ms / 35.9 ms 6.3 ms / 6.5 ms
tsid 30.4 ms / 33.3 ms 5.5 ms / 5.8 ms
xid 31.2 ms / 32.9 ms 6.2 ms / 6.5 ms

Comparison vs Main

Benchmark Baseline Current Change
tests/bench/compat.bench.ts > Generation > ULID > uniku 4.6M ops/s 3.7M ops/s 🔴 -21.0% (+/-2.3% RME)
tests/bench/compat.bench.ts > Validation > TypeID > uniku 4.5M ops/s 4.3M ops/s ⚪ -3.7% (+/-1.9% RME)
tests/bench/compat.bench.ts > Generation > XID > uniku 7.2M ops/s 7.4M ops/s ⚪ +2.8% (+/-2.8% RME)
tests/bench/compat.bench.ts > Validation > UUID v7 > uniku 8.6M ops/s 8.9M ops/s ⚪ +2.7% (+/-3.9% RME)
tests/bench/compat.bench.ts > Generation > UUID v4 > uniku 10.7M ops/s 10.4M ops/s ⚪ -2.6% (+/-7.6% RME)
tests/bench/compat.bench.ts > Generation > NanoID > uniku 9.8M ops/s 10M ops/s ⚪ +1.9% (+/-6.8% RME)
tests/bench/compat.bench.ts > Validation > ULID > uniku 9.5M ops/s 9.3M ops/s ⚪ -1.9% (+/-6.0% RME)
tests/bench/compat.bench.ts > Validation > XID > uniku 10.1M ops/s 9.9M ops/s ⚪ -1.7% (+/-3.5% RME)
tests/bench/compat.bench.ts > Validation > UUID v5 > uniku 8.9M ops/s 9M ops/s ⚪ +1.3% (+/-2.5% RME)
tests/bench/compat.bench.ts > Generation > ObjectID > uniku 4.7M ops/s 4.8M ops/s ⚪ +1.2% (+/-2.4% RME)
tests/bench/compat.bench.ts > Generation > NanoID (10) > uniku 11.9M ops/s 11.8M ops/s ⚪ -1.1% (+/-2.1% RME)
tests/bench/compat.bench.ts > Validation > UUID v4 > uniku 8.9M ops/s 8.8M ops/s ⚪ -1.0% (+/-3.4% RME)
tests/bench/compat.bench.ts > Generation > TypeID > uniku 2M ops/s 2.1M ops/s ⚪ +0.8% (+/-3.1% RME)
tests/bench/compat.bench.ts > Validation > NanoID > uniku 12.5M ops/s 12.6M ops/s ⚪ +0.7% (+/-7.2% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 > uniku 1.1M ops/s 1.1M ops/s ⚪ +0.7% (+/-7.6% RME)
tests/bench/compat.bench.ts > Validation > ObjectID > uniku 9.5M ops/s 9.5M ops/s ⚪ -0.7% (+/-1.7% RME)
tests/bench/compat.bench.ts > Generation > UUID v7 > uniku 3.5M ops/s 3.5M ops/s ⚪ -0.6% (+/-2.4% RME)
tests/bench/compat.bench.ts > Validation > CUID2 > uniku 12.7M ops/s 12.8M ops/s ⚪ +0.5% (+/-4.0% RME)
tests/bench/compat.bench.ts > Generation > CUID2 > uniku 44.2K ops/s 44.3K ops/s ⚪ +0.4% (+/-1.2% RME)
tests/bench/compat.bench.ts > Generation > KSUID > uniku 368.8K ops/s 370K ops/s ⚪ +0.3% (+/-1.3% RME)
tests/bench/compat.bench.ts > Validation > TSID > uniku 4.4M ops/s 4.3M ops/s ⚪ -0.2% (+/-10.9% RME)
tests/bench/compat.bench.ts > Generation > TSID > uniku 6.9M ops/s 6.9M ops/s ⚪ -0.2% (+/-1.8% RME)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > uniku 408.2K ops/s 408.7K ops/s ⚪ +0.1% (+/-7.6% RME)
tests/bench/compat.bench.ts > Validation > KSUID > uniku 8.6M ops/s 8.6M ops/s ⚪ -0.1% (+/-4.0% RME)
tests/bench/compat.bench.ts > Validation > XID > npm 3.8M ops/s 3.5M ops/s ℹ️ -6.8% (+/-22.4% RME) (ref)
tests/bench/compat.bench.ts > Validation > TypeID > npm 1.2M ops/s 1.2M ops/s ℹ️ -5.1% (+/-7.2% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v5 > npm 4.8M ops/s 4.9M ops/s ℹ️ +2.8% (+/-3.1% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 > npm 365K ops/s 356.9K ops/s ℹ️ -2.2% (+/-18.2% RME) (ref)
tests/bench/compat.bench.ts > Validation > ULID > npm 3.6M ops/s 3.6M ops/s ℹ️ -2.0% (+/-2.8% RME) (ref)
tests/bench/compat.bench.ts > Validation > CUID2 > npm 13.9M ops/s 14.1M ops/s ℹ️ +1.9% (+/-1.7% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v4 > npm 8.7M ops/s 8.8M ops/s ℹ️ +1.5% (+/-9.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > TypeID > npm 582.2K ops/s 574.5K ops/s ℹ️ -1.3% (+/-2.4% RME) (ref)
tests/bench/compat.bench.ts > Generation > TSID > npm 2.6M ops/s 2.6M ops/s ℹ️ -1.1% (+/-2.7% RME) (ref)
tests/bench/compat.bench.ts > Validation > ObjectID > npm 11.6M ops/s 11.5M ops/s ℹ️ -1.1% (+/-2.9% RME) (ref)
tests/bench/compat.bench.ts > Validation > TSID > npm 1.5M ops/s 1.4M ops/s ℹ️ -1.1% (+/-3.1% RME) (ref)
tests/bench/compat.bench.ts > Generation > ULID > npm 28.4K ops/s 28.2K ops/s ℹ️ -0.9% (+/-29.6% RME) (ref)
tests/bench/compat.bench.ts > Generation > ObjectID > npm 7.9M ops/s 7.8M ops/s ℹ️ -0.9% (+/-3.0% RME) (ref)
tests/bench/compat.bench.ts > Validation > KSUID > npm 692.4K ops/s 697.8K ops/s ℹ️ +0.8% (+/-0.7% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID (10) > npm 18.4M ops/s 18.2M ops/s ℹ️ -0.7% (+/-1.1% RME) (ref)
tests/bench/compat.bench.ts > Generation > XID > npm 5.5M ops/s 5.5M ops/s ℹ️ +0.7% (+/-2.9% RME) (ref)
tests/bench/compat.bench.ts > Generation > CUID2 > npm 6.5K ops/s 6.6K ops/s ℹ️ +0.7% (+/-9.4% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v7 > npm 468.3K ops/s 471K ops/s ℹ️ +0.6% (+/-3.0% RME) (ref)
tests/bench/compat.bench.ts > Generation > NanoID > npm 14.8M ops/s 14.8M ops/s ℹ️ -0.5% (+/-1.5% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v7 > npm 4.9M ops/s 5M ops/s ℹ️ +0.4% (+/-1.3% RME) (ref)
tests/bench/compat.bench.ts > Validation > UUID v4 > npm 4.9M ops/s 4.9M ops/s ℹ️ +0.2% (+/-5.5% RME) (ref)
tests/bench/compat.bench.ts > Generation > KSUID > npm 214.1K ops/s 213.8K ops/s ℹ️ -0.2% (+/-7.8% RME) (ref)
tests/bench/compat.bench.ts > Validation > NanoID > regex 14.2M ops/s 14.2M ops/s ℹ️ -0.1% (+/-6.1% RME) (ref)
tests/bench/compat.bench.ts > Generation > UUID v5 (256-char name) > npm 199.6K ops/s 199.4K ops/s ℹ️ -0.1% (+/-30.9% RME) (ref)

❌ Performance regression detected!

  • tests/bench/compat.bench.ts > Generation > ULID > uniku: -21.0% (4.6M -> 3.7M ops/s, +/-2.3% combined RME)

Bundle Impact

Import Minified + gzipped
uniku/uuid/v4 ~1.3 KB
uniku/uuid/v5 ~1.8 KB
uniku/uuid/v7 ~1.8 KB
uniku/ulid ~1.9 KB
uniku/typeid ~3.0 KB
uniku/cuid2 ~1.0 KB*
uniku/cuid/v2 ~1.0 KB*
uniku/nanoid ~1.2 KB
uniku/ksuid ~1.5 KB
uniku/objectid ~1.6 KB
uniku/tsid ~1.7 KB
uniku/xid ~2.1 KB
uniku/errors ~437 B
uniku/generators ~101 B

*Excludes external dependencies (e.g., @noble/hashes)

Generated by workflow run 43772711628

@jkomyno
jkomyno merged commit cc6551a into fix/unify-monotonic-overflow Nov 16, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant