@@ -387,13 +387,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
387387 }
388388
389389 function queryError ( query , err ) {
390- query . reject ( Object . create ( err , {
390+ Object . defineProperties ( err , {
391391 stack : { value : err . stack + query . origin . replace ( / .* \n / , '\n' ) , enumerable : options . debug } ,
392392 query : { value : query . string , enumerable : options . debug } ,
393393 parameters : { value : query . parameters , enumerable : options . debug } ,
394394 args : { value : query . args , enumerable : options . debug } ,
395395 types : { value : query . statement && query . statement . types , enumerable : options . debug }
396- } ) )
396+ } )
397+ query . reject ( err )
397398 }
398399
399400 function end ( ) {
@@ -442,7 +443,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
442443 closedDate = performance . now ( )
443444 hadError && options . shared . retries ++
444445 delay = ( typeof backoff === 'function' ? backoff ( options . shared . retries ) : backoff ) * 1000
445- onclose ( connection )
446+ onclose ( connection , Errors . connection ( 'CONNECTION_CLOSED' , options , socket ) )
446447 }
447448
448449 /* Handlers */
@@ -658,27 +659,30 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
658659
659660 /* c8 ignore next 5 */
660661 async function AuthenticationCleartextPassword ( ) {
662+ const payload = await Pass ( )
661663 write (
662- b ( ) . p ( ) . str ( await Pass ( ) ) . z ( 1 ) . end ( )
664+ b ( ) . p ( ) . str ( payload ) . z ( 1 ) . end ( )
663665 )
664666 }
665667
666668 async function AuthenticationMD5Password ( x ) {
667- write (
668- b ( ) . p ( ) . str (
669- 'md5' +
670- ( await md5 ( Buffer . concat ( [
669+ const payload = 'md5' + (
670+ await md5 (
671+ Buffer . concat ( [
671672 Buffer . from ( await md5 ( ( await Pass ( ) ) + user ) ) ,
672673 x . subarray ( 9 )
673- ] ) ) )
674- ) . z ( 1 ) . end ( )
674+ ] )
675+ )
676+ )
677+ write (
678+ b ( ) . p ( ) . str ( payload ) . z ( 1 ) . end ( )
675679 )
676680 }
677681
678682 async function SASL ( ) {
683+ nonce = ( await crypto . randomBytes ( 18 ) ) . toString ( 'base64' )
679684 b ( ) . p ( ) . str ( 'SCRAM-SHA-256' + b . N )
680685 const i = b . i
681- nonce = ( await crypto . randomBytes ( 18 ) ) . toString ( 'base64' )
682686 write ( b . inc ( 4 ) . str ( 'n,,n=*,r=' + nonce ) . i32 ( b . i - i - 4 , i ) . end ( ) )
683687 }
684688
@@ -700,12 +704,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
700704
701705 serverSignature = ( await hmac ( await hmac ( saltedPassword , 'Server Key' ) , auth ) ) . toString ( 'base64' )
702706
707+ const payload = 'c=biws,r=' + res . r + ',p=' + xor (
708+ clientKey , Buffer . from ( await hmac ( await sha256 ( clientKey ) , auth ) )
709+ ) . toString ( 'base64' )
710+
703711 write (
704- b ( ) . p ( ) . str (
705- 'c=biws,r=' + res . r + ',p=' + xor (
706- clientKey , Buffer . from ( await hmac ( await sha256 ( clientKey ) , auth ) )
707- ) . toString ( 'base64' )
708- ) . end ( )
712+ b ( ) . p ( ) . str ( payload ) . end ( )
709713 )
710714 }
711715
0 commit comments