@@ -86,6 +86,8 @@ class Client extends EventEmitter {
86
86
_connect ( callback ) {
87
87
var self = this
88
88
var con = this . connection
89
+ this . _connectionCallback = callback
90
+
89
91
if ( this . _connecting || this . _connected ) {
90
92
const err = new Error ( 'Client has already been connected. You cannot reuse a client.' )
91
93
process . nextTick ( ( ) => {
@@ -122,50 +124,7 @@ class Client extends EventEmitter {
122
124
con . startup ( self . getStartupConf ( ) )
123
125
} )
124
126
125
- // password request handling
126
- con . on ( 'authenticationCleartextPassword' , this . handleAuthenticationCleartextPassword . bind ( this ) )
127
- // password request handling
128
- con . on ( 'authenticationMD5Password' , this . handleAuthenticationMD5Password . bind ( this ) )
129
- // password request handling (SASL)
130
- con . on ( 'authenticationSASL' , this . handleAuthenticationSASL . bind ( this ) )
131
- con . on ( 'authenticationSASLContinue' , this . handleAuthenticationSASLContinue . bind ( this ) )
132
- con . on ( 'authenticationSASLFinal' , this . handleAuthenticationSASLFinal . bind ( this ) )
133
- con . once ( 'backendKeyData' , this . handleBackendKeyData . bind ( this ) )
134
-
135
- this . _connectionCallback = callback
136
- const connectingErrorHandler = this . handleErrorWhileConnecting . bind ( this )
137
-
138
- const connectedErrorHandler = this . handleErrorWhileConnected . bind ( this )
139
-
140
- const connectedErrorMessageHandler = this . handleErrorMessage . bind ( this )
141
-
142
- con . on ( 'error' , connectingErrorHandler )
143
- con . on ( 'errorMessage' , connectingErrorHandler )
144
-
145
- // hook up query handling events to connection
146
- // after the connection initially becomes ready for queries
147
- con . once ( 'readyForQuery' , ( ) => {
148
- self . _connecting = false
149
- self . _connected = true
150
- con . removeListener ( 'error' , connectingErrorHandler )
151
- con . removeListener ( 'errorMessage' , connectingErrorHandler )
152
- con . on ( 'error' , connectedErrorHandler )
153
- con . on ( 'errorMessage' , connectedErrorMessageHandler )
154
- clearTimeout ( this . connectionTimeoutHandle )
155
-
156
- // process possible callback argument to Client#connect
157
- if ( this . _connectionCallback ) {
158
- this . _connectionCallback ( null , self )
159
- // remove callback for proper error handling
160
- // after the connect event
161
- this . _connectionCallback = null
162
- }
163
- self . emit ( 'connect' )
164
- } )
165
-
166
- con . on ( 'readyForQuery' , this . handleReadyForQuery . bind ( this ) )
167
- con . on ( 'notice' , this . handleNotice . bind ( this ) )
168
- self . _attachListeners ( con )
127
+ this . _attachListeners ( con )
169
128
170
129
con . once ( 'end' , ( ) => {
171
130
const error = this . _ending ? new Error ( 'Connection terminated' ) : new Error ( 'Connection terminated unexpectedly' )
@@ -182,10 +141,10 @@ class Client extends EventEmitter {
182
141
if ( this . _connectionCallback ) {
183
142
this . _connectionCallback ( error )
184
143
} else {
185
- connectedErrorHandler ( error )
144
+ this . handleErrorWhileConnected ( error )
186
145
}
187
146
} else if ( ! this . _connectionError ) {
188
- connectedErrorHandler ( error )
147
+ this . handleErrorWhileConnected ( error )
189
148
}
190
149
}
191
150
@@ -213,6 +172,19 @@ class Client extends EventEmitter {
213
172
}
214
173
215
174
_attachListeners ( con ) {
175
+ // password request handling
176
+ con . on ( 'authenticationCleartextPassword' , this . handleAuthenticationCleartextPassword . bind ( this ) )
177
+ // password request handling
178
+ con . on ( 'authenticationMD5Password' , this . handleAuthenticationMD5Password . bind ( this ) )
179
+ // password request handling (SASL)
180
+ con . on ( 'authenticationSASL' , this . handleAuthenticationSASL . bind ( this ) )
181
+ con . on ( 'authenticationSASLContinue' , this . handleAuthenticationSASLContinue . bind ( this ) )
182
+ con . on ( 'authenticationSASLFinal' , this . handleAuthenticationSASLFinal . bind ( this ) )
183
+ con . on ( 'backendKeyData' , this . handleBackendKeyData . bind ( this ) )
184
+ con . on ( 'error' , this . handleErrorWhileConnecting )
185
+ con . on ( 'errorMessage' , this . handleErrorMessage )
186
+ con . on ( 'readyForQuery' , this . handleReadyForQuery . bind ( this ) )
187
+ con . on ( 'notice' , this . handleNotice . bind ( this ) )
216
188
con . on ( 'rowDescription' , this . handleRowDescription . bind ( this ) )
217
189
con . on ( 'dataRow' , this . handleDataRow . bind ( this ) )
218
190
con . on ( 'portalSuspended' , this . handlePortalSuspended . bind ( this ) )
@@ -283,7 +255,7 @@ class Client extends EventEmitter {
283
255
284
256
handleAuthenticationSASLContinue ( msg ) {
285
257
const { saslSession } = this
286
- sasl . continueSession ( saslSession , self . password , msg . data )
258
+ sasl . continueSession ( saslSession , this . password , msg . data )
287
259
con . sendSCRAMClientFinalMessage ( saslSession . response )
288
260
}
289
261
@@ -298,6 +270,23 @@ class Client extends EventEmitter {
298
270
}
299
271
300
272
handleReadyForQuery ( msg ) {
273
+ if ( this . _connecting ) {
274
+ this . _connecting = false
275
+ this . _connected = true
276
+ const con = this . connection
277
+ con . removeListener ( 'error' , this . handleErrorWhileConnecting )
278
+ con . on ( 'error' , this . handleErrorWhileConnected )
279
+ clearTimeout ( this . connectionTimeoutHandle )
280
+
281
+ // process possible callback argument to Client#connect
282
+ if ( this . _connectionCallback ) {
283
+ this . _connectionCallback ( null , this )
284
+ // remove callback for proper error handling
285
+ // after the connect event
286
+ this . _connectionCallback = null
287
+ }
288
+ this . emit ( 'connect' )
289
+ }
301
290
const { activeQuery } = this
302
291
this . activeQuery = null
303
292
this . readyForQuery = true
@@ -307,8 +296,8 @@ class Client extends EventEmitter {
307
296
this . _pulseQueryQueue ( )
308
297
}
309
298
310
- // if we receieve an error during the connection process we handle it here
311
- handleErrorWhileConnecting ( err ) {
299
+ // if we receieve an error event or error message during the connection process we handle it here
300
+ handleErrorWhileConnecting = ( err ) => {
312
301
if ( this . _connectionError ) {
313
302
// TODO(bmc): this is swallowing errors - we shouldn't do this
314
303
return
@@ -324,14 +313,17 @@ class Client extends EventEmitter {
324
313
// if we're connected and we receive an error event from the connection
325
314
// this means the socket is dead - do a hard abort of all queries and emit
326
315
// the socket error on the client as well
327
- handleErrorWhileConnected ( err ) {
316
+ handleErrorWhileConnected = ( err ) => {
328
317
this . _queryable = false
329
318
this . _errorAllQueries ( err )
330
319
this . emit ( 'error' , err )
331
320
}
332
321
333
322
// handle error messages from the postgres backend
334
- handleErrorMessage ( msg ) {
323
+ handleErrorMessage = ( msg ) => {
324
+ if ( this . _connecting ) {
325
+ return this . handleErrorWhileConnecting ( msg )
326
+ }
335
327
const activeQuery = this . activeQuery
336
328
337
329
if ( ! activeQuery ) {
0 commit comments