@@ -30,6 +30,8 @@ const QUERY_FAILURE = 2;
30
30
const SHARD_CONFIG_STALE = 4 ;
31
31
const AWAIT_CAPABLE = 8 ;
32
32
33
+ const encodeUTF8Into = BSON . BSON . onDemand . ByteUtils . encodeUTF8Into ;
34
+
33
35
/** @internal */
34
36
export type WriteProtocolMessageType = OpQueryRequest | OpMsgRequest ;
35
37
@@ -413,11 +415,9 @@ export interface OpMsgOptions {
413
415
414
416
/** @internal */
415
417
export class DocumentSequence {
416
- field : string ;
417
418
documents : Document [ ] ;
418
419
419
- constructor ( field : string , documents : Document [ ] ) {
420
- this . field = field ;
420
+ constructor ( documents : Document [ ] ) {
421
421
this . documents = documents ;
422
422
}
423
423
}
@@ -506,7 +506,7 @@ export class OpMsgRequest {
506
506
*/
507
507
makeSections ( buffers : Uint8Array [ ] , document : Document ) : number {
508
508
const sequencesBuffer = this . extractDocumentSequences ( document ) ;
509
- const payloadTypeBuffer = Buffer . alloc ( 1 ) ;
509
+ const payloadTypeBuffer = Buffer . allocUnsafe ( 1 ) ;
510
510
payloadTypeBuffer [ 0 ] = 0 ;
511
511
512
512
const documentBuffer = this . serializeBson ( document ) ;
@@ -530,23 +530,20 @@ export class OpMsgRequest {
530
530
for ( const [ key , value ] of Object . entries ( document ) ) {
531
531
if ( value instanceof DocumentSequence ) {
532
532
// Document sequences starts with type 1 at the first byte.
533
- const payloadTypeBuffer = Buffer . alloc ( 1 ) ;
534
- payloadTypeBuffer [ 0 ] = 1 ;
535
- chunks . push ( payloadTypeBuffer ) ;
536
- // Second part of the sequence is the length;
537
- const lengthBuffer = Buffer . alloc ( 4 ) ;
538
- chunks . push ( lengthBuffer ) ;
539
- // Third part is the field name.
540
- const fieldBuffer = Buffer . from ( key ) ;
541
- chunks . push ( fieldBuffer ) ;
533
+ const buffer = Buffer . allocUnsafe ( 1 + 4 + key . length ) ;
534
+ buffer [ 0 ] = 1 ;
535
+ // Third part is the field name at offset 5.
536
+ encodeUTF8Into ( buffer , key , 5 ) ;
537
+ chunks . push ( buffer ) ;
542
538
// Fourth part are the documents' bytes.
543
539
let docsLength = 0 ;
544
540
for ( const doc of value . documents ) {
545
541
const docBson = this . serializeBson ( doc ) ;
546
542
docsLength += docBson . length ;
547
543
chunks . push ( docBson ) ;
548
544
}
549
- lengthBuffer . writeInt32LE ( fieldBuffer . length + docsLength ) ;
545
+ // Second part of the sequence is the length at offset 1;
546
+ buffer . writeInt32LE ( key . length + docsLength , 1 ) ;
550
547
// Why are we removing the field from the command? This is because it needs to be
551
548
// removed in the OP_MSG request first section, and DocumentSequence is not a
552
549
// BSON type and is specific to the MongoDB wire protocol so there's nothing
@@ -560,6 +557,8 @@ export class OpMsgRequest {
560
557
if ( chunks . length > 0 ) {
561
558
return Buffer . concat ( chunks ) ;
562
559
}
560
+ // If we have no document sequences we return an empty buffer for nothing to add
561
+ // to the payload.
563
562
return Buffer . alloc ( 0 ) ;
564
563
}
565
564
0 commit comments