code wiki / (root) / nanoid.nx

nanoid.nx

buildroot/runtime/nanoid.nx

3181 B101 linesdepth 4pulls 4 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nanoid.nx -- modern URL-safe ID generator. Andrey Sitnik 2017, widely adopted in the JS ecosystem (Next.js, Prisma, PlanetScale, tRPC). Alternative to UUID + ULID with different trade-offs: - Default 21 chars ~= 126 bits of entropy (UUID: 128) - URL-safe alphabet: A-Z a-z 0-9 _ - (64 chars, 6 bits each) - No structure: pure random, no embedded timestamp - No lexicographic sort: use ULID if sorting matters Use cases: URL slugs that need collision-resistance, short share codes, DB primary keys where you don't need time-sort. Algorithm: Request N random bytes from /dev/urandom via rand.nx. Mask each byte to 6 bits + retry on overflow (standard rejection sampling -- keeps the output alphabet uniform). Composes rand.nx. Invariants: N1 Output is fixed-length caller-chosen (default 21). N2 Every byte is from the 64-char URL-safe alphabet. N3 Rejection sampling ensures uniform distribution -- we don't use mod-256 which would bias bytes 0..31.

dependencies 2 imports · 0 importers

syscalls.nx rand.nx nanoid.nx

imports: syscalls.nxrand.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main nanoid_default nanoid rand_bytes rand_open_urandom sys_read nano_char

structs

none

consts

31const NANOID_DEFAULT_LEN: i64 = 21

functions

35func nano_char(idx: i64) -> i64 {
called by 1: nanoid
48func nanoid(out: *u8, n: i64) -> i64 {
called by 1: nanoid_default calls 2: rand_bytesnano_char
62func nanoid_default(out: *u8) -> i64 {
called by 1: main calls 1: nanoid
67func main() -> i64 {
calls 1: nanoid_default