All files / src/services generateUniqueToken.ts

30% Statements 12/40
100% Branches 0/0
0% Functions 0/1
30% Lines 12/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                          
 
import { v4 as uuidv4 } from 'uuid'
import crypto from 'crypto'
 
/**
 * Generates a unique token.
 * @param {number} length - The desired length of the token. Must be at least 8. Default: 20.
 * @param {'alphanumeric' | 'hexadecimal'} mode - The mode of the token, either 'alphanumeric' or 'hexadecimal'. Default: 'alphanumeric'.
 * @returns {string} - A unique token of the specified length.
 * @throws {Error} - Throws an error if the length is less than 8.
 * @note - The function generates a token based on a UUID and ensures uniqueness. A cache is used to avoid collisions.
 */
export function generateUniqueToken(length: number = 20, mode: 'alphanumeric' | 'hexadecimal' = 'alphanumeric'): string {
  if (length < 8) {
    throw new Error('Length must be at least 8 characters.')
  }

  const charset = mode === 'alphanumeric'
    ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    : '0123456789abcdef'

  let token: string

  token = uuidv4().replace(/-/g, '')

  const randomValues = new Uint8Array(length)
  crypto.getRandomValues(randomValues)

  if (mode === 'alphanumeric') {
    token = Array.from(token)
      .map(char => charset.includes(char) ? char : charset[Math.floor(Math.random() * charset.length)])
      .join('')
  }

  while (token.length < length) {
    token += charset[randomValues[token.length % length] % charset.length]
  }

  return token.slice(0, length)
}