compress

package
v5.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Brotli added in v5.2.0

type Brotli struct {
	// Quality controls the compression-speed vs compression-density trade-offs.
	// The higher the quality, the slower the compression. Range is 0 to 11.
	Quality int
	// LGWin is the base 2 logarithm of the sliding window size.
	// Range is 10 to 24. 0 indicates automatic configuration based on Quality.
	LGWin int
}

Brotli encoder for the br compression format

func (*Brotli) Encoding added in v5.2.0

func (w *Brotli) Encoding() string

Encoding returns "br".

func (*Brotli) NewWriter added in v5.2.0

func (w *Brotli) NewWriter(wr io.Writer) io.WriteCloser

NewWriter returns a new `brotli.Writer` using the Compression Quality and LGWin provided in the Brotli encoder

type Encoder

type Encoder interface {
	NewWriter(wr io.Writer) io.WriteCloser
	Encoding() string
}

Encoder is an interface that wraps the methods returning the information necessary for the compress middleware to work.

`NewWriter` returns any `io.WriteCloser`, allowing the middleware to support any compression algorithm.

`Encoding` returns the name of the compression algorithm. Using the returned value, the middleware:

  1. detects the client's preferred encoding with the `Accept-Encoding` request header
  2. replaces the response writer with the writer returned by `NewWriter`
  3. sets the `Content-Encoding` response header

type Gzip

type Gzip struct {
	Level int
}

Gzip encoder for the gzip format using Go's standard `compress/gzip` package.

Takes a compression level as parameter. Accepted values are defined by constants in the standard `compress/gzip` package.

func (*Gzip) Encoding

func (w *Gzip) Encoding() string

Encoding returns "gzip".

func (*Gzip) NewWriter

func (w *Gzip) NewWriter(wr io.Writer) io.WriteCloser

NewWriter returns a new `compress/gzip.Writer` using the compression level defined in this Gzip encoder.

type LZW added in v5.2.0

type LZW struct {
	// Order specifies the bit ordering in an LZW data stream.
	// It is optional, and the default value is lzw.LSB (Least Significant Bits)
	Order lzw.Order
	// LitWidth specifies the number of bits to use for literal codes
	// Must be in the range [2,8] and is typically 8.
	// Input bytes must be less than 1<<litWidth.
	LitWidth int
}

LZW encoder for the compress format using Go's standard `compress/lzw` package.

Takes an Order specifying the bit ordering in an LZW data stream level, and number of bits to use for literal codes, litWidth, as parameters. Accepted values are defined by constants in the standard `compress/lzw` package.

func (*LZW) Encoding added in v5.2.0

func (w *LZW) Encoding() string

Encoding returns "compress".

func (*LZW) NewWriter added in v5.2.0

func (w *LZW) NewWriter(wr io.Writer) io.WriteCloser

NewWriter returns a new `compress/lzw.Writer` using an LZW bit ordering type, and an int for number of bits to use for literal codes - must be within range [2, 8] Default to a LitWidth of 8 if LitWidth was not set

type Middleware

type Middleware struct {
	goyave.Component
	Encoders []Encoder
}

Middleware compresses HTTP responses.

This middleware supports multiple algorithms thanks to the `Encoders` slice. The encoder will be chosen depending on the request's `Accept-Encoding` header, and the value returned by the `Encoder`'s `Encoding()` method. Quality values in the headers are taken into account.

In case of equal priority, the encoding that is the earliest in the slice is chosen. If the header's value is `*` and no encoding already matched, the first element of the slice is used.

If none of the accepted encodings are available in the `Encoders` slice, then the response will not be compressed and the middleware immediately passes.

If the middleware successfully replaces the response writer, the `Accept-Encoding` header is removed from the request to avoid potential clashes with potential other encoding middleware.

If not set at the first call of `Write()`, the middleware will automatically detect and set the `Content-Type` header using `http.DetectContentType()`.

The middleware ignores hijacked responses or requests containing the `Upgrade` header.

**Example:**

compressMiddleware := &compress.Middleware{
	Encoders: []compress.Encoder{
		&compress.Gzip{Level: gzip.BestCompression},
	},
}

func (*Middleware) Handle

func (m *Middleware) Handle(next goyave.Handler) goyave.Handler

Handle implementation of `goyave.Middleware`.

type Zlib added in v5.2.0

type Zlib struct {
	// The dictionary is optional and may be nil
	Dict  []byte
	Level int
}

Zlib encoder for the deflate format using Go's standard `compress/zlib` package. Takes a compression level and "dict" ([]byte) as parameters. Accepted values are defined by constants in the standard `compress/zlib` package.

func (*Zlib) Encoding added in v5.2.0

func (w *Zlib) Encoding() string

Encoding returns "deflate".

func (*Zlib) NewWriter added in v5.2.0

func (w *Zlib) NewWriter(wr io.Writer) io.WriteCloser

NewWriter returns a new `compress/zlib.Writer` using the compression level defined in this Zlib encoder. You may also provide a dict to compress with, or leave as nil

type Zstd added in v5.2.0

type Zstd struct {
	Options []zstd.EOption
}

Zstd encoder for the Zstandard compression algorithm You may provide a list of zstd.EOptions as parameters. Refer to the package documentation for more information

func (*Zstd) Encoding added in v5.2.0

func (w *Zstd) Encoding() string

Encoding returns "zstd".

func (*Zstd) NewWriter added in v5.2.0

func (w *Zstd) NewWriter(wr io.Writer) io.WriteCloser

NewWriter returns a new `zstd.Encoder` using the zstd.EOptions defined in the Zstd encoder.

Jump to

Keyboard shortcuts

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