Skip to content

Commit

Permalink
Ensure compatibility with esbuilds' base32 digests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Nov 11, 2021
1 parent 3d1171d commit 53ce8d6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/sprockets/digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def hexdigest_integrity_uri(hexdigest)
#
# Returns true if the name contains a digest like string and .digested before the extension
def already_digested?(name)
return name =~ /-([0-9a-f]{7,128})\.digested/
return name =~ /-([0-9a-zA-Z]{7,128})\.digested/
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/sprockets/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def headers(env, asset, length)
# # => "0aa2105d29558f3eb790d411d7d8fb66"
#
def path_fingerprint(path)
path[/-([0-9a-f]{7,128})\.[^.]+\z/, 1]
path[/-([0-9a-zA-Z]{7,128})\.[^.]+\z/, 1]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("I was hashed by esbuild!");
12 changes: 12 additions & 0 deletions test/test_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,18 @@ def setup
FileUtils.rm(digested) if File.exist?(digested)
end

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

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

def asset(logical_path, options = {})
@env.find_asset(logical_path, **{pipeline: @pipeline}.merge(options))
end
Expand Down
14 changes: 12 additions & 2 deletions test/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ def app
assert_equal 'edabfd0f1ac5fcdae82cc7d92d1c52abb671797a3948fa9040aec1db8e61c327', digest
end

test "200 response for prehashed esbuild asset with etag digest by sprockets" do
get "/assets/esbuild-TQDC3LZV.digested.js"
assert_equal 200, last_response.status

etag = last_response.headers['ETag']
digest = etag[/"(.+)"/, 1]

assert_equal '3ebac3dc00b383de6cbdfa470d105f5a9f22708fb72c63db917ad37f288ac708', digest
end

test "ok response with fingerprint and if-nonematch etags don't match" do
get "/assets/application.js"
assert_equal 200, last_response.status
Expand Down Expand Up @@ -308,7 +318,7 @@ def app

sandbox filename do
get "/assets/tree.js"
assert_equal %[var foo;\n\n(function() {\n application.boot();\n})();\nvar bar;\nconsole.log("I was already hashed!");\nvar japanese = \"日本語\";\n], last_response.body
assert_equal %[var foo;\n\n(function() {\n application.boot();\n})();\nvar bar;\nconsole.log(\"I was hashed by esbuild!\");\nconsole.log("I was already hashed!");\nvar japanese = \"日本語\";\n], last_response.body

File.open(filename, "w") do |f|
f.write "var baz;\n"
Expand All @@ -319,7 +329,7 @@ def app
File.utime(mtime, mtime, path)

get "/assets/tree.js"
assert_equal %[var foo;\n\n(function() {\n application.boot();\n})();\nvar bar;\nvar baz;\nconsole.log("I was already hashed!");\nvar japanese = \"日本語\";\n], last_response.body
assert_equal %[var foo;\n\n(function() {\n application.boot();\n})();\nvar bar;\nvar baz;\nconsole.log(\"I was hashed by esbuild!\");\nconsole.log("I was already hashed!");\nvar japanese = \"日本語\";\n], last_response.body
end
end

Expand Down

0 comments on commit 53ce8d6

Please sign in to comment.