Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Move fingerprint utils out of Pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 26, 2011
1 parent 603f580 commit 8920f22
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 45 deletions.
22 changes: 1 addition & 21 deletions lib/sprockets/engine_pathname.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pathname'
require 'rack/mime'
require 'sprockets/template_mappings'
require 'sprockets/utils'

module Sprockets
class EnginePathname < ::Pathname
Expand Down Expand Up @@ -55,27 +56,6 @@ def without_engine_extensions
end
end

def fingerprint
if defined? @fingerprint
@fingerprint
elsif basename_without_extensions.to_s =~ /-([0-9a-f]{7,40})$/
@fingerprint = $1
else
@fingerprint = nil
end
end

def with_fingerprint(digest)
if fingerprint
path = self.to_s.sub(fingerprint, digest)
else
basename = "#{basename_without_extensions}-#{digest}#{extensions.join}"
path = dirname.to_s == '.' ? basename : dirname.join(basename)
end

self.class.new(path)
end

private
def lookup_mime_type(ext)
Rack::Mime.mime_type(ext, nil)
Expand Down
3 changes: 2 additions & 1 deletion lib/sprockets/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'sprockets/environment_index'
require 'sprockets/server'
require 'sprockets/template_mappings'
require 'sprockets/utils'
require 'fileutils'
require 'hike'
require 'logger'
Expand Down Expand Up @@ -106,7 +107,7 @@ def find_fresh_asset_from_cache(logical_path)
logical_path = EnginePathname.new(logical_path)

if asset = @cache[logical_path.to_s]
if logical_path.fingerprint
if Utils.path_fingerprint(logical_path)
asset
elsif asset.stale?
logger.warn "[Sprockets] #{logical_path} #{asset.digest} stale"
Expand Down
3 changes: 2 additions & 1 deletion lib/sprockets/environment_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def precompile(*paths)
end

if asset = @path_index.find_asset(logical_path)
filename = @static_index.root.join(logical_path.with_fingerprint(asset.digest))
digest_path = Utils.path_with_fingerprint(logical_path, asset.digest)
filename = @static_index.root.join(digest_path)

FileUtils.mkdir_p filename.dirname

Expand Down
3 changes: 2 additions & 1 deletion lib/sprockets/path_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'sprockets/engine_pathname'
require 'sprockets/errors'
require 'sprockets/static_asset'
require 'sprockets/utils'
require 'pathname'
require 'set'

Expand Down Expand Up @@ -66,7 +67,7 @@ def find_asset(logical_path)
return @assets[logical_path.to_s]
end

if fingerprint = logical_path.fingerprint
if fingerprint = Utils.path_fingerprint(logical_path)
pathname = resolve(logical_path.to_s.sub("-#{fingerprint}", ''))
else
pathname = resolve(logical_path)
Expand Down
10 changes: 4 additions & 6 deletions lib/sprockets/server.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'sprockets/utils'
require 'rack/request'
require 'sprockets/engine_pathname'
require 'time'

module Sprockets
Expand All @@ -21,12 +21,10 @@ def call(env)
end

def path(logical_path, fingerprint = true, prefix = nil)
logical_path = EnginePathname.new(logical_path)

if fingerprint && asset = find_asset(logical_path)
url = logical_path.with_fingerprint(asset.digest).to_s
url = Utils.path_with_fingerprint(logical_path, asset.digest)
else
url = logical_path.to_s
url = logical_path
end

url = File.join(prefix, url) if prefix
Expand Down Expand Up @@ -90,7 +88,7 @@ def headers(asset, env)
headers["Last-Modified"] = asset.mtime.httpdate
headers["ETag"] = etag(asset)

if EnginePathname.new(env["PATH_INFO"]).fingerprint
if Utils.path_fingerprint(env["PATH_INFO"])
headers["Cache-Control"] << ", max-age=31536000"
else
headers["Cache-Control"] << ", must-revalidate"
Expand Down
5 changes: 3 additions & 2 deletions lib/sprockets/static_asset.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'sprockets/engine_pathname'
require 'sprockets/utils'
require 'digest/md5'
require 'time'

Expand All @@ -12,7 +13,7 @@ def initialize(pathname)
@mtime = @pathname.mtime
@length = @pathname.size

if digest = @pathname.fingerprint
if digest = Utils.path_fingerprint(@pathname)
@digest = digest
else
@digest = Digest::MD5.hexdigest(pathname.read)
Expand All @@ -24,7 +25,7 @@ def content_type
end

def stale?
if pathname.fingerprint
if Utils.path_fingerprint(pathname)
false
else
mtime < pathname.mtime
Expand Down
2 changes: 1 addition & 1 deletion lib/sprockets/static_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def find_asset(logical_path)
return nil
end

if !pathname.fingerprint
if !Utils.path_fingerprint(pathname)
pattern = /^#{Regexp.escape(pathname.basename_without_extensions.to_s)}
-[0-9a-f]{7,40}
#{Regexp.escape(pathname.extensions.join)}$/x
Expand Down
26 changes: 26 additions & 0 deletions lib/sprockets/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'pathname'

module Sprockets
# TODO: Junk drawer
module Utils
def path_fingerprint(path)
pathname = Pathname.new(path)
extensions = pathname.basename.to_s.scan(/\.[^.]+/).join
pathname.basename(extensions).to_s =~ /-([0-9a-f]{7,40})$/ ? $1 : nil
end
module_function :path_fingerprint

def path_with_fingerprint(path, digest)
pathname = Pathname.new(path)
extensions = pathname.basename.to_s.scan(/\.[^.]+/).join

if pathname.basename(extensions).to_s =~ /-([0-9a-f]{7,40})$/
path.sub($1, digest)
else
basename = "#{pathname.basename(extensions)}-#{digest}#{extensions}"
pathname.dirname.to_s == '.' ? basename : pathname.dirname.join(basename).to_s
end
end
module_function :path_with_fingerprint
end
end
12 changes: 0 additions & 12 deletions test/test_engine_pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,4 @@ class TestEnginePathname < Sprockets::TestCase
EnginePathname.new("application.coffee").content_type
end
end

test "fingerprint" do
assert_nil EnginePathname.new("print.css").fingerprint
assert_equal "f7531e2d0ea27233ce00b5f01c5bf335", EnginePathname.new("print-f7531e2d0ea27233ce00b5f01c5bf335.css").fingerprint
end

test "with fingerprint" do
assert_equal "print-f7531e2d0ea27233ce00b5f01c5bf335.css",
EnginePathname.new("print.css").with_fingerprint("f7531e2d0ea27233ce00b5f01c5bf335").to_s
assert_equal "/stylesheets/print-f7531e2d0ea27233ce00b5f01c5bf335.css",
EnginePathname.new("/stylesheets/print-37b51d194a7513e45b56f6524f2d51f2.css").with_fingerprint("f7531e2d0ea27233ce00b5f01c5bf335").to_s
end
end

0 comments on commit 8920f22

Please sign in to comment.