Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Small API cleanup (breaking).

  • Loading branch information...
commit 54e843c5f782cb1801dde1b0c031b4f44f8cb576 1 parent f96d139
@kunklejr authored
View
9 CHANGELOG.md
@@ -0,0 +1,9 @@
+## 0.2.0
+
+Breaking API:
+
+ - Creating a parser is now done via the pcapp.parse method rather than
+ new pcapp.Parser(). Explicitly calling the parse method on the
+ returned parser is no longer necessary, or possible. All parsing
+ events are emitted on the next tick.
+
View
12 README.md
@@ -11,23 +11,23 @@ Packet capture (pcap) file parser for Node.js
```javascript
var pcapp = require('pcap-parser');
-var parser = new pcapp.Parser('/path/to/file.pcap');
+var parser = pcapp.parse('/path/to/file.pcap');
parser.on('packet', function(packet) {
// do your packet processing
});
-parser.parse(); // to kick things off
```
## Events
pcap-parser emits five different events, only some of which you'll
likely care about. Each event is emitted from the parser created with
-`new pcapp.Parser`. The `pcapp.Parser` constructor can be passed a
+`pcapp.parse`. The `pcapp.parse` method can be passed a
file path or a readable stream.
-pcap-parser only parses version 2.4 of the libpcap file format.
-Please see http://wiki.wireshark.org/Development/LibpcapFileFormat for
-detailed documentation of the pcap file format.
+pcap-parser only parses version 2.4 of the libpcap file format in big
+or little endian format. Please see
+http://wiki.wireshark.org/Development/LibpcapFileFormat for detailed
+documentation of the pcap file format.
### globalHeader
View
10 lib/pcap-parser.js
@@ -141,11 +141,11 @@ function Parser(input) {
this.buffer = null;
this.state = parseGlobalHeader;
this.endianness = null;
+
+ process.nextTick(this.stream.resume.bind(this.stream));
}
util.inherits(Parser, events.EventEmitter);
-Parser.prototype.parse = function() {
- this.stream.resume();
-};
-
-exports.Parser = Parser;
+exports.parse = function (input) {
+ return new Parser(input);
+}
View
4 package.json
@@ -3,7 +3,7 @@
"description": "Packet capture (PCAP) parser for node",
"author": "Jeff Kunkle <jeff.kunkle@nearinfinity.com>",
"keywords": ["pcap", "parser"],
- "version": "0.1.3",
+ "version": "0.2.0",
"engines": { "node" : ">=0.6.0" },
"maintainers": [
{ "name": "Jeff Kunkle", "email": "jeff.kunkle@nearinfinity.com" },
@@ -16,6 +16,6 @@
"vows": "~0.5.13"
},
"scripts": {
- "test": "vows test/pcap-parser.js"
+ "test": "vows"
}
}
View
22 test/pcap-parser-test.js
@@ -6,12 +6,11 @@ var pcapp = require('../lib/pcap-parser');
vows.describe('pcap-parser').addBatch({
'given a bad/malformed pcap file': {
- topic: new pcapp.Parser(fs.createReadStream(path.join(__dirname, 'malformed.pcap'))),
+ topic: pcapp.parse(fs.createReadStream(path.join(__dirname, 'malformed.pcap'))),
'the parser should emit an error event': {
topic: function(parser) {
parser.once('error', this.callback.bind(this, null));
- parser.parse();
},
'an error event should have been emitted': function(err) {
@@ -21,12 +20,11 @@ vows.describe('pcap-parser').addBatch({
},
'given a readable stream of a little-endian pcap file': {
- topic: new pcapp.Parser(fs.createReadStream(path.join(__dirname, 'smtp.pcap'))),
+ topic: pcapp.parse(fs.createReadStream(path.join(__dirname, 'smtp.pcap'))),
'the parser should emit globalHeader events': {
topic: function(parser) {
parser.once('globalHeader', this.callback.bind(this, null));
- parser.parse();
},
'global header values should be correct': function(header) {
@@ -44,7 +42,6 @@ vows.describe('pcap-parser').addBatch({
'the parser should emit packetHeader events': {
topic: function(parser) {
parser.once('packetHeader', this.callback.bind(this, null));
- parser.parse();
},
'packet header values should be correct': function(packetHeader) {
@@ -59,7 +56,6 @@ vows.describe('pcap-parser').addBatch({
'the parser should emit packetData events': {
topic: function(parser) {
parser.once('packetData', this.callback.bind(this, null));
- parser.parse();
},
'packet data buffer should not be empty': function(packetData) {
@@ -71,7 +67,6 @@ vows.describe('pcap-parser').addBatch({
'the parser should emit packet events': {
topic: function(parser) {
parser.once('packet', this.callback.bind(this, null));
- parser.parse();
},
'packet values should be correct': function(packet) {
@@ -103,8 +98,6 @@ vows.describe('pcap-parser').addBatch({
}).on('end', function() {
this.callback(null, count);
}.bind(this));
-
- parser.parse();
},
'it should process 60 packets': function(count) {
@@ -114,12 +107,11 @@ vows.describe('pcap-parser').addBatch({
},
'given a readable stream of a big-endian pcap file': {
- topic: new pcapp.Parser(fs.createReadStream(path.join(__dirname, 'be.pcap'))),
+ topic: pcapp.parse(fs.createReadStream(path.join(__dirname, 'be.pcap'))),
'the parser should emit globalHeader events': {
topic: function(parser) {
parser.once('globalHeader', this.callback.bind(this, null));
- parser.parse();
},
'global header values should be correct': function(header) {
@@ -137,7 +129,6 @@ vows.describe('pcap-parser').addBatch({
'the parser should emit packetHeader events': {
topic: function(parser) {
parser.once('packetHeader', this.callback.bind(this, null));
- parser.parse();
},
'packet header values should be correct': function(packetHeader) {
@@ -152,7 +143,6 @@ vows.describe('pcap-parser').addBatch({
'the parser should emit packetData events': {
topic: function(parser) {
parser.once('packetData', this.callback.bind(this, null));
- parser.parse();
},
'packet data buffer should not be empty': function(packetData) {
@@ -164,7 +154,6 @@ vows.describe('pcap-parser').addBatch({
'the parser should emit packet events': {
topic: function(parser) {
parser.once('packet', this.callback.bind(this, null));
- parser.parse();
},
'packet values should be correct': function(packet) {
@@ -196,8 +185,6 @@ vows.describe('pcap-parser').addBatch({
}).on('end', function() {
this.callback(null, count);
}.bind(this));
-
- parser.parse();
},
'it should process 5 packets': function(count) {
@@ -207,7 +194,7 @@ vows.describe('pcap-parser').addBatch({
},
'given a path to a pcap file': {
- topic: new pcapp.Parser(path.join(__dirname, 'smtp.pcap')),
+ topic: pcapp.parse(path.join(__dirname, 'smtp.pcap')),
'the parser should emit all the same events': {
topic: function(parser) {
@@ -229,7 +216,6 @@ vows.describe('pcap-parser').addBatch({
parser.once('packetHeader', ifDone.bind(this, 'packetHeader'));
parser.once('packetData', ifDone.bind(this, 'packetData'));
parser.once('packet', ifDone.bind(this, 'packet'));
- parser.parse();
},
'all events should have been emitted': function(confirmation) {
Please sign in to comment.
Something went wrong with that request. Please try again.