1

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.

user2405589
  • 881
  • 2
  • 14
  • 32

2 Answers2

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();
AndrewK
  • 345
  • 3
  • 9
-1

Write a Duplex stream and stick it in the middle.

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

tadman
  • 208,517
  • 23
  • 234
  • 262