minify.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Package minify relates MIME type to minifiers. Several minifiers are provided in the subpackages.
  2. package minify
  3. import (
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "mime"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "os/exec"
  15. "path"
  16. "regexp"
  17. "strings"
  18. "sync"
  19. "github.com/tdewolff/parse/v2"
  20. "github.com/tdewolff/parse/v2/buffer"
  21. )
  22. // Warning is used to report usage warnings such as using a deprecated feature
  23. var Warning = log.New(os.Stderr, "WARNING: ", 0)
  24. // ErrNotExist is returned when no minifier exists for a given mimetype.
  25. var ErrNotExist = errors.New("minifier does not exist for mimetype")
  26. // ErrClosedWriter is returned when writing to a closed writer.
  27. var ErrClosedWriter = errors.New("write on closed writer")
  28. ////////////////////////////////////////////////////////////////
  29. // MinifierFunc is a function that implements Minifer.
  30. type MinifierFunc func(*M, io.Writer, io.Reader, map[string]string) error
  31. // Minify calls f(m, w, r, params)
  32. func (f MinifierFunc) Minify(m *M, w io.Writer, r io.Reader, params map[string]string) error {
  33. return f(m, w, r, params)
  34. }
  35. // Minifier is the interface for minifiers.
  36. // The *M parameter is used for minifying embedded resources, such as JS within HTML.
  37. type Minifier interface {
  38. Minify(*M, io.Writer, io.Reader, map[string]string) error
  39. }
  40. ////////////////////////////////////////////////////////////////
  41. type patternMinifier struct {
  42. pattern *regexp.Regexp
  43. Minifier
  44. }
  45. type cmdMinifier struct {
  46. cmd *exec.Cmd
  47. }
  48. var cmdArgExtension = regexp.MustCompile(`^\.[0-9a-zA-Z]+`)
  49. func (c *cmdMinifier) Minify(_ *M, w io.Writer, r io.Reader, _ map[string]string) error {
  50. cmd := &exec.Cmd{}
  51. *cmd = *c.cmd // concurrency safety
  52. var in, out *os.File
  53. for i, arg := range cmd.Args {
  54. if j := strings.Index(arg, "$in"); j != -1 {
  55. var err error
  56. ext := cmdArgExtension.FindString(arg[j+3:])
  57. if in, err = ioutil.TempFile("", "minify-in-*"+ext); err != nil {
  58. return err
  59. }
  60. cmd.Args[i] = arg[:j] + in.Name() + arg[j+3+len(ext):]
  61. } else if j := strings.Index(arg, "$out"); j != -1 {
  62. var err error
  63. ext := cmdArgExtension.FindString(arg[j+4:])
  64. if out, err = ioutil.TempFile("", "minify-out-*"+ext); err != nil {
  65. return err
  66. }
  67. cmd.Args[i] = arg[:j] + out.Name() + arg[j+4+len(ext):]
  68. }
  69. }
  70. if in == nil {
  71. cmd.Stdin = r
  72. } else if _, err := io.Copy(in, r); err != nil {
  73. return err
  74. }
  75. if out == nil {
  76. cmd.Stdout = w
  77. } else {
  78. defer io.Copy(w, out)
  79. }
  80. stderr := &bytes.Buffer{}
  81. cmd.Stderr = stderr
  82. err := cmd.Run()
  83. if _, ok := err.(*exec.ExitError); ok {
  84. if stderr.Len() != 0 {
  85. err = fmt.Errorf("%s", stderr.String())
  86. }
  87. err = fmt.Errorf("command %s failed: %w", cmd.Path, err)
  88. }
  89. return err
  90. }
  91. ////////////////////////////////////////////////////////////////
  92. // M holds a map of mimetype => function to allow recursive minifier calls of the minifier functions.
  93. type M struct {
  94. mutex sync.RWMutex
  95. literal map[string]Minifier
  96. pattern []patternMinifier
  97. URL *url.URL
  98. }
  99. // New returns a new M.
  100. func New() *M {
  101. return &M{
  102. sync.RWMutex{},
  103. map[string]Minifier{},
  104. []patternMinifier{},
  105. nil,
  106. }
  107. }
  108. // Add adds a minifier to the mimetype => function map (unsafe for concurrent use).
  109. func (m *M) Add(mimetype string, minifier Minifier) {
  110. m.mutex.Lock()
  111. m.literal[mimetype] = minifier
  112. m.mutex.Unlock()
  113. }
  114. // AddFunc adds a minify function to the mimetype => function map (unsafe for concurrent use).
  115. func (m *M) AddFunc(mimetype string, minifier MinifierFunc) {
  116. m.mutex.Lock()
  117. m.literal[mimetype] = minifier
  118. m.mutex.Unlock()
  119. }
  120. // AddRegexp adds a minifier to the mimetype => function map (unsafe for concurrent use).
  121. func (m *M) AddRegexp(pattern *regexp.Regexp, minifier Minifier) {
  122. m.mutex.Lock()
  123. m.pattern = append(m.pattern, patternMinifier{pattern, minifier})
  124. m.mutex.Unlock()
  125. }
  126. // AddFuncRegexp adds a minify function to the mimetype => function map (unsafe for concurrent use).
  127. func (m *M) AddFuncRegexp(pattern *regexp.Regexp, minifier MinifierFunc) {
  128. m.mutex.Lock()
  129. m.pattern = append(m.pattern, patternMinifier{pattern, minifier})
  130. m.mutex.Unlock()
  131. }
  132. // AddCmd adds a minify function to the mimetype => function map (unsafe for concurrent use) that executes a command to process the minification.
  133. // It allows the use of external tools like ClosureCompiler, UglifyCSS, etc. for a specific mimetype.
  134. func (m *M) AddCmd(mimetype string, cmd *exec.Cmd) {
  135. m.mutex.Lock()
  136. m.literal[mimetype] = &cmdMinifier{cmd}
  137. m.mutex.Unlock()
  138. }
  139. // AddCmdRegexp adds a minify function to the mimetype => function map (unsafe for concurrent use) that executes a command to process the minification.
  140. // It allows the use of external tools like ClosureCompiler, UglifyCSS, etc. for a specific mimetype regular expression.
  141. func (m *M) AddCmdRegexp(pattern *regexp.Regexp, cmd *exec.Cmd) {
  142. m.mutex.Lock()
  143. m.pattern = append(m.pattern, patternMinifier{pattern, &cmdMinifier{cmd}})
  144. m.mutex.Unlock()
  145. }
  146. // Match returns the pattern and minifier that gets matched with the mediatype.
  147. // It returns nil when no matching minifier exists.
  148. // It has the same matching algorithm as Minify.
  149. func (m *M) Match(mediatype string) (string, map[string]string, MinifierFunc) {
  150. m.mutex.RLock()
  151. defer m.mutex.RUnlock()
  152. mimetype, params := parse.Mediatype([]byte(mediatype))
  153. if minifier, ok := m.literal[string(mimetype)]; ok { // string conversion is optimized away
  154. return string(mimetype), params, minifier.Minify
  155. }
  156. for _, minifier := range m.pattern {
  157. if minifier.pattern.Match(mimetype) {
  158. return minifier.pattern.String(), params, minifier.Minify
  159. }
  160. }
  161. return string(mimetype), params, nil
  162. }
  163. // Minify minifies the content of a Reader and writes it to a Writer (safe for concurrent use).
  164. // An error is returned when no such mimetype exists (ErrNotExist) or when an error occurred in the minifier function.
  165. // Mediatype may take the form of 'text/plain', 'text/*', '*/*' or 'text/plain; charset=UTF-8; version=2.0'.
  166. func (m *M) Minify(mediatype string, w io.Writer, r io.Reader) error {
  167. mimetype, params := parse.Mediatype([]byte(mediatype))
  168. return m.MinifyMimetype(mimetype, w, r, params)
  169. }
  170. // MinifyMimetype minifies the content of a Reader and writes it to a Writer (safe for concurrent use).
  171. // It is a lower level version of Minify and requires the mediatype to be split up into mimetype and parameters.
  172. // It is mostly used internally by minifiers because it is faster (no need to convert a byte-slice to string and vice versa).
  173. func (m *M) MinifyMimetype(mimetype []byte, w io.Writer, r io.Reader, params map[string]string) error {
  174. m.mutex.RLock()
  175. defer m.mutex.RUnlock()
  176. if minifier, ok := m.literal[string(mimetype)]; ok { // string conversion is optimized away
  177. return minifier.Minify(m, w, r, params)
  178. }
  179. for _, minifier := range m.pattern {
  180. if minifier.pattern.Match(mimetype) {
  181. return minifier.Minify(m, w, r, params)
  182. }
  183. }
  184. return ErrNotExist
  185. }
  186. // Bytes minifies an array of bytes (safe for concurrent use). When an error occurs it return the original array and the error.
  187. // It returns an error when no such mimetype exists (ErrNotExist) or any error occurred in the minifier function.
  188. func (m *M) Bytes(mediatype string, v []byte) ([]byte, error) {
  189. out := buffer.NewWriter(make([]byte, 0, len(v)))
  190. if err := m.Minify(mediatype, out, buffer.NewReader(v)); err != nil {
  191. return v, err
  192. }
  193. return out.Bytes(), nil
  194. }
  195. // String minifies a string (safe for concurrent use). When an error occurs it return the original string and the error.
  196. // It returns an error when no such mimetype exists (ErrNotExist) or any error occurred in the minifier function.
  197. func (m *M) String(mediatype string, v string) (string, error) {
  198. out := buffer.NewWriter(make([]byte, 0, len(v)))
  199. if err := m.Minify(mediatype, out, buffer.NewReader([]byte(v))); err != nil {
  200. return v, err
  201. }
  202. return string(out.Bytes()), nil
  203. }
  204. // Reader wraps a Reader interface and minifies the stream.
  205. // Errors from the minifier are returned by the reader.
  206. func (m *M) Reader(mediatype string, r io.Reader) io.Reader {
  207. pr, pw := io.Pipe()
  208. go func() {
  209. if err := m.Minify(mediatype, pw, r); err != nil {
  210. pw.CloseWithError(err)
  211. } else {
  212. pw.Close()
  213. }
  214. }()
  215. return pr
  216. }
  217. // writer makes sure that errors from the minifier are passed down through Close (can be blocking).
  218. type writer struct {
  219. pw *io.PipeWriter
  220. wg sync.WaitGroup
  221. err error
  222. closed bool
  223. }
  224. // Write intercepts any writes to the writer.
  225. func (w *writer) Write(b []byte) (int, error) {
  226. if w.closed {
  227. return 0, ErrClosedWriter
  228. }
  229. n, err := w.pw.Write(b)
  230. if w.err != nil {
  231. err = w.err
  232. }
  233. return n, err
  234. }
  235. // Close must be called when writing has finished. It returns the error from the minifier.
  236. func (w *writer) Close() error {
  237. if !w.closed {
  238. w.pw.Close()
  239. w.wg.Wait()
  240. w.closed = true
  241. }
  242. return w.err
  243. }
  244. // Writer wraps a Writer interface and minifies the stream.
  245. // Errors from the minifier are returned by Close on the writer.
  246. // The writer must be closed explicitly.
  247. func (m *M) Writer(mediatype string, w io.Writer) io.WriteCloser {
  248. pr, pw := io.Pipe()
  249. mw := &writer{pw, sync.WaitGroup{}, nil, false}
  250. mw.wg.Add(1)
  251. go func() {
  252. defer mw.wg.Done()
  253. if err := m.Minify(mediatype, w, pr); err != nil {
  254. mw.err = err
  255. }
  256. pr.Close()
  257. }()
  258. return mw
  259. }
  260. // responseWriter wraps an http.ResponseWriter and makes sure that errors from the minifier are passed down through Close (can be blocking).
  261. // All writes to the response writer are intercepted and minified on the fly.
  262. // http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ...
  263. type responseWriter struct {
  264. http.ResponseWriter
  265. writer *writer
  266. m *M
  267. mediatype string
  268. }
  269. // WriteHeader intercepts any header writes and removes the Content-Length header.
  270. func (w *responseWriter) WriteHeader(status int) {
  271. w.ResponseWriter.Header().Del("Content-Length")
  272. w.ResponseWriter.WriteHeader(status)
  273. }
  274. // Write intercepts any writes to the response writer.
  275. // The first write will extract the Content-Type as the mediatype. Otherwise it falls back to the RequestURI extension.
  276. func (w *responseWriter) Write(b []byte) (int, error) {
  277. if w.writer == nil {
  278. // first write
  279. if mediatype := w.ResponseWriter.Header().Get("Content-Type"); mediatype != "" {
  280. w.mediatype = mediatype
  281. }
  282. w.writer = w.m.Writer(w.mediatype, w.ResponseWriter).(*writer)
  283. }
  284. return w.writer.Write(b)
  285. }
  286. // Close must be called when writing has finished. It returns the error from the minifier.
  287. func (w *responseWriter) Close() error {
  288. if w.writer != nil {
  289. return w.writer.Close()
  290. }
  291. return nil
  292. }
  293. // ResponseWriter minifies any writes to the http.ResponseWriter.
  294. // http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ...
  295. // Minification might be slower than just sending the original file! Caching is advised.
  296. func (m *M) ResponseWriter(w http.ResponseWriter, r *http.Request) *responseWriter {
  297. mediatype := mime.TypeByExtension(path.Ext(r.RequestURI))
  298. return &responseWriter{w, nil, m, mediatype}
  299. }
  300. // Middleware provides a middleware function that minifies content on the fly by intercepting writes to http.ResponseWriter.
  301. // http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ...
  302. // Minification might be slower than just sending the original file! Caching is advised.
  303. func (m *M) Middleware(next http.Handler) http.Handler {
  304. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  305. mw := m.ResponseWriter(w, r)
  306. next.ServeHTTP(mw, r)
  307. mw.Close()
  308. })
  309. }
  310. // MiddlewareWithError provides a middleware function that minifies content on the fly by intercepting writes to http.ResponseWriter. The error function allows handling minification errors.
  311. // http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ...
  312. // Minification might be slower than just sending the original file! Caching is advised.
  313. func (m *M) MiddlewareWithError(next http.Handler, errorFunc func(w http.ResponseWriter, r *http.Request, err error)) http.Handler {
  314. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  315. mw := m.ResponseWriter(w, r)
  316. next.ServeHTTP(mw, r)
  317. if err := mw.Close(); err != nil {
  318. errorFunc(w, r, err)
  319. return
  320. }
  321. })
  322. }