Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moving parser to use prototypes, like the producer.

This will make unit tests possible, more consistent code and well, you get the
idea. We're instantiating a new instance on export because this will mean that
users won't have to change how they already use the library - it should just
work as is.
  • Loading branch information...
commit d3165c30641a63572985c5c366b77db7f5be1ffd 1 parent 38cdaa1
@squeeks authored
Showing with 22 additions and 20 deletions.
  1. +22 −20 lib/glossy/parse.js
View
42 lib/glossy/parse.js
@@ -96,6 +96,8 @@ var signedBlockValues = {
};
+var GlossyParser = function() {};
+
/*
* Parse the raw message received.
*
@@ -103,7 +105,7 @@ var signedBlockValues = {
* @param {Function} callback Callback to run after parse is complete
* @return {Object} map containing all successfully parsed data.
*/
-function parseMessage(rawMessage, callback) {
+GlossyParser.prototype.parse = function(rawMessage, callback) {
// Are you node.js? Is this a Buffer?
if(typeof Buffer == 'function' && Buffer.isBuffer(rawMessage)) {
@@ -119,7 +121,7 @@ function parseMessage(rawMessage, callback) {
var segments = rawMessage.split(' ');
if(segments.length < 2) return parsedMessage;
- var priKeys = decodePri(segments[0]);
+ var priKeys = this.decodePri(segments[0]);
if(priKeys) {
for (key in priKeys) parsedMessage[key] = priKeys[key];
}
@@ -128,11 +130,11 @@ function parseMessage(rawMessage, callback) {
if(segments[0].match(/^(<\d+>\d)$/)) {
segments.shift(); // Shift the prival off
var timeStamp = segments.shift();
- parsedMessage.time = parseTimeStamp(timeStamp);
- parsedMessage.host = decideValue(segments.shift());
- parsedMessage.appName = decideValue(segments.shift());
- parsedMessage.pid = decideValue(segments.shift());
- parsedMessage.msgID = decideValue(segments.shift());
+ parsedMessage.time = this.parseTimeStamp(timeStamp);
+ parsedMessage.host = this.decideValue(segments.shift());
+ parsedMessage.appName = this.decideValue(segments.shift());
+ parsedMessage.pid = this.decideValue(segments.shift());
+ parsedMessage.msgID = this.decideValue(segments.shift());
if(segments[0] !== '-') {
var spliceMarker = 0;
@@ -145,14 +147,14 @@ function parseMessage(rawMessage, callback) {
}
if(spliceMarker != 0) {
var sd = segments.splice(0, spliceMarker).join(' ');
- parsedMessage.structuredData = parseStructure(sd);
+ parsedMessage.structuredData = this.parseStructure(sd);
if(parsedMessage.structuredData['ssign']) {
parsedMessage.structuredData.signedBlock =
- parseSignedBlock(parsedMessage.structuredData);
+ this.parseSignedBlock(parsedMessage.structuredData);
} else if(parsedMessage.structuredData['ssign-cert']) {
parsedMessage.structuredData.signedBlock =
- parseSignedCertificate(parsedMessage.structuredData);
+ this.parseSignedCertificate(parsedMessage.structuredData);
}
}
@@ -164,14 +166,14 @@ function parseMessage(rawMessage, callback) {
} else if (segments[0].match(/^(<\d+>\d+:)$/)) {
parsedMessage.type = 'RFC3164';
var timeStamp = segments.splice(0,1).join(' ').replace(/^(<\d+>)/,'');
- parsedMessage.time = parseTimeStamp(timeStamp);
+ parsedMessage.time = this.parseTimeStamp(timeStamp);
parsedMessage.message = segments.join(' ');
} else if(segments[0].match(/^(<\d+>\w+)/)) {
parsedMessage.type = 'RFC3164';
if (segments[1] == '') segments.splice(1,1);
var timeStamp = segments.splice(0,3).join(' ').replace(/^(<\d+>)/,'');
- parsedMessage.time = parseTimeStamp(timeStamp);
+ parsedMessage.time = this.parseTimeStamp(timeStamp);
parsedMessage.host = segments.shift();
parsedMessage.message = segments.join(' ');
}
@@ -189,7 +191,7 @@ function parseMessage(rawMessage, callback) {
* @param {String} a section from an RFC5424 message
* @return {Boolean/String} null if string is entirely '-', or the original value
*/
-function decideValue(value) {
+GlossyParser.prototype.decideValue = function(value) {
return value === '-' ? null : value;
}
@@ -200,7 +202,7 @@ function decideValue(value) {
* @return {Object} Returns object containing Facility, Severity and Version
* if correctly parsed, empty values on failure.
*/
-function decodePri(message) {
+GlossyParser.prototype.decodePri = function(message) {
if(typeof message != 'string') return;
var privalMatch = message.match(/^<(\d+)>/);
@@ -233,7 +235,7 @@ function decodePri(message) {
* not the entire message
* @return {Object} Date object on success
*/
-function parseTimeStamp(timeStamp) {
+GlossyParser.prototype.parseTimeStamp = function(timeStamp) {
if(typeof timeStamp != 'string') return;
@@ -307,7 +309,7 @@ function parseTimeStamp(timeStamp) {
* @param {String} msg The STRUCTURED-DATA section
* @return {Object} sdStructure parsed structure
*/
-function parseStructure(msg) {
+GlossyParser.prototype.parseStructure = function(msg) {
var sdStructure = { };
var sdElements = msg.split('][');
//TODO could we improve things here?
@@ -339,7 +341,7 @@ function parseStructure(msg) {
* @return {Object} validatedBlock translated and named values, binary
* elements will be Buffer objects, if available
*/
-function parseSignedBlock(block) {
+GlossyParser.prototype.parseSignedBlock = function(block) {
if(typeof block != 'object') return false;
@@ -395,7 +397,7 @@ function parseSignedBlock(block) {
* @return {Object} validatedBlock translated and named values, binary
* elements will be Buffer objects, if available
*/
-function parseSignedCertificate(block) {
+GlossyParser.prototype.parseSignedCertificate = function(block) {
if(typeof block != 'object') return false;
@@ -428,7 +430,7 @@ function parseSignedCertificate(block) {
validatedBlock.fragmentLength = parseInt(signedBlock.FLEN);
var payloadFragment = signedBlock.FRAG.split(/\s/);
- validatedBlock.payloadTimestamp = parseTimeStamp(payloadFragment[0]);
+ validatedBlock.payloadTimestamp = this.parseTimeStamp(payloadFragment[0]);
validatedBlock.payloadType = payloadFragment[1];
validatedBlock.payloadName = signedBlockValues.keyBlobType[payloadFragment[1]];
@@ -448,5 +450,5 @@ function parseSignedCertificate(block) {
if(typeof module == 'object') {
- module.exports.parse = parseMessage;
+ module.exports = new GlossyParser();
}
Please sign in to comment.
Something went wrong with that request. Please try again.