diff --git a/lib/index.js b/lib/index.js index 507bc24c..fbf5a521 100644 --- a/lib/index.js +++ b/lib/index.js @@ -45,27 +45,27 @@ Client.prototype.connect = function() { }); }; -var Parser = function() { +var Parser = function(buffer) { this.offset = 0; + this.buffer = buffer; }; var p = Parser.prototype; - p.parse = function(buffer) { - switch(buffer[this.offset]) { + switch(this.buffer[this.offset]) { case UTF8.R: - return this.parseR(buffer); + return this.parseR(); case UTF8.S: - return this.parseS(buffer); + return this.parseS(); default: throw new Error("Unsupported message ID"); } }; -p.parseR = function(buffer) { - var type = buffer[this.offset++]; - var length = this.parseLength(buffer); +p.parseR = function() { + var type = this.buffer[this.offset++]; + var length = this.parseLength(); if(length == 8) { return { name: 'AuthenticationOk', @@ -76,18 +76,10 @@ p.parseR = function(buffer) { throw new Error("Unknown AuthenticatinOk message type"); }; p.parseS = function(buffer) { - var offset = 0; - var type = buffer[this.offset++]; - var length = this.parseLength(buffer,this.offset); - offset += 4; - var start = this.offset; - while(buffer[offset++]) { } - var end = offset -1; - var parameterName = buffer.toString('utf8',start, end); - var start = offset; - while(buffer[offset++]) { } - var end = offset - 1; - var parameterValue = buffer.toString('utf8', start, end); + var type = this.buffer[this.offset++]; + var length = this.parseLength(this.buffer); + var parameterName = this.parseCString(); + var parameterValue = this.parseCString(); return { name: 'ParameterStatus', id: 'S', @@ -97,19 +89,19 @@ p.parseS = function(buffer) { } }; -p.parseLength = function(buffer) { - var length = ((buffer[this.offset++] << 24) + - (buffer[this.offset++] << 16) + - (buffer[this.offset++] << 8) + - buffer[this.offset++]); - return length; +p.parseLength = function() { + var buffer = this.buffer; + return ((buffer[this.offset++] << 24) + + (buffer[this.offset++] << 16) + + (buffer[this.offset++] << 8) + + buffer[this.offset++]); + }; p.parseCString = function(buffer) { var start = this.offset; - while(buffer[this.offset++]) { }; - var end = this.offset - 1; - return buffer.toString('utf8',start, end); + while(this.buffer[this.offset++]) { }; + return this.buffer.toString('utf8',start, this.offset - 1); }; diff --git a/test/parser-tests.js b/test/parser-tests.js index 8b3abd02..a809a276 100644 --- a/test/parser-tests.js +++ b/test/parser-tests.js @@ -1,7 +1,8 @@ require(__dirname+'/test-helper'); test('Parser on single messages', function() { test('parses AuthenticationOk message', function() { - var result = new Parser().parse(Buffer([0x52, 00, 00, 00, 08, 00, 00, 00, 00])); + var buffer = Buffer([0x52, 00, 00, 00, 08, 00, 00, 00, 00]); + var result = new Parser(buffer).parse(); assert.equal(result.name, 'AuthenticationOk'); assert.equal(result.id, 'R'); assert.equal(result.length, 8); @@ -11,7 +12,8 @@ test('Parser on single messages', function() { var firstString = [0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0]; var secondString = [0x55, 0x54, 0x46, 0x38, 0]; var bytes = [0x53, 0, 0, 0, 0x19].concat(firstString).concat(secondString); - var result = new Parser().parse(Buffer(bytes)); + var buffer = Buffer(bytes); + var result = new Parser(buffer).parse(); assert.equal(result.name, 'ParameterStatus'); assert.equal(result.id, 'S'); assert.equal(result.length, 25); @@ -20,13 +22,20 @@ test('Parser on single messages', function() { }); test('parses normal CString', function() { - var result = new Parser().parseCString(Buffer([33,00])); + var result = new Parser(Buffer([33,0])).parseCString(); assert.equal(result,"!"); }); test('parses empty CString', function() { - var result = new Parser().parseCString(Buffer([0])); + var result = new Parser(Buffer([0])).parseCString(); assert.equal(result, ''); }); + + test('parses length', function() { + var parser = new Parser(Buffer([0,0,0,3])); + var result = parser.parseLength(); + assert.equal(result, 3); + assert.equal(parser.offset, 4); + }); });