From b8eaeef8b02163a9b07371a5917f472d86dc28c7 Mon Sep 17 00:00:00 2001 From: Ryunosuke Sato Date: Mon, 23 Sep 2024 21:52:05 +0900 Subject: [PATCH] Drop base64 gem from dependency This following warning is shown in test. ``` /Users/ryunosuke.sato/src/github.com/rails/sprockets/lib/sprockets/mime.rb:2: warning: base64 was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.4.0. You can add base64 to your Gemfile or gemspec to silence this warning. ``` Instead of adding base64 to dependency, the usage of `Base64#strict_encode64` and `Base64#encode64` have replaced with `Array#pack`. --- lib/sprockets/encoding_utils.rb | 3 +-- test/fixtures/asset/sprite.css.erb | 3 +-- test/test_context.rb | 10 +++------- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/sprockets/encoding_utils.rb b/lib/sprockets/encoding_utils.rb index d0bda9e02..32d63ea9c 100644 --- a/lib/sprockets/encoding_utils.rb +++ b/lib/sprockets/encoding_utils.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true -require 'base64' require 'stringio' require 'zlib' @@ -71,7 +70,7 @@ def gzip(str) # # Returns a encoded String def base64(str) - Base64.strict_encode64(str) + [str].pack("m0") end diff --git a/test/fixtures/asset/sprite.css.erb b/test/fixtures/asset/sprite.css.erb index e9f8c55bd..b276514ee 100644 --- a/test/fixtures/asset/sprite.css.erb +++ b/test/fixtures/asset/sprite.css.erb @@ -3,9 +3,8 @@ */ <% - require 'base64' path = File.expand_path("../POW.png", __FILE__) - data = Base64.encode64(File.open(path, "rb") { |f| f.read }) + data = [File.open(path, "rb") { |f| f.read }].pack('m') %> .pow { background: url(data:image/png;base64,<%= data %>) no-repeat; diff --git a/test/test_context.rb b/test/test_context.rb index 1838242d4..164493152 100644 --- a/test/test_context.rb +++ b/test/test_context.rb @@ -47,8 +47,7 @@ def setup test "extend context" do @env.context_class.class_eval do def datauri(path) - require 'base64' - Base64.encode64(File.open(path, "rb") { |f| f.read }) + [File.open(path, "rb") { |f| f.read }].pack('m') end end @@ -89,14 +88,13 @@ def setup assert_equal "var Foo = {};\n\nvar Bar = {};\n", @env['application.js'].to_s end - require 'base64' DataUriProcessor = proc do |input| env = input[:environment] data = input[:data] data.gsub(/url\(\"(.+?)\"\)/) do uri, _ = env.resolve($1) path, _ = env.parse_asset_uri(uri) - data = Base64.encode64(File.open(path, "rb") { |f| f.read }) + data = [File.open(path, "rb") { |f| f.read }].pack('m') "url(data:image/png;base64,#{data})" end end @@ -111,14 +109,12 @@ def setup end test "block custom processor" do - require 'base64' - @env.register_preprocessor 'text/css' do |input| env = input[:environment] input[:data].gsub(/url\(\"(.+?)\"\)/) do uri, _ = env.resolve($1) path, _ = env.parse_asset_uri(uri) - data = Base64.encode64(File.open(path, "rb") { |f| f.read }) + data = [File.open(path, "rb") { |f| f.read }].pack('m') "url(data:image/png;base64,#{data})" end end