@@ -49,7 +49,7 @@ class BaseBuffer {
4949 this . buf = Buffer . alloc ( initializer ) ;
5050 this . size = 0 ;
5151 this . maxSize = initializer ;
52- } else {
52+ } else if ( initializer ) {
5353 this . buf = initializer ;
5454 this . size = this . maxSize = initializer . length ;
5555 }
@@ -548,11 +548,12 @@ class BaseBuffer {
548548 // remain in the buffer, the buffer is grown.
549549 //---------------------------------------------------------------------------
550550 reserveBytes ( numBytes ) {
551- if ( numBytes > this . numBytesLeft ( ) )
551+ if ( numBytes > this . numBytesLeft ( ) ) {
552552 this . _grow ( this . pos + numBytes ) ;
553- const buf = this . buf . subarray ( this . pos , this . pos + numBytes ) ;
553+ }
554+ const pos = this . pos ;
554555 this . pos += numBytes ;
555- return buf ;
556+ return pos ;
556557 }
557558
558559 //---------------------------------------------------------------------------
@@ -618,20 +619,20 @@ class BaseBuffer {
618619 //---------------------------------------------------------------------------
619620 writeBinaryDouble ( n ) {
620621 this . writeUInt8 ( 8 ) ;
621- const buf = this . reserveBytes ( 8 ) ;
622- buf . writeDoubleBE ( n ) ;
623- if ( ( buf [ 0 ] & 0x80 ) === 0 ) {
624- buf [ 0 ] |= 0x80 ;
622+ const pos = this . reserveBytes ( 8 ) ;
623+ this . buf . writeDoubleBE ( n , pos ) ;
624+ if ( ( this . buf [ pos ] & 0x80 ) === 0 ) {
625+ this . buf [ pos ] |= 0x80 ;
625626 } else {
626627 // We complement the bits for a negative number
627- buf [ 0 ] ^= 0xff ;
628- buf [ 1 ] ^= 0xff ;
629- buf [ 2 ] ^= 0xff ;
630- buf [ 3 ] ^= 0xff ;
631- buf [ 4 ] ^= 0xff ;
632- buf [ 5 ] ^= 0xff ;
633- buf [ 6 ] ^= 0xff ;
634- buf [ 7 ] ^= 0xff ;
628+ this . buf [ pos ] ^= 0xff ;
629+ this . buf [ pos + 1 ] ^= 0xff ;
630+ this . buf [ pos + 2 ] ^= 0xff ;
631+ this . buf [ pos + 3 ] ^= 0xff ;
632+ this . buf [ pos + 4 ] ^= 0xff ;
633+ this . buf [ pos + 5 ] ^= 0xff ;
634+ this . buf [ pos + 6 ] ^= 0xff ;
635+ this . buf [ pos + 7 ] ^= 0xff ;
635636 }
636637 }
637638
@@ -642,16 +643,16 @@ class BaseBuffer {
642643 //---------------------------------------------------------------------------
643644 writeBinaryFloat ( n ) {
644645 this . writeUInt8 ( 4 ) ;
645- const buf = this . reserveBytes ( 4 ) ;
646- buf . writeFloatBE ( n ) ;
647- if ( ( buf [ 0 ] & 0x80 ) === 0 ) {
648- buf [ 0 ] |= 0x80 ;
646+ const pos = this . reserveBytes ( 4 ) ;
647+ this . buf . writeFloatBE ( n , pos ) ;
648+ if ( ( this . buf [ pos ] & 0x80 ) === 0 ) {
649+ this . buf [ pos ] |= 0x80 ;
649650 } else {
650651 // We complement the bits for a negative number
651- buf [ 0 ] ^= 0xff ;
652- buf [ 1 ] ^= 0xff ;
653- buf [ 2 ] ^= 0xff ;
654- buf [ 3 ] ^= 0xff ;
652+ this . buf [ pos ] ^= 0xff ;
653+ this . buf [ pos + 1 ] ^= 0xff ;
654+ this . buf [ pos + 2 ] ^= 0xff ;
655+ this . buf [ pos + 3 ] ^= 0xff ;
655656 }
656657 }
657658
@@ -745,31 +746,31 @@ class BaseBuffer {
745746 if ( writeLength ) {
746747 this . writeUInt8 ( length ) ;
747748 }
748- const ptr = this . reserveBytes ( length ) ;
749+ const pos = this . reserveBytes ( length ) ;
749750 if ( type === types . DB_TYPE_DATE || type == types . DB_TYPE_TIMESTAMP ) {
750751 const year = date . getFullYear ( ) ;
751- ptr [ 0 ] = Math . trunc ( year / 100 ) + 100 ;
752- ptr [ 1 ] = year % 100 + 100 ;
753- ptr [ 2 ] = date . getMonth ( ) + 1 ;
754- ptr [ 3 ] = date . getDate ( ) ;
755- ptr [ 4 ] = date . getHours ( ) + 1 ;
756- ptr [ 5 ] = date . getMinutes ( ) + 1 ;
757- ptr [ 6 ] = date . getSeconds ( ) + 1 ;
752+ this . buf [ pos ] = Math . trunc ( year / 100 ) + 100 ;
753+ this . buf [ pos + 1 ] = year % 100 + 100 ;
754+ this . buf [ pos + 2 ] = date . getMonth ( ) + 1 ;
755+ this . buf [ pos + 3 ] = date . getDate ( ) ;
756+ this . buf [ pos + 4 ] = date . getHours ( ) + 1 ;
757+ this . buf [ pos + 5 ] = date . getMinutes ( ) + 1 ;
758+ this . buf [ pos + 6 ] = date . getSeconds ( ) + 1 ;
758759 } else {
759760 const year = date . getUTCFullYear ( ) ;
760- ptr [ 0 ] = Math . trunc ( year / 100 ) + 100 ;
761- ptr [ 1 ] = year % 100 + 100 ;
762- ptr [ 2 ] = date . getUTCMonth ( ) + 1 ;
763- ptr [ 3 ] = date . getUTCDate ( ) ;
764- ptr [ 4 ] = date . getUTCHours ( ) + 1 ;
765- ptr [ 5 ] = date . getUTCMinutes ( ) + 1 ;
766- ptr [ 6 ] = date . getUTCSeconds ( ) + 1 ;
761+ this . buf [ pos ] = Math . trunc ( year / 100 ) + 100 ;
762+ this . buf [ pos + 1 ] = year % 100 + 100 ;
763+ this . buf [ pos + 2 ] = date . getUTCMonth ( ) + 1 ;
764+ this . buf [ pos + 3 ] = date . getUTCDate ( ) ;
765+ this . buf [ pos + 4 ] = date . getUTCHours ( ) + 1 ;
766+ this . buf [ pos + 5 ] = date . getUTCMinutes ( ) + 1 ;
767+ this . buf [ pos + 6 ] = date . getUTCSeconds ( ) + 1 ;
767768 }
768769 if ( length > 7 ) {
769- ptr . writeInt32BE ( fsec , 7 ) ;
770+ this . buf . writeInt32BE ( fsec , pos + 7 ) ;
770771 if ( length > 11 ) {
771- ptr [ 11 ] = constants . TZ_HOUR_OFFSET ;
772- ptr [ 12 ] = constants . TZ_MINUTE_OFFSET ;
772+ this . buf [ pos + 11 ] = constants . TZ_HOUR_OFFSET ;
773+ this . buf [ pos + 12 ] = constants . TZ_MINUTE_OFFSET ;
773774 }
774775 }
775776 }
@@ -843,19 +844,19 @@ class BaseBuffer {
843844 } else if ( value . length === 0 && exponent === 0 ) {
844845 exponentOnWire = 128 ;
845846 }
846- const buf = this . reserveBytes ( numPairs + 2 + appendSentinel ) ;
847- buf [ 0 ] = numPairs + 1 + appendSentinel ;
848- buf [ 1 ] = exponentOnWire ;
849- for ( let i = 0 , pos = 2 ; i < value . length ; i += 2 , pos ++ ) {
847+ let pos = this . reserveBytes ( numPairs + 2 + appendSentinel ) ;
848+ this . buf [ pos ++ ] = numPairs + 1 + appendSentinel ;
849+ this . buf [ pos ++ ] = exponentOnWire ;
850+ for ( let i = 0 ; i < value . length ; i += 2 ) {
850851 const base100Digit = Number ( value . substring ( i , i + 2 ) ) ;
851852 if ( isNegative ) {
852- buf [ pos ] = 101 - base100Digit ;
853+ this . buf [ pos ++ ] = 101 - base100Digit ;
853854 } else {
854- buf [ pos ] = base100Digit + 1 ;
855+ this . buf [ pos ++ ] = base100Digit + 1 ;
855856 }
856857 }
857858 if ( appendSentinel ) {
858- buf [ buf . length - 1 ] = 102 ;
859+ this . buf [ pos ] = 102 ;
859860 }
860861
861862 }
@@ -898,8 +899,8 @@ class BaseBuffer {
898899 // Writes a signed 32-bit integer to the buffer in big endian order.
899900 //---------------------------------------------------------------------------
900901 writeInt32BE ( n ) {
901- const buf = this . reserveBytes ( 4 ) ;
902- buf . writeInt32BE ( n ) ;
902+ const pos = this . reserveBytes ( 4 ) ;
903+ this . buf . writeInt32BE ( n , pos ) ;
903904 }
904905
905906 //---------------------------------------------------------------------------
@@ -971,8 +972,8 @@ class BaseBuffer {
971972 // Writes an unsigned 8-bit integer to the buffer.
972973 //---------------------------------------------------------------------------
973974 writeUInt8 ( n ) {
974- const buf = this . reserveBytes ( 1 ) ;
975- buf [ 0 ] = n ;
975+ const pos = this . reserveBytes ( 1 ) ;
976+ this . buf [ pos ] = n ;
976977 }
977978
978979 //---------------------------------------------------------------------------
@@ -981,8 +982,8 @@ class BaseBuffer {
981982 // Writes an unsigned 16-bit integer to the buffer in big endian order.
982983 //---------------------------------------------------------------------------
983984 writeUInt16BE ( n ) {
984- const buf = this . reserveBytes ( 2 ) ;
985- buf . writeUInt16BE ( n ) ;
985+ const pos = this . reserveBytes ( 2 ) ;
986+ this . buf . writeUInt16BE ( n , pos ) ;
986987 }
987988
988989 //---------------------------------------------------------------------------
@@ -991,8 +992,8 @@ class BaseBuffer {
991992 // Writes an unsigned 32-bit integer to the buffer in big endian order.
992993 //---------------------------------------------------------------------------
993994 writeUInt32BE ( n ) {
994- const buf = this . reserveBytes ( 4 ) ;
995- buf . writeUInt32BE ( n ) ;
995+ const pos = this . reserveBytes ( 4 ) ;
996+ this . buf . writeUInt32BE ( n , pos ) ;
996997 }
997998
998999 //---------------------------------------------------------------------------
@@ -1003,9 +1004,9 @@ class BaseBuffer {
10031004 // higher order bits are simply written as 0.
10041005 //---------------------------------------------------------------------------
10051006 writeUInt64BE ( n ) {
1006- const buf = this . reserveBytes ( 8 ) ;
1007- buf . writeUInt32BE ( 0 ) ;
1008- buf . writeUInt32BE ( n , 4 ) ;
1007+ const pos = this . reserveBytes ( 8 ) ;
1008+ this . buf . writeUInt32BE ( 0 , pos ) ;
1009+ this . buf . writeUInt32BE ( n , pos + 4 ) ;
10091010 }
10101011
10111012 //---------------------------------------------------------------------------
@@ -1014,8 +1015,8 @@ class BaseBuffer {
10141015 // Writes an unsigned 16-bit integer to the buffer in little endian order.
10151016 //---------------------------------------------------------------------------
10161017 writeUInt16LE ( n ) {
1017- const buf = this . reserveBytes ( 2 ) ;
1018- buf . writeUInt16LE ( n ) ;
1018+ const pos = this . reserveBytes ( 2 ) ;
1019+ this . buf . writeUInt16LE ( n , pos ) ;
10191020 }
10201021
10211022}
@@ -1048,7 +1049,7 @@ class GrowableBuffer extends BaseBuffer {
10481049 if ( remainder > 0 ) {
10491050 numBytes += ( constants . BUFFER_CHUNK_SIZE - remainder ) ;
10501051 }
1051- const buf = Buffer . alloc ( numBytes ) ;
1052+ const buf = Buffer . allocUnsafe ( numBytes ) ;
10521053 this . buf . copy ( buf ) ;
10531054 this . buf = buf ;
10541055 this . maxSize = this . size = numBytes ;
0 commit comments