| 758 | * Reads a file in chunks to avoid loading entire file into memory. |
| 759 | */ |
| 760 | export async function* readFileInChunks( |
| 761 | filePath: string, |
| 762 | chunkSize: number = 64 * 1024 |
| 763 | ): AsyncGenerator<string, void, undefined> { |
| 764 | const fs = await import('fs'); |
| 765 | |
| 766 | const fd = fs.openSync(filePath, 'r'); |
| 767 | const buffer = Buffer.alloc(chunkSize); |
| 768 | |
| 769 | try { |
| 770 | let bytesRead: number; |
| 771 | while ((bytesRead = fs.readSync(fd, buffer, 0, chunkSize, null)) > 0) { |
| 772 | yield buffer.toString('utf-8', 0, bytesRead); |
| 773 | } |
| 774 | } finally { |
| 775 | fs.closeSync(fd); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Debounce a function |