Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
  • 8 commits
  • 7 files changed
  • 0 commit comments
  • 3 contributors
Showing with 38 additions and 41 deletions.
  1. +1 −0 .gitignore
  2. +14 −0 CHANGELOG.md
  3. +6 −6 README.md
  4. +0 −3 index.js
  5. +7 −6 lib/pcap-parser.js
  6. +6 −8 package.json
  7. +4 −18 test/pcap-parser-test.js
View
1 .gitignore
@@ -3,3 +3,4 @@ node_modules/
coverage/
coverage.*
lib-cov/
+.idea
View
14 CHANGELOG.md
@@ -0,0 +1,14 @@
+## 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.
+
+## 0.2.1
+
+Bug:
+
+ - index.js was not updated to reflect the API change in 0.2.0
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
3 index.js
@@ -1,3 +0,0 @@
-var pp = require('./lib/pcap-parser');
-
-exports.Parser = pp.Parser;
View
13 lib/pcap-parser.js
@@ -41,6 +41,7 @@ function parseGlobalHeader() {
var buffer = this.buffer;
if (buffer.length >= GLOBAL_HEADER_LENGTH) {
+ var msg;
var magicNumber = buffer.toString('hex', 0, 4);
// determine pcap endianness
@@ -51,7 +52,7 @@ function parseGlobalHeader() {
} else {
this.errored = true;
this.stream.pause();
- var msg = util.format('unknown magic number: %s', magicNumber);
+ msg = util.format('unknown magic number: %s', magicNumber);
this.emit('error', new Error(msg));
onEnd.call(this);
return false;
@@ -70,7 +71,7 @@ function parseGlobalHeader() {
if (header.majorVersion != 2 && header.minorVersion != 4) {
this.errored = true;
this.stream.pause();
- var msg = util.format('unsupported version %d.%d. pcap-parser only parses libpcap file format 2.4', header.majorVersion, header.minorVersion);
+ msg = util.format('unsupported version %d.%d. pcap-parser only parses libpcap file format 2.4', header.majorVersion, header.minorVersion);
this.emit('error', new Error(msg));
onEnd.call(this);
} else {
@@ -141,11 +142,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.parse = function (input) {
+ return new Parser(input);
};
-
-exports.Parser = Parser;
View
14 package.json
@@ -3,22 +3,20 @@
"description": "Packet capture (PCAP) parser for node",
"author": "Jeff Kunkle <jeff.kunkle@nearinfinity.com>",
"keywords": ["pcap", "parser"],
- "homepage": "https://github.com/nearinfinity/node-pcap-parser",
- "version": "0.1.2",
+ "version": "0.2.1",
"engines": { "node" : ">=0.6.0" },
"maintainers": [
{ "name": "Jeff Kunkle", "email": "jeff.kunkle@nearinfinity.com" },
{ "name": "Joe Ferner", "email": "joe.ferner@nearinfinity.com" }
],
"bugs": { "url": "https://github.com/nearinfinity/node-pcap-parser/issues" },
- "licenses": [ { "type" : "MIT" } ],
- "repositories": [
- { "type": "git", "url": "https://github.com/nearinfinity/node-pcap-parser.git" }
- ],
+ "license": "MIT",
+ "repository": { "type": "git", "url": "https://github.com/nearinfinity/node-pcap-parser.git" },
"devDependencies": {
"vows": "~0.5.13"
},
"scripts": {
- "test": "vows test/pcap-parser.js"
- }
+ "test": "vows"
+ },
+ "main": "./lib/pcap-parser.js"
}
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) {

No commit comments for this range

Something went wrong with that request. Please try again.