compress

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 8 Imported by: 0

README

Compression Middleware

What it does

Compresses eligible responses when the client supports an accepted encoding. It reduces bandwidth for text, JSON, HTML, and other compressible payloads.

How to implement

package main

import (
	"github.com/oarkflow/fh"
	"github.com/oarkflow/fh/mw/compress"
)

func main() {
	app := fh.New()
	app.Use(compress.New(compress.Config{}))

	app.Get("/", func(c fh.Ctx) error {
		return c.Status(fh.StatusOK).SendString("ok")
	})
}

Impact

Saves network bandwidth at the cost of CPU. For small responses compression can hurt latency.

Ordering guidance

Run late enough to see the response body, but avoid compressing already-compressed assets. Usually after cache decisions.

Production considerations

Set minimum body size. Disable or be careful for sensitive reflected content if BREACH-style risks apply. Do not compress images, archives, or already-compressed data.

Documentation

Index

Constants

View Source
const (
	EncodingGzip    = "gzip"
	EncodingDeflate = "deflate"
)

Variables

View Source
var DefaultConfig = Config{
	Level:   gzip.BestSpeed,
	MinSize: 512,
	CompressibleTypes: []string{
		"text/",
		"application/json",
		"application/xml",
		"application/javascript",
		"application/x-javascript",
		"image/svg+xml",
	},
	BreachProtection: true,
}

Functions

func New

func New(config ...Config) fh.HandlerFunc

Types

type Config

type Config struct {
	Level int

	MinSize int

	// CompressibleTypes uses prefix matching.
	CompressibleTypes []string

	Encoder Encoder

	Next func(ctx fh.Ctx) bool

	// BreachProtection skips compression when the response carries
	// security-sensitive headers (Set-Cookie, Content-Security-Policy,
	// Authorization) to mitigate BREACH-style side-channel attacks.
	BreachProtection bool
}

type DeflateEncoder

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

DeflateEncoder compresses responses using the DEFLATE algorithm (compress/flate). Deflate is universally supported by browsers and requires no external dependencies.

func NewDeflateEncoder

func NewDeflateEncoder(level int) *DeflateEncoder

NewDeflateEncoder creates a new deflate encoder at the given compression level.

func (*DeflateEncoder) Encode

func (d *DeflateEncoder) Encode(dst *bytes.Buffer, src []byte) error

func (*DeflateEncoder) Encoding

func (d *DeflateEncoder) Encoding() string

type Encoder

type Encoder interface {
	Encoding() string
	Encode(dst *bytes.Buffer, src []byte) error
}

type GzipEncoder

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

func NewGzipEncoder

func NewGzipEncoder(level int) *GzipEncoder

func (*GzipEncoder) Encode

func (e *GzipEncoder) Encode(dst *bytes.Buffer, src []byte) error

func (*GzipEncoder) Encoding

func (e *GzipEncoder) Encoding() string

Jump to

Keyboard shortcuts

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