Skip to content

Commit 389732c

Browse files
committed
Do not set variable GZip#mtime but do always set File#mtime
1 parent 4dff018 commit 389732c

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

lib/sprockets/encoding_utils.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def unmarshaled_deflated(str, window_bits = -Zlib::MAX_WBITS)
5050
Marshal.load(marshaled)
5151
end
5252

53+
GZIP_MTIME = RUBY_VERSION >= "2.7" ? 0 : 1
54+
5355
# Public: Use gzip to compress data.
5456
#
5557
# str - String data
@@ -58,7 +60,7 @@ def unmarshaled_deflated(str, window_bits = -Zlib::MAX_WBITS)
5860
def gzip(str)
5961
io = StringIO.new
6062
gz = Zlib::GzipWriter.new(io, Zlib::BEST_COMPRESSION)
61-
gz.mtime = 1
63+
gz.mtime = Sprockets::EncodingUtils::GZIP_MTIME
6264
gz << str
6365
gz.finish
6466
io.string

lib/sprockets/utils/gzip.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ class Gzip
1010
# writes contents to the `file` passed in. Sets `mtime` of
1111
# written file to passed in `mtime`
1212
module ZlibArchiver
13-
def self.call(file, source, mtime)
13+
MTIME = RUBY_VERSION >= "2.7" ? 0 : 1
14+
15+
def self.call(file, source)
1416
gz = Zlib::GzipWriter.new(file, Zlib::BEST_COMPRESSION)
15-
gz.mtime = mtime
17+
gz.mtime = MTIME
1618
gz.write(source)
1719
gz.close
1820

19-
File.utime(mtime, mtime, file.path)
21+
nil
2022
end
2123
end
2224

@@ -28,8 +30,8 @@ def self.call(file, source, mtime)
2830
# writes contents to the `file` passed in. Sets `mtime` of
2931
# written file to passed in `mtime`
3032
module ZopfliArchiver
31-
def self.call(file, source, mtime)
32-
compressed_source = Autoload::Zopfli.deflate(source, format: :gzip, mtime: mtime)
33+
def self.call(file, source)
34+
compressed_source = Autoload::Zopfli.deflate(source, format: :gzip)
3335
file.write(compressed_source)
3436
file.close
3537

@@ -90,7 +92,8 @@ def cannot_compress?
9092
# Returns nothing.
9193
def compress(file, target)
9294
mtime = Sprockets::PathUtils.stat(target).mtime
93-
archiver.call(file, source, mtime)
95+
archiver.call(file, source)
96+
File.utime(mtime, mtime, file.path)
9497

9598
nil
9699
end

test/test_encoding_utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_deflate
1515
def test_gzip
1616
output = gzip("foobar")
1717
assert_equal 26, output.length
18-
assert_equal [31, 139, 8, 0, 1, 0, 0, 0], output.bytes.take(8)
18+
assert_equal [31, 139, 8, 0, Sprockets::EncodingUtils::GZIP_MTIME, 0, 0, 0], output.bytes.take(8)
1919
end
2020

2121
def test_base64

0 commit comments

Comments
 (0)