@@ -17,7 +17,7 @@ var Connection = function(config) {
1717 this . encoding = 'utf8' ;
1818 this . parsedStatements = { } ;
1919 this . writer = new Writer ( ) ;
20- this . checkSslResponse = false ;
20+ this . ssl = config . ssl || false ;
2121} ;
2222
2323util . inherits ( Connection , EventEmitter ) ;
@@ -26,10 +26,9 @@ var p = Connection.prototype;
2626
2727p . connect = function ( port , host ) {
2828
29- if ( this . stream . readyState === 'closed' ) {
29+ if ( this . stream . readyState === 'closed' ) {
3030 this . stream . connect ( port , host ) ;
31- }
32- else if ( this . stream . readyState == 'open' ) {
31+ } else if ( this . stream . readyState == 'open' ) {
3332 this . emit ( 'connect' ) ;
3433 }
3534
@@ -39,46 +38,53 @@ p.connect = function(port, host) {
3938 self . emit ( 'connect' ) ;
4039 } ) ;
4140
42- this . on ( 'sslresponse' , function ( msg ) {
43- if ( msg . text == 0x53 ) {
44- var tls = require ( 'tls' ) ;
45- self . stream . removeAllListeners ( ) ;
46- self . stream = tls . connect ( { socket : self . stream , servername : host , rejectUnauthorized : true } ) ;
47- self . stream . on ( 'data' , function ( buffer ) {
48- self . setBuffer ( buffer ) ;
49- var msg ;
50- while ( msg = self . parseMessage ( ) ) {
51- self . emit ( 'message' , msg ) ;
52- self . emit ( msg . name , msg ) ;
53- }
54- } ) ;
55- self . stream . on ( 'error' , function ( error ) {
56- self . emit ( 'error' , error ) ;
57- } ) ;
58- self . emit ( 'sslconnect' ) ;
59- } else {
60- throw new Error ( "The server doesn't support SSL/TLS connections." ) ;
61- }
41+ this . stream . on ( 'error' , function ( error ) {
42+ self . emit ( 'error' , error ) ;
6243 } ) ;
6344
64- this . stream . on ( 'data' , function ( buffer ) {
65- self . setBuffer ( buffer ) ;
66- var msg ;
67- if ( self . checkSslResponse ) {
68- while ( msg = self . readSslResponse ( ) ) {
69- self . emit ( 'message' , msg ) ;
70- self . emit ( msg . name , msg ) ;
45+ if ( this . ssl ) {
46+ this . stream . once ( 'data' , function ( buffer ) {
47+ self . setBuffer ( buffer ) ;
48+ var msg = self . readSslResponse ( ) ;
49+ self . emit ( 'message' , msg ) ;
50+ self . emit ( msg . name , msg ) ;
51+ } ) ;
52+ this . once ( 'sslresponse' , function ( msg ) {
53+ if ( msg . text == 0x53 ) {
54+ var tls = require ( 'tls' ) ;
55+ self . stream . removeAllListeners ( ) ;
56+ self . stream = tls . connect ( {
57+ socket : self . stream ,
58+ servername : host ,
59+ rejectUnauthorized : ssl . rejectUnauthorized ,
60+ ca : ssl . ca ,
61+ pfx : ssl . pfx ,
62+ key : ssl . key ,
63+ passphrase : ssl . passphrase ,
64+ cert : ssl . cert ,
65+ NPNProtocols : ssl . NPNProtocols
66+ } ) ;
67+ self . attachListeners ( self . stream ) ;
68+ self . emit ( 'sslconnect' ) ;
69+ } else {
70+ self . emit ( 'error' , new Error ( "The server doesn't support SSL/TLS connections." ) ) ;
7171 }
72- } else {
73- while ( msg = self . parseMessage ( ) ) {
74- self . emit ( 'message' , msg ) ;
75- self . emit ( msg . name , msg ) ;
76- }
77- }
7872 } ) ;
7973
80- this . stream . on ( 'error' , function ( error ) {
81- self . emit ( 'error' , error ) ;
74+ } else {
75+ this . attachListeners ( this . stream ) ;
76+ }
77+ } ;
78+
79+ p . attachListeners = function ( stream ) {
80+ var self = this ;
81+ stream . on ( 'data' , function ( buffer ) {
82+ self . setBuffer ( buffer ) ;
83+ var msg ;
84+ while ( msg = self . parseMessage ( ) ) {
85+ self . emit ( 'message' , msg ) ;
86+ self . emit ( msg . name , msg ) ;
87+ }
8288 } ) ;
8389} ;
8490
0 commit comments