source: imaps-frontend/node_modules/nanoid/index.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 3.3 KB
Line 
1import crypto from 'crypto'
2
3import { urlAlphabet } from './url-alphabet/index.js'
4
5// It is best to make fewer, larger requests to the crypto module to
6// avoid system call overhead. So, random numbers are generated in a
7// pool. The pool is a Buffer that is larger than the initial random
8// request size by this multiplier. The pool is enlarged if subsequent
9// requests exceed the maximum buffer size.
10const POOL_SIZE_MULTIPLIER = 128
11let pool, poolOffset
12
13let fillPool = bytes => {
14 if (!pool || pool.length < bytes) {
15 pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
16 crypto.randomFillSync(pool)
17 poolOffset = 0
18 } else if (poolOffset + bytes > pool.length) {
19 crypto.randomFillSync(pool)
20 poolOffset = 0
21 }
22 poolOffset += bytes
23}
24
25let random = bytes => {
26 // `|=` convert `bytes` to number to prevent `valueOf` abusing and pool pollution
27 fillPool((bytes |= 0))
28 return pool.subarray(poolOffset - bytes, poolOffset)
29}
30
31let customRandom = (alphabet, defaultSize, getRandom) => {
32 // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
33 // values closer to the alphabet size. The bitmask calculates the closest
34 // `2^31 - 1` number, which exceeds the alphabet size.
35 // For example, the bitmask for the alphabet size 30 is 31 (00011111).
36 let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
37 // Though, the bitmask solution is not perfect since the bytes exceeding
38 // the alphabet size are refused. Therefore, to reliably generate the ID,
39 // the random bytes redundancy has to be satisfied.
40
41 // Note: every hardware random generator call is performance expensive,
42 // because the system call for entropy collection takes a lot of time.
43 // So, to avoid additional system calls, extra bytes are requested in advance.
44
45 // Next, a step determines how many random bytes to generate.
46 // The number of random bytes gets decided upon the ID size, mask,
47 // alphabet size, and magic number 1.6 (using 1.6 peaks at performance
48 // according to benchmarks).
49 let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
50
51 return (size = defaultSize) => {
52 let id = ''
53 while (true) {
54 let bytes = getRandom(step)
55 // A compact alternative for `for (let i = 0; i < step; i++)`.
56 let i = step
57 while (i--) {
58 // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
59 id += alphabet[bytes[i] & mask] || ''
60 if (id.length === size) return id
61 }
62 }
63 }
64}
65
66let customAlphabet = (alphabet, size = 21) =>
67 customRandom(alphabet, size, random)
68
69let nanoid = (size = 21) => {
70 // `|=` convert `size` to number to prevent `valueOf` abusing and pool pollution
71 fillPool((size |= 0))
72 let id = ''
73 // We are reading directly from the random pool to avoid creating new array
74 for (let i = poolOffset - size; i < poolOffset; i++) {
75 // It is incorrect to use bytes exceeding the alphabet size.
76 // The following mask reduces the random byte in the 0-255 value
77 // range to the 0-63 value range. Therefore, adding hacks, such
78 // as empty string fallback or magic numbers, is unneccessary because
79 // the bitmask trims bytes down to the alphabet size.
80 id += urlAlphabet[pool[i] & 63]
81 }
82 return id
83}
84
85export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
Note: See TracBrowser for help on using the repository browser.