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