@@ -86,6 +86,8 @@ class Client extends EventEmitter {
8686 _connect ( callback ) {
8787 var self = this
8888 var con = this . connection
89+ this . _connectionCallback = callback
90+
8991 if ( this . _connecting || this . _connected ) {
9092 const err = new Error ( 'Client has already been connected. You cannot reuse a client.' )
9193 process . nextTick ( ( ) => {
@@ -122,50 +124,7 @@ class Client extends EventEmitter {
122124 con . startup ( self . getStartupConf ( ) )
123125 } )
124126
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 )
169128
170129 con . once ( 'end' , ( ) => {
171130 const error = this . _ending ? new Error ( 'Connection terminated' ) : new Error ( 'Connection terminated unexpectedly' )
@@ -182,10 +141,10 @@ class Client extends EventEmitter {
182141 if ( this . _connectionCallback ) {
183142 this . _connectionCallback ( error )
184143 } else {
185- connectedErrorHandler ( error )
144+ this . handleErrorWhileConnected ( error )
186145 }
187146 } else if ( ! this . _connectionError ) {
188- connectedErrorHandler ( error )
147+ this . handleErrorWhileConnected ( error )
189148 }
190149 }
191150
@@ -213,6 +172,19 @@ class Client extends EventEmitter {
213172 }
214173
215174 _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 ) )
216188 con . on ( 'rowDescription' , this . handleRowDescription . bind ( this ) )
217189 con . on ( 'dataRow' , this . handleDataRow . bind ( this ) )
218190 con . on ( 'portalSuspended' , this . handlePortalSuspended . bind ( this ) )
@@ -283,7 +255,7 @@ class Client extends EventEmitter {
283255
284256 handleAuthenticationSASLContinue ( msg ) {
285257 const { saslSession } = this
286- sasl . continueSession ( saslSession , self . password , msg . data )
258+ sasl . continueSession ( saslSession , this . password , msg . data )
287259 con . sendSCRAMClientFinalMessage ( saslSession . response )
288260 }
289261
@@ -298,6 +270,23 @@ class Client extends EventEmitter {
298270 }
299271
300272 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+ }
301290 const { activeQuery } = this
302291 this . activeQuery = null
303292 this . readyForQuery = true
@@ -307,8 +296,8 @@ class Client extends EventEmitter {
307296 this . _pulseQueryQueue ( )
308297 }
309298
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 ) => {
312301 if ( this . _connectionError ) {
313302 // TODO(bmc): this is swallowing errors - we shouldn't do this
314303 return
@@ -324,14 +313,17 @@ class Client extends EventEmitter {
324313 // if we're connected and we receive an error event from the connection
325314 // this means the socket is dead - do a hard abort of all queries and emit
326315 // the socket error on the client as well
327- handleErrorWhileConnected ( err ) {
316+ handleErrorWhileConnected = ( err ) => {
328317 this . _queryable = false
329318 this . _errorAllQueries ( err )
330319 this . emit ( 'error' , err )
331320 }
332321
333322 // handle error messages from the postgres backend
334- handleErrorMessage ( msg ) {
323+ handleErrorMessage = ( msg ) => {
324+ if ( this . _connecting ) {
325+ return this . handleErrorWhileConnecting ( msg )
326+ }
335327 const activeQuery = this . activeQuery
336328
337329 if ( ! activeQuery ) {
0 commit comments