Skip to content

Commit ad338d8

Browse files
committed
Fix invalid atime being set
It's not super clear from the node docs, but it appears that the value of atime needs to be in seconds since the eppoc, but Date.now returns milliseconds which sets the timestamp far into the future. This causes node tar to die when it attempts to encode this time and finds a very large integer. We can also pass a Date object directly here so that's what I've done.
1 parent 9a5eb6f commit ad338d8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module.exports = {
9797
fs.stat(fullPath, function(err, stats) {
9898
if (err) resolve(); // ignore errors
9999
else {
100-
fs.utimes(outFilePath, Date.now(), stats.mtime, function () {
100+
fs.utimes(outFilePath, new Date(), stats.mtime, function () {
101101
resolve();
102102
});
103103
}

tests/index-test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('gzip plugin', function() {
141141
if (!fs.existsSync(context.distDir)) { fs.mkdirSync(context.distDir); }
142142
if (!fs.existsSync(path.join(context.distDir, 'assets'))) { fs.mkdirSync(path.join(context.distDir, 'assets')); }
143143
fs.writeFileSync(path.join(context.distDir, context.distFiles[0]), 'alert("Hello foo world!");', 'utf8');
144-
fs.utimesSync(path.join(context.distDir, context.distFiles[0]), Date.now(), new Date("2020-01-01T00:01:02Z"));
144+
fs.utimesSync(path.join(context.distDir, context.distFiles[0]), new Date(), new Date("2020-01-01T00:01:02Z"));
145145
fs.writeFileSync(path.join(context.distDir, context.distFiles[1]), 'alert("Hello bar world!");', 'utf8');
146146
fs.writeFileSync(path.join(context.distDir, context.distFiles[2]), 'alert("Hello ignore world!");', 'utf8');
147147
plugin.beforeHook(context);
@@ -187,7 +187,7 @@ describe('gzip plugin', function() {
187187
});
188188
});
189189

190-
it('has the same timestamp as the original', function(done) {
190+
it('has the same mtime timestamp as the original', function(done) {
191191
assert.isFulfilled(plugin.willUpload(context))
192192
.then(function(result) {
193193
var mtime_gz = fs.statSync(path.join(context.distDir, result.gzippedFiles[0])).mtime.valueOf();
@@ -201,6 +201,20 @@ describe('gzip plugin', function() {
201201
});
202202
});
203203

204+
it('atime timestamp is slightly newer than the original', function(done) {
205+
assert.isFulfilled(plugin.willUpload(context))
206+
.then(function(result) {
207+
var atime_gz = fs.statSync(path.join(context.distDir, result.gzippedFiles[0])).atime.valueOf();
208+
var atime_orig = fs.statSync(path.join(context.distDir, context.distFiles[0])).atime.valueOf();
209+
210+
assert.ok(atime_gz - atime_orig < 1000);
211+
212+
done();
213+
}).catch(function(reason){
214+
done(reason);
215+
});
216+
});
217+
204218
it('does not use the same object for gzippedFiles and distFiles', function(done) {
205219
assert.isFulfilled(plugin.willUpload(context))
206220
.then(function(result) {

0 commit comments

Comments
 (0)