[
  {
    "path": ".gitignore",
    "content": "/.bundle/\n/.yardoc\n/_yardoc/\n/coverage/\n/doc/\n/pkg/\n/spec/reports/\n/tmp/\n\n# rspec failure tracking\n.rspec_status\n"
  },
  {
    "path": ".rspec",
    "content": "--colour\n--format progress\n--tag ~slow\n"
  },
  {
    "path": "Gemfile",
    "content": "# frozen_string_literal: true\n\nsource 'https://rubygems.org'\n\ngemspec\n\ngem 'rack', '>=1.4.5'\n\ngroup :development do\n  gem 'bundler'\n  gem 'rspec'\n  gem 'rails', '~>6.1'\nend\n"
  },
  {
    "path": "LICENSE.txt",
    "content": "Copyright (c) 2012 Roman Shterenzon\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# heroku-deflater\n\nA simple rack middleware that enables compressing of your sprockets and\nwebpacker assets and application responses on Heroku, while not wasting CPU\ncycles on pointlessly compressing images and other binary responses.\n\nIt also includes code from https://github.com/mattolson/heroku_rails_deflate\n\nBefore serving a file from disk to a gzip-enabled client, it will look for a\nprecompressed file in the same location that ends in \".gz\". The purpose is to\navoid compressing the same file each time it is requested.\n\n## Installing\n\nAdd to your Gemfile:\n\n    gem 'heroku-deflater', :group => :production\n\n\n## Contributing to heroku-deflater\n\n* Check out the latest master to make sure the feature hasn't been implemented\nor the bug hasn't been fixed yet.\n* Check out the issue tracker to make sure someone already hasn't requested it\nand/or contributed it.\n* Fork the project.\n* Start a feature/bugfix branch.\n* Commit and push until you are happy with your contribution.\n* Make sure to add tests for it. This is important so I don't break it in a\nfuture version unintentionally.\n* Please try not to mess with the Rakefile, version, or history. If you want to\nhave your own version, or is otherwise necessary, that is fine, but please\nisolate to its own commit so I can cherry-pick around it.\n\n## Copyright\n\nCopyright (c) 2012 Roman Shterenzon. See LICENSE.txt for further details.\n\n[1]: https://github.com/rack/rack/issues/349\n"
  },
  {
    "path": "Rakefile",
    "content": "# frozen_string_literal: true\n\nrequire 'bundler/gem_tasks'\nbegin\n  require 'rspec/core/rake_task'\n  RSpec::Core::RakeTask.new(:spec)\nrescue LoadError\nend\n\ntask default: :spec\n\nrequire 'rdoc/task'\nRake::RDocTask.new do |rdoc|\n  version = File.exist?('VERSION') ? File.read('VERSION') : ''\n\n  rdoc.rdoc_dir = 'rdoc'\n  rdoc.title = \"heroku-deflater #{version}\"\n  rdoc.rdoc_files.include('README*')\n  rdoc.rdoc_files.include('lib/**/*.rb')\nend\n"
  },
  {
    "path": "VERSION",
    "content": "0.6.3"
  },
  {
    "path": "heroku-deflater.gemspec",
    "content": "# frozen_string_literal: true\n\nrequire_relative 'lib/heroku-deflater/version'\n\nGem::Specification.new do |s|\n  s.name = 'heroku-deflater'\n  s.version = HerokuDeflater::VERSION\n  s.authors = ['Roman Shterenzon']\n  s.email = 'romanbsd@yahoo.com'\n\n  s.summary = 'Deflate assets on heroku'\n  s.description = 'Deflate assets on Heroku'\n  s.homepage = 'https://github.com/romanbsd/heroku-deflater'\n  s.required_ruby_version = Gem::Requirement.new('>= 2.3.0')\n  s.require_paths = ['lib']\n  s.extra_rdoc_files = [\n    'LICENSE.txt',\n    'README.md'\n  ]\n  s.licenses = ['MIT']\n  s.files = Dir.chdir(File.expand_path(__dir__)) do\n    `git ls-files -z`.split(\"\\x0\").reject { |f| f.match(%r{^(test|spec|features)/}) }\n  end\nend\n"
  },
  {
    "path": "lib/heroku-deflater/cache_control_manager.rb",
    "content": "module HerokuDeflater\n  class CacheControlManager\n    DEFAULT_MAX_AGE = '86400'.freeze\n    attr_reader :app, :max_age\n\n    def initialize(app)\n      @app = app\n      @max_age = DEFAULT_MAX_AGE\n    end\n\n    def setup_max_age(max_age)\n      @max_age = max_age\n    end\n\n    def cache_control_headers\n      if HerokuDeflater.rails_version_5?\n        headers = app.config.public_file_server.headers ||= {}\n        headers['Cache-Control'] ||= \"public, max-age=#{max_age}\"\n        headers\n      else\n        app.config.static_cache_control ||= \"public, max-age=#{max_age}\"\n      end\n    end\n  end\nend\n"
  },
  {
    "path": "lib/heroku-deflater/railtie.rb",
    "content": "require 'rack/deflater'\nrequire 'heroku-deflater/skip_binary'\nrequire 'heroku-deflater/serve_zipped_assets'\nrequire 'heroku-deflater/cache_control_manager'\n\nmodule HerokuDeflater\n  class Railtie < Rails::Railtie\n    initializer 'heroku_deflater.configure_rails_initialization' do |app|\n      app.middleware.insert_before ActionDispatch::Static, Rack::Deflater\n      app.middleware.insert_before ActionDispatch::Static, HerokuDeflater::SkipBinary\n\n      assets_prefix = app.config.assets.prefix if app.config.respond_to?(:assets)\n\n      if Object.const_defined?(:Webpacker)\n        public_path = Webpacker.config.public_path.to_s\n        public_output_path = Webpacker.config.public_output_path.to_s\n        webpacker_prefix = public_output_path.split(public_path).last\n      end\n\n      if assets_prefix || webpacker_prefix\n        paths = [assets_prefix, webpacker_prefix].compact\n        app.middleware.insert_before Rack::Deflater, HerokuDeflater::ServeZippedAssets,\n          app.paths['public'].first, paths, self.class.cache_control_manager(app)\n      end\n    end\n\n    def self.cache_control_manager(app)\n      @_cache_control_manager ||= CacheControlManager.new(app)\n    end\n\n    # Set default Cache-Control headers to 24 hours.\n    # The configuration block in config/application.rb overrides this.\n    config.before_initialize do |app|\n      cache_control = cache_control_manager(app)\n      cache_control.setup_max_age(86400)\n    end\n  end\nend\n"
  },
  {
    "path": "lib/heroku-deflater/serve_zipped_assets.rb",
    "content": "require 'action_controller'\nrequire 'action_dispatch/middleware/static'\n\nif Rails::VERSION::MAJOR < 7\n  # Deprecated in Rails 7.0, and removed in 7.1\n  require 'active_support/core_ext/uri'\nend\n\n# Adapted from https://gist.github.com/guyboltonking/2152663\n#\n# Taken from: https://github.com/mattolson/heroku_rails_deflate\n#\nmodule HerokuDeflater\n  class ServeZippedAssets\n    def initialize(app, root, paths, cache_control_manager)\n      @app = app\n      @paths = paths.map { |p| p.chomp('/') + '/' }\n\n      if HerokuDeflater.rails_version_5?\n        @file_handler = ActionDispatch::FileHandler.new(root, headers: cache_control_manager.cache_control_headers)\n      else\n        @file_handler = ActionDispatch::FileHandler.new(root, cache_control_manager.cache_control_headers)\n      end\n    end\n\n    def call(env)\n      if env['REQUEST_METHOD'] == 'GET'\n        request = Rack::Request.new(env)\n        encoding = Rack::Utils.select_best_encoding(%w(gzip identity), request.accept_encoding)\n\n        if encoding == 'gzip'\n          # See if gzipped version exists in assets directory\n          compressed_path = env['PATH_INFO'] + '.gz'\n\n          if compressed_path.start_with?(*@paths) && (match = @file_handler.match?(compressed_path))\n            # Get the FileHandler to serve up the gzipped file, then strip the .gz suffix\n            env['PATH_INFO'] = match\n            status, headers, body = @file_handler.call(env)\n            env['PATH_INFO'].chomp!('.gz')\n\n            # Set the Vary HTTP header.\n            vary = headers['Vary'].to_s.split(',').map(&:strip)\n\n            unless vary.include?('*') || vary.include?('Accept-Encoding')\n              headers['Vary'] = vary.push('Accept-Encoding').join(',')\n            end\n\n            # Add encoding and type\n            headers['Content-Encoding'] = 'gzip'\n            headers['Content-Type'] = Rack::Mime.mime_type(File.extname(env['PATH_INFO']), 'text/plain')\n\n            body.close if body.respond_to?(:close)\n            return [status, headers, body]\n          end\n        end\n      end\n\n      @app.call(env)\n    end\n  end\nend\n"
  },
  {
    "path": "lib/heroku-deflater/skip_binary.rb",
    "content": "module HerokuDeflater\n  # Add a no-transform Cache-Control header to binary types, so they won't get gzipped\n  class SkipBinary\n    def initialize(app)\n      @app = app\n    end\n\n    WHITELIST = [\n      %r{^text/},\n      'application/javascript',\n      %r{^application/.*?json},\n      %r{^application/.*?xml},\n      'application/x-font-ttf',\n      'font/opentype',\n      'image/svg+xml'\n    ].freeze\n\n    def call(env)\n      status, headers, body = @app.call(env)\n      headers = Rack::Utils::HeaderHash.new(headers) if defined?(Rack::Utils::HeaderHash)\n      content_type = headers['content-type']\n      cache_control = headers['cache-control'].to_s.downcase\n\n      unless cache_control.include?('no-transform') || WHITELIST.any? { |type| type === content_type }\n        if cache_control.empty?\n          headers['cache-control'] = 'no-transform'\n        else\n          headers['cache-control'] += ', no-transform'\n        end\n      end\n\n      body.close if body.respond_to?(:close)\n      [status, headers.to_h, body]\n    end\n  end\nend\n"
  },
  {
    "path": "lib/heroku-deflater/version.rb",
    "content": "# frozen_string_literal: true\n\nmodule HerokuDeflater\n  VERSION = '0.7.0'\nend\n"
  },
  {
    "path": "lib/heroku-deflater.rb",
    "content": "require 'heroku-deflater/railtie'\n\nmodule HerokuDeflater\n  def self.rails_version_5?\n    Rails::VERSION::MAJOR >= 5\n  end\nend\n"
  },
  {
    "path": "spec/cache_control_manager_spec.rb",
    "content": "require 'spec_helper'\n\ndescribe HerokuDeflater::CacheControlManager do\n  subject { described_class.new(app) }\n  before { subject.setup_max_age(86400) }\n\n  context 'Rails 4.x and below' do\n    let(:app) { Rails4App.new }\n\n    before do\n      allow(HerokuDeflater).to receive(:rails_version_5?).and_return(false)\n      subject.cache_control_headers\n    end\n\n    it 'sets max age for static_cache_control config option' do\n      expect(app.config.static_cache_control).to eq('public, max-age=86400')\n    end\n\n    it 'cache_control_headers returns cache control option' do\n      expect(subject.cache_control_headers).to eq('public, max-age=86400')\n    end\n  end\n\n  context 'Rails 5' do\n    let(:app) { Rails5App.new }\n\n    before do\n      allow(HerokuDeflater).to receive(:rails_version_5?).and_return(true)\n      subject.cache_control_headers\n    end\n\n    it 'sets max age for public_file_server config option' do\n      expect(app.config.public_file_server.headers['Cache-Control']).to eq('public, max-age=86400')\n    end\n\n    it 'cache_control_headers returns hash cache control headers' do\n      expect(subject.cache_control_headers).to eq({'Cache-Control' =>'public, max-age=86400'})\n    end\n  end\nend\n"
  },
  {
    "path": "spec/fixtures/assets/spec.js",
    "content": "// Fixture file\n(function() {\n  console.log(\"Hello world\");\n  console.log(\"Hello world\");\n  console.log(\"Hello world\");\n})();\n"
  },
  {
    "path": "spec/fixtures/assets/spec2.js",
    "content": "// Fixture file\n(function() {\n  console.log(\"Hello world\");\n})();\n"
  },
  {
    "path": "spec/fixtures/packs/spec.js",
    "content": "// Fixture file\n(function() {\n  console.log(\"Hello world\");\n  console.log(\"Hello world\");\n  console.log(\"Hello world\");\n})();\n"
  },
  {
    "path": "spec/fixtures/packs/spec2.js",
    "content": "// Fixture file\n(function() {\n  console.log(\"Hello world\");\n})();\n"
  },
  {
    "path": "spec/serve_zipped_assets_spec.rb",
    "content": "require 'spec_helper'\n\ndescribe HerokuDeflater::ServeZippedAssets do\n  shared_examples_for 'ServeZippedAssets' do\n    %w(assets packs).each do |path|\n      it 'does nothing for clients which do not want gzip' do\n        status, headers, body = process(\"/#{path}/spec.js\", nil)\n        expect(status).to eq(404)\n      end\n\n      it 'handles the pre-gzipped assets' do\n        status, headers, body = process(\"/#{path}/spec.js\")\n        expect(status).to eq(200)\n      end\n\n      it 'has correct content type' do\n        status, headers, body = process(\"/#{path}/spec.js\")\n        expect(headers['Content-Type']).to eq('application/javascript')\n      end\n\n      it 'has correct content encoding' do\n        status, headers, body = process(\"/#{path}/spec.js\")\n        expect(headers['Content-Encoding']).to eq('gzip')\n      end\n\n      it 'has correct content length' do\n        status, headers, body = process(\"/#{path}/spec.js\")\n        expect(headers['Content-Length']).to eq('86')\n      end\n\n      it 'has correct cache control' do\n        status, headers, body = process(\"/#{path}/spec.js\")\n        expect(headers['Cache-Control']).to eq('public, max-age=86400')\n      end\n\n      it 'does not serve non-gzipped assets' do\n        status, headers, body = process(\"/#{path}/spec2.js\")\n        expect(status).to eq(404)\n      end\n\n      it 'does not serve anything from non-asset directories' do\n        status, headers, body = process(\"/non-#{path}/spec.js\")\n        expect(status).to eq(404)\n      end\n    end\n  end\n\n  describe 'Rais 4.x' do\n    let(:app) { Rails4App.new }\n\n    before do\n      allow(HerokuDeflater).to receive(:rails_version_5?) { false }\n    end\n\n    # FIXME: It will work only with gemfiles\n    # it_behaves_like 'ServeZippedAssets'\n  end\n\n  describe 'Rais 5.x' do\n    let(:app) { Rails5App.new }\n\n    before do\n      allow(HerokuDeflater).to receive(:rails_version_5?) { true }\n    end\n\n    it_behaves_like 'ServeZippedAssets'\n  end\n\n  def process(path, accept_encoding = 'compress, gzip, deflate')\n    env = Rack::MockRequest.env_for(path)\n    env['HTTP_ACCEPT_ENCODING'] = accept_encoding\n    middleware.call(env)\n  end\n\n  def middleware\n    @middleware ||= begin\n      root_path = File.expand_path('../fixtures', __FILE__)\n      cache_control_manager = HerokuDeflater::CacheControlManager.new(app)\n      mock = lambda { |env| [404, {'X-Cascade' => 'pass'}, []] }\n      described_class.new(mock, root_path, ['/assets', '/packs'], cache_control_manager)\n    end\n  end\nend\n"
  },
  {
    "path": "spec/skip_binary_spec.rb",
    "content": "require 'spec_helper'\n\ndescribe HerokuDeflater::SkipBinary do\n  let(:env) { Rack::MockRequest.env_for('/') }\n\n  def app_returning_type(type, headers = {})\n    lambda { |env| [200, {'Content-Type' => type}.merge(headers), ['']] }\n  end\n\n  def process(type, headers = {})\n    described_class.new(app_returning_type(type, headers)).call(env)[1]\n  end\n\n  it \"forbids compressing of binary types\" do\n    %w[application/gzip application/pdf image/jpeg].each do |type|\n      headers = process(type)\n      expect(headers['Cache-Control'].to_s).to include('no-transform')\n    end\n  end\n\n  it \"allows compressing of text types\" do\n    %w[text/plain text/html application/json application/javascript\n    application/rss+xml application/vnd.api+json].each do |type|\n      headers = process(type)\n      expect(headers['Cache-Control'].to_s).not_to include('no-transform')\n    end\n  end\n\n  it \"adds to existing headers\" do\n    headers = process('image/gif', 'Cache-Control' => 'public')\n    expect(headers['Cache-Control']).to eq('public, no-transform')\n  end\n\n  it \"doesn't add 'no-transform' if it's already present\" do\n    headers = process('image/gif', 'Cache-Control' => 'public, no-transform')\n    expect(headers['Cache-Control']).to eq('public, no-transform')\n  end\nend\n"
  },
  {
    "path": "spec/spec_helper.rb",
    "content": "require 'rails'\nrequire 'rack/mock'\nrequire 'rack/static'\n\nrequire 'heroku-deflater'\nrequire 'heroku-deflater/serve_zipped_assets'\nrequire 'heroku-deflater/cache_control_manager'\nrequire 'heroku-deflater/skip_binary'\n\nclass Rails5App\n  def config\n    @_config ||= Config.new\n  end\n\n  class Config\n    attr_accessor :headers\n\n    def initialize\n      @headers = {}\n    end\n\n    def public_file_server\n      self\n    end\n  end\nend\n\nclass Rails4App\n  def config\n    @_config ||= Config.new\n  end\n\n  class Config\n    attr_accessor :static_cache_control\n  end\nend\n"
  }
]