1 | let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
|
---|
2 |
|
---|
3 | let customAlphabet = (alphabet, defaultSize = 21) => {
|
---|
4 | // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
---|
5 | // values closer to the alphabet size. The bitmask calculates the closest
|
---|
6 | // `2^31 - 1` number, which exceeds the alphabet size.
|
---|
7 | // For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
---|
8 | // `Math.clz32` is not used, because it is not available in browsers.
|
---|
9 | let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
|
---|
10 | // Though, the bitmask solution is not perfect since the bytes exceeding
|
---|
11 | // the alphabet size are refused. Therefore, to reliably generate the ID,
|
---|
12 | // the random bytes redundancy has to be satisfied.
|
---|
13 |
|
---|
14 | // Note: every hardware random generator call is performance expensive,
|
---|
15 | // because the system call for entropy collection takes a lot of time.
|
---|
16 | // So, to avoid additional system calls, extra bytes are requested in advance.
|
---|
17 |
|
---|
18 | // Next, a step determines how many random bytes to generate.
|
---|
19 | // The number of random bytes gets decided upon the ID size, mask,
|
---|
20 | // alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
---|
21 | // according to benchmarks).
|
---|
22 |
|
---|
23 | // `-~f => Math.ceil(f)` if f is a float
|
---|
24 | // `-~i => i + 1` if i is an integer
|
---|
25 | let step = -~((1.6 * mask * defaultSize) / alphabet.length)
|
---|
26 |
|
---|
27 | return async (size = defaultSize) => {
|
---|
28 | let id = ''
|
---|
29 | while (true) {
|
---|
30 | let bytes = crypto.getRandomValues(new Uint8Array(step))
|
---|
31 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
---|
32 | let i = step | 0
|
---|
33 | while (i--) {
|
---|
34 | // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
---|
35 | id += alphabet[bytes[i] & mask] || ''
|
---|
36 | if (id.length === size) return id
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | let nanoid = async (size = 21) => {
|
---|
43 | let id = ''
|
---|
44 | let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))
|
---|
45 |
|
---|
46 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
---|
47 | while (size--) {
|
---|
48 | // It is incorrect to use bytes exceeding the alphabet size.
|
---|
49 | // The following mask reduces the random byte in the 0-255 value
|
---|
50 | // range to the 0-63 value range. Therefore, adding hacks, such
|
---|
51 | // as empty string fallback or magic numbers, is unneccessary because
|
---|
52 | // the bitmask trims bytes down to the alphabet size.
|
---|
53 | let byte = bytes[size] & 63
|
---|
54 | if (byte < 36) {
|
---|
55 | // `0-9a-z`
|
---|
56 | id += byte.toString(36)
|
---|
57 | } else if (byte < 62) {
|
---|
58 | // `A-Z`
|
---|
59 | id += (byte - 26).toString(36).toUpperCase()
|
---|
60 | } else if (byte < 63) {
|
---|
61 | id += '_'
|
---|
62 | } else {
|
---|
63 | id += '-'
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return id
|
---|
67 | }
|
---|
68 |
|
---|
69 | export { nanoid, customAlphabet, random }
|
---|