Skip to content

blackbull.grpc.codec

blackbull.grpc.codec

gRPC Length-Prefixed-Message codec.

The gRPC wire format (https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md) carries each message inside an HTTP/2 DATA stream as:

Length-Prefixed-Message = Compressed-Flag (1 byte, unsigned)
                          Message-Length  (4 bytes, big-endian uint32)
                          Message         (Message-Length bytes)

Compressed-Flag is 0 for an uncompressed message and 1 when the message body is compressed with the algorithm named in the grpc-encoding header. This module is pure framing: it carries the flag but does not (de)compress — :mod:~blackbull.grpc.compression holds the gzip codec and the ASGI bridge sets/reads the flag around it.

This module is pure binary framing — no protobuf dependency. Protobuf serialisation is the application's concern; handlers receive and return the raw message bytes.

GrpcDecodeError

Bases: ValueError

Raised when a DATA buffer is not a valid sequence of Length-Prefixed-Messages (truncated prefix or short body).

decode_messages(data)

Parse data into a list of (compressed, payload) messages.

A single DATA buffer may contain zero, one, or many framed messages (gRPC permits multiple messages per stream and does not align them to DATA-frame boundaries). Raises :class:GrpcDecodeError on a truncated prefix or a message body shorter than its declared length.

encode_message(payload, *, compressed=False)

Frame payload as a single gRPC Length-Prefixed-Message.