source: frontend/node_modules/nanoid/index.cjs

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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