From 2454d6b355991f217c369afcb899ce363a05566d Mon Sep 17 00:00:00 2001 From: brianc Date: Sun, 31 Oct 2010 17:58:32 -0500 Subject: [PATCH] a bit of code cleanup --- lib/connection.js | 95 +++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 56 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index f03c62b..c73f487 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -199,84 +199,84 @@ p.parseMessage = function() { return false; } - var messageType = this.buffer[this.offset++]; + //read message id code + var id = this.buffer[this.offset++]; + //read message length + var length = this.parseInt32(); - var message = { - length: this.parseInt32() - }; - - if(remaining <= message.length) { + if(remaining <= length) { this.lastBuffer = this.buffer; //rewind the last 5 bytes we read this.lastOffset = this.offset-5; return false; } - switch(messageType) + var msg = { + length: length + }; + + switch(id) { case 0x52: //R - message.name = 'authenticationOk'; - return this.parseR(message); + msg.name = 'authenticationOk'; + return this.parseR(msg); case 0x53: //S - message.name = 'parameterStatus'; - return this.parseS(message); + msg.name = 'parameterStatus'; + return this.parseS(msg); case 0x4b: //K - message.name = 'backendKeyData'; - return this.parseK(message); + msg.name = 'backendKeyData'; + return this.parseK(msg); case 0x43: //C - message.name = 'commandComplete'; - return this.parseC(message); + msg.name = 'commandComplete'; + return this.parseC(msg); case 0x5a: //Z - message.name = 'readyForQuery'; - return this.parseZ(message); + msg.name = 'readyForQuery'; + return this.parseZ(msg); case 0x54: //T - message.name = 'rowDescription'; - return this.parseT(message); + msg.name = 'rowDescription'; + return this.parseT(msg); case 0x44: //D - message.name = 'dataRow'; - return this.parseD(message); + msg.name = 'dataRow'; + return this.parseD(msg); case 0x45: //E - message.name = 'error'; - return this.parseE(message); + msg.name = 'error'; + return this.parseE(msg); case 0x4e: //N - message.name = 'notice'; - return this.parseN(message); + msg.name = 'notice'; + return this.parseN(msg); case 0x31: //1 - message.name = 'parseComplete'; - return this.parse1(message); + msg.name = 'parseComplete'; + return msg; case 0x32: //2 - message.name = 'bindComplete'; - return this.parse2(message); + msg.name = 'bindComplete'; + return msg; case 0x41: //A - message.name = 'notification'; - return this.parseA(message); + msg.name = 'notification'; + return this.parseA(msg); case 0x6e: //n - message.name = 'noData'; - return this.parsen(message); + msg.name = 'noData'; + return msg; case 0x49: //I - message.name = 'emptyQuery'; - return this.parseI(message); + msg.name = 'emptyQuery'; + return msg; default: - throw new Error("Unrecognized message code " + messageType); + throw new Error("Unrecognized message code " + id); } - - - }; p.parseR = function(msg) { @@ -384,19 +384,6 @@ p.parseE = function(msg) { //same thing, different name p.parseN = p.parseE; -//some messages are only a header portion and -//require no more parsing -var noParse = function(msg) { return msg; }; - -//parses parseComplete -p.parse1 = noParse; - -//parses bindComplete -p.parse2 = noParse; - -//parse emptyQuery -p.parseI = noParse; - p.parseA = function(msg) { msg.processId = this.parseInt32(); msg.channel = this.parseCString(); @@ -404,10 +391,6 @@ p.parseA = function(msg) { return msg; }; -p.parsen = function(msg) { - return msg; -}; - p.readChar = function() { return Buffer([this.buffer[this.offset++]]).toString(this.encoding); };