Repository navigation
chore: correct stale comments and contracts that contradicted the code - #119
Conversation
|
Preview removed because the pull request was closed. |
commit: |
6880ddb to
59e495b
Compare
c5afbf7 to
e2903cb
Compare
Codex reviewReviewed with 1. "Rejects IDs generated with a custom alphabet" was falseI wrote that in two places. It is wrong: 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 resetI 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:
Also verified by the review, no action needed
|
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.
59e495b to
c7feebf
Compare
e2903cb to
d04a92c
Compare
Benchmark ResultsGeneration
Validation
Cold Start (Node)
Comparison vs Main
❌ Performance regression detected!
Bundle Impact
*Excludes external dependencies (e.g., @noble/hashes) Generated by workflow run 43772711628 |
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 stringwhile the JSDoc advertised
@throws Length must be between 0 and 2048and the CLI documentsLength of ID (1-256). Zero is now rejected like every other invalid length, with one check over1..2048and one message.2.
nanoid.isValid()was documented as if it checked anything usefulIt cannot check the length, because a nanoid carries none —
nanoid.isValid('a')andnanoid.isValid('a'.repeat(4000))are bothtrue. 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.
nanoidused the barecryptoglobalEvery other module uses
globalThis.crypto, which is the conventionAGENTS.mdstates. Now it does too.4. Two internal encoders silently truncated
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.mdcalled 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
incrementBytesInPlacecredited KSUID with a call KSUID never makes (KSUID has no monotonic sequencing).objectid/hex.tsreported its decoding table as "128 KiB";new Uint8Array(65536)is 64 KiB.Tests
nanoidlength 0 / 2049 rejection, the1 and 2048message,nanoid(2048)succeeding, and exact-length rejection forksuid.fromBytes/objectid.fromBytesat 0, 11/19, 13/21, and 16/32 bytes.Changeset is
minorbecausenanoid(0)changes from returning an empty string to throwing.