agouti 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +2 -0
- data/agouti.gemspec +25 -0
- data/lib/agouti.rb +4 -0
- data/lib/agouti/rack/package_limiter.rb +82 -0
- data/lib/agouti/railtie.rb +10 -0
- data/lib/agouti/version.rb +3 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bbc9f2fca59ad3b615499be8155878ccaa9267f9
|
4
|
+
data.tar.gz: 10a0ff265c3886f821d05901e8f8361dbf3cfbee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 34b66fbb60d367a245c65b7b210fb6fc3bb9de556961549f2104019dcb2727119d4e08a2ec578b8507a7efe11289c5badafbb2495cc251f79e7fa1df82a152ff
|
7
|
+
data.tar.gz: 6bbb152fd5e018607e4fdf0c92b2170a19d8b5b4cd017d8f5066be70115a33319093ecfa4fd0b70f5d254aaeed634ccff64f80a11bca1973fbc18b34d7583caf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 dwayhs
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Agouti
|
2
|
+
|
3
|
+
Gem for testing above the fold render on the first tcp round trip.
|
4
|
+
|
5
|
+
This gem is a Rack middleware that truncates the gzipped response to 14kb.
|
6
|
+
|
7
|
+
Usefull for testing [critical rendering path optimization](https://developers.google.com/web/fundamentals/performance/critical-rendering-path/).
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'agouti'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install agouti
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To enable the middleware, it is necessary to add the following header to the request:
|
28
|
+
```
|
29
|
+
X-Agouti-Enable: 1
|
30
|
+
```
|
31
|
+
|
32
|
+
It is possible to customize the length of the content that the server will respond with the following header:
|
33
|
+
```
|
34
|
+
X-Agouti-Limit: 14000
|
35
|
+
```
|
36
|
+
|
37
|
+
### Example usage with Cucumber, Capybara and Poltergeist
|
38
|
+
```
|
39
|
+
Given /^(?:|I )navigate to '(.+)'$/ do |page_path|
|
40
|
+
visit page_path
|
41
|
+
|
42
|
+
Given /^(?:|I )navigate to '(.+)' waiting only one tcp round trip$/ do |page_path|
|
43
|
+
page.driver.add_header("X-Agouti-Enable", "1", permanent: false)
|
44
|
+
visit page_path
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it ( https://github.com/CWISoftware/agouti/fork )
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/agouti.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'agouti/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "agouti"
|
8
|
+
spec.version = Agouti::VERSION
|
9
|
+
spec.authors = ["dwayhs"]
|
10
|
+
spec.email = ["danielwayhs@gmail.com"]
|
11
|
+
spec.summary = 'Gem for testing above the fold render on the first tcp round trip.'
|
12
|
+
spec.description = ''
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "rack", "~> 1.5"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
data/lib/agouti.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
module Agouti
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
|
5
|
+
class PackageLimiter
|
6
|
+
|
7
|
+
ENABLE_HEADER = 'X-Agouti-Enable'
|
8
|
+
LIMIT_HEADER = 'X-Agouti-Limit'
|
9
|
+
DEFAULT_LIMIT = 14000
|
10
|
+
|
11
|
+
##
|
12
|
+
# Creates Agouti::Rack::PackageLimiter middleware.
|
13
|
+
#
|
14
|
+
# [app] rack app instance
|
15
|
+
# [options] hash of package limiter options, i.e.
|
16
|
+
# 'if' - a lambda enabling / disabling deflation based on returned boolean value
|
17
|
+
# e.g use Rack::Deflater, :if => lambda { |env, status, headers, body| body.length > 512 }
|
18
|
+
def initialize(app, options = {})
|
19
|
+
@app = app
|
20
|
+
@condition = options[:if]
|
21
|
+
@limit = options[:limit] || 14000
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_http_header env, header
|
25
|
+
env["HTTP_#{header.upcase.gsub('-', '_')}"]
|
26
|
+
end
|
27
|
+
|
28
|
+
def call(env)
|
29
|
+
status, headers, body = @app.call(env)
|
30
|
+
|
31
|
+
set_limit(env)
|
32
|
+
|
33
|
+
if enabled?(env)
|
34
|
+
# Just execute for html
|
35
|
+
# TODO: find a better way of doing it
|
36
|
+
unless (headers.has_key? 'Content-Type' and headers['Content-Type'].include? 'text/html')
|
37
|
+
# Returns empty responses for requests that are not html
|
38
|
+
return [204, {}, []]
|
39
|
+
end
|
40
|
+
|
41
|
+
headers = ::Rack::Utils::HeaderHash.new(headers)
|
42
|
+
|
43
|
+
headers['Content-Encoding'] = "gzip"
|
44
|
+
headers.delete('Content-Length')
|
45
|
+
mtime = headers.key?("Last-Modified") ? Time.httpdate(headers["Last-Modified"]) : Time.now
|
46
|
+
|
47
|
+
[status, headers, GzipTruncatedStream.new(body, mtime, @limit)]
|
48
|
+
else
|
49
|
+
[status, headers, body]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def enabled? env
|
56
|
+
get_http_header(env, ENABLE_HEADER) and get_http_header(env, ENABLE_HEADER) == '1'
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_limit env
|
60
|
+
@limit = (get_http_header(env, LIMIT_HEADER)) ? get_http_header(env, LIMIT_HEADER).to_i : DEFAULT_LIMIT
|
61
|
+
end
|
62
|
+
|
63
|
+
class GzipTruncatedStream < ::Rack::Deflater::GzipStream
|
64
|
+
def initialize body, mtime, byte_limit
|
65
|
+
super body, mtime
|
66
|
+
@byte_limit = byte_limit
|
67
|
+
@total_sent_bytes = 0
|
68
|
+
end
|
69
|
+
|
70
|
+
def write(data)
|
71
|
+
# slices data if total sent bytes reaches byte limit
|
72
|
+
if @total_sent_bytes + data.bytesize > @byte_limit
|
73
|
+
data = data.byteslice(0, @byte_limit - @total_sent_bytes)
|
74
|
+
end
|
75
|
+
|
76
|
+
@total_sent_bytes += data.bytesize
|
77
|
+
@writer.call(data)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: agouti
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dwayhs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- danielwayhs@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- agouti.gemspec
|
68
|
+
- lib/agouti.rb
|
69
|
+
- lib/agouti/rack/package_limiter.rb
|
70
|
+
- lib/agouti/railtie.rb
|
71
|
+
- lib/agouti/version.rb
|
72
|
+
homepage: ''
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Gem for testing above the fold render on the first tcp round trip.
|
96
|
+
test_files: []
|