Repository navigation
Expand file tree
/
Copy pathindex.browser.js
More file actions
87 lines (77 loc) · 2.99 KB
/
Copy pathindex.browser.js
File metadata and controls
87 lines (77 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* @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
}