Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not fingerprint if filename contains a valid digest #714

Merged
merged 1 commit into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket

## Master

- Do not fingerprint files that already contain a valid digest in their name
- Remove remaining support for Ruby < 2.4.[#672](https://github.com/rails/sprockets/pull/672)

## 4.0.2
Expand Down
6 changes: 5 additions & 1 deletion lib/sprockets/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ def to_hash
#
# Returns String.
def digest_path
logical_path.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" }
if DigestUtils.already_digested?(@name)
logical_path
else
logical_path.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" }
end
end

# Public: Return load path + logical path with digest spliced in.
Expand Down
13 changes: 13 additions & 0 deletions lib/sprockets/digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ def hexdigest_integrity_uri(hexdigest)
integrity_uri(unpack_hexdigest(hexdigest))
end

# Internal: Checks an asset name for a valid digest
#
# name - The name of the asset
#
# Returns true if the name contains a digest recognized by one of the valid digest classes
def already_digested?(name)
return if name.nil?

if hexdigest = name.scan(/[a-fA-F0-9]+\z/).last
detect_digest_class(unpack_hexdigest(hexdigest))
end
end

private
def build_digest(obj)
digest = digest_class.new
Expand Down
26 changes: 26 additions & 0 deletions test/test_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,32 @@ def read(logical_path)
end
end

class PreDigestedAssetTest < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('asset'))
@env.cache = {}

@pipeline = nil
end

test "digest path" do
path = File.expand_path("test/fixtures/asset/application")
original = "#{path}.js"
digested = "#{path}-d41d8cd98f00b204e9800998ecf8427e.js"
FileUtils.cp(original, digested)

assert_equal "application-d41d8cd98f00b204e9800998ecf8427e.js",
asset("application-d41d8cd98f00b204e9800998ecf8427e.js").digest_path
ensure
FileUtils.rm(digested) if File.exists?(digested)
end

def asset(logical_path, options = {})
@env.find_asset(logical_path, **{pipeline: @pipeline}.merge(options))
end
end


class DebugAssetTest < Sprockets::TestCase
def setup
Expand Down
13 changes: 13 additions & 0 deletions test/test_digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,17 @@ def test_hexdigest_integrity_uri
assert_equal "sha512-+uuYUxxe7oWIShQrWEmMn/fixz/rxDP4qcAZddXLDM3nN8/tpk1ZC2jXQk6N+mXE65jwfzNVUJL/qjA3y9KbuQ==",
hexdigest_integrity_uri(sha512)
end

def test_already_digested
refute already_digested?(nil)
refute already_digested?("application-d41d8cd98f00b204e9800998ecf8427z")
refute already_digested?("application-d41d8cd98f00b204e9800998ecf8427ef")
refute already_digested?("application-d41d8cd98f00b204e9800998ecf8427e-")

assert already_digested?("application-" + Digest::MD5.new.hexdigest)
assert already_digested?("application-" + Digest::SHA1.new.hexdigest)
assert already_digested?("application-" + Digest::SHA256.new.hexdigest)
assert already_digested?("application-" + Digest::SHA384.new.hexdigest)
assert already_digested?("application-" + Digest::SHA512.new.hexdigest)
end
end