Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Upgrade vendored sinatra to 1.0.a and include rack 1.1.0
Don't shrink variables when javascript file includes 'eval(' to prevent breaking eval calls
  • Loading branch information
darkhelmet committed Feb 21, 2010
1 parent ac86c9a commit febdfbb
Show file tree
Hide file tree
Showing 182 changed files with 12,537 additions and 314 deletions.
118 changes: 4 additions & 114 deletions lib/sinatra/bundles.rb
@@ -1,120 +1,10 @@
require 'rainpress'
require 'packr'

module Sinatra
# Main Bundles Module
module Bundles
# The base class for a bundle of files.
# The developer user sinatra-bundles should
# never have to deal with this directly
class Bundle
def initialize(app, files)
@app = app
@files = files
end

# Since we pass Bundles back as the body,
# this follows Rack standards and supports an each method
# to yield parts of the body, in our case, the files.
def each
@files.each do |f|
content = File.read(path(f))
content = compress(content) if @app.compress_bundles
# Include a new line to prevent weirdness at file boundaries
yield("#{content}\n")
end
end

private

# The timestamp of the bundle, which is the newest file in the bundle.
#
# @return [Integer] The timestamp of the bundle
def stamp
@files.map do |f|
File.mtime(path(f))
end.sort.first.to_i
end
end

# Bundle for stylesheets
class StylesheetBundle < Bundle
# Generate the HTML tag for the stylesheet
#
# @param [String] name The name of a bundle
# @return [String] The HTML that can be inserted into the doc
def to_html(name)
"<link type='text/css' href='/stylesheets/bundles/#{name}.css#{@app.stamp_bundles ? "?#{stamp}" : ''}' rel='stylesheet' media='screen' />"
end

protected

# Compress CSS
#
# @param [String] css The CSS to compress
# @return [String] Compressed CSS
def compress(css)
Rainpress.compress(css)
end

# Get the path of the file on disk
#
# @param [String] filename The name of sheet,
# assumed to be in the public directory, under 'stylesheets'
# @return [String] The full path to the file
def path(filename)
File.join(@app.public, 'stylesheets', "#{filename}.css")
end
end

# Bundle for javascripts
class JavascriptBundle < Bundle
# Generate the HTML tag for the script file
#
# @param [String] name The name of a bundle
# @return [String] The HTML that can be inserted into the doc
def to_html(name)
"<script type='text/javascript' src='/javascripts/bundles/#{name}.js#{@app.stamp_bundles ? "?#{stamp}" : ''}'></script>"
end

protected

# Compress Javascript
#
# @param [String] js The Javascript to compress
# @return [String] Compressed Javascript
def compress(js)
Packr.pack(js, :shrink_vars => true)
end

# Get the path of the file on disk
#
# @param [String] filename The name of sheet,
# assumed to be in the public directory, under 'javascripts'
# @return [String] The full path to the file
def path(filename)
File.join(@app.public, 'javascripts', "#{filename}.js")
end
end

# View helpers
module Helpers
# Emit a script tag for a javascript bundle
#
# @param [Symbol,String] bundle The bundle name
# @return [String] HTML script tag
def javascript_bundle_include_tag(bundle)
options.javascript_bundles[bundle].to_html(bundle)
end

# Emit a script tag for a stylesheet bundle
#
# @param [Symbol,String] bundle The bundle name
# @return [String] HTML link tag
def stylesheet_bundle_link_tag(bundle)
options.stylesheet_bundles[bundle].to_html(bundle)
end
end
autoload :Helpers, 'sinatra/bundles/helpers'
autoload :Bundle, 'sinatra/bundles/bundle'
autoload :JavascriptBundle, 'sinatra/bundles/javascript_bundle'
autoload :StylesheetBundle, 'sinatra/bundles/stylesheet_bundle'

# Set a Javascript bundle
# javascript_bundle(:all, %w(jquery lightbox))
Expand Down
36 changes: 36 additions & 0 deletions lib/sinatra/bundles/bundle.rb
@@ -0,0 +1,36 @@
module Sinatra
module Bundles
# The base class for a bundle of files.
# The developer user sinatra-bundles should
# never have to deal with this directly
class Bundle
def initialize(app, files)
@app = app
@files = files
end

# Since we pass Bundles back as the body,
# this follows Rack standards and supports an each method
# to yield parts of the body, in our case, the files.
def each
@files.each do |f|
content = File.read(path(f))
content = compress(content) if @app.compress_bundles
# Include a new line to prevent weirdness at file boundaries
yield("#{content}\n")
end
end

private

# The timestamp of the bundle, which is the newest file in the bundle.
#
# @return [Integer] The timestamp of the bundle
def stamp
@files.map do |f|
File.mtime(path(f))
end.sort.first.to_i
end
end
end
end
22 changes: 22 additions & 0 deletions lib/sinatra/bundles/helpers.rb
@@ -0,0 +1,22 @@
module Sinatra
module Bundles
# View helpers
module Helpers
# Emit a script tag for a javascript bundle
#
# @param [Symbol,String] bundle The bundle name
# @return [String] HTML script tag
def javascript_bundle_include_tag(bundle)
options.javascript_bundles[bundle].to_html(bundle)
end

# Emit a script tag for a stylesheet bundle
#
# @param [Symbol,String] bundle The bundle name
# @return [String] HTML link tag
def stylesheet_bundle_link_tag(bundle)
options.stylesheet_bundles[bundle].to_html(bundle)
end
end
end
end
37 changes: 37 additions & 0 deletions lib/sinatra/bundles/javascript_bundle.rb
@@ -0,0 +1,37 @@
require 'sinatra/bundles/bundle'
require 'packr'

module Sinatra
module Bundles
# Bundle for javascripts
class JavascriptBundle < Bundle
# Generate the HTML tag for the script file
#
# @param [String] name The name of a bundle
# @return [String] The HTML that can be inserted into the doc
def to_html(name)
"<script type='text/javascript' src='/javascripts/bundles/#{name}.js#{@app.stamp_bundles ? "?#{stamp}" : ''}'></script>"
end

protected

# Compress Javascript
#
# @param [String] js The Javascript to compress
# @return [String] Compressed Javascript
def compress(js)
# Don't shrink variables if the file includes a call to `eval`
Packr.pack(js, :shrink_vars => !js.include?('eval('))
end

# Get the path of the file on disk
#
# @param [String] filename The name of sheet,
# assumed to be in the public directory, under 'javascripts'
# @return [String] The full path to the file
def path(filename)
File.join(@app.public, 'javascripts', "#{filename}.js")
end
end
end
end
36 changes: 36 additions & 0 deletions lib/sinatra/bundles/stylesheet_bundle.rb
@@ -0,0 +1,36 @@
require 'sinatra/bundles/bundle'
require 'rainpress'

module Sinatra
module Bundles
# Bundle for stylesheets
class StylesheetBundle < Bundle
# Generate the HTML tag for the stylesheet
#
# @param [String] name The name of a bundle
# @return [String] The HTML that can be inserted into the doc
def to_html(name)
"<link type='text/css' href='/stylesheets/bundles/#{name}.css#{@app.stamp_bundles ? "?#{stamp}" : ''}' rel='stylesheet' media='screen' />"
end

protected

# Compress CSS
#
# @param [String] css The CSS to compress
# @return [String] Compressed CSS
def compress(css)
Rainpress.compress(css)
end

# Get the path of the file on disk
#
# @param [String] filename The name of sheet,
# assumed to be in the public directory, under 'stylesheets'
# @return [String] The full path to the file
def path(filename)
File.join(@app.public, 'stylesheets', "#{filename}.css")
end
end
end
end
2 changes: 1 addition & 1 deletion spec/app.rb
Expand Up @@ -7,5 +7,5 @@ class TestApp < Sinatra::Base
register Sinatra::Bundles

stylesheet_bundle(:test, %w(test1 test2))
javascript_bundle(:test, %w(test1 test2))
javascript_bundle(:test, %w(test1 test2 eval))
end
2 changes: 1 addition & 1 deletion spec/production_app.rb
@@ -1,4 +1,4 @@
gem 'sinatra', '>= 0.10.1'
gem 'sinatra', '>= 1.0.a'
require 'sinatra/base'
require 'sinatra/bundles'

Expand Down
5 changes: 5 additions & 0 deletions spec/public/javascripts/eval.js
@@ -0,0 +1,5 @@
function something(name) {
eval('alert(name);');
}

something('bob');
11 changes: 10 additions & 1 deletion spec/sinatra-bundles_spec.rb
Expand Up @@ -29,7 +29,7 @@ def css_stamp(names)
end

before do
@scripts = %w(test1 test2).map do |name|
@scripts = %w(test1 test2 eval).map do |name|
File.expand_path(File.join(File.dirname(__FILE__), 'public', 'javascripts', "#{name}.js"))
end

Expand Down Expand Up @@ -111,6 +111,15 @@ def css_stamp(names)
last_response.headers['Vary'].should == 'Accept-Encoding'
last_response.headers['Cache-Control'].should == 'public, must-revalidate, max-age=31536000'
end

it 'should not shrink vars on javascript files that use eval' do
app.enable(:compress_bundles)
get '/javascripts/bundles/test.js'
last_response.should be_ok
js = File.read(File.join(File.dirname(__FILE__), 'public/javascripts/eval.js'))
last_response.body.include?(Packr.pack(js)).should be_true
last_response.body.include?(Packr.pack(js, :shrink_vars => true)).should be_false
end
end

context 'stylesheet bundles' do
Expand Down
19 changes: 19 additions & 0 deletions vendor/bin/rackup
@@ -0,0 +1,19 @@
#!/Users/darkhelmet/local/ree/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'rack' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0"

if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
version = $1
ARGV.shift
end

gem 'rack', version
load Gem.bin_path('rack', 'rackup', version)
Binary file added vendor/cache/rack-1.1.0.gem
Binary file not shown.
Binary file added vendor/cache/sinatra-1.0.a.gem
Binary file not shown.
18 changes: 18 additions & 0 deletions vendor/gems/rack-1.1.0/COPYING
@@ -0,0 +1,18 @@
Copyright (c) 2007, 2008, 2009, 2010 Christian Neukirchen <purl.org/net/chneukirchen>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 changes: 21 additions & 0 deletions vendor/gems/rack-1.1.0/KNOWN-ISSUES
@@ -0,0 +1,21 @@
= Known issues with Rack and Web servers

* Lighttpd sets wrong SCRIPT_NAME and PATH_INFO if you mount your
FastCGI app at "/". This can be fixed by using this middleware:

class LighttpdScriptNameFix
def initialize(app)
@app = app
end

def call(env)
env["PATH_INFO"] = env["SCRIPT_NAME"].to_s + env["PATH_INFO"].to_s
env["SCRIPT_NAME"] = ""
@app.call(env)
end
end

Of course, use this only when your app runs at "/".

Since lighttpd 1.4.23, you also can use the "fix-root-scriptname" flag
in fastcgi.server.
Empty file added vendor/gems/rack-1.1.0/RDOX
Empty file.

0 comments on commit febdfbb

Please sign in to comment.