Skip to content

Commit 68d4ae0

Browse files
zceirafaelfranca
authored andcommitted
Add integrity method on static and dynamic resolvers
1 parent 2c1203b commit 68d4ae0

File tree

8 files changed

+63
-16
lines changed

8 files changed

+63
-16
lines changed

lib/propshaft/manifest.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def to_json
5050

5151
class << self
5252
def from_path(manifest_path)
53-
JSON.parse(manifest_path.read, symbolize_names: false).tap do |serialized_manifest|
54-
Manifest.new.tap do |manifest|
53+
Manifest.new.tap do |manifest|
54+
JSON.parse(manifest_path.read, symbolize_names: false).tap do |serialized_manifest|
5555
serialized_manifest.each_pair do |key, value|
5656
# Compatibility mode to be able to
5757
# read the old "simple manifest" format

lib/propshaft/resolver/dynamic.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ def resolve(logical_path)
1212
end
1313
end
1414

15+
def integrity(logical_path)
16+
hash_algorithm = load_path.integrity_hash_algorithm
17+
if (asset = load_path.find(logical_path)) && hash_algorithm
18+
asset.integrity(hash_algorithm:)
19+
end
20+
end
21+
1522
def read(logical_path, options = {})
1623
if asset = load_path.find(logical_path)
1724
asset.content(**options)

lib/propshaft/resolver/static.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def resolve(logical_path)
1212
end
1313
end
1414

15+
def integrity(logical_path)
16+
entry = manifest[logical_path]
17+
18+
entry&.integrity
19+
end
20+
1521
def read(logical_path, encoding: "ASCII-8BIT")
1622
if asset_path = digested_path(logical_path)
1723
File.read(manifest_path.dirname.join(asset_path), encoding: encoding)
@@ -26,11 +32,7 @@ def manifest
2632
def digested_path(logical_path)
2733
entry = manifest[logical_path]
2834

29-
if entry.is_a?(String)
30-
return entry
31-
elsif entry.is_a?(Hash)
32-
entry["digested_path"]
33-
end
35+
entry&.digested_path
3436
end
3537
end
3638
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"one.txt": {"digested_path": "one-f2e1ec14.txt","integrity": "sha384-LdS8l2QTAF8bD8WPb8QSQv0skTWHhmcnS2XU5LBkVQneGzqIqnDRskQtJvi7ADMe"}}
1+
{"one.txt": {"digested_path": "one-f2e1ec14.txt","integrity": "sha384-LdS8l2QTAF8bD8WPb8QSQv0skTWHhmcnS2XU5LBkVQneGzqIqnDRskQtJvi7ADMe"}}

test/propshaft/assembly_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ class Propshaft::AssemblyTest < ActiveSupport::TestCase
4444
assert assembly.processor.is_a?(Propshaft::Processor)
4545
end
4646

