@@ -433,7 +433,7 @@ export class DocumentSequence {
433433 documents : Document [ ] ;
434434 serializedDocumentsLength : number ;
435435 private chunks : Uint8Array [ ] ;
436- private header ? : Buffer ;
436+ private header : Buffer ;
437437
438438 /**
439439 * Create a new document sequence for the provided field.
@@ -444,33 +444,27 @@ export class DocumentSequence {
444444 this . documents = [ ] ;
445445 this . chunks = [ ] ;
446446 this . serializedDocumentsLength = 0 ;
447- this . init ( ) ;
448- if ( documents ) {
449- for ( const doc of documents ) {
450- this . push ( doc , BSON . serialize ( doc ) ) ;
451- }
452- }
453- }
454-
455- /**
456- * Initialize the buffer chunks.
457- */
458- private init ( ) {
459447 // Document sequences starts with type 1 at the first byte.
448+ // Field strings must always be UTF-8.
460449 const buffer = Buffer . allocUnsafe ( 1 + 4 + this . field . length + 1 ) ;
461450 buffer [ 0 ] = 1 ;
462451 // Third part is the field name at offset 5 with trailing null byte.
463452 encodeUTF8Into ( buffer , `${ this . field } \0` , 5 ) ;
464453 this . chunks . push ( buffer ) ;
465454 this . header = buffer ;
455+ if ( documents ) {
456+ for ( const doc of documents ) {
457+ this . push ( doc , BSON . serialize ( doc ) ) ;
458+ }
459+ }
466460 }
467461
468462 /**
469463 * Push a document to the document sequence. Will serialize the document
470464 * as well and return the current serialized length of all documents.
471465 * @param document - The document to add.
472466 * @param buffer - The serialized document in raw BSON.
473- * @returns The new totoal document sequence length.
467+ * @returns The new total document sequence length.
474468 */
475469 push ( document : Document , buffer : Uint8Array ) : number {
476470 this . serializedDocumentsLength += buffer . length ;
0 commit comments