diff --git a/lib/sprockets/encoding_utils.rb b/lib/sprockets/encoding_utils.rb index 32d63ea9c..434505355 100644 --- a/lib/sprockets/encoding_utils.rb +++ b/lib/sprockets/encoding_utils.rb @@ -50,6 +50,8 @@ def unmarshaled_deflated(str, window_bits = -Zlib::MAX_WBITS) Marshal.load(marshaled) end + GZIP_MTIME = RUBY_VERSION >= "2.7" ? 0 : 1 + # Public: Use gzip to compress data. # # str - String data @@ -58,7 +60,7 @@ def unmarshaled_deflated(str, window_bits = -Zlib::MAX_WBITS) def gzip(str) io = StringIO.new gz = Zlib::GzipWriter.new(io, Zlib::BEST_COMPRESSION) - gz.mtime = 1 + gz.mtime = Sprockets::EncodingUtils::GZIP_MTIME gz << str gz.finish io.string diff --git a/lib/sprockets/utils/gzip.rb b/lib/sprockets/utils/gzip.rb index 3fd5228a6..ecf2c8c30 100644 --- a/lib/sprockets/utils/gzip.rb +++ b/lib/sprockets/utils/gzip.rb @@ -10,13 +10,15 @@ class Gzip # writes contents to the `file` passed in. Sets `mtime` of # written file to passed in `mtime` module ZlibArchiver - def self.call(file, source, mtime) + MTIME = RUBY_VERSION >= "2.7" ? 0 : 1 + + def self.call(file, source) gz = Zlib::GzipWriter.new(file, Zlib::BEST_COMPRESSION) - gz.mtime = mtime + gz.mtime = MTIME gz.write(source) gz.close - File.utime(mtime, mtime, file.path) + nil end end @@ -28,8 +30,8 @@ def self.call(file, source, mtime) # writes contents to the `file` passed in. Sets `mtime` of # written file to passed in `mtime` module ZopfliArchiver - def self.call(file, source, mtime) - compressed_source = Autoload::Zopfli.deflate(source, format: :gzip, mtime: mtime) + def self.call(file, source) + compressed_source = Autoload::Zopfli.deflate(source, format: :gzip) file.write(compressed_source) file.close @@ -90,7 +92,8 @@ def cannot_compress? # Returns nothing. def compress(file, target) mtime = Sprockets::PathUtils.stat(target).mtime - archiver.call(file, source, mtime) + archiver.call(file, source) + File.utime(mtime, mtime, file.path) nil end diff --git a/test/test_encoding_utils.rb b/test/test_encoding_utils.rb index 724d923ff..36e8eabad 100644 --- a/test/test_encoding_utils.rb +++ b/test/test_encoding_utils.rb @@ -15,7 +15,7 @@ def test_deflate def test_gzip output = gzip("foobar") assert_equal 26, output.length - assert_equal [31, 139, 8, 0, 1, 0, 0, 0], output.bytes.take(8) + assert_equal [31, 139, 8, 0, Sprockets::EncodingUtils::GZIP_MTIME, 0, 0, 0], output.bytes.take(8) end def test_base64