Skip to content

Commit

Permalink
Merge pull request cdnjs#78 from tc80/brotli_cli
Browse files Browse the repository at this point in the history
brotli and gzip native/cli approaches
  • Loading branch information
tc80 authored Jul 7, 2020
2 parents c4942a2 + 2d9a294 commit 62bcc5a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 70 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ Repository that contains various tools for maintaining cdnjs.
- `BOT_BASE_PATH`: cdnjs home
- `SENTRY_DSN` sentry data source name (DSN)

## Dependencies

In `tools/` run `npm install`.

- [jpegoptim](https://www.kokkonen.net/tjko/projects.html)
- [zopflipng](https://github.com/google/zopfli)

## Setup a local environment

All the tools uses `BOT_BASE_PATH` to define a "cdnjs home".
Expand Down
50 changes: 50 additions & 0 deletions compress/algorithm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package compress

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

"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

err := cmd.Run()
util.Check(err)

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

return stdOut.Bytes()
}

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

// Gzip9Native returns a gzip compressed file as bytes
// at optimal compression (level 9).
func Gzip9Native(uncompressed []byte) []byte {
var b bytes.Buffer

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

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

return b.Bytes()
}
13 changes: 1 addition & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,19 @@ go 1.13

require (
cloud.google.com/go v0.53.0 // indirect
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/blang/semver v3.5.1+incompatible
github.com/certifi/gocertifi v0.0.0-20200211180108-c7c1fbc02894 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/getsentry/raven-go v0.2.0
github.com/getsentry/sentry-go v0.6.1
github.com/gobwas/glob v0.2.3 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/karrick/godirwalk v1.15.6
github.com/pachyderm/ohmyglob v0.0.0-20190808212558-a8e61fd76805
github.com/pkg/errors v0.8.1
github.com/sergi/go-diff v1.1.0 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sys v0.0.0-20200217220822-9197077df867 // indirect
golang.org/x/tools v0.0.0-20200701151220-7cb253f4c4f8 // indirect
google.golang.org/api v0.17.0
gopkg.in/src-d/go-git.v4 v4.13.1
honnef.co/go/tools v0.0.1-2019.2.3
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
Loading

0 comments on commit 62bcc5a

Please sign in to comment.