| 425 | * Reads a file in chunks to avoid loading entire file into memory. |
| 426 | */ |
| 427 | export async function* readFileInChunks( |
| 428 | filePath: string, |
| 429 | chunkSize: number = 64 * 1024 |
| 430 | ): AsyncGenerator<string, void, undefined> { |
| 431 | const fs = await import('fs'); |
| 432 | |
| 433 | const fd = fs.openSync(filePath, 'r'); |
| 434 | const buffer = Buffer.alloc(chunkSize); |
| 435 | |
| 436 | try { |
| 437 | let bytesRead: number; |
| 438 | while ((bytesRead = fs.readSync(fd, buffer, 0, chunkSize, null)) > 0) { |
| 439 | yield buffer.toString('utf-8', 0, bytesRead); |
| 440 | } |
| 441 | } finally { |
| 442 | fs.closeSync(fd); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Debounce a function |