What is Stream Chaining in Node.js?
What is Stream Chaining in Node.js?
Stream chaining in Node.js is a technique used to connect multiple streams together to form a data processing pipeline. This method allows for the sequential execution of operations on streaming data, where the output of one stream becomes the input of the next stream in the chain. Stream chaining is particularly useful for efficiently handling large volumes of data or for performing complex transformations on data as it flows through the system.
In practical terms, stream chaining typically involves using the .pipe()
method to link streams. For example, you might have a readable stream that extracts data from a file, a transform stream that modifies the data in some way, and a writable stream that outputs the modified data to another file or system. By chaining these streams, you can efficiently process data from start to finish without requiring large amounts of memory to store intermediate states.
Here’s a basic example of stream chaining in Node.js using file streams and a transform stream:
const fs = require('fs');
const { Transform } = require('stream');
// Create a readable stream from a source file
const readStream = fs.createReadStream('input.txt');
// Create a writable stream to an output file
const writeStream = fs.createWriteStream('output.txt');
// Create a transform stream to modify the data
const transformStream = new Transform({
transform...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào