index.browser.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // This file replaces `index.js` in bundlers like webpack or Rollup,
  2. // according to `browser` config in `package.json`.
  3. import { urlAlphabet } from './url-alphabet/index.js'
  4. if (process.env.NODE_ENV !== 'production') {
  5. // All bundlers will remove this block in the production bundle.
  6. if (
  7. typeof navigator !== 'undefined' &&
  8. navigator.product === 'ReactNative' &&
  9. typeof crypto === 'undefined'
  10. ) {
  11. throw new Error(
  12. 'React Native does not have a built-in secure random generator. ' +
  13. 'If you don’t need unpredictable IDs use `nanoid/non-secure`. ' +
  14. 'For secure IDs, import `react-native-get-random-values` ' +
  15. 'before Nano ID.'
  16. )
  17. }
  18. if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
  19. throw new Error(
  20. 'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
  21. ' before importing Nano ID to fix IE 11 support'
  22. )
  23. }
  24. if (typeof crypto === 'undefined') {
  25. throw new Error(
  26. 'Your browser does not have secure random generator. ' +
  27. 'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
  28. )
  29. }
  30. }
  31. let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
  32. let customRandom = (alphabet, size, 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. // `Math.clz32` is not used, because it is not available in browsers.
  38. let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
  39. // Though, the bitmask solution is not perfect since the bytes exceeding
  40. // the alphabet size are refused. Therefore, to reliably generate the ID,
  41. // the random bytes redundancy has to be satisfied.
  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. // 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. // `-~f => Math.ceil(f)` if f is a float
  50. // `-~i => i + 1` if i is an integer
  51. let step = -~((1.6 * mask * size) / alphabet.length)
  52. return () => {
  53. let id = ''
  54. while (true) {
  55. let bytes = getRandom(step)
  56. // A compact alternative for `for (var i = 0; i < step; i++)`.
  57. let j = step
  58. while (j--) {
  59. // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
  60. id += alphabet[bytes[j] & mask] || ''
  61. if (id.length === size) return id
  62. }
  63. }
  64. }
  65. }
  66. let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
  67. let nanoid = (size = 21) => {
  68. let id = ''
  69. let bytes = crypto.getRandomValues(new Uint8Array(size))
  70. // A compact alternative for `for (var i = 0; i < step; i++)`.
  71. while (size--) {
  72. // It is incorrect to use bytes exceeding the alphabet size.
  73. // The following mask reduces the random byte in the 0-255 value
  74. // range to the 0-63 value range. Therefore, adding hacks, such
  75. // as empty string fallback or magic numbers, is unneccessary because
  76. // the bitmask trims bytes down to the alphabet size.
  77. let byte = bytes[size] & 63
  78. if (byte < 36) {
  79. // `0-9a-z`
  80. id += byte.toString(36)
  81. } else if (byte < 62) {
  82. // `A-Z`
  83. id += (byte - 26).toString(36).toUpperCase()
  84. } else if (byte < 63) {
  85. id += '_'
  86. } else {
  87. id += '-'
  88. }
  89. }
  90. return id
  91. }
  92. export { nanoid, customAlphabet, customRandom, urlAlphabet, random }