codec

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codec encodes and decodes WebSocket frames using byte slices. Payload masking is explicit; message assembly and connection handling belong to the caller.

Index

Constants

View Source
const MaxHeaderSize = 14

MaxHeaderSize is the maximum encoded header size.

Variables

View Source
var (
	// ErrPayloadPending means NextHeader was called before draining the payload.
	ErrPayloadPending = errors.New("ews/codec: previous payload has not been consumed")
	// ErrInvalidPayloadLength indicates a nonminimal or out-of-range length.
	ErrInvalidPayloadLength = errors.New("ews/codec: invalid payload length encoding")
)
View Source
var (
	ErrInvalidOpcode          = errors.New("ews/codec: invalid opcode")
	ErrInvalidControlFrame    = errors.New("ews/codec: invalid control frame")
	ErrInvalidCompressedFrame = errors.New("ews/codec: only data frames may be compressed")
)

Functions

func Mask

func Mask(payload []byte, key [4]byte, offset uint8) uint8

Mask masks or unmasks payload in place, returning the next offset modulo four. Start each frame at offset zero and carry the returned offset between chunks.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder incrementally decodes WebSocket frames. The zero value is ready to use.

func (*Decoder) Buffered

func (d *Decoder) Buffered() int

Buffered returns the number of fed bytes not yet consumed.

func (*Decoder) Feed

func (d *Decoder) Feed(b []byte)

Feed appends input, borrowing b when no input is pending.

func (*Decoder) NextHeader

func (d *Decoder) NextHeader() (Header, bool, error)

NextHeader reads a header; ok is false if incomplete. Errors consume nothing. Unread payloads and invalid lengths are errors; other validation is up to the caller.

func (*Decoder) Payload

func (d *Decoder) Payload() (chunk []byte, done bool)

Payload consumes available bytes from this frame without unmasking. Bytes are borrowed until the next decoder call. Returns nil, false if waiting for input, or nil, true if no payload remains (including before the first header).

func (*Decoder) PayloadN

func (d *Decoder) PayloadN(n int) (chunk []byte, done bool)

PayloadN is like Payload but consumes at most n bytes. If n <= 0, it consumes nothing.

func (*Decoder) Preserve

func (d *Decoder) Preserve()

Preserve copies pending input into reusable storage so the feed buffer can be reused.

func (*Decoder) Reset

func (d *Decoder) Reset()

Reset clears decoder state, retaining scratch capacity.

type Encoder

type Encoder struct {
	// ScratchKeep bounds the masking scratch kept between frames. A payload
	// that needs more gets a scratch of its size, which the next frame that
	// fits within the bound releases, so one huge masked message does not
	// pin a copy for the life of the encoder. Zero keeps whatever was needed.
	ScratchKeep int
	// contains filtered or unexported fields
}

Encoder prepares WebSocket frames. The zero value is ready to use.

func (*Encoder) Encode

func (e *Encoder) Encode(final bool, opcode Opcode, payload []byte, key *[4]byte) error

Encode borrows payload if key is nil; otherwise it masks a copy in scratch. Errors leave the frame unchanged.

func (*Encoder) EncodeCompressed

func (e *Encoder) EncodeCompressed(final bool, opcode Opcode, payload []byte, key *[4]byte) error

EncodeCompressed encodes precompressed message data, setting RSV1 on its first frame. Continuation frames leave RSV1 clear. Control frames are rejected.

func (*Encoder) HeaderBytes

func (e *Encoder) HeaderBytes() []byte

HeaderBytes borrows the header until the next Encode or Reset.

func (*Encoder) PayloadBytes

func (e *Encoder) PayloadBytes() []byte

PayloadBytes borrows the payload until the next Encode or Reset.

func (*Encoder) Reset

func (e *Encoder) Reset()

Reset clears the frame, retaining scratch capacity.

type Header struct {
	// contains filtered or unexported fields
}

Header stores a WebSocket frame header. Accessors assume valid input.

func (*Header) Bytes

func (h *Header) Bytes() []byte

Bytes borrows the encoded header bytes.

func (*Header) Final

func (h *Header) Final() bool

Final reports whether this is the final fragment of a message.

func (*Header) Len

func (h *Header) Len() int

Len returns the encoded header length in bytes.

func (*Header) MaskKey

func (h *Header) MaskKey() []byte

MaskKey borrows the four-byte masking key, or returns nil if unmasked.

func (*Header) Masked

func (h *Header) Masked() bool

Masked reports whether the payload is masked.

func (*Header) Opcode

func (h *Header) Opcode() Opcode

Opcode returns the frame's opcode.

func (*Header) PayloadLen

func (h *Header) PayloadLen() uint64

PayloadLen returns the payload size in bytes.

func (*Header) RSV1

func (h *Header) RSV1() bool

RSV1 reports whether the first reserved bit is set.

func (*Header) RSV2

func (h *Header) RSV2() bool

RSV2 reports whether the second reserved bit is set.

func (*Header) RSV3

func (h *Header) RSV3() bool

RSV3 reports whether the third reserved bit is set.

type Opcode

type Opcode uint8

Opcode identifies a WebSocket frame's type.

const (
	Continuation Opcode = 0x0
	Text         Opcode = 0x1
	Binary       Opcode = 0x2
	Close        Opcode = 0x8
	Ping         Opcode = 0x9
	Pong         Opcode = 0xA
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL