parsing of parameterstatus messsage

This commit is contained in:
brianc 2010-09-29 00:08:53 -05:00
parent dc427a9923
commit a9a9f2e03a
2 changed files with 40 additions and 7 deletions

View File

@ -3,9 +3,10 @@ var sys = require('sys');
var net = require('net');
var NUL = '\0';
var chars = Buffer('R','utf8');
var chars = Buffer('RS','utf8');
var UTF8 = {
R: chars[0]
R: chars[0],
S: chars[1]
};
@ -52,16 +53,16 @@ var Parser = {
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 = ((buffer[offset++] << 24) +
(buffer[offset++] << 16) +
(buffer[offset++] << 8) +
buffer[offset++]);
var length = Parser.parseLength(buffer, offset);
offset += 4;
if(length == 8) {
return {
name: 'AuthenticationOk',
@ -69,6 +70,34 @@ var Parser = {
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;
}
};

View File

@ -12,7 +12,11 @@ test('Parser on single messages', function() {
var secondString = [0x55, 0x54, 0x46, 0x38, 0];
var bytes = [0x53, 0, 0, 0, 0x19].concat(firstString).concat(secondString);
var result = Parser.parse(Buffer(bytes));
assert.equal(result.name, 'ParameterStatus');
assert.equal(result.id, 'S');
assert.equal(result.length, 25);
assert.equal(result.parameterName, "client_encoding");
assert.equal(result.parameterValue, "UTF8");
});
});