Skip to content

Commit

Permalink
convert to native
Browse files Browse the repository at this point in the history
  • Loading branch information
tc80 committed Jul 7, 2020
1 parent ddf4737 commit 396f706
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
46 changes: 22 additions & 24 deletions compress/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,37 @@ package compress

import (
"bytes"
"fmt"
"os/exec"
"compress/gzip"

"github.com/andybalholm/brotli"
"github.com/cdnjs/tools/util"
)

// Runs an algorithm with a set of arguments,
// and returns its stdout as bytes.
// Note, this function will panic if anything is
// output to stderr.
func runAlgorithm(alg string, args ...string) []byte {
cmd := exec.Command(alg, args...)
var stdOut, stdErr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdOut, &stdErr
// Brotli11 returns a brotli compressed file as bytes
// at optimal compression (quality 11).
func Brotli11(uncompressed []byte) []byte {
var b bytes.Buffer

err := cmd.Run()
w := brotli.NewWriterLevel(&b, brotli.BestCompression)
_, err := w.Write(uncompressed)
util.Check(err)
util.Check(w.Flush())

if stdErr.Len() > 0 {
panic(fmt.Sprintf("%s failed: %s", alg, stdErr.String()))
}

return stdOut.Bytes()
}

// Brotli11 returns a brotli compressed file as bytes
// at optimal compression (quality 11).
func Brotli11(filePath string) []byte {
return runAlgorithm("brotli", "-c", "-q", "11", filePath)
return b.Bytes()
}

// Gzip9 returns a gzip compressed file as bytes
// at optimal compression (level 9).
func Gzip9(filePath string) []byte {
return runAlgorithm("gzip", "-c", "-9", filePath)
func Gzip9(uncompressed []byte) []byte {
var b bytes.Buffer

w, err := gzip.NewWriterLevel(&b, gzip.BestCompression)
util.Check(err)
defer w.Close()

_, err = w.Write(uncompressed)
util.Check(err)
util.Check(w.Flush())

return b.Bytes()
}
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 (
cloud.google.com/go/logging v1.0.0
cloud.google.com/go/storage v1.5.0
github.com/algolia/algoliasearch-client-go/v3 v3.4.0
github.com/andybalholm/brotli v1.0.0
github.com/blang/semver v3.5.1+incompatible
github.com/certifi/gocertifi v0.0.0-20200211180108-c7c1fbc02894 // indirect
github.com/davecgh/go-spew v1.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/algolia/algoliasearch-client-go v2.25.0+incompatible h1:FGQr9l++u4uQP
github.com/algolia/algoliasearch-client-go/v3 v3.4.0 h1:eeVU30L5DkKUK2q/EjXw+8o7reoK4QB1mS+BG0Jbd4Y=
github.com/algolia/algoliasearch-client-go/v3 v3.4.0/go.mod h1:d0/D54BCmkwhLxT5VIQBeYLAz2GbZHFX9OptYyohTr0=
github.com/algolia/algoliasearch-client-go/v3 v3.8.1 h1:Bt9z5gZpPxp/6XoL1pJrzmvJ3uOc0kU1dItHIeufoOc=
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
Expand Down

0 comments on commit 396f706

Please sign in to comment.