Quick start

Fetch the CSV and write eight transformed records to a local JSON Lines file.

1 Set up the project

mkdir exstream-quickstart
cd exstream-quickstart
npm init -y
npm install exstream.js

2 Save as index.mjs

import { createWriteStream } from 'node:fs'
import exstream from 'exstream.js'

const dataUrl =
  'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'

const response = await fetch(dataUrl)
if (!response.ok || !response.body) {
  throw new Error(`Download failed: ${response.status}`)
}

const countries = exstream(response.body)
  .csv({ header: true })
  .filter((row) => row.year === '2007')
  .map((row) => ({
    country: row.country,
    continent: row.continent,
    lifeExpectancy: Number(row.lifeExp),
  }))
  .take(8)

await countries
  .jsonlStringify()
  .pipeTo(createWriteStream('countries.jsonl'))

console.log('Wrote countries.jsonl')

3 Run it

node index.mjs

The Node.js example writes JSON Lines to a file. The browser examples write the same records into the page. Only the source and destination change.

Run the browser pipeline here

The file is fetched and parsed when you press Run. Its records are written into this table.

CountryContinentLife expectancy
No records yet.

What the pipeline does

fetch() provides the response body as a stream. csv() converts incoming bytes into rows, filter() and map() process each row, and take() stops after eight results. The terminal pipeTo() call starts the work and waits for the destination to finish.

The mapping here is synchronous. When each record needs a database query, HTTP request, or other asynchronous work, use mapAsync() with bounded concurrency.

Continue