@@ -164,7 +164,7 @@ function deserializeObject(
164
164
// Reflects utf-8 validation setting regardless of global or specific key validation
165
165
let validationSetting : boolean ;
166
166
// Set of keys either to enable or disable validation on
167
- const utf8KeysSet = new Set ( ) ;
167
+ let utf8KeysSet ;
168
168
169
169
// Check for boolean uniformity and empty validation option
170
170
const utf8ValidatedKeys = validation . utf8 ;
@@ -190,6 +190,8 @@ function deserializeObject(
190
190
191
191
// Add keys to set that will either be validated or not based on validationSetting
192
192
if ( ! globalUTFValidation ) {
193
+ utf8KeysSet = new Set ( ) ;
194
+
193
195
for ( const key of Object . keys ( utf8ValidatedKeys ) ) {
194
196
utf8KeysSet . add ( key ) ;
195
197
}
@@ -216,8 +218,9 @@ function deserializeObject(
216
218
217
219
let isPossibleDBRef = isArray ? false : null ;
218
220
221
+ let dataView ;
222
+
219
223
// While we have more left data left keep parsing
220
- const dataview = new DataView ( buffer . buffer , buffer . byteOffset , buffer . byteLength ) ;
221
224
while ( ! done ) {
222
225
// Read the type
223
226
const elementType = buffer [ index ++ ] ;
@@ -240,7 +243,7 @@ function deserializeObject(
240
243
241
244
// shouldValidateKey is true if the key should be validated, false otherwise
242
245
let shouldValidateKey = true ;
243
- if ( globalUTFValidation || utf8KeysSet . has ( name ) ) {
246
+ if ( globalUTFValidation || utf8KeysSet ? .has ( name ) ) {
244
247
shouldValidateKey = validationSetting ;
245
248
} else {
246
249
shouldValidateKey = ! validationSetting ;
@@ -284,10 +287,12 @@ function deserializeObject(
284
287
( buffer [ index ++ ] << 16 ) |
285
288
( buffer [ index ++ ] << 24 ) ;
286
289
} else if ( elementType === constants . BSON_DATA_NUMBER && promoteValues === false ) {
287
- value = new Double ( dataview . getFloat64 ( index , true ) ) ;
290
+ dataView ??= new DataView ( buffer . buffer , buffer . byteOffset , buffer . byteLength ) ;
291
+ value = new Double ( dataView . getFloat64 ( index , true ) ) ;
288
292
index = index + 8 ;
289
293
} else if ( elementType === constants . BSON_DATA_NUMBER ) {
290
- value = dataview . getFloat64 ( index , true ) ;
294
+ dataView ??= new DataView ( buffer . buffer , buffer . byteOffset , buffer . byteLength ) ;
295
+ value = dataView . getFloat64 ( index , true ) ;
291
296
index = index + 8 ;
292
297
} else if ( elementType === constants . BSON_DATA_DATE ) {
293
298
const lowBits =
@@ -300,6 +305,7 @@ function deserializeObject(
300
305
( buffer [ index ++ ] << 8 ) |
301
306
( buffer [ index ++ ] << 16 ) |
302
307
( buffer [ index ++ ] << 24 ) ;
308
+
303
309
value = new Date ( new Long ( lowBits , highBits ) . toNumber ( ) ) ;
304
310
} else if ( elementType === constants . BSON_DATA_BOOLEAN ) {
305
311
if ( buffer [ index ] !== 0 && buffer [ index ] !== 1 )
0 commit comments