()
| 138 | let isEnded = false |
| 139 | |
| 140 | const processChunks = async () => { |
| 141 | while (!isEnded || buffer.length > 0) { |
| 142 | // Check if aborted |
| 143 | if (abortController.signal.aborted) { |
| 144 | if (!stream.destroyed) { |
| 145 | stream.destroy() |
| 146 | } |
| 147 | onStreamDestroy?.() |
| 148 | reject(new Error('TTS generation aborted')) |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | if (buffer.length >= TARGET_CHUNK_SIZE) { |
| 153 | const chunk = buffer.subarray(0, TARGET_CHUNK_SIZE) |
| 154 | buffer = buffer.subarray(TARGET_CHUNK_SIZE) |
| 155 | onChunk(chunk) |
| 156 | await sleep(RATE_LIMIT_MS) |
| 157 | } else if (isEnded && buffer.length > 0) { |
| 158 | onChunk(buffer) |
| 159 | buffer = Buffer.alloc(0) |
| 160 | } else if (!isEnded) { |
| 161 | await sleep(RATE_LIMIT_MS) |
| 162 | } else { |
| 163 | break |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | onEnd() |
| 168 | resolve() |
| 169 | } |
| 170 | |
| 171 | stream.on('data', (chunk) => { |
| 172 | if (!abortController.signal.aborted) { |
no test coverage detected