@@ -17,7 +17,7 @@ var Connection = function(config) {
17
17
this . encoding = 'utf8' ;
18
18
this . parsedStatements = { } ;
19
19
this . writer = new Writer ( ) ;
20
- this . checkSslResponse = false ;
20
+ this . ssl = config . ssl || false ;
21
21
} ;
22
22
23
23
util . inherits ( Connection , EventEmitter ) ;
@@ -26,10 +26,9 @@ var p = Connection.prototype;
26
26
27
27
p . connect = function ( port , host ) {
28
28
29
- if ( this . stream . readyState === 'closed' ) {
29
+ if ( this . stream . readyState === 'closed' ) {
30
30
this . stream . connect ( port , host ) ;
31
- }
32
- else if ( this . stream . readyState == 'open' ) {
31
+ } else if ( this . stream . readyState == 'open' ) {
33
32
this . emit ( 'connect' ) ;
34
33
}
35
34
@@ -39,46 +38,53 @@ p.connect = function(port, host) {
39
38
self . emit ( 'connect' ) ;
40
39
} ) ;
41
40
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 ) ;
62
43
} ) ;
63
44
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." ) ) ;
71
71
}
72
- } else {
73
- while ( msg = self . parseMessage ( ) ) {
74
- self . emit ( 'message' , msg ) ;
75
- self . emit ( msg . name , msg ) ;
76
- }
77
- }
78
72
} ) ;
79
73
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
+ }
82
88
} ) ;
83
89
} ;
84
90
0 commit comments