index.browser.js
This file
Path
index.browser.js
87 lines · 3.0 KiB · JavaScript
Last changed by
Reduce Nano ID size to 117 bytes (#613)Alexander Bayandinbefore adoption05057d8
History of this fileTake it
The walk stopped after 200 commits: lines older than cdc3edc point at it.
Add JSR supportAndrey Sitnikbefore adoption5014f1e
lines 1–2why, one step back1/* @ts-self-types="./index.d.ts" */2Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 3–5why, one step back3// This file replaces `index.js` in bundlers like webpack or Rollup,4// according to `browser` config in `package.json`.5Simplify codeAndrey Sitnikbefore adoption57a250c
line 6why, one step back6import { urlAlphabet } from './url-alphabet/index.js'Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 7why, one step back7Simplify codeAndrey Sitnikbefore adoption57a250c
line 8why, one step back8export { urlAlphabet }Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 9–12why, one step back910export let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))1112export let customRandom = (alphabet, defaultSize, getRandom) => {Improve comments (#583)Andrey Sitnikbefore adoptiond3030b7
lines 13–14why, one step back13 // Random bytes are 0-255. `random % alphabet.length` can waste14 // that entropy by making some symbols more likely.Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37
line 15why, one step back15 //Improve comments (#583)Andrey Sitnikbefore adoptiond3030b7
lines 16–18why, one step back16 // `safeByteCutoff` will be divided by `alphabet.length` without remainder17 // fixing issue of broken distribution.18 //Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37
lines 19–23why, one step back19 // Example: with 17 symbols, `safeByteCutoff` is 255.20 // Bytes 0-254 preserve entropy evenly: each symbol gets 15 source bytes.21 // Byte 255 would map to `0` again, making one symbol slightly more likely.22 // So we reject 255.23 let safeByteCutoff = 256 - (256 % alphabet.length)Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 24why, one step back24Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37
lines 25–27why, one step back25 // Power-of-two alphabets can use `& mask` instead of modulo.26 if (safeByteCutoff === 256) {27 let mask = alphabet.length - 1Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 28why, one step back28Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37
lines 29–32why, one step back29 return (size = defaultSize) => {30 if (!size) return ''31 let id = ''32 while (true) {Improve comments (#583)Andrey Sitnikbefore adoptiond3030b7
line 33why, one step back33 let bytes = getRandom(size)Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37
line 34why, one step back34 // A compact alternative for `for (var i = 0; i < step; i++)`.Fix non-integer size in customRandom() (#616)lostsignalsbefore adoption0b2cec5
lines 35–36why, one step back35 // `|=` convert `size` to integer, so it can be used as a byte index36 let j = (size |= 0)Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37
lines 37–44why, one step back37 while (j--) {38 // Here, `& mask` is equivalent to `% alphabet.length`, but faster39 id += alphabet[bytes[j] & mask]40 if (id.length >= size) return id41 }42 }43 }44 }Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 45–46why, one step back4546 return (size = defaultSize) => {Fix 0-size browser customAlphabet (#575)Denis Saripovbefore adoption6004712
line 47why, one step back47 if (!size) return ''Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 48why, one step back48 let id = ''fix: size customRandom's byte request from the ID length (#612)00200200before adoptionccb0e6e
lines 49–55why, one step back49 // Secure random calls are expensive because system calls50 // for entropy collection take time. To avoid extra calls,51 // extra bytes are requested in advance to cover rejections.52 //53 // `step` determines how many random bytes to request.54 // `1.6` is a magic number chosen from benchmarks.55 let step = Math.ceil((1.6 * 256 * size) / safeByteCutoff)Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 56–58why, one step back56 while (true) {57 let bytes = getRandom(step)58 // A compact alternative for `for (var i = 0; i < step; i++)`.Remove redundant browser step cast (#578)Denis Saripovbefore adoption234a6c3
line 59why, one step back59 let j = stepUpdate size limitAndrey Sitnikbefore adoptioncdc3edc
line 60why, one step back60 while (j--) {Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37
lines 61–66why, one step back61 // Reject bytes >= `safeByteCutoff` to avoid modulo bias62 // and give each symbol an equal chance.63 if (bytes[j] < safeByteCutoff) {64 id += alphabet[bytes[j] % alphabet.length]65 if (id.length >= size) return id66 }Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 67–75why, one step back67 }68 }69 }70}7172export let customAlphabet = (alphabet, size = 21) =>73 customRandom(alphabet, size | 0, random)7475export let nanoid = (size = 21) => {Reduce Nano ID size to 117 bytes (#613)Alexander Bayandinbefore adoption05057d8
lines 76–80why, one step back76 // `var` lets Rolldown fold both declarations into the loop, saving 1 B.77 // oxlint-disable-next-line no-var, prefer-let/prefer-let78 var id = ''79 // oxlint-disable-next-line no-var, prefer-let/prefer-let80 var bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 81why, one step back81 while (size--) {Improve comments (#583)Andrey Sitnikbefore adoptiond3030b7
lines 82–83why, one step back82 // The following mask reduces the random byte in the 0-255 value83 // range to the 0-63 value range.Simplify codeAndrey Sitnikbefore adoption57a250c
line 84why, one step back84 id += urlAlphabet[bytes[size] & 63]Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 85–87why, one step back85 }86 return id87}