compress

package module
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2023 License: MIT Imports: 9 Imported by: 2

README

elton-compress

license Build Status

More compressor for elton compress middleware.

  • SnappyCompressor Snappy compression algorithm is fast, but not aim for maximum compression. It's useful for Intranet. Not support compress level.
  • ZstdCompressor Zstandard is a real-time compression algorithm, providing high compression ratios. Compress level is 1-2, default 0(2).
  • Lz4Compressor LZ4 is lossless compression algorithm, providing compression speed > 500 MB/s per core, scalable with multi-cores CPU. Compress level higher is better, use 0 for fastest compression.
package main

import (
	"bytes"
	"io/ioutil"
	"net/http"

	"github.com/vicanso/elton"
	compress "github.com/vicanso/elton-compress"
	"github.com/vicanso/elton/middleware"
)

func main() {
	e := elton.New()
	// 需要注意添加的顺序,压缩是按添加的选择顺序选择适合的压缩方式
	// 此处只是示例所有的压缩器,正常使用时,按需使用1,2个压缩方式则可
	config := middleware.NewCompressConfig(
		&middleware.BrCompressor{
			MinLength: 1024,
		},
		new(middleware.GzipCompressor),
		new(compress.SnappyCompressor),
		new(compress.ZstdCompressor),
		new(compress.S2Compressor),
		&compress.Lz4Compressor{
			MinLength: 10 * 1024,
		},
	)
	e.Use(middleware.NewCompress(config))

	e.GET("/", func(c *elton.Context) (err error) {
		resp, err := http.Get("https://code.jquery.com/jquery-3.4.1.min.js")
		if err != nil {
			return
		}
		defer resp.Body.Close()
		buf, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return
		}
		c.SetContentTypeByExt(".js")
		c.BodyBuffer = bytes.NewBuffer(buf)
		return
	})
	err := e.ListenAndServe(":3000")
	if err != nil {
		panic(err)
	}
}

Example

middleware example

jquery-3.4.1.min.js(88145 bytes)

compression size ratio level
gzip 30827 2.859 6
br 29897 2.948 6
snappy 47709 1.847 -
zstd 32816 2.686 2
lz4 39434 2.235 6

Documentation

Index

Constants

View Source
const (
	// Lz4Encoding lz4 encoding
	Lz4Encoding = "lz4"
)
View Source
const (
	// SnappyEncoding snappy encoding
	SnappyEncoding = "snz"
)
View Source
const (
	// https://en.wikipedia.org/wiki/Zstd
	// In 2018 the algorithm was published as RFC 8478, which also defines an associated media type "application/zstd", filename extension "zst", and HTTP content encoding "zstd".[15]
	// ZstdEncoding zstd encoding
	ZstdEncoding = "zstd"
)

Variables

View Source
var ErrLz4IsNotCompressible = errors.New("Is not compressible for lz4")

Functions

This section is empty.

Types

type Lz4Compressor added in v0.1.4

type Lz4Compressor struct {
	Level     int
	MinLength int
}

Lz4Compressor lz4 compress

func (*Lz4Compressor) Accept added in v0.1.4

func (l *Lz4Compressor) Accept(c *elton.Context, bodySize int) (acceptable bool, encoding string)

Accept check accept encoding

func (*Lz4Compressor) Compress added in v0.1.4

func (l *Lz4Compressor) Compress(buf []byte, levels ...int) (*bytes.Buffer, error)

Compress lz4 compress

func (*Lz4Compressor) Pipe added in v0.1.4

func (l *Lz4Compressor) Pipe(c *elton.Context) (err error)

Pipe lz4 pipe compress

type SnappyCompressor

type SnappyCompressor struct {
	MinLength int
}

SnappyCompressor snappy compress

func (*SnappyCompressor) Accept

func (s *SnappyCompressor) Accept(c *elton.Context, bodySize int) (acceptable bool, encoding string)

Accept check accept encoding

func (*SnappyCompressor) Compress

func (s *SnappyCompressor) Compress(buf []byte, levels ...int) (*bytes.Buffer, error)

Compress snappy compress

func (*SnappyCompressor) Pipe

func (s *SnappyCompressor) Pipe(c *elton.Context) (err error)

Pipe snappy pipe

type ZstdCompressor added in v0.1.4

type ZstdCompressor struct {
	Level     int
	MinLength int
}

ZstdCompressor zstd compress

func (*ZstdCompressor) Accept added in v0.1.4

func (z *ZstdCompressor) Accept(c *elton.Context, bodySize int) (acceptable bool, encoding string)

Accept check accept encoding

func (*ZstdCompressor) Compress added in v0.1.4

func (z *ZstdCompressor) Compress(buf []byte, levels ...int) (*bytes.Buffer, error)

Compress zstd compress

func (*ZstdCompressor) Pipe added in v0.1.4

func (z *ZstdCompressor) Pipe(c *elton.Context) (err error)

Pipe zstd pike

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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