heroku-deflater 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.md +30 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/heroku-deflater.gemspec +53 -0
- data/lib/heroku-deflater.rb +1 -0
- data/lib/heroku-deflater/deflater.rb +105 -0
- data/lib/heroku-deflater/railtie.rb +12 -0
- data/lib/heroku-deflater/skip_images.rb +22 -0
- metadata +93 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.8.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rdoc
|
10
|
+
json (1.7.5)
|
11
|
+
rake (10.0.3)
|
12
|
+
rdoc (3.12)
|
13
|
+
json (~> 1.4)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
bundler
|
20
|
+
jeweler
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Roman Shterenzon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# heroku-deflater
|
2
|
+
|
3
|
+
A simple rack middleware that enables compressing of your assets and application
|
4
|
+
responses on Heroku, while not wasting CPU cycles on pointlessly gzipping images.
|
5
|
+
|
6
|
+
It also bundles the fix for the nasty Rack::Deflater [bug][1].
|
7
|
+
|
8
|
+
## Installing
|
9
|
+
|
10
|
+
Add to your Gemfile:
|
11
|
+
|
12
|
+
gem 'heroku-deflater', :group => :production
|
13
|
+
|
14
|
+
|
15
|
+
## Contributing to heroku-deflater
|
16
|
+
|
17
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
18
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
19
|
+
* Fork the project.
|
20
|
+
* Start a feature/bugfix branch.
|
21
|
+
* Commit and push until you are happy with your contribution.
|
22
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
23
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
24
|
+
|
25
|
+
## Copyright
|
26
|
+
|
27
|
+
Copyright (c) 2012 Roman Shterenzon. See LICENSE.txt for
|
28
|
+
further details.
|
29
|
+
|
30
|
+
[1]: https://github.com/rack/rack/issues/349
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "heroku-deflater"
|
18
|
+
gem.homepage = "http://github.com/romanbsd/heroku-deflater"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Deflate assets on heroku}
|
21
|
+
gem.description = gem.summary
|
22
|
+
gem.email = "romanbsd@yahoo.com"
|
23
|
+
gem.authors = ["Roman Shterenzon"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :test
|
36
|
+
|
37
|
+
require 'rdoc/task'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "heroku-deflater #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "heroku-deflater"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Roman Shterenzon"]
|
12
|
+
s.date = "2012-12-25"
|
13
|
+
s.description = "Deflate assets on heroku"
|
14
|
+
s.email = "romanbsd@yahoo.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.md",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"heroku-deflater.gemspec",
|
27
|
+
"lib/heroku-deflater.rb",
|
28
|
+
"lib/heroku-deflater/deflater.rb",
|
29
|
+
"lib/heroku-deflater/railtie.rb",
|
30
|
+
"lib/heroku-deflater/skip_images.rb"
|
31
|
+
]
|
32
|
+
s.homepage = "http://github.com/romanbsd/heroku-deflater"
|
33
|
+
s.licenses = ["MIT"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = "1.8.23"
|
36
|
+
s.summary = "Deflate assets on heroku"
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
43
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
46
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
47
|
+
end
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
50
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'heroku-deflater/railtie'
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# Fixes https://github.com/rack/rack/issues/349
|
2
|
+
require "zlib"
|
3
|
+
require "stringio"
|
4
|
+
require "time" # for Time.httpdate
|
5
|
+
require 'rack/utils'
|
6
|
+
|
7
|
+
module Rack
|
8
|
+
class Deflater
|
9
|
+
def initialize(app)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
status, headers, body = @app.call(env)
|
15
|
+
headers = Utils::HeaderHash.new(headers)
|
16
|
+
|
17
|
+
# Skip compressing empty entity body responses and responses with
|
18
|
+
# no-transform set.
|
19
|
+
if Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
|
20
|
+
headers['Cache-Control'].to_s =~ /\bno-transform\b/ ||
|
21
|
+
(headers['Content-Encoding'] && headers['Content-Encoding'] !~ /\bidentity\b/)
|
22
|
+
return [status, headers, body]
|
23
|
+
end
|
24
|
+
|
25
|
+
request = Request.new(env)
|
26
|
+
|
27
|
+
encoding = Utils.select_best_encoding(%w(gzip deflate identity),
|
28
|
+
request.accept_encoding)
|
29
|
+
|
30
|
+
# Set the Vary HTTP header.
|
31
|
+
vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
|
32
|
+
unless vary.include?("*") || vary.include?("Accept-Encoding")
|
33
|
+
headers["Vary"] = vary.push("Accept-Encoding").join(",")
|
34
|
+
end
|
35
|
+
|
36
|
+
case encoding
|
37
|
+
when "gzip"
|
38
|
+
headers['Content-Encoding'] = "gzip"
|
39
|
+
headers.delete('Content-Length')
|
40
|
+
mtime = headers.key?("Last-Modified") ?
|
41
|
+
Time.httpdate(headers["Last-Modified"]) : Time.now
|
42
|
+
[status, headers, GzipStream.new(body, mtime)]
|
43
|
+
when "deflate"
|
44
|
+
headers['Content-Encoding'] = "deflate"
|
45
|
+
headers.delete('Content-Length')
|
46
|
+
[status, headers, DeflateStream.new(body)]
|
47
|
+
when "identity"
|
48
|
+
[status, headers, body]
|
49
|
+
when nil
|
50
|
+
body.close if body.respond_to?(:close)
|
51
|
+
message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
|
52
|
+
[406, {"Content-Type" => "text/plain", "Content-Length" => message.length.to_s}, [message]]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class GzipStream
|
57
|
+
def initialize(body, mtime)
|
58
|
+
@body = body
|
59
|
+
@mtime = mtime
|
60
|
+
end
|
61
|
+
|
62
|
+
def each(&block)
|
63
|
+
@writer = block
|
64
|
+
gzip =::Zlib::GzipWriter.new(self)
|
65
|
+
gzip.mtime = @mtime
|
66
|
+
@body.each { |part|
|
67
|
+
gzip.write(part)
|
68
|
+
gzip.flush
|
69
|
+
}
|
70
|
+
ensure
|
71
|
+
@body.close if @body.respond_to?(:close)
|
72
|
+
gzip.close
|
73
|
+
@writer = nil
|
74
|
+
end
|
75
|
+
|
76
|
+
def write(data)
|
77
|
+
@writer.call(data)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class DeflateStream
|
82
|
+
DEFLATE_ARGS = [
|
83
|
+
Zlib::DEFAULT_COMPRESSION,
|
84
|
+
# drop the zlib header which causes both Safari and IE to choke
|
85
|
+
-Zlib::MAX_WBITS,
|
86
|
+
Zlib::DEF_MEM_LEVEL,
|
87
|
+
Zlib::DEFAULT_STRATEGY
|
88
|
+
]
|
89
|
+
|
90
|
+
def initialize(body)
|
91
|
+
@body = body
|
92
|
+
end
|
93
|
+
|
94
|
+
def each
|
95
|
+
deflater = ::Zlib::Deflate.new(*DEFLATE_ARGS)
|
96
|
+
@body.each { |part| yield deflater.deflate(part, Zlib::SYNC_FLUSH) }
|
97
|
+
yield deflater.finish
|
98
|
+
nil
|
99
|
+
ensure
|
100
|
+
@body.close if @body.respond_to?(:close)
|
101
|
+
deflater.close
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rack/deflater'
|
2
|
+
require 'heroku-deflater/deflater'
|
3
|
+
require 'heroku-deflater/skip_images'
|
4
|
+
|
5
|
+
module HerokuDeflater
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer "heroku_deflater.configure_rails_initialization" do |app|
|
8
|
+
app.middleware.insert_before ActionDispatch::Static, Rack::Deflater
|
9
|
+
app.middleware.insert_before ActionDispatch::Static, HerokuDeflater::SkipImages
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module HerokuDeflater
|
2
|
+
# Add a no-transform Cache-Control header to images, so they won't get gzipped
|
3
|
+
class SkipImages
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
# TODO: whitelist rather than blacklist
|
9
|
+
def call(env)
|
10
|
+
status, headers, body = @app.call(env)
|
11
|
+
headers = Rack::Utils::HeaderHash.new(headers)
|
12
|
+
if headers['Content-Type'].start_with?('image/')
|
13
|
+
if headers['Cache-Control'].nil? or headers['Cache-Control'].empty?
|
14
|
+
headers['Cache-Control'] = 'no-transform'
|
15
|
+
else
|
16
|
+
headers['Cache-Control'] += ', no-transform'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
[status, headers, body]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: heroku-deflater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Roman Shterenzon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: jeweler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Deflate assets on heroku
|
47
|
+
email: romanbsd@yahoo.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files:
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
files:
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- VERSION
|
60
|
+
- heroku-deflater.gemspec
|
61
|
+
- lib/heroku-deflater.rb
|
62
|
+
- lib/heroku-deflater/deflater.rb
|
63
|
+
- lib/heroku-deflater/railtie.rb
|
64
|
+
- lib/heroku-deflater/skip_images.rb
|
65
|
+
homepage: http://github.com/romanbsd/heroku-deflater
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: 2254873390403201438
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.23
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Deflate assets on heroku
|
93
|
+
test_files: []
|