@@ -53,6 +53,7 @@ const errorFields = {
5353
5454function Connection ( options , queues = { } , { onopen = noop , onend = noop , onclose = noop } = { } ) {
5555 const {
56+ sslnegotiation,
5657 ssl,
5758 max,
5859 user,
@@ -79,14 +80,15 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
7980
8081 let socket = null
8182 , cancelMessage
83+ , errorResponse = null
8284 , result = new Result ( )
8385 , incoming = Buffer . alloc ( 0 )
8486 , needsTypes = options . fetch_types
8587 , backendParameters = { }
8688 , statements = { }
8789 , statementId = Math . random ( ) . toString ( 36 ) . slice ( 2 )
8890 , statementCount = 1
89- , closedDate = 0
91+ , closedTime = 0
9092 , remaining = 0
9193 , hostIndex = 0
9294 , retries = 0
@@ -157,6 +159,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
157159 if ( terminated )
158160 return queryError ( q , Errors . connection ( 'CONNECTION_DESTROYED' , options ) )
159161
162+ if ( stream )
163+ return queryError ( q , Errors . generic ( 'COPY_IN_PROGRESS' , 'You cannot execute queries during copy' ) )
164+
160165 if ( q . cancelled )
161166 return
162167
@@ -261,25 +266,29 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
261266 }
262267
263268 async function secure ( ) {
264- write ( SSLRequest )
265- const canSSL = await new Promise ( r => socket . once ( 'data' , x => r ( x [ 0 ] === 83 ) ) ) // S
269+ if ( sslnegotiation !== 'direct' ) {
270+ write ( SSLRequest )
271+ const canSSL = await new Promise ( r => socket . once ( 'data' , x => r ( x [ 0 ] === 83 ) ) ) // S
266272
267- if ( ! canSSL && ssl === 'prefer' )
268- return connected ( )
273+ if ( ! canSSL && ssl === 'prefer' )
274+ return connected ( )
275+ }
269276
270- socket . removeAllListeners ( )
271- socket = tls . connect ( {
277+ const options = {
272278 socket,
273- servername : net . isIP ( socket . host ) ? undefined : socket . host ,
274- ...( ssl === 'require' || ssl === 'allow' || ssl === 'prefer'
275- ? { rejectUnauthorized : false }
276- : ssl === 'verify-full'
277- ? { }
278- : typeof ssl === 'object'
279- ? ssl
280- : { }
281- )
282- } )
279+ servername : net . isIP ( socket . host ) ? undefined : socket . host
280+ }
281+
282+ if ( sslnegotiation === 'direct' )
283+ options . ALPNProtocols = [ 'postgresql' ]
284+
285+ if ( ssl === 'require' || ssl === 'allow' || ssl === 'prefer' )
286+ options . rejectUnauthorized = false
287+ else if ( typeof ssl === 'object' )
288+ Object . assign ( options , ssl )
289+
290+ socket . removeAllListeners ( )
291+ socket = tls . connect ( options )
283292 socket . on ( 'secureConnect' , connected )
284293 socket . on ( 'error' , error )
285294 socket . on ( 'close' , closed )
@@ -352,7 +361,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
352361 }
353362
354363 function reconnect ( ) {
355- setTimeout ( connect , closedDate ? closedDate + delay - performance . now ( ) : 0 )
364+ setTimeout ( connect , closedTime ? Math . max ( 0 , closedTime + delay - performance . now ( ) ) : 0 )
356365 }
357366
358367 function connected ( ) {
@@ -444,7 +453,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
444453 return reconnect ( )
445454
446455 ! hadError && ( query || sent . length ) && error ( Errors . connection ( 'CONNECTION_CLOSED' , options , socket ) )
447- closedDate = performance . now ( )
456+ closedTime = performance . now ( )
448457 hadError && options . shared . retries ++
449458 delay = ( typeof backoff === 'function' ? backoff ( options . shared . retries ) : backoff ) * 1000
450459 onclose ( connection , Errors . connection ( 'CONNECTION_CLOSED' , options , socket ) )
@@ -526,8 +535,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
526535 }
527536
528537 function ReadyForQuery ( x ) {
529- query && query . options . simple && query . resolve ( results || result )
530- query = results = null
538+ if ( query ) {
539+ if ( errorResponse ) {
540+ query . retried
541+ ? errored ( query . retried )
542+ : query . prepared && retryRoutines . has ( errorResponse . routine )
543+ ? retry ( query , errorResponse )
544+ : errored ( errorResponse )
545+ } else {
546+ query . resolve ( results || result )
547+ }
548+ } else if ( errorResponse ) {
549+ errored ( errorResponse )
550+ }
551+
552+ query = results = errorResponse = null
531553 result = new Result ( )
532554 connectTimer . cancel ( )
533555
@@ -592,8 +614,6 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
592614 result . count && query . cursorFn ( result )
593615 write ( Sync )
594616 }
595-
596- query . resolve ( result )
597617 }
598618
599619 function ParseComplete ( ) {
@@ -792,13 +812,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
792812 }
793813
794814 function ErrorResponse ( x ) {
795- query && ( query . cursorFn || query . describeFirst ) && write ( Sync )
796- const error = Errors . postgres ( parseError ( x ) )
797- query && query . retried
798- ? errored ( query . retried )
799- : query && query . prepared && retryRoutines . has ( error . routine )
800- ? retry ( query , error )
801- : errored ( error )
815+ if ( query ) {
816+ ( query . cursorFn || query . describeFirst ) && write ( Sync )
817+ errorResponse = Errors . postgres ( parseError ( x ) )
818+ } else {
819+ errored ( Errors . postgres ( parseError ( x ) ) )
820+ }
802821 }
803822
804823 function retry ( q , error ) {
@@ -851,6 +870,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
851870 final ( callback ) {
852871 socket . write ( b ( ) . c ( ) . end ( ) )
853872 final = callback
873+ stream = null
854874 }
855875 } )
856876 query . resolve ( stream )
0 commit comments