Skip to content

Commit

Permalink
Add support for digest_class config
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Sep 28, 2023
1 parent 18979c1 commit 11fbecf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
8 changes: 6 additions & 2 deletions lib/propshaft/assembly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def initialize(config)
end

def load_path
@load_path ||= Propshaft::LoadPath.new(config.paths, version: config.version)
@load_path ||= Propshaft::LoadPath.new(
config.paths,
version: config.version,
digest_class: confing.digest_class,
)
end

def resolver
Expand Down Expand Up @@ -46,7 +50,7 @@ def compilers

def reveal(path_type = :logical_path)
path_type = path_type.presence_in(%i[ logical_path path ]) || raise(ArgumentError, "Unknown path_type: #{path_type}")

load_path.assets.collect do |asset|
asset.send(path_type)
end
Expand Down
11 changes: 7 additions & 4 deletions lib/propshaft/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
require "action_dispatch/http/mime_type"

class Propshaft::Asset
attr_reader :path, :logical_path, :version
attr_reader :path, :logical_path, :digest_class, :version

def initialize(path, logical_path:, version: nil)
@path, @logical_path, @version = path, Pathname.new(logical_path), version
def initialize(path, logical_path:, digest_class: Digest::SHA1, version: nil)
@path = path
@logical_path = Pathname.new(logical_path)
@digest_class = digest_class
@version = version
end

def content
Expand All @@ -21,7 +24,7 @@ def length
end

def digest
@digest ||= Digest::SHA1.hexdigest("#{content}#{version}")
@digest ||= digest_class.hexdigest("#{content}#{version}")
end

def digested_path
Expand Down
16 changes: 11 additions & 5 deletions lib/propshaft/load_path.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require "propshaft/asset"

class Propshaft::LoadPath
attr_reader :paths, :version
attr_reader :paths, :digest_class, :version

def initialize(paths = [], version: nil)
@paths = dedup(paths)
@version = version
def initialize(paths = [], digest_class: Digest::SHA1, version: nil)
@paths = dedup(paths)
@version = version
@digest_class = digest_class
end

def find(asset_name)
Expand Down Expand Up @@ -48,7 +49,12 @@ def assets_by_path
paths.each do |path|
without_dotfiles(all_files_from_tree(path)).each do |file|
logical_path = file.relative_path_from(path)
mapped[logical_path.to_s] ||= Propshaft::Asset.new(file, logical_path: logical_path, version: version)
mapped[logical_path.to_s] ||= Propshaft::Asset.new(
file,
logical_path: logical_path,
version: version,
digest_class: digest_class,
)
end if path.exist?
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/propshaft/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Railtie < ::Rails::Railtie
config.assets.sweep_cache = Rails.env.development?
config.assets.server = Rails.env.development? || Rails.env.test?
config.assets.relative_url_root = nil
config.assets.digest_class = Digest::SHA1

# Register propshaft initializer to copy the assets path in all the Rails Engines.
# This makes possible for us to keep all `assets` config in this Railtie, but still
Expand Down
10 changes: 8 additions & 2 deletions test/propshaft/asset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ class Propshaft::AssetTest < ActiveSupport::TestCase
assert_equal asset.digest.object_id, asset.digest.object_id
end

test "custom digest class" do
asset = find_asset("one.txt", digest_class: Digest::SHA256)
assert_equal "one-f82fcaff474fbc87520bcae5fcd0d2f33a8fa74f11d151fe84a30ce03f2d349b.txt",
asset.digested_path.to_s
end

private
def find_asset(logical_path)
def find_asset(logical_path, digest_class: nil)
root_path = Pathname.new("#{__dir__}/../fixtures/assets/first_path")
path = root_path.join(logical_path)
Propshaft::Asset.new(path, logical_path: logical_path)
Propshaft::Asset.new(path, **{ logical_path: logical_path, digest_class: }.compact)
end
end
8 changes: 8 additions & 0 deletions test/propshaft/load_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ class Propshaft::LoadPathTest < ActiveSupport::TestCase
assert_nil Propshaft::LoadPath.new(Pathname.new("#{__dir__}/../fixtures/assets/nowhere")).find("missing")
end

test "custom digest class" do
@load_path = Propshaft::LoadPath.new(@load_path.paths, version: "1", digest_class: Digest::SHA256)
@load_path.manifest.tap do |manifest|
assert_equal "one-eb08cb2ea42bc732b848716bbc062289448f1bd3c12c5949aeb80f511c9bb99a.txt", manifest["one.txt"]
assert_equal "nested/three-68540fa36185a8a95d7d6b34d96915f466204e8b72fb1292aafdc6086547df26.txt", manifest["nested/three.txt"]
end
end

test "deduplicate paths" do
load_path = Propshaft::LoadPath.new [
"app/javascript",
Expand Down

0 comments on commit 11fbecf

Please sign in to comment.