Discard chunk in NodeJS Transform stream and read the next one

821 Views Asked by At

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.

2

There are 2 best solutions below

0
On

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();
0
On

Write a Duplex stream and stick it in the middle.

To simplify that, look at libraries like event-stream which can simplify implementation.