made Parser non-static

This commit is contained in:
brianc 2010-09-29 00:12:04 -05:00
parent a9a9f2e03a
commit a921d6c144
2 changed files with 57 additions and 53 deletions

View File

@ -45,62 +45,66 @@ Client.prototype.connect = function() {
});
};
var Parser = function() {
};
var p = Parser.prototype;
var Parser = {
parse: function(buffer, offset) {
offset = offset || 0;
switch(buffer[offset]) {
case UTF8.R:
return Parser.parseR(buffer, offset);
case UTF8.S:
return Parser.parseS(buffer, offset);
default:
throw new Error("Unsupported message ID");
}
},
parseR: function(buffer, offset) {
var type = buffer[offset++];
var length = Parser.parseLength(buffer, offset);
offset += 4;
if(length == 8) {
return {
name: 'AuthenticationOk',
id: 'R',
length: length
}
}
throw new Erro("Unknown AuthenticatinOk message type");
},
parseS: function(buffer, offset) {
var type = buffer[offset++];
var length = Parser.parseLength(buffer, offset);
offset += 4;
var start = 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);
return {
name: 'ParameterStatus',
id: 'S',
length: length,
parameterName: parameterName,
parameterValue: parameterValue
}
},
parseLength: function(buffer, offset) {
var length = ((buffer[offset++] << 24) +
(buffer[offset++] << 16) +
(buffer[offset++] << 8) +
buffer[offset++]);
return length;
p.parse = function(buffer, offset) {
offset = offset || 0;
switch(buffer[offset]) {
case UTF8.R:
return this.parseR(buffer, offset);
case UTF8.S:
return this.parseS(buffer, offset);
default:
throw new Error("Unsupported message ID");
}
};
p.parseR = function(buffer, offset) {
var type = buffer[offset++];
var length = this.parseLength(buffer, offset);
offset += 4;
if(length == 8) {
return {
name: 'AuthenticationOk',
id: 'R',
length: length
}
}
throw new Error("Unknown AuthenticatinOk message type");
};
p.parseS = function(buffer, offset) {
var type = buffer[offset++];
var length = this.parseLength(buffer, offset);
offset += 4;
var start = 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);
return {
name: 'ParameterStatus',
id: 'S',
length: length,
parameterName: parameterName,
parameterValue: parameterValue
}
};
p.parseLength = function(buffer, offset) {
var length = ((buffer[offset++] << 24) +
(buffer[offset++] << 16) +
(buffer[offset++] << 8) +
buffer[offset++]);
return length;
};
module.exports = {
Client: Client,

View File

@ -1,7 +1,7 @@
require(__dirname+'/test-helper');
test('Parser on single messages', function() {
test('parses AuthenticationOk message', function() {
var result = Parser.parse(Buffer([0x52, 00, 00, 00, 08, 00, 00, 00, 00]));
var result = new Parser().parse(Buffer([0x52, 00, 00, 00, 08, 00, 00, 00, 00]));
assert.equal(result.name, 'AuthenticationOk');
assert.equal(result.id, 'R');
assert.equal(result.length, 8);
@ -11,7 +11,7 @@ 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 = Parser.parse(Buffer(bytes));
var result = new Parser().parse(Buffer(bytes));
assert.equal(result.name, 'ParameterStatus');
assert.equal(result.id, 'S');
assert.equal(result.length, 25);