MCPcopy
hub / github.com/tdewolff/minify / Write

Method Write

minify.go:321–344  ·  view source on GitHub ↗

Write intercepts any writes to the response writer. The first write will extract the Content-Type as the mediatype. Otherwise it falls back to the RequestURI extension.

(b []byte)

Source from the content-addressed store, hash-verified

319// Write intercepts any writes to the response writer.
320// The first write will extract the Content-Type as the mediatype. Otherwise it falls back to the RequestURI extension.
321func (w *responseWriter) Write(b []byte) (int, error) {
322 if w.z == nil {
323 // first write
324 if mediatype := w.ResponseWriter.Header().Get("Content-Type"); mediatype != "" {
325 w.mediatype = mediatype
326 }
327 if _, params, minifier := w.m.Match(w.mediatype); minifier != nil {
328 pr, pw := io.Pipe()
329 z := &writer{pw, sync.WaitGroup{}, false, nil}
330 z.wg.Add(1)
331 go func() {
332 defer z.wg.Done()
333 defer pr.Close()
334 if err := minifier(w.m, w.ResponseWriter, pr, params); err != nil {
335 z.err = err
336 }
337 }()
338 w.z = z
339 } else {
340 w.z = w.ResponseWriter
341 }
342 }
343 return w.z.Write(b)
344}
345
346// Close must be called when writing has finished. It returns the error from the minifier.
347func (w *responseWriter) Close() error {

Callers 15

TestDataURIFunction · 0.45
MinifyMethod · 0.45
writeMethod · 0.45
writeSpaceAfterIdentMethod · 0.45
writeSemicolonMethod · 0.45
MinifyMethod · 0.45
TestSpecialTagClosingFunction · 0.45
ExampleMinify_writerFunction · 0.45
StringMethod · 0.45
MinifyMethod · 0.45
minifyGrammarMethod · 0.45
minifySelectorsMethod · 0.45

Calls 4

HeaderMethod · 0.80
MatchMethod · 0.80
AddMethod · 0.80
CloseMethod · 0.45

Tested by 3

TestDataURIFunction · 0.36
TestSpecialTagClosingFunction · 0.36
ExampleMinify_writerFunction · 0.36