| 734 | * Reads a file in chunks to avoid loading entire file into memory. |
| 735 | */ |
| 736 | export async function* readFileInChunks( |
| 737 | filePath: string, |
| 738 | chunkSize: number = 64 * 1024 |
| 739 | ): AsyncGenerator<string, void, undefined> { |
| 740 | const fs = await import('fs'); |
| 741 | |
| 742 | const fd = fs.openSync(filePath, 'r'); |
| 743 | const buffer = Buffer.alloc(chunkSize); |
| 744 | |
| 745 | try { |
| 746 | let bytesRead: number; |
| 747 | while ((bytesRead = fs.readSync(fd, buffer, 0, chunkSize, null)) > 0) { |
| 748 | yield buffer.toString('utf-8', 0, bytesRead); |
| 749 | } |
| 750 | } finally { |
| 751 | fs.closeSync(fd); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * Debounce a function |