heroku-deflater 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +3 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +4 -4
- data/VERSION +1 -1
- data/heroku-deflater.gemspec +8 -6
- data/lib/heroku-deflater/railtie.rb +6 -4
- data/lib/heroku-deflater/skip_binary.rb +30 -0
- data/spec/skip_binary_spec.rb +39 -0
- metadata +10 -8
- data/lib/heroku-deflater/skip_images.rb +0 -22
data/.rspec
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -7,10 +7,10 @@ GEM
|
|
7
7
|
git (>= 1.2.5)
|
8
8
|
rake
|
9
9
|
rdoc
|
10
|
-
json (1.7.
|
11
|
-
rack (1.4.
|
10
|
+
json (1.7.7)
|
11
|
+
rack (1.4.5)
|
12
12
|
rake (10.0.3)
|
13
|
-
rdoc (3.12)
|
13
|
+
rdoc (3.12.1)
|
14
14
|
json (~> 1.4)
|
15
15
|
|
16
16
|
PLATFORMS
|
@@ -19,4 +19,4 @@ PLATFORMS
|
|
19
19
|
DEPENDENCIES
|
20
20
|
bundler
|
21
21
|
jeweler
|
22
|
-
rack (
|
22
|
+
rack (~> 1.4.5)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/heroku-deflater.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "heroku-deflater"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Roman Shterenzon"]
|
12
|
-
s.date = "2013-
|
12
|
+
s.date = "2013-02-22"
|
13
13
|
s.description = "Deflate assets on heroku"
|
14
14
|
s.email = "romanbsd@yahoo.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
+
".rspec",
|
20
21
|
"Gemfile",
|
21
22
|
"Gemfile.lock",
|
22
23
|
"LICENSE.txt",
|
@@ -26,7 +27,8 @@ Gem::Specification.new do |s|
|
|
26
27
|
"heroku-deflater.gemspec",
|
27
28
|
"lib/heroku-deflater.rb",
|
28
29
|
"lib/heroku-deflater/railtie.rb",
|
29
|
-
"lib/heroku-deflater/
|
30
|
+
"lib/heroku-deflater/skip_binary.rb",
|
31
|
+
"spec/skip_binary_spec.rb"
|
30
32
|
]
|
31
33
|
s.homepage = "http://github.com/romanbsd/heroku-deflater"
|
32
34
|
s.licenses = ["MIT"]
|
@@ -38,16 +40,16 @@ Gem::Specification.new do |s|
|
|
38
40
|
s.specification_version = 3
|
39
41
|
|
40
42
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
41
|
-
s.add_runtime_dependency(%q<rack>, ["
|
43
|
+
s.add_runtime_dependency(%q<rack>, ["~> 1.4.5"])
|
42
44
|
s.add_development_dependency(%q<bundler>, [">= 0"])
|
43
45
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
44
46
|
else
|
45
|
-
s.add_dependency(%q<rack>, ["
|
47
|
+
s.add_dependency(%q<rack>, ["~> 1.4.5"])
|
46
48
|
s.add_dependency(%q<bundler>, [">= 0"])
|
47
49
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
48
50
|
end
|
49
51
|
else
|
50
|
-
s.add_dependency(%q<rack>, ["
|
52
|
+
s.add_dependency(%q<rack>, ["~> 1.4.5"])
|
51
53
|
s.add_dependency(%q<bundler>, [">= 0"])
|
52
54
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
53
55
|
end
|
@@ -1,15 +1,17 @@
|
|
1
1
|
require 'rack/deflater'
|
2
|
-
require 'heroku-deflater/
|
2
|
+
require 'heroku-deflater/skip_binary'
|
3
3
|
|
4
4
|
module HerokuDeflater
|
5
5
|
class Railtie < Rails::Railtie
|
6
6
|
initializer "heroku_deflater.configure_rails_initialization" do |app|
|
7
7
|
app.middleware.insert_before ActionDispatch::Static, Rack::Deflater
|
8
|
-
app.middleware.insert_before ActionDispatch::Static, HerokuDeflater::
|
8
|
+
app.middleware.insert_before ActionDispatch::Static, HerokuDeflater::SkipBinary
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
# Set default Cache-Control headers to one week.
|
12
|
+
# The configuration block in config/application.rb overrides this.
|
13
|
+
config.before_configuration do |app|
|
14
|
+
app.config.static_cache_control = 'public, max-age=604800'
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module HerokuDeflater
|
2
|
+
# Add a no-transform Cache-Control header to binary types, so they won't get gzipped
|
3
|
+
class SkipBinary
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
WHITELIST = [
|
9
|
+
%r{^text/},
|
10
|
+
'application/javascript',
|
11
|
+
'application/json'
|
12
|
+
].freeze
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
status, headers, body = @app.call(env)
|
16
|
+
headers = Rack::Utils::HeaderHash.new(headers)
|
17
|
+
content_type = headers['Content-Type']
|
18
|
+
cache_control = headers['Cache-Control'].to_s.downcase
|
19
|
+
|
20
|
+
unless cache_control.include?('no-transform') or WHITELIST.any? { |type| type === content_type }
|
21
|
+
if cache_control.empty?
|
22
|
+
headers['Cache-Control'] = 'no-transform'
|
23
|
+
else
|
24
|
+
headers['Cache-Control'] += ', no-transform'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
[status, headers, body]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rack/mock'
|
2
|
+
require 'heroku-deflater/skip_binary'
|
3
|
+
|
4
|
+
describe HerokuDeflater::SkipBinary do
|
5
|
+
let(:env) { Rack::MockRequest.env_for('/') }
|
6
|
+
|
7
|
+
def app_returning_type(type, headers = {})
|
8
|
+
lambda { |env| [200, {'Content-Type' => type}.merge(headers), ['']] }
|
9
|
+
end
|
10
|
+
|
11
|
+
def process(type, headers = {})
|
12
|
+
described_class.new(app_returning_type(type, headers)).call(env)[1]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "forbids compressing of binary types" do
|
16
|
+
%w[application/gzip application/pdf image/jpeg].each do |type|
|
17
|
+
headers = process(type)
|
18
|
+
headers['Cache-Control'].to_s.should include('no-transform')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "allows compressing of text types" do
|
23
|
+
%w[text/plain text/html application/json application/javascript].each do |type|
|
24
|
+
headers = process(type)
|
25
|
+
headers['Cache-Control'].to_s.should_not include('no-transform')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "adds to existing headers" do
|
30
|
+
headers = process('image/gif', 'Cache-Control' => 'public')
|
31
|
+
headers['Cache-Control'].should eq('public, no-transform')
|
32
|
+
end
|
33
|
+
|
34
|
+
it "doesn't add 'no-transform' if it's already present" do
|
35
|
+
headers = process('image/gif', 'Cache-Control' => 'public, no-transform')
|
36
|
+
headers['Cache-Control'].should eq('public, no-transform')
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: heroku-deflater
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 1.4.
|
21
|
+
version: 1.4.5
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 1.4.
|
29
|
+
version: 1.4.5
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: bundler
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,6 +67,7 @@ extra_rdoc_files:
|
|
67
67
|
- LICENSE.txt
|
68
68
|
- README.md
|
69
69
|
files:
|
70
|
+
- .rspec
|
70
71
|
- Gemfile
|
71
72
|
- Gemfile.lock
|
72
73
|
- LICENSE.txt
|
@@ -76,7 +77,8 @@ files:
|
|
76
77
|
- heroku-deflater.gemspec
|
77
78
|
- lib/heroku-deflater.rb
|
78
79
|
- lib/heroku-deflater/railtie.rb
|
79
|
-
- lib/heroku-deflater/
|
80
|
+
- lib/heroku-deflater/skip_binary.rb
|
81
|
+
- spec/skip_binary_spec.rb
|
80
82
|
homepage: http://github.com/romanbsd/heroku-deflater
|
81
83
|
licenses:
|
82
84
|
- MIT
|
@@ -92,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
94
|
version: '0'
|
93
95
|
segments:
|
94
96
|
- 0
|
95
|
-
hash:
|
97
|
+
hash: 3241685697845166073
|
96
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
99
|
none: false
|
98
100
|
requirements:
|
@@ -1,22 +0,0 @@
|
|
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'].to_s.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
|