-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrotli_impl.go
More file actions
81 lines (76 loc) · 2.26 KB
/
brotli_impl.go
File metadata and controls
81 lines (76 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package compression
import (
"io"
"github.com/andybalholm/brotli"
)
// isBrotliAvailable checks if brotli compression is available.
//
// This function checks if the github.com/andybalholm/brotli package
// is available and can be used for compression. It's used internally
// by BrotliProvider to determine if brotli compression should be
// offered to clients.
//
// The function returns true if the brotli package is available,
// false otherwise. This allows the compression package to gracefully
// handle cases where brotli is not available without causing
// compilation errors.
//
// Returns:
// - bool: true if brotli compression is available, false otherwise
//
// Example:
//
// // This function is used internally by BrotliProvider
// if isBrotliAvailable() {
// fmt.Println("Brotli compression is available")
// } else {
// fmt.Println("Brotli compression is not available")
// }
func isBrotliAvailable() bool {
return true
}
// newBrotliWriter creates a new brotli writer.
//
// This function creates a new brotli.Writer with the specified
// compression level and output writer. It handles the mapping
// of compression levels to brotli's quality settings.
//
// Compression level mapping:
// - 0: brotli.BestSpeed (fastest compression)
// - 1-10: Direct mapping to brotli quality levels
// - 11: brotli.BestCompression (best compression)
//
// Args:
// - w: The output writer to compress to
// - level: The compression level (0-11)
//
// Returns:
// - io.WriteCloser: A brotli compression writer
// - error: Any error that occurred during creation
//
// Example:
//
// // Create a brotli writer with maximum compression
// writer, err := newBrotliWriter(responseWriter, 11)
// if err != nil {
// return err
// }
// defer writer.Close()
//
// // Use the writer
// _, err = writer.Write([]byte("Hello, World!"))
func newBrotliWriter(w io.Writer, level int) (io.WriteCloser, error) {
// Brotli levels are 0-11, but we need to map them to brotli.WriterOptions
// Level 0 = brotli.BestSpeed, Level 11 = brotli.BestCompression
var quality int
switch {
case level <= 0:
quality = brotli.BestSpeed
case level >= 11:
quality = brotli.BestCompression
default:
// Map 1-10 to appropriate quality levels
quality = level
}
return brotli.NewWriterLevel(w, quality), nil
}