deflate

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: 5 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 deflate implements permessage-deflate. Helpers operate on complete, unmasked message payloads. Configure context takeover to match negotiation and use the default 32 KB window.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMessageTooLarge = errors.New("ews/deflate: decompressed message exceeds limit")
	ErrInvalidLimit    = errors.New("ews/deflate: negative output limit")
	ErrNoMessage       = errors.New("ews/deflate: no message in progress")
	ErrInvalidWindow   = errors.New("ews/deflate: window bits must be 8 to 15")
)

Functions

This section is empty.

Types

type ChunkSource

type ChunkSource interface {
	NextChunk() ([]byte, error)
}

ChunkSource supplies the compressed bytes of one message in order. Chunks are borrowed until the next call and may be empty. It returns io.EOF after the last chunk. Any other error ends the message and is returned by Read.

type Compressor

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

Compressor compresses messages using reusable storage. Context takeover state lives in the Window passed to each call, so one compressor can serve many connections in turn. Priming the encoder from a window costs about as much as compressing the window, so a compressor that keeps serving the same window continues its stream instead and pays nothing; a connection that wants speed keeps a compressor attached, one that wants memory shares it.

func NewCompressor

func NewCompressor(level int) (*Compressor, error)

NewCompressor creates a compressor using a flate compression level and the full 32 KB window.

func NewCompressorWindow

func NewCompressorWindow(windowBits int) (*Compressor, error)

NewCompressorWindow creates a compressor whose matches never reach back more than 1<<windowBits bytes, for peers that negotiated a smaller window. windowBits is 8 to 15; the compression level is fixed by the encoder.

func (*Compressor) Compress

func (c *Compressor) Compress(payload []byte, w *Window) ([]byte, error)

Compress borrows its output until the next call. Input remains unchanged. With a window, the message continues that direction's history and the window is updated; with nil, it is compressed on its own.

func (*Compressor) CompressChunk

func (c *Compressor) CompressChunk(payload []byte, w *Window, final bool) ([]byte, error)

CompressChunk compresses one fragment of a message sent as it is produced. The first chunk primes or continues the stream as Compress does, and later chunks continue it, so the fragments decode as one message. Only the final chunk drops the sync-flush tail; middle chunks keep it, since the receiver only restores it once at the end. Output is borrowed until the next call.

func (*Compressor) Reset

func (c *Compressor) Reset()

Reset clears output and detaches from any window, retaining storage.

type Decompressor

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

Decompressor decompresses messages using reusable storage. It carries no state between messages, so one decompressor can serve many connections in turn; context takeover lives in the Window passed to each call. The zero value is ready to use.

func (*Decompressor) Begin

func (d *Decompressor) Begin(src ChunkSource, w *Window) error

Begin starts decompressing a message streamed from src; drain it with Read. The window works as for Decompress. A message still in progress is abandoned, which clears its window.

func (*Decompressor) Decompress

func (d *Decompressor) Decompress(payload []byte, maxSize int, w *Window) ([]byte, error)

Decompress borrows its output until the next call. maxSize must be nonnegative. It returns ErrMessageTooLarge if the decompressed size exceeds maxSize. With a window, the message continues that direction's history and the window is updated; with nil, it is decompressed on its own. Any error clears the window.

func (*Decompressor) Read

func (d *Decompressor) Read(p []byte) (int, error)

Read decompresses into p and returns io.EOF, with no data, after the last byte of the message. Output is never returned together with io.EOF. Any other error ends the message and clears its window.

func (*Decompressor) Reset

func (d *Decompressor) Reset()

Reset abandons any message in progress and clears output, retaining storage.

type Window

type Window struct {
	// Bits is the window size as a power of two, 8 to 15; zero means 15. Set
	// it to the negotiated size before use: for a send window the size this
	// endpoint compresses with, for a receive window the size the peer
	// declared. Only that much history is then kept and copied.
	Bits int
	// contains filtered or unexported fields
}

Window is one direction's compression history for context takeover: the most recent 32 KB of payload sent or received on a connection. Give each direction of each connection its own Window and pass it to every call for that direction; nil means no context takeover. The zero value is ready to use. A failed message clears the window, since the peers' histories have diverged.

func (*Window) Add

func (w *Window) Add(payload []byte)

Add records payload as compressed or decompressed data sent through some other path, such as a message compressed once for many recipients, so the history stays in step with the peer's. A compressor that was continuing this window primes again on its next message.

func (*Window) Reset

func (w *Window) Reset()

Reset forgets the history, retaining storage.

func (*Window) Size

func (w *Window) Size() int

Size returns the window size in bytes.

Jump to

Keyboard shortcuts

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