Categorygithub.com/kataras/compress
modulepackage
0.0.6
Repository: https://github.com/kataras/compress.git
Documentation: pkg.go.dev

# README

Compress

build status report card godocs

Fast and easy-to-use compression package for Go applications.

Installation

The only requirement is the Go Programming Language.

$ go get github.com/kataras/compress

Getting Started

Import the package:

package main

import "github.com/kataras/compress"

Wrap a handler to enable writing and reading using the best offered compression:

import "net/http"

mux := http.NewServeMux()
// [...]
http.ListenAndServe(":8080", compress.Handler(mux))

Wrap any io.Writer for writing data using compression with NewWriter:

import "bytes"
import "encoding/json"

buf := new(bytes.Buffer)

w, err := compress.NewWriter(buf, compress.GZIP, -1)
if err != nil {
    panic(err)
}

json.NewEncoder(w).Encode(payload{Data: "my data"})

w.Close()

Wrap any io.Reader for reading compressed data with NewReader:

// Where resp.Body is an io.Reader.
r, err := compress.NewReader(resp.Body, compress.GZIP)
if err != nil {
    panic(err)
}
defer r.Close()

body, err := ioutil.ReadAll(r)

To retrieve the underline http.ResponseWriter please use w.(*compress.ResponseWriter).ResponseWriter.

Example Code:

import "net/http"

func handler(w http.ResponseWriter, r *http.Request) {
    target := "/your/asset.js"

	if pusher, ok := w.(*compress.ResponseWriter).ResponseWriter.(http.Pusher); ok {
		err := pusher.Push(target, &http.PushOptions{
            Header: http.Header{
                "Accept-Encoding": r.Header["Accept-Encoding"],
        }})
		if err != nil {
			if err == http.ErrNotSupported {
				http.Error(w, "HTTP/2 push not supported", http.StatusHTTPVersionNotSupported)
			} else {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
			return
		}
    }
    
    // [...]
}

The http.CloseNotifier is obselete by Go authors, please use Request.Context().Done() instead.

Supported compression algorithms:

  • gzip
  • deflate
  • brotli
  • snappy

Please navigate through _examples directory for more.

License

This software is licensed under the MIT License.

# Functions

AddCompressHeaders just adds the headers "Vary" to "Accept-Encoding" and "Content-Encoding" to the given encoding.
GetEncoding extracts the best available encoding from the request.
Handler wraps a Handler and returns a new one which makes future Write calls to compress the data before sent and future request body to decompress the incoming data before read.
NewReader returns a new "Reader" wrapper of "src".
NewResponseWriter wraps the "w" response writer and returns a new compress response writer instance.
NewWriter returns a Writer of "w" based on the given "encoding".
ReadHandler is the decompress and read request body middleware.
WriteHandler is the write using compression middleware.

# Constants

Header keys.
The available builtin compression algorithms.
Header keys.
Header keys.
Header keys.
The available builtin compression algorithms.
The available builtin compression algorithms.
IDENTITY when no transformation whatsoever.
The available builtin compression algorithms.
The available builtin compression algorithms.
Header keys.

# Variables

DefaultOffers is a slice of default content encodings.
ErrNotSupportedCompression returned from NewResponseWriter, NewWriter and NewReader when the request's Accept-Encoding was not found in the server's supported compression algorithms.
ErrRequestNotCompressed returned from NewReader when request is not compressed.
ErrResponseNotCompressed returned from NewResponseWriter when response's Content-Type header is missing due to golang/go/issues/31753 or when accept-encoding is empty.

# Structs

Reader is a structure which wraps a compressed reader.
ResponseWriter is a compressed data http.ResponseWriter.

# Interfaces

Writer is an interface which all compress writers should implement.