-
Notifications
You must be signed in to change notification settings - Fork 37
/
coffer.go
210 lines (179 loc) · 3.98 KB
/
coffer.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package air
import (
"bytes"
"compress/gzip"
"encoding/binary"
"io/ioutil"
"mime"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/VictoriaMetrics/fastcache"
"github.com/aofei/mimesniffer"
"github.com/cespare/xxhash/v2"
"github.com/fsnotify/fsnotify"
)
// coffer is a binary asset file manager that uses runtime memory to reduce disk
// I/O pressure.
type coffer struct {
a *Air
loadOnce *sync.Once
loadError error
watcher *fsnotify.Watcher
assets sync.Map
cache *fastcache.Cache
}
// newCoffer returns a new instance of the `coffer` with the a.
func newCoffer(a *Air) *coffer {
return &coffer{
a: a,
loadOnce: &sync.Once{},
}
}
// load loads the stuff of the c up.
func (c *coffer) load() {
defer func() {
if c.loadError != nil {
c.loadOnce = &sync.Once{}
}
}()
if c.watcher == nil {
c.watcher, c.loadError = fsnotify.NewWatcher()
if c.loadError != nil {
return
}
go func() {
for {
select {
case e := <-c.watcher.Events:
ai, ok := c.assets.Load(e.Name)
if !ok {
break
}
a := ai.(*asset)
c.assets.Delete(a.name)
c.cache.Del(a.digest)
if a.gzippedDigest != nil {
c.cache.Del(a.gzippedDigest)
}
case err := <-c.watcher.Errors:
c.a.logErrorf(
"air: coffer watcher error: %v",
err,
)
case <-c.a.context.Done():
return
}
}
}()
}
c.cache = fastcache.New(c.a.CofferMaxMemoryBytes)
}
// asset returns an `asset` from the c for the name.
func (c *coffer) asset(name string) (*asset, error) {
if c.loadOnce.Do(c.load); c.loadError != nil {
return nil, c.loadError
} else if ai, ok := c.assets.Load(name); ok {
return ai.(*asset), nil
} else if ar, err := filepath.Abs(c.a.CofferAssetRoot); err != nil {
return nil, err
} else if !strings.HasPrefix(name, ar) {
return nil, nil
}
ext := filepath.Ext(name)
if !stringSliceContains(c.a.CofferAssetExts, ext, true) {
return nil, nil
}
fi, err := os.Stat(name)
if err != nil {
return nil, err
}
b, err := ioutil.ReadFile(name)
if err != nil {
return nil, err
}
var (
mt = mime.TypeByExtension(ext)
minified bool
gb []byte
)
if mt == "" {
mt = mimesniffer.Sniff(b)
}
pmt, _, err := mime.ParseMediaType(mt)
if err != nil {
return nil, err
}
if c.a.MinifierEnabled &&
stringSliceContains(c.a.MinifierMIMETypes, pmt, true) {
if b, err = c.a.minifier.minify(pmt, b); err != nil {
return nil, err
}
minified = true
}
if c.a.GzipEnabled && int64(len(b)) >= c.a.GzipMinContentLength &&
stringSliceContains(c.a.GzipMIMETypes, pmt, true) {
buf := bytes.Buffer{}
if gw, err := gzip.NewWriterLevel(
&buf,
c.a.GzipCompressionLevel,
); err != nil {
return nil, err
} else if _, err = gw.Write(b); err != nil {
return nil, err
} else if err = gw.Close(); err != nil {
return nil, err
}
gb = buf.Bytes()
}
if err := c.watcher.Add(name); err != nil {
return nil, err
}
a := &asset{
coffer: c,
name: name,
mimeType: mt,
modTime: fi.ModTime(),
minified: minified,
digest: make([]byte, 8),
}
binary.BigEndian.PutUint64(a.digest, xxhash.Sum64(b))
c.cache.SetBig(a.digest, b)
if gb != nil {
a.gzippedDigest = make([]byte, 8)
binary.BigEndian.PutUint64(a.gzippedDigest, xxhash.Sum64(gb))
c.cache.SetBig(a.gzippedDigest, gb)
}
c.assets.Store(name, a)
return a, nil
}
// asset is a binary asset file.
type asset struct {
coffer *coffer
name string
mimeType string
modTime time.Time
minified bool
digest []byte
gzippedDigest []byte
}
// content returns the content of the a with the gzipped.
func (a *asset) content(gzipped bool) []byte {
var c []byte
if gzipped {
c = a.coffer.cache.GetBig(nil, a.gzippedDigest)
} else {
c = a.coffer.cache.GetBig(nil, a.digest)
}
if len(c) == 0 {
a.coffer.assets.Delete(a.name)
a.coffer.cache.Del(a.digest)
if a.gzippedDigest != nil {
a.coffer.cache.Del(a.gzippedDigest)
}
return nil
}
return c
}