diff --git a/lib/connection.js b/lib/connection.js index 5ea1ce239..9a2b89836 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -24,6 +24,24 @@ util.inherits(Connection, EventEmitter); var p = Connection.prototype; +p.mapMsgNames = { + 0x52: 'authenticationOk', + 0x53: 'parameterStatus', + 0x4b: 'backendKeyData', + 0x43: 'commandComplete', + 0x5a: 'readyForQuery', + 0x54: 'rowDescription', + 0x44: 'dataRow', + 0x45: 'error', + 0x4e: 'notice', + 0x31: 'parseComplete', + 0x32: 'bindComplete', + 0x41: 'notification', + 0x6e: 'noData', + 0x49: 'emptyQuery', + 0x73: 'portalSuspended' +}; + p.connect = function(port, host) { if(this.stream.readyState === 'closed'){ @@ -281,97 +299,36 @@ p.readSslResponse = function() { }; p.parseMessage = function() { - var remaining = this.buffer.length - (this.offset); - if(remaining < 5) { - //cannot read id + length without at least 5 bytes - //just abort the read now - this.lastBuffer = this.buffer; - this.lastOffset = this.offset; - return false; - } - - //read message id code - var id = this.buffer[this.offset++]; - //read message length - var length = this.parseInt32(); - - if(remaining <= length) { - this.lastBuffer = this.buffer; - //rewind the last 5 bytes we read - this.lastOffset = this.offset-5; - return false; - } - - var msg = { - length: length - }; - - switch(id) - { - - case 0x52: //R - msg.name = 'authenticationOk'; - return this.parseR(msg); - - case 0x53: //S - msg.name = 'parameterStatus'; - return this.parseS(msg); - - case 0x4b: //K - msg.name = 'backendKeyData'; - return this.parseK(msg); - - case 0x43: //C - msg.name = 'commandComplete'; - return this.parseC(msg); - - case 0x5a: //Z - msg.name = 'readyForQuery'; - return this.parseZ(msg); - - case 0x54: //T - msg.name = 'rowDescription'; - return this.parseT(msg); - - case 0x44: //D - msg.name = 'dataRow'; - return this.parseD(msg); - - case 0x45: //E - msg.name = 'error'; - return this.parseE(msg); - - case 0x4e: //N - msg.name = 'notice'; - return this.parseN(msg); + var remaining = this.buffer.length - this.offset; + if(remaining >= 5) { + //read message id code + var id = this.buffer[this.offset++]; + //read message length + var length = this.parseInt32(); - case 0x31: //1 - msg.name = 'parseComplete'; - return msg; + if(remaining > length) { + if ( id in this.mapMsgNames ) { + var msg = { + length: length, + name: this.mapMsgNames[id] + }; - case 0x32: //2 - msg.name = 'bindComplete'; - return msg; - - case 0x41: //A - msg.name = 'notification'; - return this.parseA(msg); + var fnc = this["parse" + String.fromCharCode( id )]; + return fnc instanceof Function ? fnc.call( this, msg ) : msg; + } - case 0x6e: //n - msg.name = 'noData'; - return msg; + throw new Error("Unrecognized message code " + id); + } - case 0x49: //I - msg.name = 'emptyQuery'; - return msg; + this.offset -= 5; + } - case 0x73: //s - msg.name = 'portalSuspended'; - return msg; + //cannot read full response + //just abort the read now + this.lastBuffer = this.buffer; + this.lastOffset = this.offset; - default: - throw new Error("Unrecognized message code " + id); - } + return false; }; p.parseR = function(msg) { diff --git a/lib/utils.js b/lib/utils.js index 07a379289..111ae4675 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -75,15 +75,19 @@ var getLibpgConString = function(config, callback) { params.push("dbname='" + config.database + "'"); } if(config.host) { - if(config.host != 'localhost' && config.host != '127.0.0.1') { - //do dns lookup - return require('dns').lookup(config.host, 4, function(err, address) { - if(err) return callback(err, null); - params.push("hostaddr="+address) - callback(null, params.join(" ")) - }) + if (!config.host.indexOf("/")) { + params.push("host=" + config.host); + } else { + if(config.host != 'localhost' && config.host != '127.0.0.1') { + //do dns lookup + return require('dns').lookup(config.host, 4, function(err, address) { + if(err) return callback(err, null); + params.push("hostaddr="+address) + callback(null, params.join(" ")) + }) + } + params.push("hostaddr=127.0.0.1 "); } - params.push("hostaddr=127.0.0.1 "); } callback(null, params.join(" ")); } else {