@@ -87,68 +87,27 @@ function serializeNumber(
87
87
isArray ?: boolean
88
88
) {
89
89
// We have an integer value
90
- // TODO(NODE-2769): Issue serializing large integers as doubles
91
90
// TODO(NODE-2529): Add support for big int
92
91
if (
93
- Math . floor ( value ) === value &&
94
- value >= constants . JS_INT_MIN &&
95
- value <= constants . JS_INT_MAX
92
+ Number . isInteger ( value ) &&
93
+ value >= constants . BSON_INT32_MIN &&
94
+ value <= constants . BSON_INT32_MAX
96
95
) {
97
- // If the value fits in 32 bits encode as int, if it fits in a double
98
- // encode it as a double, otherwise long
99
- if ( value >= constants . BSON_INT32_MIN && value <= constants . BSON_INT32_MAX ) {
100
- // Set int type 32 bits or less
101
- buffer [ index ++ ] = constants . BSON_DATA_INT ;
102
- // Number of written bytes
103
- const numberOfWrittenBytes = ! isArray
104
- ? buffer . write ( key , index , undefined , 'utf8' )
105
- : buffer . write ( key , index , undefined , 'ascii' ) ;
106
- // Encode the name
107
- index = index + numberOfWrittenBytes ;
108
- buffer [ index ++ ] = 0 ;
109
- // Write the int value
110
- buffer [ index ++ ] = value & 0xff ;
111
- buffer [ index ++ ] = ( value >> 8 ) & 0xff ;
112
- buffer [ index ++ ] = ( value >> 16 ) & 0xff ;
113
- buffer [ index ++ ] = ( value >> 24 ) & 0xff ;
114
- } else if ( value >= constants . JS_INT_MIN && value <= constants . JS_INT_MAX ) {
115
- // Encode as double
116
- buffer [ index ++ ] = constants . BSON_DATA_NUMBER ;
117
- // Number of written bytes
118
- const numberOfWrittenBytes = ! isArray
119
- ? buffer . write ( key , index , undefined , 'utf8' )
120
- : buffer . write ( key , index , undefined , 'ascii' ) ;
121
- // Encode the name
122
- index = index + numberOfWrittenBytes ;
123
- buffer [ index ++ ] = 0 ;
124
- // Write float
125
- writeIEEE754 ( buffer , value , index , 'little' , 52 , 8 ) ;
126
- // Adjust index
127
- index = index + 8 ;
128
- } else {
129
- // Set long type
130
- buffer [ index ++ ] = constants . BSON_DATA_LONG ;
131
- // Number of written bytes
132
- const numberOfWrittenBytes = ! isArray
133
- ? buffer . write ( key , index , undefined , 'utf8' )
134
- : buffer . write ( key , index , undefined , 'ascii' ) ;
135
- // Encode the name
136
- index = index + numberOfWrittenBytes ;
137
- buffer [ index ++ ] = 0 ;
138
- const longVal = Long . fromNumber ( value ) ;
139
- const lowBits = longVal . getLowBits ( ) ;
140
- const highBits = longVal . getHighBits ( ) ;
141
- // Encode low bits
142
- buffer [ index ++ ] = lowBits & 0xff ;
143
- buffer [ index ++ ] = ( lowBits >> 8 ) & 0xff ;
144
- buffer [ index ++ ] = ( lowBits >> 16 ) & 0xff ;
145
- buffer [ index ++ ] = ( lowBits >> 24 ) & 0xff ;
146
- // Encode high bits
147
- buffer [ index ++ ] = highBits & 0xff ;
148
- buffer [ index ++ ] = ( highBits >> 8 ) & 0xff ;
149
- buffer [ index ++ ] = ( highBits >> 16 ) & 0xff ;
150
- buffer [ index ++ ] = ( highBits >> 24 ) & 0xff ;
151
- }
96
+ // If the value fits in 32 bits encode as int32
97
+ // Set int type 32 bits or less
98
+ buffer [ index ++ ] = constants . BSON_DATA_INT ;
99
+ // Number of written bytes
100
+ const numberOfWrittenBytes = ! isArray
101
+ ? buffer . write ( key , index , undefined , 'utf8' )
102
+ : buffer . write ( key , index , undefined , 'ascii' ) ;
103
+ // Encode the name
104
+ index = index + numberOfWrittenBytes ;
105
+ buffer [ index ++ ] = 0 ;
106
+ // Write the int value
107
+ buffer [ index ++ ] = value & 0xff ;
108
+ buffer [ index ++ ] = ( value >> 8 ) & 0xff ;
109
+ buffer [ index ++ ] = ( value >> 16 ) & 0xff ;
110
+ buffer [ index ++ ] = ( value >> 24 ) & 0xff ;
152
111
} else {
153
112
// Encode as double
154
113
buffer [ index ++ ] = constants . BSON_DATA_NUMBER ;
0 commit comments