@@ -264,8 +264,8 @@ function deserializeObject(
264264 value = ByteUtils . toUTF8 ( buffer , index , index + stringSize - 1 , shouldValidateKey ) ;
265265 index = index + stringSize ;
266266 } else if ( elementType === constants . BSON_DATA_OID ) {
267- const oid = ByteUtils . allocate ( 12 ) ;
268- oid . set ( buffer . subarray ( index , index + 12 ) ) ;
267+ const oid = ByteUtils . allocateUnsafe ( 12 ) ;
268+ for ( let i = 0 ; i < 12 ; i ++ ) oid [ i ] = buffer [ index + i ] ;
269269 value = new ObjectId ( oid ) ;
270270 index = index + 12 ;
271271 } else if ( elementType === constants . BSON_DATA_INT && promoteValues === false ) {
@@ -355,9 +355,9 @@ function deserializeObject(
355355 }
356356 } else if ( elementType === constants . BSON_DATA_DECIMAL128 ) {
357357 // Buffer to contain the decimal bytes
358- const bytes = ByteUtils . allocate ( 16 ) ;
358+ const bytes = ByteUtils . allocateUnsafe ( 16 ) ;
359359 // Copy the next 16 bytes into the bytes buffer
360- bytes . set ( buffer . subarray ( index , index + 16 ) , 0 ) ;
360+ for ( let i = 0 ; i < 16 ; i ++ ) bytes [ i ] = buffer [ index + i ] ;
361361 // Update index
362362 index = index + 16 ;
363363 // Assign the new Decimal128 value
@@ -398,7 +398,6 @@ function deserializeObject(
398398 }
399399 }
400400 } else {
401- const _buffer = ByteUtils . allocate ( binarySize ) ;
402401 // If we have subtype 2 skip the 4 bytes for the size
403402 if ( subType === Binary . SUBTYPE_BYTE_ARRAY ) {
404403 binarySize = NumberUtils . getInt32LE ( buffer , index ) ;
@@ -411,13 +410,12 @@ function deserializeObject(
411410 throw new BSONError ( 'Binary type with subtype 0x02 contains too short binary size' ) ;
412411 }
413412
414- // Copy the data
415- for ( i = 0 ; i < binarySize ; i ++ ) {
416- _buffer [ i ] = buffer [ index + i ] ;
417- }
418-
419413 if ( promoteBuffers && promoteValues ) {
420- value = _buffer ;
414+ value = ByteUtils . allocateUnsafe ( binarySize ) ;
415+ // Copy the data
416+ for ( i = 0 ; i < binarySize ; i ++ ) {
417+ value [ i ] = buffer [ index + i ] ;
418+ }
421419 } else {
422420 value = new Binary ( buffer . slice ( index , index + binarySize ) , subType ) ;
423421 if ( subType === constants . BSON_BINARY_SUBTYPE_UUID_NEW && UUID . isValid ( value ) ) {
@@ -616,8 +614,8 @@ function deserializeObject(
616614 index = index + stringSize ;
617615
618616 // Read the oid
619- const oidBuffer = ByteUtils . allocate ( 12 ) ;
620- oidBuffer . set ( buffer . subarray ( index , index + 12 ) , 0 ) ;
617+ const oidBuffer = ByteUtils . allocateUnsafe ( 12 ) ;
618+ for ( let i = 0 ; i < 12 ; i ++ ) oidBuffer [ i ] = buffer [ index + i ] ;
621619 const oid = new ObjectId ( oidBuffer ) ;
622620
623621 // Update the index
0 commit comments