typescriptintermediate

CSV Parse with Streaming

Parse large CSV files using Node.js streams with row-by-row processing and backpressure handling.

typescript
import { createReadStream } from 'fs';
import { createInterface } from 'readline';

interface CsvRow {
  [key: string]: string;
}

export async function* parseCsv(filePath: string): AsyncGenerator<CsvRow> {
  const stream = createReadStream(filePath, { encoding: 'utf-8' });
  const rl = createInterface({ input: stream, crlfDelay: Infinity });
  let headers: string[] = [];
  let isFirst = true;

  for await (const line of rl) {
    if (isFirst) {
      headers = line.split(',').map((h) => h.trim().replace(/^"|"$/g, ''));
      isFirst = false;
      continue;
    }
    const values = line.split(',').map((v) => v.trim().replace(/^"|"$/g, ''));
    const row: CsvRow = {};
    headers.forEach((h, i) => (row[h] = values[i] ?? ''));
    yield row;
  }
}

// Usage:
// for await (const row of parseCsv('data.csv')) {
//   console.log(row);
// }

Use Cases

  • Data import pipelines
  • ETL processing
  • Log file analysis

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.