-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
148 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package compress | ||
|
||
import ( | ||
"compress/gzip" | ||
"regexp" | ||
"sort" | ||
) | ||
|
||
var DefaultAllowedTypes = []*regexp.Regexp{ | ||
regexp.MustCompile(`^text/`), | ||
regexp.MustCompile(`^application/json`), | ||
regexp.MustCompile(`^application/javascript`), | ||
regexp.MustCompile(`\+(xml|json)$`), | ||
regexp.MustCompile(`^image/svg`), | ||
} | ||
|
||
type config struct { | ||
supportedEncoding []string | ||
|
||
encoders map[string]*encoder | ||
allowedType []*regexp.Regexp | ||
minSize uint64 | ||
silent bool | ||
} | ||
|
||
func newConfig(options ...Option) *config { | ||
c := &config{minSize: 4 * 1024, allowedType: DefaultAllowedTypes, encoders: map[string]*encoder{}} | ||
WithGzip(100, gzip.DefaultCompression)(c) | ||
for _, o := range options { | ||
o(c) | ||
} | ||
c.populateSupportedEncoding() | ||
return c | ||
} | ||
|
||
func (c *config) populateSupportedEncoding() { | ||
type encoderWithName struct { | ||
*encoder | ||
name string | ||
} | ||
|
||
encList := make([]encoderWithName, 0, len(c.encoders)) | ||
for name, enc := range c.encoders { | ||
encList = append(encList, encoderWithName{enc, name}) | ||
} | ||
sort.Slice(encList, func(i, j int) bool { | ||
return encList[i].priority < encList[j].priority | ||
}) | ||
c.supportedEncoding = make([]string, 0, len(encList)) | ||
for _, enc := range encList { | ||
c.supportedEncoding = append(c.supportedEncoding, enc.name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.