@@ -35,6 +35,16 @@ class Connection extends EventEmitter {
3535 } )
3636 }
3737
38+ _reportStreamError ( error ) {
39+ const self = this
40+
41+ // errors about disconnections should be ignored during disconnect
42+ if ( self . _ending && ( error . code === 'ECONNRESET' || error . code === 'EPIPE' ) ) {
43+ return
44+ }
45+ self . emit ( 'error' , error )
46+ }
47+
3848 connect ( port , host ) {
3949 const self = this
4050
@@ -49,14 +59,7 @@ class Connection extends EventEmitter {
4959 self . emit ( 'connect' )
5060 } )
5161
52- const reportStreamError = function ( error ) {
53- // errors about disconnections should be ignored during disconnect
54- if ( self . _ending && ( error . code === 'ECONNRESET' || error . code === 'EPIPE' ) ) {
55- return
56- }
57- self . emit ( 'error' , error )
58- }
59- this . stream . on ( 'error' , reportStreamError )
62+ this . stream . on ( 'error' , self . _reportStreamError . bind ( self ) )
6063
6164 this . stream . on ( 'close' , function ( ) {
6265 self . emit ( 'end' )
@@ -65,46 +68,6 @@ class Connection extends EventEmitter {
6568 if ( ! this . ssl ) {
6669 return this . attachListeners ( this . stream )
6770 }
68-
69- this . stream . once ( 'data' , function ( buffer ) {
70- const responseCode = buffer . toString ( 'utf8' )
71- switch ( responseCode ) {
72- case 'S' : // Server supports SSL connections, continue with a secure connection
73- break
74- case 'N' : // Server does not support SSL connections
75- self . stream . end ( )
76- return self . emit ( 'error' , new Error ( 'The server does not support SSL connections' ) )
77- default :
78- // Any other response byte, including 'E' (ErrorResponse) indicating a server error
79- self . stream . end ( )
80- return self . emit ( 'error' , new Error ( 'There was an error establishing an SSL connection' ) )
81- }
82- const options = {
83- socket : self . stream ,
84- }
85-
86- if ( self . ssl !== true ) {
87- Object . assign ( options , self . ssl )
88-
89- if ( 'key' in self . ssl ) {
90- options . key = self . ssl . key
91- }
92- }
93-
94- const net = require ( 'net' )
95- if ( net . isIP && net . isIP ( host ) === 0 ) {
96- options . servername = host
97- }
98- try {
99- self . stream = getSecureStream ( options )
100- } catch ( err ) {
101- return self . emit ( 'error' , err )
102- }
103- self . attachListeners ( self . stream )
104- self . stream . on ( 'error' , reportStreamError )
105-
106- self . emit ( 'sslconnect' )
107- } )
10871 }
10972
11073 attachListeners ( stream ) {
@@ -117,8 +80,64 @@ class Connection extends EventEmitter {
11780 } )
11881 }
11982
83+ _setUpSslConnection ( ) {
84+ const self = this
85+
86+ const options = {
87+ socket : self . stream ,
88+ ALPNProtocols : [ 'postgresql' ] ,
89+ }
90+
91+ if ( self . ssl !== true ) {
92+ Object . assign ( options , self . ssl )
93+
94+ if ( 'key' in self . ssl ) {
95+ options . key = self . ssl . key
96+ }
97+ }
98+
99+ const net = require ( 'net' )
100+ const host = this . stream . _host
101+ if ( host && net . isIP && net . isIP ( host ) === 0 ) {
102+ options . servername = host
103+ }
104+ try {
105+ self . stream = getSecureStream ( options )
106+ } catch ( err ) {
107+ return self . emit ( 'error' , err )
108+ }
109+ self . attachListeners ( self . stream )
110+ this . stream . on ( 'error' , self . _reportStreamError . bind ( self ) )
111+
112+ self . emit ( 'sslconnect' )
113+ }
114+
120115 requestSsl ( ) {
121- this . stream . write ( serialize . requestSsl ( ) )
116+ const self = this
117+
118+ const direct = false
119+
120+ if ( direct ) {
121+ self . _setUpSslConnection ( )
122+ } else {
123+ this . stream . once ( 'data' , function ( buffer ) {
124+ const responseCode = buffer . toString ( 'utf8' )
125+ switch ( responseCode ) {
126+ case 'S' : // Server supports SSL connections, continue with a secure connection
127+ break
128+ case 'N' : // Server does not support SSL connections
129+ self . stream . end ( )
130+ return self . emit ( 'error' , new Error ( 'The server does not support SSL connections' ) )
131+ default :
132+ // Any other response byte, including 'E' (ErrorResponse) indicating a server error
133+ self . stream . end ( )
134+ return self . emit ( 'error' , new Error ( 'There was an error establishing an SSL connection' ) )
135+ }
136+
137+ self . _setUpSslConnection ( )
138+ } )
139+ this . stream . write ( serialize . requestSsl ( ) )
140+ }
122141 }
123142
124143 startup ( config ) {
0 commit comments