'Discard chunk in NodeJS Transform stream and read the next one
How do I discard a chunk in a NodeJS transform stream and read the next one from the previous stream?
stream.on('data',function(data){}).pipe(rulesCheck).pipe(step1).pipe(step2).pipe(step3).pipe(insertDataIntoDB);
I would like to discard the chunk if it doesn't pass certain criteria in the ruleCheck stream.
Solution 1:[1]
Sorry for the topic necromancing. Adding here just in case anyone else will be looking for the same.
const stream = require('stream');
const check = new stream.Transform({
transform(data, encoding, callback){
// Discard if the string representation of the chunk is "abc"
if(data.toString() === 'abc') callback(null, Buffer.alloc(0));
else callback(null, data);
},
});
check.pipe(process.stdout);
check.write('123');
check.write('4567');
check.write('abc');
check.write('defg\n');
check.end();
Solution 2:[2]
Write a Duplex stream and stick it in the middle.
To simplify that, look at libraries like event-stream which can simplify implementation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AndrewK |
| Solution 2 | tadman |