47-
class Propshaft::AssemblyTest::WithIntegrityTest < ActiveSupport::TestCase
47+
class Propshaft::AssemblyTest::WithExtensibleManifest < ActiveSupport::TestCase
4848
test "uses static resolver when manifest is present" do
4949
assembly = Propshaft::Assembly.new(ActiveSupport::OrderedOptions.new.tap { |config|
5050
config.output_path = Pathname.new("#{__dir__}/../fixtures/output")
51-
config.manifest_path = config.output_path.join(".manifest_with_integrity.json")
51+
config.manifest_path = config.output_path.join(".extensible_manifest.json")
5252
config.prefix = "/assets"
5353

5454
config.integrity_hash_algorithm = "sha384"
@@ -60,7 +60,7 @@ class Propshaft::AssemblyTest::WithIntegrityTest < ActiveSupport::TestCase
6060
test "uses dynamic resolver when manifest is missing" do
6161
assembly = Propshaft::Assembly.new(ActiveSupport::OrderedOptions.new.tap { |config|
6262
config.output_path = Pathname.new("#{__dir__}/../fixtures/assets")
63-
config.manifest_path = config.output_path.join(".manifest_with_integrity.json")
63+
config.manifest_path = config.output_path.join(".extensible_manifest.json")
6464
config.prefix = "/assets"
6565

6666
config.integrity_hash_algorithm = "sha384"

test/propshaft/output_path_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Propshaft::OutputPathTest < ActiveSupport::TestCase
88
setup do
99
@manifest = {
1010
".manifest.json": ".manifest.json",
11-
".manifest_with_integrity.json": ".manifest_with_integrity.json",
11+
".extensible_manifest.json": ".extensible_manifest.json",
1212
"one.txt": "one-f2e1ec14.txt",
1313
"one.txt.map": "one-f2e1ec15.txt.map"
1414
}.stringify_keys

test/propshaft/resolver/dynamic_test.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
class Propshaft::Resolver::DynamicTest < ActiveSupport::TestCase
55
setup do
6-
@load_path = Propshaft::LoadPath.new Pathname.new("#{__dir__}/../../fixtures/assets/first_path"), compilers: Propshaft::Compilers.new(nil)
7-
@resolver = Propshaft::Resolver::Dynamic.new(load_path: @load_path, prefix: "/assets")
6+
@resolver = create_resolver
87
end
98

109
test "resolving present asset returns uri path" do
@@ -25,4 +24,27 @@ class Propshaft::Resolver::DynamicTest < ActiveSupport::TestCase
2524
test "resolving missing asset returns nil" do
2625
assert_nil @resolver.resolve("nowhere.txt")
2726
end
27+
28+
test "integrity for asset returns value for configured hash format" do
29+
resolver = create_resolver(load_path_integrity_hash_algorithm: "sha384")
30+
assert_equal "sha384-LdS8l2QTAF8bD8WPb8QSQv0skTWHhmcnS2XU5LBkVQneGzqIqnDRskQtJvi7ADMe", resolver.integrity("one.txt")
31+
end
32+
33+
test "integrity for asset returns nil for no configured hash format" do
34+
assert_nil @resolver.integrity("one.txt")
35+
end
36+
37+
test "integrity for missing asset returns nil" do
38+
assert_nil @resolver.integrity("nowhere.txt")
39+
end
40+
41+
private
42+
def create_resolver(load_path_integrity_hash_algorithm: nil)
43+
load_path = Propshaft::LoadPath.new(
44+
Pathname.new("#{__dir__}/../../fixtures/assets/first_path"),
45+
compilers: Propshaft::Compilers.new(nil),
46+
integrity_hash_algorithm: load_path_integrity_hash_algorithm
47+
)
48+
Propshaft::Resolver::Dynamic.new(load_path: load_path, prefix: "/assets")
49+
end
2850
end

test/propshaft/resolver/static_test.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ class Propshaft::Resolver::StaticTest < ActiveSupport::TestCase
3030
assert_nil @resolver.resolve("nowhere.txt")
3131
end
3232

33+
test "integrity for asset returns nil for simple manifest" do
34+
assert_nil @resolver.integrity("one.txt")
35+
end
36+
37+
test "integrity for missing asset returns nil" do
38+
assert_nil @resolver.integrity("nowhere.txt")
39+
end
40+
3341
test "resolver requests json optimizer gems to keep parsed manifest keys as strings" do
3442
stub = Proc.new do |_, opts|
3543
assert_equal false, opts[:symbolize_names]
@@ -41,10 +49,10 @@ class Propshaft::Resolver::StaticTest < ActiveSupport::TestCase
4149
end
4250
end
4351

44-
class Propshaft::Resolver::StaticTest::WithIntegrityTest < ActiveSupport::TestCase
52+
class Propshaft::Resolver::StaticTest::WithExtensibleManifest < ActiveSupport::TestCase
4553
setup do
4654
@resolver = Propshaft::Resolver::Static.new(
47-
manifest_path: Pathname.new("#{__dir__}/../../fixtures/output/.manifest_with_integrity.json"),
55+
manifest_path: Pathname.new("#{__dir__}/../../fixtures/output/.extensible_manifest.json"),
4856
prefix: "/assets"
4957
)
5058
end
@@ -69,6 +77,14 @@ class Propshaft::Resolver::StaticTest::WithIntegrityTest < ActiveSupport::TestCa
6977
assert_nil @resolver.resolve("nowhere.txt")
7078
end
7179

80+
test "integrity for asset returns value from extensible manifest" do
81+
assert_equal "sha384-LdS8l2QTAF8bD8WPb8QSQv0skTWHhmcnS2XU5LBkVQneGzqIqnDRskQtJvi7ADMe", @resolver.integrity("one.txt")
82+
end
83+
84+
test "integrity for missing asset returns nil" do
85+
assert_nil @resolver.integrity("nowhere.txt")
86+
end
87+
7288
test "resolver requests json optimizer gems to keep parsed manifest keys as strings" do
7389
stub = Proc.new do |_, opts|
7490
assert_equal false, opts[:symbolize_names]

0 commit comments

Comments
 (0)