@@ -49,7 +49,7 @@ class BaseBuffer {
49
49
this . buf = Buffer . alloc ( initializer ) ;
50
50
this . size = 0 ;
51
51
this . maxSize = initializer ;
52
- } else {
52
+ } else if ( initializer ) {
53
53
this . buf = initializer ;
54
54
this . size = this . maxSize = initializer . length ;
55
55
}
@@ -548,11 +548,12 @@ class BaseBuffer {
548
548
// remain in the buffer, the buffer is grown.
549
549
//---------------------------------------------------------------------------
550
550
reserveBytes ( numBytes ) {
551
- if ( numBytes > this . numBytesLeft ( ) )
551
+ if ( numBytes > this . numBytesLeft ( ) ) {
552
552
this . _grow ( this . pos + numBytes ) ;
553
- const buf = this . buf . subarray ( this . pos , this . pos + numBytes ) ;
553
+ }
554
+ const pos = this . pos ;
554
555
this . pos += numBytes ;
555
- return buf ;
556
+ return pos ;
556
557
}
557
558
558
559
//---------------------------------------------------------------------------
@@ -618,20 +619,20 @@ class BaseBuffer {
618
619
//---------------------------------------------------------------------------
619
620
writeBinaryDouble ( n ) {
620
621
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 ;
625
626
} else {
626
627
// 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 ;
635
636
}
636
637
}
637
638
@@ -642,16 +643,16 @@ class BaseBuffer {
642
643
//---------------------------------------------------------------------------
643
644
writeBinaryFloat ( n ) {
644
645
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 ;
649
650
} else {
650
651
// 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 ;
655
656
}
656
657
}
657
658
@@ -745,31 +746,31 @@ class BaseBuffer {
745
746
if ( writeLength ) {
746
747
this . writeUInt8 ( length ) ;
747
748
}
748
- const ptr = this . reserveBytes ( length ) ;
749
+ const pos = this . reserveBytes ( length ) ;
749
750
if ( type === types . DB_TYPE_DATE || type == types . DB_TYPE_TIMESTAMP ) {
750
751
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 ;
758
759
} else {
759
760
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 ;
767
768
}
768
769
if ( length > 7 ) {
769
- ptr . writeInt32BE ( fsec , 7 ) ;
770
+ this . buf . writeInt32BE ( fsec , pos + 7 ) ;
770
771
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 ;
773
774
}
774
775
}
775
776
}
@@ -843,19 +844,19 @@ class BaseBuffer {
843
844
} else if ( value . length === 0 && exponent === 0 ) {
844
845
exponentOnWire = 128 ;
845
846
}
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 ) {
850
851
const base100Digit = Number ( value . substring ( i , i + 2 ) ) ;
851
852
if ( isNegative ) {
852
- buf [ pos ] = 101 - base100Digit ;
853
+ this . buf [ pos ++ ] = 101 - base100Digit ;
853
854
} else {
854
- buf [ pos ] = base100Digit + 1 ;
855
+ this . buf [ pos ++ ] = base100Digit + 1 ;
855
856
}
856
857
}
857
858
if ( appendSentinel ) {
858
- buf [ buf . length - 1 ] = 102 ;
859
+ this . buf [ pos ] = 102 ;
859
860
}
860
861
861
862
}
@@ -898,8 +899,8 @@ class BaseBuffer {
898
899
// Writes a signed 32-bit integer to the buffer in big endian order.
899
900
//---------------------------------------------------------------------------
900
901
writeInt32BE ( n ) {
901
- const buf = this . reserveBytes ( 4 ) ;
902
- buf . writeInt32BE ( n ) ;
902
+ const pos = this . reserveBytes ( 4 ) ;
903
+ this . buf . writeInt32BE ( n , pos ) ;
903
904
}
904
905
905
906
//---------------------------------------------------------------------------
@@ -971,8 +972,8 @@ class BaseBuffer {
971
972
// Writes an unsigned 8-bit integer to the buffer.
972
973
//---------------------------------------------------------------------------
973
974
writeUInt8 ( n ) {
974
- const buf = this . reserveBytes ( 1 ) ;
975
- buf [ 0 ] = n ;
975
+ const pos = this . reserveBytes ( 1 ) ;
976
+ this . buf [ pos ] = n ;
976
977
}
977
978
978
979
//---------------------------------------------------------------------------
@@ -981,8 +982,8 @@ class BaseBuffer {
981
982
// Writes an unsigned 16-bit integer to the buffer in big endian order.
982
983
//---------------------------------------------------------------------------
983
984
writeUInt16BE ( n ) {
984
- const buf = this . reserveBytes ( 2 ) ;
985
- buf . writeUInt16BE ( n ) ;
985
+ const pos = this . reserveBytes ( 2 ) ;
986
+ this . buf . writeUInt16BE ( n , pos ) ;
986
987
}
987
988
988
989
//---------------------------------------------------------------------------
@@ -991,8 +992,8 @@ class BaseBuffer {
991
992
// Writes an unsigned 32-bit integer to the buffer in big endian order.
992
993
//---------------------------------------------------------------------------
993
994
writeUInt32BE ( n ) {
994
- const buf = this . reserveBytes ( 4 ) ;
995
- buf . writeUInt32BE ( n ) ;
995
+ const pos = this . reserveBytes ( 4 ) ;
996
+ this . buf . writeUInt32BE ( n , pos ) ;
996
997
}
997
998
998
999
//---------------------------------------------------------------------------
@@ -1003,9 +1004,9 @@ class BaseBuffer {
1003
1004
// higher order bits are simply written as 0.
1004
1005
//---------------------------------------------------------------------------
1005
1006
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 ) ;
1009
1010
}
1010
1011
1011
1012
//---------------------------------------------------------------------------
@@ -1014,8 +1015,8 @@ class BaseBuffer {
1014
1015
// Writes an unsigned 16-bit integer to the buffer in little endian order.
1015
1016
//---------------------------------------------------------------------------
1016
1017
writeUInt16LE ( n ) {
1017
- const buf = this . reserveBytes ( 2 ) ;
1018
- buf . writeUInt16LE ( n ) ;
1018
+ const pos = this . reserveBytes ( 2 ) ;
1019
+ this . buf . writeUInt16LE ( n , pos ) ;
1019
1020
}
1020
1021
1021
1022
}
@@ -1048,7 +1049,7 @@ class GrowableBuffer extends BaseBuffer {
1048
1049
if ( remainder > 0 ) {
1049
1050
numBytes += ( constants . BUFFER_CHUNK_SIZE - remainder ) ;
1050
1051
}
1051
- const buf = Buffer . alloc ( numBytes ) ;
1052
+ const buf = Buffer . allocUnsafe ( numBytes ) ;
1052
1053
this . buf . copy ( buf ) ;
1053
1054
this . buf = buf ;
1054
1055
this . maxSize = this . size = numBytes ;
0 commit comments