All files / server service.js

96.92% Statements 63/65
100% Branches 6/6
89.47% Functions 17/19
100% Lines 60/60

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                                    21x 21x 21x 21x 21x         1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x         3x   3x 2x 1x 1x   1x   3x             5x 5x 5x 5x               4x         6x         4x 4x 4x 4x 8x 4x 3x     1x 1x             4x 4x 3x 3x               3x 2x 2x               3x 3x 2x         1x 1x                   1x 1x 1x 1x           1x         1x 1x 1x 1x           1x      
import child_process from 'node:child_process'
import { randomUUID } from 'node:crypto'
import { once } from 'node:events'
import fs from 'node:fs'
import fsPromises from 'node:fs/promises'
import { extname, join } from 'node:path'
import { PassThrough, Writable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import Throttle from 'throttle'
 
import { FX_DIR, PUBLIC_DIR, SOX } from './config.js'
import { BAD_REQUEST_ERROR } from './errors.js'
import { logger } from './util.js'
 
 
export default class Service {
 
  constructor () {
    this.bitrate = 0
    this.clientStreams = new Map()
    this.readStream = null
    this.songPath = SOX.CONVERSATION
    this.throttle = null
  }
 
 
  appendFxStream (command) {
    const fxPath = this.getFxPath(command)
    const newThrottle = new Throttle(this.bitrate)
    pipeline(newThrottle, this.broadcast())
    this.throttle.on('unpipe', () => this.appendFxStreamOnUnpipe(fxPath, newThrottle))
    this.throttle.pause()
    this.readStream.unpipe(this.throttle)
  }
 
 
  appendFxStreamOnUnpipe (fxPath, newThrottle) {
    const newReadStream = this.mergeAudioStreams(fxPath, this.readStream)
    this.readStream = newReadStream
    this.throttle = newThrottle
    this.readStream.removeListener('unpipe', () => this.appendFxStreamOnUnpipe(fxPath, newThrottle))
    pipeline(newReadStream, newThrottle)
  }
 
 
  broadcast () {
    return new Writable({
      write: (chunk, encoding, callback) => {
        for (const [id, stream] of this.clientStreams) {
          if (stream.writableEnded) {
            this.clientStreams.delete(id)
            continue
          }
          stream.write(chunk)
        }
        callback()
      }
    })
  }
 
 
  createClientStream () {
    const id = randomUUID()
    const stream = new PassThrough()
    this.clientStreams.set(id, stream)
    return {
      id, stream
    }
  }
 
 
  /** @param {string} fileName */
  createFileStream (fileName) {
    return fs.createReadStream(fileName)
  }
 
 
  executeSoxCommand (args) {
    return child_process.spawn('sox', args)
  }
 
 
  async getBitrate (filePath) {
    try {
      const args = ['--i', '-B', filePath]
      const { stdout, stderr } = this.executeSoxCommand(args)
      await Promise.all([once(stdout, 'readable'), once(stderr, 'readable')])
      const [success, error] = [stdout, stderr].map(stream => stream.read())
      if (error) await Promise.reject(error)
      return success.toString().trim().replace(/k/, '000')
    }
    catch (error) {
      logger.error(error)
      return SOX.FALLBACK_BITRATE
    }
  }
 
 
  /** @param {string} fileName */
  async getFileInfo (fileName) {
    const name = join(PUBLIC_DIR, fileName)
    await fsPromises.access(name)
    const type = extname(name)
    return {
      name, type
    }
  }
 
 
  /** @param {string} fileName */
  async getFileStream (fileName) {
    const { name, type } = await this.getFileInfo(fileName)
    const stream = this.createFileStream(name)
    return {
      stream, type
    }
  }
 
 
  /** @param {string} fileName */
  getFxPath (fileName) {
    const filePath = join(FX_DIR, `${fileName}.mp3`)
    if (!fs.existsSync(filePath)) throw BAD_REQUEST_ERROR
    return filePath
  }
 
 
  mergeAudioStreams (fxPath, readStream) {
    const transform = new PassThrough()
    const args = [
      '-t', SOX.AUDIO_MEDIA_TYPE,
      '-v', SOX.SONG_VOLUME,
      '-m', '-',
      '-t', SOX.AUDIO_MEDIA_TYPE,
      '-v', SOX.FX_VOLUME,
      fxPath,
      '-t', SOX.AUDIO_MEDIA_TYPE,
      '-'
    ]
    const { stdin, stdout } = this.executeSoxCommand(args)
    pipeline(readStream, stdin)
    pipeline(stdout, transform)
    return transform
  }
 
 
  /** @param {`${string}-${string}-${string}-${string}-${string}`} id */
  removeClientStream (id) {
    this.clientStreams.delete(id)
  }
 
 
  async startStream () {
    const bitrate = this.bitrate = await this.getBitrate(this.songPath) / SOX.BITRATE_DIVISOR
    const throttle = this.throttle = new Throttle(bitrate)
    const readStream = this.readStream = this.createFileStream(this.songPath)
    return pipeline(readStream, throttle, this.broadcast())
  }
 
 
  async stopStream () {
    // this.throttle?.end?.()
    this.readStream.destroy()
  }
 
}