@@ -411,6 +411,17 @@ export interface OpMsgOptions {
411
411
readPreference : ReadPreference ;
412
412
}
413
413
414
+ /** @internal */
415
+ export class DocumentSequence {
416
+ field : string ;
417
+ documents : Document [ ] ;
418
+
419
+ constructor ( field : string , documents : Document [ ] ) {
420
+ this . field = field ;
421
+ this . documents = documents ;
422
+ }
423
+ }
424
+
414
425
/** @internal */
415
426
export class OpMsgRequest {
416
427
requestId : number ;
@@ -491,14 +502,48 @@ export class OpMsgRequest {
491
502
}
492
503
493
504
makeDocumentSegment ( buffers : Uint8Array [ ] , document : Document ) : number {
505
+ const sequencesBuffer = this . extractDocumentSequences ( document ) ;
494
506
const payloadTypeBuffer = Buffer . alloc ( 1 ) ;
495
507
payloadTypeBuffer [ 0 ] = 0 ;
496
508
497
509
const documentBuffer = this . serializeBson ( document ) ;
498
510
buffers . push ( payloadTypeBuffer ) ;
499
511
buffers . push ( documentBuffer ) ;
512
+ buffers . push ( sequencesBuffer ) ;
500
513
501
- return payloadTypeBuffer . length + documentBuffer . length ;
514
+ return payloadTypeBuffer . length + documentBuffer . length + sequencesBuffer . length ;
515
+ }
516
+
517
+ extractDocumentSequences ( document : Document ) : Uint8Array {
518
+ // Pull out any field in the command document that's value is a document sequence.
519
+ const chunks = [ ] ;
520
+ for ( const [ key , value ] of Object . entries ( document ) ) {
521
+ if ( value instanceof DocumentSequence ) {
522
+ // Document sequences starts with type 1 at the first byte.
523
+ const payloadTypeBuffer = Buffer . alloc ( 1 ) ;
524
+ payloadTypeBuffer [ 0 ] = 1 ;
525
+ chunks . push ( payloadTypeBuffer ) ;
526
+ // Second part of the sequence is the length;
527
+ const lengthBuffer = Buffer . alloc ( 4 ) ;
528
+ chunks . push ( lengthBuffer ) ;
529
+ // Third part is the field name.
530
+ const fieldBuffer = Buffer . from ( key ) ;
531
+ chunks . push ( fieldBuffer ) ;
532
+ // Fourth part are the documents' bytes.
533
+ let docsLength = 0 ;
534
+ for ( const doc of value . documents ) {
535
+ const docBson = this . serializeBson ( doc ) ;
536
+ docsLength += docBson . length ;
537
+ chunks . push ( docBson ) ;
538
+ }
539
+ lengthBuffer . writeInt32LE ( fieldBuffer . length + docsLength ) ;
540
+ delete document [ key ] ;
541
+ }
542
+ }
543
+ if ( chunks . length > 0 ) {
544
+ return Buffer . concat ( chunks ) ;
545
+ }
546
+ return Buffer . alloc ( 0 ) ;
502
547
}
503
548
504
549
serializeBson ( document : Document ) : Uint8Array {
0 commit comments