parsing of ready for query

This commit is contained in:
brianc 2010-09-30 00:14:41 -05:00
parent 7c9de7f442
commit 124bd0960e
2 changed files with 68 additions and 10 deletions

View File

@ -3,10 +3,12 @@ var sys = require('sys');
var net = require('net'); var net = require('net');
var NUL = '\0'; var NUL = '\0';
var chars = Buffer('RS','utf8'); var chars = Buffer('RSKZ','utf8');
var UTF8 = { var UTF8 = {
R: chars[0], R: chars[0],
S: chars[1] S: chars[1],
K: chars[2],
Z: chars[3]
}; };
@ -69,6 +71,10 @@ p.parse = function() {
return this.parseR(); return this.parseR();
case UTF8.S: case UTF8.S:
return this.parseS(); return this.parseS();
case UTF8.K:
return this.parseK();
case UTF8.Z:
return this.parseZ();
default: default:
throw new Error("Unsupported message ID: " + Buffer([messageID]).toString('utf8') + " (" + messageID.toString(16) + ")"); throw new Error("Unsupported message ID: " + Buffer([messageID]).toString('utf8') + " (" + messageID.toString(16) + ")");
} }
@ -102,13 +108,42 @@ p.parseS = function(buffer) {
} }
}; };
p.parseLength = function() { p.parseK = function() {
var type = this.buffer[this.offset++];
var length = this.readInt32();
var processID = this.readInt32();
var secretKey = this.readInt32();
return {
name: 'BackendKeyData',
id: 'K',
length: length,
processID: processID,
secretKey: secretKey
}
};
p.parseZ = function() {
var type = this.buffer[this.offset++];
var length = this.readInt32();
var status = this.buffer[this.offset++];
return {
name: 'ReadyForQuery',
id: 'Z',
length: length,
status: Buffer([status]).toString('utf8')
}
};
p.readInt32 = function() {
var buffer = this.buffer; var buffer = this.buffer;
return ((buffer[this.offset++] << 24) + return ((buffer[this.offset++] << 24) +
(buffer[this.offset++] << 16) + (buffer[this.offset++] << 16) +
(buffer[this.offset++] << 8) + (buffer[this.offset++] << 8) +
buffer[this.offset++]); buffer[this.offset++]);
};
p.parseLength = function() {
return this.readInt32();
}; };
p.parseCString = function(buffer) { p.parseCString = function(buffer) {

View File

@ -1,19 +1,25 @@
require(__dirname+'/test-helper'); require(__dirname+'/test-helper');
test('Parser on single messages', function() { test('Parser on single messages', function() {
var authenticationOkBuffer = Buffer([0x52, 00, 00, 00, 08, 00, 00, 00, 00]);
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 parameterStatusBuffer = Buffer(bytes);
var backendKeyDataBuffer = Buffer([0x4b, 0, 0, 0, 0x0c, 0, 0, 0, 1, 0, 0, 0, 2]);
var readyForQueryBuffer = Buffer([0x5a, 0, 0, 0, 5, 'I'.charCodeAt(0)])
test('parses AuthenticationOk message', function() { test('parses AuthenticationOk message', function() {
var buffer = Buffer([0x52, 00, 00, 00, 08, 00, 00, 00, 00]); var result = new Parser(authenticationOkBuffer).parse();
var result = new Parser(buffer).parse();
assert.equal(result.name, 'AuthenticationOk'); assert.equal(result.name, 'AuthenticationOk');
assert.equal(result.id, 'R'); assert.equal(result.id, 'R');
assert.equal(result.length, 8); assert.equal(result.length, 8);
}); });
test('parses ParameterStatus message', function() { test('parses ParameterStatus message', function() {
var firstString = [0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0]; var result = new Parser(parameterStatusBuffer).parse();
var secondString = [0x55, 0x54, 0x46, 0x38, 0];
var bytes = [0x53, 0, 0, 0, 0x19].concat(firstString).concat(secondString);
var buffer = Buffer(bytes);
var result = new Parser(buffer).parse();
assert.equal(result.name, 'ParameterStatus'); assert.equal(result.name, 'ParameterStatus');
assert.equal(result.id, 'S'); assert.equal(result.id, 'S');
assert.equal(result.length, 25); assert.equal(result.length, 25);
@ -21,6 +27,23 @@ test('Parser on single messages', function() {
assert.equal(result.parameterValue, "UTF8"); assert.equal(result.parameterValue, "UTF8");
}); });
test('parses BackendKeyData message', function() {
var result = new Parser(backendKeyDataBuffer).parse();
assert.equal(result.name, 'BackendKeyData');
assert.equal(result.id, 'K');
assert.equal(result.length, 12);
assert.equal(result.processID, 1);
assert.equal(result.secretKey, 2);
});
test('parses ReadyForQuery message', function() {
var result = new Parser(readyForQueryBuffer).parse();
assert.equal(result.name, 'ReadyForQuery');
assert.equal(result.id, 'Z');
assert.equal(result.length, 5);
assert.equal(result.status, 'I');
});
test('parses normal CString', function() { test('parses normal CString', function() {
var result = new Parser(Buffer([33,0])).parseCString(); var result = new Parser(Buffer([33,0])).parseCString();
assert.equal(result,"!"); assert.equal(result,"!");