Ignore:
Timestamp:
12/12/24 17:06:06 (5 weeks ago)
Author:
stefan toskovski <stefantoska84@…>
Branches:
main
Parents:
d565449
Message:

Pred finalna verzija

File:
1 edited

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
    14let { urlAlphabet } = require('./url-alphabet/index.cjs')
     5
    26let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
     7
    38let 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.
    414  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
    530  let step = -~((1.6 * mask * defaultSize) / alphabet.length)
     31
    632  return (size = defaultSize) => {
    733    let id = ''
    834    while (true) {
    935      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
    1138      while (j--) {
     39        // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
    1240        id += alphabet[bytes[j] & mask] || ''
    1341        if (id.length === size) return id
     
    1644  }
    1745}
     46
    1847let customAlphabet = (alphabet, size = 21) =>
    1948  customRandom(alphabet, size, random)
     49
    2050let nanoid = (size = 21) =>
    2151  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.
    2257    byte &= 63
    2358    if (byte < 36) {
     59      // `0-9a-z`
    2460      id += byte.toString(36)
    2561    } else if (byte < 62) {
     62      // `A-Z`
    2663      id += (byte - 26).toString(36).toUpperCase()
    2764    } else if (byte > 62) {
     
    3269    return id
    3370  }, '')
     71
    3472module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
Note: See TracChangeset for help on using the changeset viewer.