/**
 * Http compression router
 * @author tala
 * 2018-05-23
 */

'use strict'

const compression = require('compression')
class HttpCompression {
  constructor(policyRouter) {
    this._policyRouter = policyRouter
  }

  init() {
    let self = this

    // compression
    self._policyRouter.use('/', compression({
      filter: function(req, res) {
        if (req.headers['x-no-compression']) {
          // don't compress responses with this request header
          return false;
        }
        // fallback to standard filter function
        return compression.filter(req, res);
      }
    }), {
      name: 'httpCompression',
    })

    return self
  }
}

module.exports = HttpCompression
