-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebassets.go
More file actions
331 lines (289 loc) · 9 KB
/
Copy pathwebassets.go
File metadata and controls
331 lines (289 loc) · 9 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package webassets
import (
"bytes"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/js"
)
// TODO(compression): extend cacheEntry to store pre-compressed variants alongside the raw
// bytes, e.g. `gzip []byte` and `br []byte`. Compression should happen once, inside
// buildFileEntry/buildBundleEntry, right after minification — so the work is paid at
// warm-up time rather than per request. The singleflight guard already ensures this only
// happens once per resource even under concurrent load.
//
// At serve time, inspect the Accept-Encoding request header (in order of preference):
// 1. br (Brotli) — best ratio; use golang.org/x/net/http2 or github.com/andybalholm/brotli
// 2. gzip — widest support; use compress/gzip from stdlib
// 3. identity — fall back to the raw bytes already in the entry
//
// Set the Content-Encoding response header accordingly and omit Content-Length
// (the compressed size differs from the raw size). Also add a Vary: Accept-Encoding
// header so that CDNs and reverse proxies cache the compressed and uncompressed
// variants separately.
//
// Only compress text-based content types (JS, CSS, HTML, JSON, SVG, XML).
// Binary formats (images, fonts, woff2) are already compressed and re-compressing
// them wastes CPU for no gain.
type cacheEntry struct {
data []byte
contentType string
}
type bundle struct {
files []string
}
// Handler serves static web assets from a directory.
// It supports in-memory caching, on-the-fly JS/CSS minification,
// and virtual bundles that concatenate multiple source files.
//
// Usage:
//
// h := webassets.New("./web/static",
// webassets.WithCache(),
// webassets.WithMinification(),
// webassets.WithBundle("app.bundle.js", "js/vendor.js", "js/app.js"),
// )
// mux.Handle("/assets/", http.StripPrefix("/assets", h))
type Handler struct {
dir string
useCache bool
cacheExclude []string
useMinify bool
bundles map[string]bundle
mu sync.RWMutex
fileCache map[string]cacheEntry
bundleCache map[string]cacheEntry
minifier *minify.M
fileServer http.Handler
sf sfGroup
errLogger func(error)
}
// Option configures a Handler.
type Option func(*Handler)
// WithCache enables in-memory caching. Each asset is read from disk on the
// first request and served from memory on all subsequent requests.
func WithCache(enabled bool) Option {
return func(h *Handler) { h.useCache = enabled }
}
// WithCacheExclude prevents paths with the given URL prefixes from being
// cached. Only applies when WithCache is also used.
func WithCacheExclude(prefixes ...string) Option {
return func(h *Handler) {
h.cacheExclude = append(h.cacheExclude, prefixes...)
}
}
// WithMinification enables on-the-fly minification of JavaScript and CSS.
// Minification errors are silently ignored and the original content is served.
func WithMinification(enabled bool) Option {
return func(h *Handler) { h.useMinify = enabled }
}
// WithErrorLogger sets a function that is called whenever an error is handled
// gracefully (e.g. a missing file, a failed minification). The handler always
// recovers and returns a safe HTTP response; this just gives you visibility.
// Example: webassets.WithErrorLogger(func(err error) { slog.Error(err.Error()) })
func WithErrorLogger(fn func(error)) Option {
return func(h *Handler) { h.errLogger = fn }
}
// WithBundle registers a virtual bundle file accessible at /<name>. When
// requested, the source files are read from the handler root directory,
// concatenated in order, and optionally minified.
func WithBundle(name string, files ...string) Option {
return func(h *Handler) {
h.bundles[name] = bundle{files: files}
}
}
// New creates a Handler that serves files from dir.
func New(dir string, opts ...Option) *Handler {
h := &Handler{
dir: dir,
bundles: make(map[string]bundle),
fileCache: make(map[string]cacheEntry),
bundleCache: make(map[string]cacheEntry),
fileServer: http.FileServer(http.Dir(dir)),
}
for _, opt := range opts {
opt(h)
}
if h.useMinify {
m := minify.New()
m.AddFunc("text/css", css.Minify)
m.AddFuncRegexp(regexp.MustCompile(`^(application|text)/(x-)?(java|ecma)script$`), js.Minify)
h.minifier = m
}
return h
}
// ServeHTTP implements http.Handler.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(path.Clean("/"+r.URL.Path), "/")
// Bundles are virtual files; check them before falling through to disk.
if b, ok := h.bundles[name]; ok {
h.serveBundle(w, r, name, b)
return
}
// TODO: If the filename is prefixed with "_" then we by default are going to consider that an internal only file. not available through the webassets endpoint
if strings.HasPrefix(filepath.Base(r.URL.Path), "_") {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
// Dev mode or excluded path: delegate to the standard file server so that
// disk changes are reflected immediately and all HTTP semantics (ETags,
// Range, Last-Modified) are handled automatically.
if !h.useCache || h.isExcluded(r.URL.Path) {
h.fileServer.ServeHTTP(w, r)
return
}
h.serveFile(w, r, name)
}
func (h *Handler) serveFile(w http.ResponseWriter, r *http.Request, name string) {
h.mu.RLock()
entry, ok := h.fileCache[name]
h.mu.RUnlock()
if !ok {
result, err, _ := h.sf.Do("f:"+name, func() (any, error) {
e, err := h.buildFileEntry(name)
if err != nil {
return cacheEntry{}, err
}
h.mu.Lock()
h.fileCache[name] = e
h.mu.Unlock()
return e, nil
})
if err != nil {
h.logError(err)
http.NotFound(w, r)
return
}
entry = result.(cacheEntry)
}
w.Header().Set("Content-Type", entry.contentType)
// TODO(compression): negotiate Content-Encoding here before writing (see cacheEntry TODO).
w.Write(entry.data)
}
func (h *Handler) serveBundle(w http.ResponseWriter, r *http.Request, name string, b bundle) {
if h.useCache {
h.mu.RLock()
entry, ok := h.bundleCache[name]
h.mu.RUnlock()
if ok {
w.Header().Set("Content-Type", entry.contentType)
w.Write(entry.data)
return
}
}
result, err, _ := h.sf.Do("b:"+name, func() (any, error) {
e, err := h.buildBundleEntry(name, b)
if err != nil {
return cacheEntry{}, err
}
if h.useCache {
h.mu.Lock()
h.bundleCache[name] = e
h.mu.Unlock()
}
return e, nil
})
if err != nil {
h.logError(err)
http.Error(w, "bundle build failed", http.StatusInternalServerError)
return
}
entry := result.(cacheEntry)
w.Header().Set("Content-Type", entry.contentType)
// TODO(compression): negotiate Content-Encoding here before writing (see cacheEntry TODO).
w.Write(entry.data)
}
func (h *Handler) buildFileEntry(name string) (cacheEntry, error) {
filePath := filepath.Join(h.dir, filepath.FromSlash(name))
data, err := os.ReadFile(filePath)
if err != nil {
return cacheEntry{}, err
}
ct := contentTypeFor(name)
return cacheEntry{data: h.minifyIfEnabled(ct, data), contentType: ct}, nil
}
func (h *Handler) buildBundleEntry(name string, b bundle) (cacheEntry, error) {
var buf bytes.Buffer
for _, f := range b.files {
data, err := os.ReadFile(filepath.Join(h.dir, filepath.FromSlash(f)))
if err != nil {
return cacheEntry{}, err
}
buf.Write(data)
buf.WriteByte('\n')
}
ct := contentTypeFor(name)
return cacheEntry{data: h.minifyIfEnabled(ct, buf.Bytes()), contentType: ct}, nil
}
func (h *Handler) minifyIfEnabled(contentType string, data []byte) []byte {
if !h.useMinify || h.minifier == nil {
return data
}
mediatype := strings.SplitN(contentType, ";", 2)[0]
minified, err := h.minifier.Bytes(strings.TrimSpace(mediatype), data)
if err != nil {
h.logError(err)
return data
}
return minified
}
func (h *Handler) logError(err error) {
if h.errLogger != nil {
h.errLogger(err)
}
}
func (h *Handler) isExcluded(urlPath string) bool {
for _, prefix := range h.cacheExclude {
if strings.HasPrefix(urlPath, prefix) {
return true
}
}
return false
}
// contentTypeFor returns the MIME type for the given file name based on its
// extension. Common web asset types are hardcoded to avoid OS-level variance
// in mime databases; everything else falls back to mime.TypeByExtension.
func contentTypeFor(name string) string {
switch strings.ToLower(path.Ext(name)) {
case ".js", ".mjs":
return "application/javascript"
case ".css":
return "text/css"
case ".html", ".htm":
return "text/html; charset=utf-8"
case ".json":
return "application/json"
case ".svg":
return "image/svg+xml"
case ".png":
return "image/png"
case ".jpg", ".jpeg":
return "image/jpeg"
case ".gif":
return "image/gif"
case ".webp":
return "image/webp"
case ".ico":
return "image/x-icon"
case ".woff":
return "font/woff"
case ".woff2":
return "font/woff2"
case ".ttf":
return "font/ttf"
case ".txt":
return "text/plain; charset=utf-8"
default:
if ct := mime.TypeByExtension(path.Ext(name)); ct != "" {
return ct
}
return "application/octet-stream"
}
}