Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than cdc3edc point at it.

Add JSR supportAndrey Sitnikbefore adoption5014f1e

lines 1–2why, one step back
1/* @ts-self-types="./index.d.ts" */
2

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 3–5why, one step back
3// This file replaces `index.js` in bundlers like webpack or Rollup,
4// according to `browser` config in `package.json`.5

Simplify codeAndrey Sitnikbefore adoption57a250c

line 6why, one step back
6import { urlAlphabet } from './url-alphabet/index.js'

Update size limitAndrey Sitnikbefore adoptioncdc3edc

line 7why, one step back
7

Simplify codeAndrey Sitnikbefore adoption57a250c

line 8why, one step back
8export { urlAlphabet }

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 9–12why, one step back
9
10export 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 back
13 // Random bytes are 0-255. `random % alphabet.length` can waste
14 // 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 back
15 //

Improve comments (#583)Andrey Sitnikbefore adoptiond3030b7

lines 16–18why, one step back
16 // `safeByteCutoff` will be divided by `alphabet.length` without remainder
17 // 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 back
19 // 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 back
24

Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37

lines 25–27why, one step back
25 // Power-of-two alphabets can use `& mask` instead of modulo.
26 if (safeByteCutoff === 256) {27 let mask = alphabet.length - 1

Update size limitAndrey Sitnikbefore adoptioncdc3edc

line 28why, one step back
28

Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37

lines 29–32why, one step back
29 return (size = defaultSize) => {
30 if (!size) return ''31 let id = ''32 while (true) {

Improve comments (#583)Andrey Sitnikbefore adoptiond3030b7

line 33why, one step back
33 let bytes = getRandom(size)

Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37

line 34why, one step back
34 // 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 back
35 // `|=` convert `size` to integer, so it can be used as a byte index
36 let j = (size |= 0)

Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37

lines 37–44why, one step back
37 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 back
45
46 return (size = defaultSize) => {

Fix 0-size browser customAlphabet (#575)Denis Saripovbefore adoption6004712

line 47why, one step back
47 if (!size) return ''

Update size limitAndrey Sitnikbefore adoptioncdc3edc

line 48why, one step back
48 let id = ''

fix: size customRandom's byte request from the ID length (#612)00200200before adoptionccb0e6e

lines 49–55why, one step back
49 // Secure random calls are expensive because system calls
50 // 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 back
56 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 back
59 let j = step

Update size limitAndrey Sitnikbefore adoptioncdc3edc

line 60why, one step back
60 while (j--) {

Use full byte range in customAlphabet (+ ~75% ops / sec optimisation) (#582)Denis Saripovbefore adoptione1acf37

lines 61–66why, one step back
61 // Reject bytes >= `safeByteCutoff` to avoid modulo bias
62 // 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 back
67 }
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 back
76 // `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 back
81 while (size--) {

Improve comments (#583)Andrey Sitnikbefore adoptiond3030b7

lines 82–83why, one step back
82 // The following mask reduces the random byte in the 0-255 value
83 // range to the 0-63 value range.

Simplify codeAndrey Sitnikbefore adoption57a250c

line 84why, one step back
84 id += urlAlphabet[bytes[size] & 63]

Update size limitAndrey Sitnikbefore adoptioncdc3edc

lines 85–87why, one step back
85 }
86 return id87}