* Extracts the document sequences from the command document and returns * a buffer to be added as multiple sections after the initial type 0 * section in the message.
(document: Document)
| 606 | * section in the message. |
| 607 | */ |
| 608 | extractDocumentSequences(document: Document): Uint8Array { |
| 609 | // Pull out any field in the command document that's value is a document sequence. |
| 610 | const chunks = []; |
| 611 | for (const [key, value] of Object.entries(document)) { |
| 612 | if (value instanceof DocumentSequence) { |
| 613 | chunks.push(value.toBin()); |
| 614 | // Why are we removing the field from the command? This is because it needs to be |
| 615 | // removed in the OP_MSG request first section, and DocumentSequence is not a |
| 616 | // BSON type and is specific to the MongoDB wire protocol so there's nothing |
| 617 | // our BSON serializer can do about this. Since DocumentSequence is not exposed |
| 618 | // in the public API and only used internally, we are never mutating an original |
| 619 | // command provided by the user, just our own, and it's cheaper to delete from |
| 620 | // our own command than copying it. |
| 621 | delete document[key]; |
| 622 | } |
| 623 | } |
| 624 | if (chunks.length > 0) { |
| 625 | return ByteUtils.concat(chunks); |
| 626 | } |
| 627 | // If we have no document sequences we return an empty buffer for nothing to add |
| 628 | // to the payload. |
| 629 | return ByteUtils.allocate(0); |
| 630 | } |
| 631 | |
| 632 | serializeBson(document: Document): Uint8Array { |
| 633 | return BSON.serialize(document, { |
no test coverage detected