Construct an _OpMsg from raw bytes.
(cls, msg: bytes | memoryview)
| 1523 | |
| 1524 | @classmethod |
| 1525 | def unpack(cls, msg: bytes | memoryview) -> _OpMsg: |
| 1526 | """Construct an _OpMsg from raw bytes.""" |
| 1527 | flags, first_payload_type, first_payload_size = cls.UNPACK_FROM(msg) |
| 1528 | if flags != 0: |
| 1529 | if flags & cls.CHECKSUM_PRESENT: |
| 1530 | raise ProtocolError(f"Unsupported OP_MSG flag checksumPresent: 0x{flags:x}") |
| 1531 | |
| 1532 | if flags ^ cls.MORE_TO_COME: |
| 1533 | raise ProtocolError(f"Unsupported OP_MSG flags: 0x{flags:x}") |
| 1534 | if first_payload_type != 0: |
| 1535 | raise ProtocolError(f"Unsupported OP_MSG payload type: 0x{first_payload_type:x}") |
| 1536 | |
| 1537 | if len(msg) != first_payload_size + 5: |
| 1538 | raise ProtocolError("Unsupported OP_MSG reply: >1 section") |
| 1539 | |
| 1540 | payload_document = msg[5:] |
| 1541 | return cls(flags, payload_document) |
| 1542 | |
| 1543 | |
| 1544 | _UNPACK_REPLY: dict[int, Callable[[bytes | memoryview], Union[_OpReply, _OpMsg]]] = { |