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 ¶
var ( // ErrMessageTooLarge is returned by Decompress when the decompressed size // exceeds maxSize. ErrMessageTooLarge = errors.New("ews/deflate: decompressed message exceeds limit") // ErrInvalidLimit is returned by Decompress for a negative maxSize. ErrInvalidLimit = errors.New("ews/deflate: negative output limit") // ErrNoMessage is returned by Read when no message was started with Begin, // or the previous one has ended. ErrNoMessage = errors.New("ews/deflate: no message in progress") // ErrInvalidWindow is returned by NewCompressorWindow for window bits // outside 8 to 15. ErrInvalidWindow = errors.New("ews/deflate: window bits must be 8 to 15") )
Functions ¶
This section is empty.
Types ¶
type ChunkSource ¶
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 ¶
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. Output is borrowed until the next call.
Only the final chunk drops the sync-flush tail; middle chunks keep it, since the receiver only restores it once at the end.
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 ¶
Decompress borrows its output until the next call. maxSize must be nonnegative, and a message that decompresses past it fails with ErrMessageTooLarge.
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) ReleaseOutput ¶ added in v0.4.1
func (d *Decompressor) ReleaseOutput()
ReleaseOutput is Reset that also gives the output and input storage back to the pool, for a caller that keeps the decompressor across messages but not a large result. The inflater and its window are kept.
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.