code wiki / (root) / nx_nanoid.nx

nx_nanoid.nx

buildroot/runtime/nx_nanoid.nx

3337 B107 linesdepth 3pulls 3 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

nx_syscalls.nx nx_rand.nx nx_nanoid.nx

imports: nx_syscalls.nxnx_rand.nx

imported by: nobody (leaf or entry point)

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

main sys_mmap nanoid_default nanoid rand_bytes rand_open_urandom sys_openat_rd sys_read sys_close nano_char

structs

none

consts

37const NANOID_DEFAULT_LEN: i64 = 21

functions

41func nano_char(idx: i64) -> i64
called by 1: nanoid
54func nanoid(out: *u8, n: i64) -> i64
called by 1: nanoid_default calls 2: rand_bytesnano_char
68func nanoid_default(out: *u8) -> i64
called by 1: main calls 1: nanoid
73func main() -> i64