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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788/* @ts-self-types="./index.d.ts" */
// This file replaces `index.js` in bundlers like webpack or Rollup,// according to `browser` config in `package.json`.
import { urlAlphabet } from './url-alphabet/index.js'
export { urlAlphabet }
export let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
export let customRandom = (alphabet, defaultSize, getRandom) => { // Random bytes are 0-255. `random % alphabet.length` can waste // that entropy by making some symbols more likely. // // `safeByteCutoff` will be divided by `alphabet.length` without remainder // fixing issue of broken distribution. // // Example: with 17 symbols, `safeByteCutoff` is 255. // Bytes 0-254 preserve entropy evenly: each symbol gets 15 source bytes. // Byte 255 would map to `0` again, making one symbol slightly more likely. // So we reject 255. let safeByteCutoff = 256 - (256 % alphabet.length)
// Power-of-two alphabets can use `& mask` instead of modulo. if (safeByteCutoff === 256) { let mask = alphabet.length - 1
return (size = defaultSize) => { if (!size) return '' let id = '' while (true) { let bytes = getRandom(size) // A compact alternative for `for (var i = 0; i < step; i++)`. // `|=` convert `size` to integer, so it can be used as a byte index let j = (size |= 0) while (j--) { // Here, `& mask` is equivalent to `% alphabet.length`, but faster id += alphabet[bytes[j] & mask] if (id.length >= size) return id } } } }
return (size = defaultSize) => { if (!size) return '' let id = '' // Secure random calls are expensive because system calls // for entropy collection take time. To avoid extra calls, // extra bytes are requested in advance to cover rejections. // // `step` determines how many random bytes to request. // `1.6` is a magic number chosen from benchmarks. let step = Math.ceil((1.6 * 256 * size) / safeByteCutoff) while (true) { let bytes = getRandom(step) // A compact alternative for `for (var i = 0; i < step; i++)`. let j = step while (j--) { // Reject bytes >= `safeByteCutoff` to avoid modulo bias // and give each symbol an equal chance. if (bytes[j] < safeByteCutoff) { id += alphabet[bytes[j] % alphabet.length] if (id.length >= size) return id } } } }}
export let customAlphabet = (alphabet, size = 21) => customRandom(alphabet, size | 0, random)
export let nanoid = (size = 21) => { // `var` lets Rolldown fold both declarations into the loop, saving 1 B. // oxlint-disable-next-line no-var, prefer-let/prefer-let var id = '' // oxlint-disable-next-line no-var, prefer-let/prefer-let var bytes = crypto.getRandomValues(new Uint8Array((size |= 0))) while (size--) { // The following mask reduces the random byte in the 0-255 value // range to the 0-63 value range. id += urlAlphabet[bytes[size] & 63] } return id}