Reading streams
Introduction
Streams handle data incrementally through Node's stream APIs. Examples use ESM imports.
- Stream API documentation (nodejs.org)
Async iteration
import { Readable } from 'node:stream'
for await (const chunk of Readable.from(['a', 'b', 'c'])) {
console.log(chunk)
}
// → a
// → b
// → c
for await attaches an error handler and stops when the stream ends.
See: async iterators
Events
import { Readable } from 'node:stream'
const source = Readable.from(['a', 'b'])
source.on('data', (chunk) => console.log('data:', chunk))
source.on('end', () => console.log('end'))
source.on('error', (err) => console.error(err))
// → data: a
// → data: b
// → end
A data listener switches the stream to flowing mode.
See: event: 'data'
Flowing and paused
import { Readable } from 'node:stream'
const source = Readable.from(['a', 'b'])
console.log(source.readableFlowing) // → null
console.log(source.read()) // → a
source.on('data', (chunk) => console.log(chunk))
// → b
source.pause() // stop emitting data
source.resume() // resume emitting data
readableFlowing is null until a reader attaches; data, pipe(), and resume() set it to true.
See: two reading modes
Writing streams
Writable
import { Writable } from 'node:stream'
const sink = new Writable({
write(chunk, _, done) {
console.log('write:', chunk.toString())
done() // call done(err) to fail
}
})
sink.on('finish', () => console.log('finish'))
sink.write('hello')
sink.end(' world')
// → write: hello
// → write: world
// → finish
Call done() once per chunk; end() flushes and emits finish.
Backpressure
import { Writable } from 'node:stream'
const slow = new Writable({
highWaterMark: 2,
write(chunk, _, done) {
setTimeout(done, 10)
}
})
console.log(slow.write('a')) // → true
console.log(slow.write('b')) // → false
slow.on('drain', () => {
console.log('drain')
slow.end()
})
Wait for drain after write() returns false. pipeline() handles this for you.
See: buffering
Pipelines
Promise pipeline
import { Readable, Transform, Writable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
const upper = new Transform({
transform(chunk, _, done) {
done(null, chunk.toString().toUpperCase())
}
})
const sink = new Writable({
write(chunk, _, done) {
console.log(chunk.toString())
done()
}
})
await pipeline(Readable.from(['hi', 'there']), upper, sink)
// → HI
// → THERE
pipeline() forwards errors, destroys streams on failure, and awaits completion.
Callback pipeline
import { Readable, Writable, pipeline } from 'node:stream'
const out = new Writable({
write(chunk, _, done) {
console.log(chunk.toString())
done()
}
})
pipeline(Readable.from(['a', 'b']), out, (err) => {
console.log(err ? err.message : 'done')
})
// → a
// → b
// → done
pipeline is also available from node:stream as a callback function.
Transform
import { Readable, Transform, Writable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
class Indexed extends Transform {
constructor() {
super({ objectMode: true })
this.count = 0
}
_transform(chunk, _, done) {
done(null, { ...chunk, index: this.count++ })
}
_flush(done) {
this.push({ total: this.count })
done()
}
}
const sink = new Writable({
objectMode: true,
write(chunk, _, done) {
console.log(chunk)
done()
}
})
await pipeline(Readable.from([{ name: 'a' }, { name: 'b' }]),
new Indexed(), sink)
// → { name: 'a', index: 0 }
// → { name: 'b', index: 1 }
// → { total: 2 }
_transform() emits one indexed chunk per input; _flush() appends a { total } summary. A transform may emit zero or more chunks per input.
Finished
import { Readable } from 'node:stream'
import { finished } from 'node:stream/promises'
const source = Readable.from(['a', 'b'])
source.resume()
await finished(source, { cleanup: true })
console.log('finished')
// → finished
cleanup: true removes listeners after settling; the default keeps them.
See: finished
Errors and abort
import { Readable, Writable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
const ac = new AbortController()
setTimeout(() => ac.abort(), 10)
const source = Readable.from(async function* () {
await new Promise((resolve) => setTimeout(resolve, 50))
yield 'x'
}())
try {
await pipeline(source, new Writable({
write(chunk, _, done) { done() }
}), { signal: ac.signal })
} catch (err) {
console.log(err.name) // → AbortError
}
Aborting destroys the pipeline. stream.destroy(err) emits error then close.
See: pipeline options
Web streams
To web
import { Readable } from 'node:stream'
const web = Readable.toWeb(Readable.from(['a', 'b']))
for await (const chunk of web) {
console.log(chunk)
}
// → a
// → b
See: Readable.toWeb()
From web
import { Writable } from 'node:stream'
const node = Writable.fromWeb(new WritableStream({
write(chunk) { console.log(chunk.toString()) }
}))
node.end('hi')
// → hi
Readable.fromWeb() and Writable.toWeb() convert in the other direction.
See: Writable.fromWeb()
Creating streams
Core types
| Type | Role |
|---|---|
Readable | Source of data |
Writable | Destination for data |
Duplex | Reads and writes independently |
Transform | Modifies chunks as they pass through |
PassThrough | Transform that echoes input |
See: types of streams
Readable
import { Readable } from 'node:stream'
class Counter extends Readable {
constructor(max) {
super({ objectMode: true })
this.n = 0
this.max = max
}
_read() {
this.n < this.max ? this.push(this.n++) : this.push(null)
}
}
for await (const n of new Counter(3)) {
console.log(n)
}
// → 0
// → 1
// → 2
push(null) ends the stream; objectMode lets it carry arbitrary values.
Async generators
import { Readable } from 'node:stream'
async function* ticks() {
for (const n of [1, 2, 3]) yield n
}
for await (const n of Readable.from(ticks())) {
console.log(n)
}
// → 1
// → 2
// → 3
Readable.from() wraps an async generator; plain iterables work too.
PassThrough
import { PassThrough } from 'node:stream'
const pass = new PassThrough()
pass.on('data', (chunk) => console.log('echo:', chunk.toString()))
pass.end('ping')
// → echo: ping
PassThrough echoes every write back on its readable side.
See: PassThrough
Object mode
import { Readable } from 'node:stream'
const objects = new Readable({ objectMode: true, read() {} })
const bytes = new Readable({ read() {} })
console.log(objects.readableHighWaterMark) // → 16
console.log(bytes.readableHighWaterMark) // → 65536
Use objectMode to carry arbitrary values. Its high-water mark counts items (16) instead of bytes (65536); string streams count UTF-16 code units.
See: object mode
0 Comments for this cheatsheet. Write yours!