index.browser.cjs 2.8 KB

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