Skip to content

Commit

Permalink
feat: add LZ4 encoder/decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
tiny-craft committed Jul 31, 2024
1 parent 70c38d9 commit 0739cb8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/types/view_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const DECODE_BASE64 = "Base64"
const DECODE_GZIP = "GZip"
const DECODE_DEFLATE = "Deflate"
const DECODE_ZSTD = "ZStd"
const DECODE_LZ4 = "LZ4"
const DECODE_BROTLI = "Brotli"
const DECODE_MSGPACK = "Msgpack"
const DECODE_PHP = "PHP"
Expand Down
7 changes: 7 additions & 0 deletions backend/utils/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
gzipConv GZipConvert
deflateConv DeflateConvert
zstdConv ZStdConvert
lz4Conv LZ4Convert
brotliConv BrotliConvert
msgpackConv MsgpackConvert
phpConv = NewPhpConvert()
Expand All @@ -44,6 +45,7 @@ var BuildInDecoders = map[string]DataConvert{
types.DECODE_GZIP: gzipConv,
types.DECODE_DEFLATE: deflateConv,
types.DECODE_ZSTD: zstdConv,
types.DECODE_LZ4: lz4Conv,
types.DECODE_BROTLI: brotliConv,
types.DECODE_MSGPACK: msgpackConv,
types.DECODE_PHP: phpConv,
Expand Down Expand Up @@ -138,6 +140,11 @@ func autoDecode(str string, customDecoder []CmdConvert) (value, resultDecode str
return
}

if value, ok = lz4Conv.Decode(str); ok {
resultDecode = types.DECODE_LZ4
return
}

// FIXME: skip decompress with brotli due to incorrect format checking
//if value, ok = decodeBrotli(str); ok {
// resultDecode = types.DECODE_BROTLI
Expand Down
39 changes: 39 additions & 0 deletions backend/utils/convert/lz4_convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package convutil

import (
"bytes"
"github.com/pierrec/lz4/v4"
"io"
)

type LZ4Convert struct{}

func (LZ4Convert) Enable() bool {
return true
}

func (LZ4Convert) Encode(str string) (string, bool) {
var compress = func(b []byte) (string, error) {
var buf bytes.Buffer
writer := lz4.NewWriter(&buf)
if _, err := writer.Write([]byte(str)); err != nil {
writer.Close()
return "", err
}
writer.Close()
return string(buf.Bytes()), nil
}

if gzipStr, err := compress([]byte(str)); err == nil {
return gzipStr, true
}
return str, false
}

func (LZ4Convert) Decode(str string) (string, bool) {
reader := lz4.NewReader(bytes.NewReader([]byte(str)))
if decompressed, err := io.ReadAll(reader); err == nil {
return string(decompressed), true
}
return str, false
}
1 change: 1 addition & 0 deletions frontend/src/consts/value_view_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const decodeTypes = {
GZIP: 'GZip',
DEFLATE: 'Deflate',
ZSTD: 'ZStd',
LZ4: 'LZ4',
BROTLI: 'Brotli',
MSGPACK: 'Msgpack',
PHP: 'PHP',
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/andybalholm/brotli v1.1.0
github.com/google/uuid v1.6.0
github.com/klauspost/compress v1.17.9
github.com/pierrec/lz4/v4 v4.1.21
github.com/redis/go-redis/v9 v9.6.1
github.com/vmihailenco/msgpack/v5 v5.4.1
github.com/vrischmann/userdir v0.0.0-20151206171402-20f291cebd68
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down

0 comments on commit 0739cb8

Please sign in to comment.