Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/sprockets/encoding_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 9 additions & 6 deletions lib/sprockets/utils/gzip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_encoding_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down