@@ -35,6 +35,16 @@ class Connection extends EventEmitter {
35
35
} )
36
36
}
37
37
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
+
38
48
connect ( port , host ) {
39
49
const self = this
40
50
@@ -49,14 +59,7 @@ class Connection extends EventEmitter {
49
59
self . emit ( 'connect' )
50
60
} )
51
61
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 ) )
60
63
61
64
this . stream . on ( 'close' , function ( ) {
62
65
self . emit ( 'end' )
@@ -65,46 +68,6 @@ class Connection extends EventEmitter {
65
68
if ( ! this . ssl ) {
66
69
return this . attachListeners ( this . stream )
67
70
}
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
- } )
108
71
}
109
72
110
73
attachListeners ( stream ) {
@@ -117,8 +80,64 @@ class Connection extends EventEmitter {
117
80
} )
118
81
}
119
82
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
+
120
115
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
+ }
122
141
}
123
142
124
143
startup ( config ) {
0 commit comments