From 53ce8d6735fc5e8923728b4068186a04b059f640 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 11 Nov 2021 16:40:54 +0100 Subject: [PATCH] Ensure compatibility with esbuilds' base32 digests --- lib/sprockets/digest_utils.rb | 2 +- lib/sprockets/server.rb | 2 +- .../app/javascripts/esbuild-TQDC3LZV.digested.js | 1 + test/test_asset.rb | 12 ++++++++++++ test/test_server.rb | 14 ++++++++++++-- 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/server/app/javascripts/esbuild-TQDC3LZV.digested.js diff --git a/lib/sprockets/digest_utils.rb b/lib/sprockets/digest_utils.rb index 041db2fe7..d50fcf0d1 100644 --- a/lib/sprockets/digest_utils.rb +++ b/lib/sprockets/digest_utils.rb @@ -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 diff --git a/lib/sprockets/server.rb b/lib/sprockets/server.rb index ce425a322..231ea7505 100644 --- a/lib/sprockets/server.rb +++ b/lib/sprockets/server.rb @@ -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 diff --git a/test/fixtures/server/app/javascripts/esbuild-TQDC3LZV.digested.js b/test/fixtures/server/app/javascripts/esbuild-TQDC3LZV.digested.js new file mode 100644 index 000000000..0fa76cff2 --- /dev/null +++ b/test/fixtures/server/app/javascripts/esbuild-TQDC3LZV.digested.js @@ -0,0 +1 @@ +console.log("I was hashed by esbuild!"); \ No newline at end of file diff --git a/test/test_asset.rb b/test/test_asset.rb index 654b5e67e..d06b6b6f4 100644 --- a/test/test_asset.rb +++ b/test/test_asset.rb @@ -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 diff --git a/test/test_server.rb b/test/test_server.rb index 40b63c3c9..8f2259102 100644 --- a/test/test_server.rb +++ b/test/test_server.rb @@ -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 @@ -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" @@ -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