From 8c30521cf94285a733f7cec2ca059677c4412caa Mon Sep 17 00:00:00 2001 From: brianc Date: Thu, 30 Sep 2010 00:27:56 -0500 Subject: [PATCH] refactored parsing --- lib/index.js | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/lib/index.js b/lib/index.js index 02ac12f..c2b9fb3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -95,43 +95,36 @@ p.parseR = function() { }; p.parseS = function(buffer) { - 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', - length: length, - parameterName: parameterName, - parameterValue: parameterValue - } + var msg = this.parseStart('ParameterStatus'); + msg.parameterName = this.parseCString(); + msg.parameterValue = this.parseCString(); + return msg; }; p.parseK = function() { - var type = this.buffer[this.offset++]; - var length = this.readInt32(); - var processID = this.readInt32(); - var secretKey = this.readInt32(); + var msg = this.parseStart('BackendKeyData'); + msg.processID = this.readInt32(); + msg.secretKey = this.readInt32(); + return msg; +}; + +//parses common start of message packets +p.parseStart = function(name) { return { - name: 'BackendKeyData', - id: 'K', - length: length, - processID: processID, - secretKey: secretKey + name: name, + id: this.readChar(), + length: this.readInt32() } }; +p.readChar = function() { + return Buffer([this.buffer[this.offset++]]).toString('utf8'); +}; + 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') - } + var msg = this.parseStart('ReadyForQuery'); + msg.status = this.readChar(); + return msg; }; p.readInt32 = function() {