From 439bf2422bac82a275529a1d216966e4f3a878ec Mon Sep 17 00:00:00 2001 From: Laurence Withers <laurence.withers@yoti.com> Date: Fri, 28 Apr 2023 15:54:56 +0100 Subject: [PATCH] cmd/htpacker: don't try to compress tiny files If we have some really tiny files, it's not worth compressing them. Among other things, this will work around a bug in go-zopfli for 0- or 1-byte files. --- cmd/htpacker/packer/packer.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cmd/htpacker/packer/packer.go b/cmd/htpacker/packer/packer.go index 6dba182..a6b0a1b 100644 --- a/cmd/htpacker/packer/packer.go +++ b/cmd/htpacker/packer/packer.go @@ -64,6 +64,11 @@ func (ignoreProgress) Begin(_, _ string) {} func (ignoreProgress) End(_, _ string) {} const ( + // minCompressionFileSize is the minimum filesize we need before + // considering compression. Note this must be at least 2, to avoid + // known bugs in go-zopfli. + minCompressionFileSize = 128 + // minCompressionSaving means we'll only use the compressed version of // the file if it's at least this many bytes smaller than the original. // Chosen somewhat arbitrarily; we have to add an HTTP header, and the @@ -433,6 +438,10 @@ func (p *packer) Uncompressed(srcPath string, dir *packed.File) error { // Gzip will gzip input data to a temporary file, and then append that to the // output file. func (p *packer) Gzip(data []byte, dir *packed.File) error { + if len(data) < minCompressionFileSize { + return nil + } + // write via temporary file tmpfile, err := ioutil.TempFile("", "") if err != nil { @@ -474,6 +483,9 @@ func (p *packer) Gzip(data []byte, dir *packed.File) error { // Brotli will compress input data to a temporary file, and then append that to // the output file. func (p *packer) Brotli(data []byte, dir *packed.File) error { + if len(data) < minCompressionFileSize { + return nil + } // write via temporary file tmpfile, err := ioutil.TempFile("", "") if err != nil {