1 | import crypto from 'crypto'
|
2 |
|
3 | import { urlAlphabet } from './url-alphabet/index.js'
|
4 |
|
5 | // We reuse buffers with the same size to avoid memory fragmentations
|
6 | // for better performance.
|
7 | let buffers = {}
|
8 | let random = bytes => {
|
9 | let buffer = buffers[bytes]
|
10 | if (!buffer) {
|
11 | // `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory.
|
12 | // Memory flushing is unnecessary since the buffer allocation itself resets
|
13 | // the memory with the new bytes.
|
14 | buffer = Buffer.allocUnsafe(bytes)
|
15 | if (bytes <= 255) buffers[bytes] = buffer
|
16 | }
|
17 | return crypto.randomFillSync(buffer)
|
18 | }
|
19 |
|
20 | let customRandom = (alphabet, size, getRandom) => {
|
21 | // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
|
22 | // values closer to the alphabet size. The bitmask calculates the closest
|
23 | // `2^31 - 1` number, which exceeds the alphabet size.
|
24 | // For example, the bitmask for the alphabet size 30 is 31 (00011111).
|
25 | let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
26 | // Though, the bitmask solution is not perfect since the bytes exceeding
|
27 | // the alphabet size are refused. Therefore, to reliably generate the ID,
|
28 | // the random bytes redundancy has to be satisfied.
|
29 |
|
30 | // Note: every hardware random generator call is performance expensive,
|
31 | // because the system call for entropy collection takes a lot of time.
|
32 | // So, to avoid additional system calls, extra bytes are requested in advance.
|
33 |
|
34 | // Next, a step determines how many random bytes to generate.
|
35 | // The number of random bytes gets decided upon the ID size, mask,
|
36 | // alphabet size, and magic number 1.6 (using 1.6 peaks at performance
|
37 | // according to benchmarks).
|
38 | let step = Math.ceil((1.6 * mask * size) / alphabet.length)
|
39 |
|
40 | return () => {
|
41 | let id = ''
|
42 | while (true) {
|
43 | let bytes = getRandom(step)
|
44 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
45 | let i = step
|
46 | while (i--) {
|
47 | // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
|
48 | id += alphabet[bytes[i] & mask] || ''
|
49 | // `id.length + 1 === size` is a more compact option.
|
50 | if (id.length === +size) return id
|
51 | }
|
52 | }
|
53 | }
|
54 | }
|
55 |
|
56 | let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
|
57 |
|
58 | let nanoid = (size = 21) => {
|
59 | let bytes = random(size)
|
60 | let id = ''
|
61 | // A compact alternative for `for (var i = 0; i < step; i++)`.
|
62 | while (size--) {
|
63 | // It is incorrect to use bytes exceeding the alphabet size.
|
64 | // The following mask reduces the random byte in the 0-255 value
|
65 | // range to the 0-63 value range. Therefore, adding hacks, such
|
66 | // as empty string fallback or magic numbers, is unneccessary because
|
67 | // the bitmask trims bytes down to the alphabet size.
|
68 | id += urlAlphabet[bytes[size] & 63]
|
69 | }
|
70 | return id
|
71 | }
|
72 |
|
73 | export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
|