node-pcap-parser icon indicating copy to clipboard operation
node-pcap-parser copied to clipboard

no way to delay start of data flow

Open wanderview opened this issue 12 years ago • 2 comments

Currently the pcap-parser always starts on the next tick due to this code:

  process.nextTick(this.stream.resume.bind(this.stream));

Even if you attempt to call parser.stream.pause() after creating the parser it will still be summarily resume()'d on the nextTick(). It would be nice if there was an option to the constructor indicating that the stream should remain paused.

Alternatively, implementing the Readable interface provided by streams2 would provide a similar effect.

I haven't looked at writing a pull request because I heard a rumor that a conversion to streams2 may be in the works. Is that true?

Thanks!

wanderview avatar Feb 11 '13 02:02 wanderview

For whatever its worth, I worked around this by wrapping pcap-parser in a Readable. I used something like this:

var Readable = require('stream').Readable;

var stream = new Readable({objectMode: true});

var parser = new PcapParser(file);
parser.on('packet', function(packet) {
  var res = stream.push(packet);
  if (!res) {
    parser.stream.pause();
  }
});

stream._read = function() {
  parser.stream.resume();
};

stream.on('readable', function() {
  var packet = stream.read();
  // process packet...
});

// Don't start the process until ready for data...
stream.read(0);

wanderview avatar Feb 12 '13 05:02 wanderview

I ended up wrapping this up into a module in node-pcap-stream. I'd be happy to convert this to a pull request if you think it makes sense to incorporate directly into node-pcap-parser. Thanks.

wanderview avatar Feb 21 '13 04:02 wanderview