Documentation
¶
Overview ¶
Package protodelim implements size-delimited encoding and decoding of Protocol Buffers messages for stream-oriented I/O. Each message is preceded by its size encoded as a base-128 varint, allowing multiple messages to be concatenated on a single io.Writer and read back sequentially from an io.Reader.
This framing format is commonly used by gRPC length-prefixed messages, protobuf streaming APIs, and any protocol that transmits a sequence of variable-length protobuf messages over a byte stream.
MarshalTo writes a single size-delimited message to an io.Writer. UnmarshalFrom reads a single size-delimited message from an io.Reader. UnmarshalOptions provides configurable limits (such as MaxSize) to prevent excessive memory allocation when reading from untrusted sources.
This package depends on proto/, wire/, and io from the standard library. It has zero external dependencies.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrMessageTooLarge = errors.New("protodelim: message size exceeds maximum allowed size")
ErrMessageTooLarge indicates that the size prefix of a delimited message exceeds the configured MaxSize limit.
Functions ¶
func MarshalTo ¶
MarshalTo writes msg to w as a size-delimited message. It first computes the serialized size via the Sizer interface, writes the size as a varint prefix, then writes the marshaled message bytes. It returns the total number of bytes written (varint prefix plus message body) and any error encountered during marshaling or writing.
func UnmarshalFrom ¶
UnmarshalFrom reads a single size-delimited message from r and unmarshals it into msg. It reads a varint size prefix, then reads exactly that many bytes, and unmarshals them into msg. If r returns io.EOF before any bytes are read, UnmarshalFrom returns io.EOF. If the stream is truncated after a partial read, an error is returned.
Types ¶
type UnmarshalOptions ¶
type UnmarshalOptions struct {
// MaxSize is the maximum allowed message size in bytes. If the varint
// size prefix indicates a message larger than MaxSize, UnmarshalFrom
// returns ErrMessageTooLarge without allocating a read buffer. A value
// of 0 uses the default of 4 MiB.
MaxSize int
}
UnmarshalOptions configures the behavior of size-delimited message reading.
func (UnmarshalOptions) UnmarshalFrom ¶
UnmarshalFrom reads a single size-delimited message from r and unmarshals it into msg, enforcing the MaxSize limit. It reads a varint size prefix byte-by-byte from r, validates the size against MaxSize, reads the message body, and unmarshals it into msg.