Node.js Streams
2020-04-23T19:36:00.000Z
This is the first topic recommend by the node certification guide, I really like using streams but the Node api is bit cumbersome compared to my previous experiences. The api provides two main classes, Writable and Redable, there's also Duplex that just inherits from both of them and Transform which is just a special form of Duplex that facilitates applying functions to streams.
const { Writable, Redable, Duplex, Transform } = require("stream")
const outStream = new Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString())
callback()
},
})
const inStream = new Readable({
read(size) {
this.push(String.fromCharCode(this.currentCharCode++))
if (this.currentCharCode > 90) {
this.push(null)
}
},
})
const inoutStream = new Duplex({
write(chunk, encoding, callback) {
console.log(chunk.toString())
callback()
},
read(size) {
this.push(String.fromCharCode(this.currentCharCode++))
if (this.currentCharCode > 90) {
this.push(null)
}
},
})
const upperCaseTr = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase())
callback()
},
})The Pipe() method on Redable streams can be used compose them with Writable streams, but the pipeline() utility provides a much nicer way of doing this.
a.pipe(read).pipe(duplex).pipe(write)
pipeline(read, duplex, write, err => {})Streams, like every javascript api, comes with a few event handlers
readable.on("data", chunk => {
writable.write(chunk)
})
readable.on("end", () => {
writable.end()
})ObjectMode is an unexpected cool property that let's you mark your stream output/input type as an Object instead of Buffer, which is the default.
const arrayToObject = new Transform({
readableObjectMode: true,
writableObjectMode: true,
transform(chunk, encoding, callback) {
const obj = {}
for (let i = 0; i < chunk.length; i += 2) {
obj[chunk[i]] = chunk[i + 1]
}
this.push(obj)
callback()
},
})the chunk argument in any stream with writableObjectMode: true will be an Object :)
A lot of Node core libraries implement the stream api, so using it feels pretty natural. Thanks to jscomplete and heynode for clear and well structure resources.