1 // Package brotli provides an Encoder for the httpbuffer package that uses 2 // brotli compression. 3 package brotli // import "vimagination.zapto.org/httpbuffer/brotli" 4 5 import ( 6 "io" 7 8 "github.com/google/brotli/go/cbrotli" 9 "vimagination.zapto.org/httpbuffer" 10 ) 11 12 type cbrotliWriter struct { 13 *cbrotli.Writer 14 } 15 16 func (c cbrotliWriter) WriteString(str string) (int, error) { 17 return c.Write([]byte(str)) 18 } 19 20 var ( 21 // Compression sets the compression options for the brotli encoder 22 Compression = cbrotli.WriterOptions{ 23 Quality: 4, 24 } 25 ) 26 27 type encoding struct{} 28 29 func (encoding) Open(w io.Writer) io.Writer { 30 return cbrotliWriter{cbrotli.NewWriter(w, Compression)} 31 } 32 33 func (encoding) Close(w io.Writer) { 34 w.(cbrotliWriter).Close() 35 } 36 37 func (encoding) Name() string { 38 return "br" 39 } 40 41 func init() { 42 httpbuffer.Register(encoding{}) 43 }