NodeJs Stream Tipeline con transformación personalizada
const fs = require('fs');
const { pipeline, Transform } = require('stream');
const readStream = fs.createReadStream("text.txt", {
flags: 'r',
encoding: 'utf-8',
autoClose: true,
});
const writeStream = fs.createWriteStream("text2.txt", {
flags: 'w',
encoding: 'utf-8',
autoClose: true,
});
const transformStream = new Transform({
//Transform data to filter for even numbers
transform(chunk, enc, cb) {
const data = chunk.toString().split(" ").filter(value => !(parseInt(value) & 1));
cb(null, data.join(" "));
}
});
pipeline(readStream, transformStream, writeStream, (err) => {
if(err)
console.error(err);
else
console.error("success");
//writeStream has written only even numbers from readStream
})
Muhammad Abdullah