index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*!
  2. * compression
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * Copyright(c) 2014 Jonathan Ong
  6. * Copyright(c) 2014-2015 Douglas Christopher Wilson
  7. * MIT Licensed
  8. */
  9. 'use strict'
  10. /**
  11. * Module dependencies.
  12. * @private
  13. */
  14. var Negotiator = require('negotiator')
  15. var Buffer = require('safe-buffer').Buffer
  16. var bytes = require('bytes')
  17. var compressible = require('compressible')
  18. var debug = require('debug')('compression')
  19. var onHeaders = require('on-headers')
  20. var vary = require('vary')
  21. var zlib = require('zlib')
  22. /**
  23. * Module exports.
  24. */
  25. module.exports = compression
  26. module.exports.filter = shouldCompress
  27. /**
  28. * @const
  29. * whether current node version has brotli support
  30. */
  31. var hasBrotliSupport = 'createBrotliCompress' in zlib
  32. /**
  33. * Module variables.
  34. * @private
  35. */
  36. var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
  37. var SUPPORTED_ENCODING = hasBrotliSupport ? ['br', 'gzip', 'deflate', 'identity'] : ['gzip', 'deflate', 'identity']
  38. var PREFERRED_ENCODING = hasBrotliSupport ? ['br', 'gzip'] : ['gzip']
  39. var encodingSupported = ['gzip', 'deflate', 'identity', 'br']
  40. /**
  41. * Compress response data with gzip / deflate.
  42. *
  43. * @param {Object} [options]
  44. * @return {Function} middleware
  45. * @public
  46. */
  47. function compression (options) {
  48. var opts = options || {}
  49. var optsBrotli = {}
  50. if (hasBrotliSupport) {
  51. Object.assign(optsBrotli, opts.brotli)
  52. var brotliParams = {}
  53. brotliParams[zlib.constants.BROTLI_PARAM_QUALITY] = 4
  54. // set the default level to a reasonable value with balanced speed/ratio
  55. optsBrotli.params = Object.assign(brotliParams, optsBrotli.params)
  56. }
  57. // options
  58. var filter = opts.filter || shouldCompress
  59. var threshold = bytes.parse(opts.threshold)
  60. var enforceEncoding = opts.enforceEncoding || 'identity'
  61. if (threshold == null) {
  62. threshold = 1024
  63. }
  64. return function compression (req, res, next) {
  65. var ended = false
  66. var length
  67. var listeners = []
  68. var stream
  69. var _end = res.end
  70. var _on = res.on
  71. var _write = res.write
  72. // flush
  73. res.flush = function flush () {
  74. if (stream) {
  75. stream.flush()
  76. }
  77. }
  78. // proxy
  79. res.write = function write (chunk, encoding) {
  80. if (ended) {
  81. return false
  82. }
  83. if (!headersSent(res)) {
  84. this.writeHead(this.statusCode)
  85. }
  86. return stream
  87. ? stream.write(toBuffer(chunk, encoding))
  88. : _write.call(this, chunk, encoding)
  89. }
  90. res.end = function end (chunk, encoding) {
  91. if (ended) {
  92. return false
  93. }
  94. if (!headersSent(res)) {
  95. // estimate the length
  96. if (!this.getHeader('Content-Length')) {
  97. length = chunkLength(chunk, encoding)
  98. }
  99. this.writeHead(this.statusCode)
  100. }
  101. if (!stream) {
  102. return _end.call(this, chunk, encoding)
  103. }
  104. // mark ended
  105. ended = true
  106. // write Buffer for Node.js 0.8
  107. return chunk
  108. ? stream.end(toBuffer(chunk, encoding))
  109. : stream.end()
  110. }
  111. res.on = function on (type, listener) {
  112. if (!listeners || type !== 'drain') {
  113. return _on.call(this, type, listener)
  114. }
  115. if (stream) {
  116. return stream.on(type, listener)
  117. }
  118. // buffer listeners for future stream
  119. listeners.push([type, listener])
  120. return this
  121. }
  122. function nocompress (msg) {
  123. debug('no compression: %s', msg)
  124. addListeners(res, _on, listeners)
  125. listeners = null
  126. }
  127. onHeaders(res, function onResponseHeaders () {
  128. // determine if request is filtered
  129. if (!filter(req, res)) {
  130. nocompress('filtered')
  131. return
  132. }
  133. // determine if the entity should be transformed
  134. if (!shouldTransform(req, res)) {
  135. nocompress('no transform')
  136. return
  137. }
  138. // vary
  139. vary(res, 'Accept-Encoding')
  140. // content-length below threshold
  141. if (Number(res.getHeader('Content-Length')) < threshold || length < threshold) {
  142. nocompress('size below threshold')
  143. return
  144. }
  145. var encoding = res.getHeader('Content-Encoding') || 'identity'
  146. // already encoded
  147. if (encoding !== 'identity') {
  148. nocompress('already encoded')
  149. return
  150. }
  151. // head
  152. if (req.method === 'HEAD') {
  153. nocompress('HEAD request')
  154. return
  155. }
  156. // compression method
  157. var negotiator = new Negotiator(req)
  158. var method = negotiator.encoding(SUPPORTED_ENCODING, PREFERRED_ENCODING)
  159. // if no method is found, use the default encoding
  160. if (!req.headers['accept-encoding'] && encodingSupported.indexOf(enforceEncoding) !== -1) {
  161. method = enforceEncoding
  162. }
  163. // negotiation failed
  164. if (!method || method === 'identity') {
  165. nocompress('not acceptable')
  166. return
  167. }
  168. // compression stream
  169. debug('%s compression', method)
  170. stream = method === 'gzip'
  171. ? zlib.createGzip(opts)
  172. : method === 'br'
  173. ? zlib.createBrotliCompress(optsBrotli)
  174. : zlib.createDeflate(opts)
  175. // add buffered listeners to stream
  176. addListeners(stream, stream.on, listeners)
  177. // header fields
  178. res.setHeader('Content-Encoding', method)
  179. res.removeHeader('Content-Length')
  180. // compression
  181. stream.on('data', function onStreamData (chunk) {
  182. if (_write.call(res, chunk) === false) {
  183. stream.pause()
  184. }
  185. })
  186. stream.on('end', function onStreamEnd () {
  187. _end.call(res)
  188. })
  189. _on.call(res, 'drain', function onResponseDrain () {
  190. stream.resume()
  191. })
  192. })
  193. next()
  194. }
  195. }
  196. /**
  197. * Add bufferred listeners to stream
  198. * @private
  199. */
  200. function addListeners (stream, on, listeners) {
  201. for (var i = 0; i < listeners.length; i++) {
  202. on.apply(stream, listeners[i])
  203. }
  204. }
  205. /**
  206. * Get the length of a given chunk
  207. */
  208. function chunkLength (chunk, encoding) {
  209. if (!chunk) {
  210. return 0
  211. }
  212. return Buffer.isBuffer(chunk)
  213. ? chunk.length
  214. : Buffer.byteLength(chunk, encoding)
  215. }
  216. /**
  217. * Default filter function.
  218. * @private
  219. */
  220. function shouldCompress (req, res) {
  221. var type = res.getHeader('Content-Type')
  222. if (type === undefined || !compressible(type)) {
  223. debug('%s not compressible', type)
  224. return false
  225. }
  226. return true
  227. }
  228. /**
  229. * Determine if the entity should be transformed.
  230. * @private
  231. */
  232. function shouldTransform (req, res) {
  233. var cacheControl = res.getHeader('Cache-Control')
  234. // Don't compress for Cache-Control: no-transform
  235. // https://tools.ietf.org/html/rfc7234#section-5.2.2.4
  236. return !cacheControl ||
  237. !cacheControlNoTransformRegExp.test(cacheControl)
  238. }
  239. /**
  240. * Coerce arguments to Buffer
  241. * @private
  242. */
  243. function toBuffer (chunk, encoding) {
  244. return Buffer.isBuffer(chunk)
  245. ? chunk
  246. : Buffer.from(chunk, encoding)
  247. }
  248. /**
  249. * Determine if the response headers have been sent.
  250. *
  251. * @param {object} res
  252. * @returns {boolean}
  253. * @private
  254. */
  255. function headersSent (res) {
  256. return typeof res.headersSent !== 'boolean'
  257. ? Boolean(res._header)
  258. : res.headersSent
  259. }