From 6e0922a7cb201a1ba05aad04750f35edc70c292a Mon Sep 17 00:00:00 2001 From: soletan Date: Fri, 24 Aug 2012 19:06:26 +0300 Subject: [PATCH 01/10] fixing support for Unix sockets in native binding Binding natively connections to Unix sockets failed due to DNS lookups applied on pathname to Unix socket's folder. --- lib/utils.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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 { From eaa2ba1dc1addaa19e7d136cb3c7304fbc4c8396 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 12:36:58 +0300 Subject: [PATCH 02/10] revising type detection of response messages --- lib/connection.js | 87 +++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 63 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 5ea1ce239..219e605ec 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -22,6 +22,24 @@ var Connection = function(config) { util.inherits(Connection, EventEmitter); +Connection.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' +}; + var p = Connection.prototype; p.connect = function(port, host) { @@ -306,72 +324,15 @@ p.parseMessage = function() { length: length }; - switch(id) + if ( id in Connection.mapMsgNames ) { + msg.name = Connection.mapMsgNames[id]; + var fncName = "parse" + String.fromCharCode( 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); - - case 0x31: //1 - msg.name = 'parseComplete'; - return msg; - - case 0x32: //2 - msg.name = 'bindComplete'; - return msg; - - case 0x41: //A - msg.name = 'notification'; - return this.parseA(msg); - - case 0x6e: //n - msg.name = 'noData'; - return msg; - - case 0x49: //I - msg.name = 'emptyQuery'; - return msg; - - case 0x73: //s - msg.name = 'portalSuspended'; - return msg; - - default: - throw new Error("Unrecognized message code " + id); + return fncName in this ? this[fncName]( msg ); } + else + throw new Error("Unrecognized message code " + id); }; p.parseR = function(msg) { From 663a9659e4ad7bcfa7d5462f7831a8c18cacfd64 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 12:38:37 +0300 Subject: [PATCH 03/10] fixing typo in recent revision --- lib/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index 219e605ec..e6a2fc5a7 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -329,7 +329,7 @@ p.parseMessage = function() { msg.name = Connection.mapMsgNames[id]; var fncName = "parse" + String.fromCharCode( id ); - return fncName in this ? this[fncName]( msg ); + return fncName in this ? this[fncName]( msg ) : msg; } else throw new Error("Unrecognized message code " + id); From 66a63add2e5db7b003dd8e60652c3e2cf681c449 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 12:40:37 +0300 Subject: [PATCH 04/10] Update lib/connection.js --- lib/connection.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index e6a2fc5a7..efb5e63d1 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -326,10 +326,10 @@ p.parseMessage = function() { if ( id in Connection.mapMsgNames ) { - msg.name = Connection.mapMsgNames[id]; - var fncName = "parse" + String.fromCharCode( id ); + msg.name = Connection.mapMsgNames[id]; + var fnc = this["parse" + String.fromCharCode( id )]; - return fncName in this ? this[fncName]( msg ) : msg; + return fnc instanceof Function ? fnc( msg ) : msg; } else throw new Error("Unrecognized message code " + id); From 6b95d754889c39fca964dc230f94c7c962f74761 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 12:44:49 +0300 Subject: [PATCH 05/10] Update lib/connection.js --- lib/connection.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index efb5e63d1..cf154cb66 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -324,15 +324,14 @@ p.parseMessage = function() { length: length }; - if ( id in Connection.mapMsgNames ) - { + if ( id in Connection.mapMsgNames ) { msg.name = Connection.mapMsgNames[id]; var fnc = this["parse" + String.fromCharCode( id )]; return fnc instanceof Function ? fnc( msg ) : msg; - } - else + } else { throw new Error("Unrecognized message code " + id); + } }; p.parseR = function(msg) { From 53f5036e28e9c3437a8f14c1f410bb59922f5977 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 12:46:19 +0300 Subject: [PATCH 06/10] Update lib/connection.js --- lib/connection.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index cf154cb66..268ed6fa1 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -22,7 +22,9 @@ var Connection = function(config) { util.inherits(Connection, EventEmitter); -Connection.mapMsgNames = { +var p = Connection.prototype; + +p.mapMsgNames = { 0x52: 'authenticationOk', 0x53: 'parameterStatus', 0x4b: 'backendKeyData', @@ -40,8 +42,6 @@ Connection.mapMsgNames = { 0x73: 'portalSuspended' }; -var p = Connection.prototype; - p.connect = function(port, host) { if(this.stream.readyState === 'closed'){ @@ -324,8 +324,8 @@ p.parseMessage = function() { length: length }; - if ( id in Connection.mapMsgNames ) { - msg.name = Connection.mapMsgNames[id]; + if ( id in this.mapMsgNames ) { + msg.name = this.mapMsgNames[id]; var fnc = this["parse" + String.fromCharCode( id )]; return fnc instanceof Function ? fnc( msg ) : msg; From c921287e864e9a0aa6af7e1ce9959293b541d406 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 13:01:25 +0300 Subject: [PATCH 07/10] Update lib/connection.js --- lib/connection.js | 52 +++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 268ed6fa1..300ede536 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -300,38 +300,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; - } + if(remaining >= 5) { + //read message id code + var id = this.buffer[this.offset++]; + //read message length + var length = this.parseInt32(); - //read message id code - var id = this.buffer[this.offset++]; - //read message length - var length = this.parseInt32(); + if(remaining >= length) { + var msg = { + length: length + }; - if(remaining <= length) { - this.lastBuffer = this.buffer; - //rewind the last 5 bytes we read - this.lastOffset = this.offset-5; - return false; + if ( id in this.mapMsgNames ) { + msg.name = this.mapMsgNames[id]; + var fnc = this["parse" + String.fromCharCode( id )]; + + return fnc instanceof Function ? fnc( msg ) : msg; + } else { + throw new Error("Unrecognized message code " + id); + } + } else { + this.offset -= 5; + } } - var msg = { - length: length - }; + //cannot read full response + //just abort the read now + this.lastBuffer = this.buffer; + this.lastOffset = this.offset; - if ( id in this.mapMsgNames ) { - msg.name = this.mapMsgNames[id]; - var fnc = this["parse" + String.fromCharCode( id )]; - - return fnc instanceof Function ? fnc( msg ) : msg; - } else { - throw new Error("Unrecognized message code " + id); - } + return false; }; p.parseR = function(msg) { From 216dc41b8308d95548945e3cd97251b40654c067 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 13:20:31 +0300 Subject: [PATCH 08/10] Update lib/connection.js --- lib/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index 300ede536..ea466800d 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -306,7 +306,7 @@ p.parseMessage = function() { //read message length var length = this.parseInt32(); - if(remaining >= length) { + if(remaining > length) { var msg = { length: length }; From 62132ac43bed8eda02136a20d80ff47c0c407030 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 13:27:34 +0300 Subject: [PATCH 09/10] Update lib/connection.js --- lib/connection.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index ea466800d..1481ed391 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -299,7 +299,7 @@ p.readSslResponse = function() { }; p.parseMessage = function() { - var remaining = this.buffer.length - (this.offset); + var remaining = this.buffer.length - this.offset; if(remaining >= 5) { //read message id code var id = this.buffer[this.offset++]; @@ -307,21 +307,20 @@ p.parseMessage = function() { var length = this.parseInt32(); if(remaining > length) { - var msg = { - length: length - }; - if ( id in this.mapMsgNames ) { - msg.name = this.mapMsgNames[id]; - var fnc = this["parse" + String.fromCharCode( id )]; - + var msg = { + length: length, + name: this.mapMsgNames[id] + }; + + var fnc = this["parse" + String.fromCharCode( id )]; return fnc instanceof Function ? fnc( msg ) : msg; - } else { - throw new Error("Unrecognized message code " + id); } - } else { - this.offset -= 5; + + throw new Error("Unrecognized message code " + id); } + + this.offset -= 5; } //cannot read full response From 596f5180aa048004f580e62bcb2a093ca39c1f35 Mon Sep 17 00:00:00 2001 From: soletan Date: Sat, 25 Aug 2012 21:39:07 +0300 Subject: [PATCH 10/10] fixing proper binding of post processor fixing issue on "this" properly passed into parseX-methods processing response messages depending on message type --- lib/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index 1481ed391..9a2b89836 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -314,7 +314,7 @@ p.parseMessage = function() { }; var fnc = this["parse" + String.fromCharCode( id )]; - return fnc instanceof Function ? fnc( msg ) : msg; + return fnc instanceof Function ? fnc.call( this, msg ) : msg; } throw new Error("Unrecognized message code " + id);