From 11fbecfdae8a0a7806647bf0c3ade92ec0345784 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 28 Sep 2023 08:16:30 -0400 Subject: [PATCH] Add support for digest_class config --- lib/propshaft/assembly.rb | 8 ++++++-- lib/propshaft/asset.rb | 11 +++++++---- lib/propshaft/load_path.rb | 16 +++++++++++----- lib/propshaft/railtie.rb | 1 + test/propshaft/asset_test.rb | 10 ++++++++-- test/propshaft/load_path_test.rb | 8 ++++++++ 6 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/propshaft/assembly.rb b/lib/propshaft/assembly.rb index 3fbb11c..c6d96d9 100644 --- a/lib/propshaft/assembly.rb +++ b/lib/propshaft/assembly.rb @@ -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 @@ -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 diff --git a/lib/propshaft/asset.rb b/lib/propshaft/asset.rb index 6278df0..4157cd8 100644 --- a/lib/propshaft/asset.rb +++ b/lib/propshaft/asset.rb @@ -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 @@ -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 diff --git a/lib/propshaft/load_path.rb b/lib/propshaft/load_path.rb index ab236b2..814d836 100644 --- a/lib/propshaft/load_path.rb +++ b/lib/propshaft/load_path.rb @@ -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) @@ -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 diff --git a/lib/propshaft/railtie.rb b/lib/propshaft/railtie.rb index ad8d769..82418e0 100644 --- a/lib/propshaft/railtie.rb +++ b/lib/propshaft/railtie.rb @@ -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 diff --git a/test/propshaft/asset_test.rb b/test/propshaft/asset_test.rb index 702a209..436b121 100644 --- a/test/propshaft/asset_test.rb +++ b/test/propshaft/asset_test.rb @@ -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 diff --git a/test/propshaft/load_path_test.rb b/test/propshaft/load_path_test.rb index 8ab8cf4..2b8a32a 100644 --- a/test/propshaft/load_path_test.rb +++ b/test/propshaft/load_path_test.rb @@ -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",