| 1 |
// Package writer provides interfaces for pluggable compression implementations |
| 2 |
// used by gzhttp. Custom gzip and zstd encoders can be provided by implementing |
| 3 |
// the GzipWriter/ZstdWriter interfaces and corresponding factory types. |
| 4 |
package writer |
| 5 |
|
| 6 |
import ( |
| 7 |
"io" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
// GzipWriter implements the functions needed for compressing content. |
| 12 |
type GzipWriter interface { |
| 13 |
Write(p []byte) (int, error) |
| 14 |
Close() error |
| 15 |
Flush() error |
| 16 |
} |
| 17 |
|
| 18 |
// GzipWriterExt implements the functions needed for compressing content |
| 19 |
// and optional extensions. |
| 20 |
type GzipWriterExt interface { |
| 21 |
GzipWriter |
| 22 |
|
| 23 |
// SetHeader will populate header fields with non-nil values in h. |
| 24 |
SetHeader(h Header) |
| 25 |
} |
| 26 |
|
| 27 |
// Header is a gzip header. |
| 28 |
type Header struct { |
| 29 |
Comment string // comment |
| 30 |
Extra []byte // "extra data" |
| 31 |
ModTime time.Time // modification time |
| 32 |
Name string // file name |
| 33 |
OS byte // operating system type |
| 34 |
} |
| 35 |
|
| 36 |
// GzipWriterFactory contains the information needed for custom gzip implementations. |
| 37 |
type GzipWriterFactory struct { |
| 38 |
// Must return the minimum and maximum supported level. |
| 39 |
Levels func() (min, max int) |
| 40 |
|
| 41 |
// New must return a new GzipWriter. |
| 42 |
// level will always be within the return limits above. |
| 43 |
New func(writer io.Writer, level int) GzipWriter |
| 44 |
} |
| 45 |
|
| 46 |
// ZstdWriter implements the functions needed for zstd compression. |
| 47 |
type ZstdWriter interface { |
| 48 |
Write(p []byte) (int, error) |
| 49 |
Close() error |
| 50 |
Flush() error |
| 51 |
} |
| 52 |
|
| 53 |
// ZstdWriterFactory contains the information needed for custom zstd implementations. |
| 54 |
type ZstdWriterFactory struct { |
| 55 |
// Must return the minimum and maximum supported level. |
| 56 |
Levels func() (min, max int) |
| 57 |
|
| 58 |
// New must return a new ZstdWriter. |
| 59 |
// level will always be within the return limits above. |
| 60 |
New func(writer io.Writer, level int) ZstdWriter |
| 61 |
} |