index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import crypto from 'crypto'
  2. import { urlAlphabet } from './url-alphabet/index.js'
  3. const POOL_SIZE_MULTIPLIER = 128
  4. let pool, poolOffset
  5. let fillPool = bytes => {
  6. if (bytes < 0 || bytes > 1024) throw new RangeError('Wrong ID size')
  7. if (!pool || pool.length < bytes) {
  8. pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
  9. crypto.randomFillSync(pool)
  10. poolOffset = 0
  11. } else if (poolOffset + bytes > pool.length) {
  12. crypto.randomFillSync(pool)
  13. poolOffset = 0
  14. }
  15. poolOffset += bytes
  16. }
  17. let random = bytes => {
  18. fillPool((bytes |= 0))
  19. return pool.subarray(poolOffset - bytes, poolOffset)
  20. }
  21. let customRandom = (alphabet, defaultSize, getRandom) => {
  22. let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
  23. let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
  24. return (size = defaultSize) => {
  25. let id = ''
  26. while (true) {
  27. let bytes = getRandom(step)
  28. let i = step
  29. while (i--) {
  30. id += alphabet[bytes[i] & mask] || ''
  31. if (id.length === size) return id
  32. }
  33. }
  34. }
  35. }
  36. let customAlphabet = (alphabet, size = 21) =>
  37. customRandom(alphabet, size, random)
  38. let nanoid = (size = 21) => {
  39. fillPool((size |= 0))
  40. let id = ''
  41. for (let i = poolOffset - size; i < poolOffset; i++) {
  42. id += urlAlphabet[pool[i] & 63]
  43. }
  44. return id
  45. }
  46. export { nanoid, customAlphabet, customRandom, urlAlphabet, random }