All files / src/request request.helper.ts

100% Statements 37/37
100% Branches 4/4
100% Functions 2/2
100% Lines 37/37

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 381x 1x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 228x 228x 228x 228x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { createHash } from 'crypto'
 
/**
 * Generates a dynamic secret (DS) string for use in the Genshin Impact API.
 *
 * @returns The generated DS string.
 */
export function generateDS(): string {
  const salt = '6s25p5ox5y14umn1p61aqyyvbvvl3lrt'
  const date = new Date()
  const time = Math.floor(date.getTime() / 1000)
 
  let random = ''
  const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  for (let i = 0; i < 6; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length)
    const randomChar = characters.charAt(randomIndex)
    random += randomChar
  }
 
  const hash = createHash('md5')
    .update(`salt=${salt}&t=${time}&r=${random}`)
    .digest('hex')
 
  return `${time},${random},${hash}`
}
 
/**
 * Delays the execution of the code for a specified number of seconds.
 * @param second - The number of seconds to delay.
 * @returns A Promise that resolves after the specified number of seconds.
 */
export function delay(second: number): Promise<void> {
  return new Promise((resolve) => {
    setTimeout(resolve, second * 1000)
  })
}