Skip to content

Commit

Permalink
Drop base64 gem from dependency
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
tricknotes committed Sep 23, 2024
1 parent 3bea557 commit b8eaeef
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
3 changes: 1 addition & 2 deletions lib/sprockets/encoding_utils.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
require 'base64'
require 'stringio'
require 'zlib'

Expand Down Expand Up @@ -71,7 +70,7 @@ def gzip(str)
#
# Returns a encoded String
def base64(str)
Base64.strict_encode64(str)
[str].pack("m0")
end


Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/asset/sprite.css.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions test/test_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b8eaeef

Please sign in to comment.