Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix mtime precision on some filesystems
- Loading branch information
Showing
4 changed files
with
86 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
function getMtimeToProbe() { | ||
const now = Date.now(); | ||
|
||
return new Date(now % 1000 === 0 ? now + 1 : now); | ||
} | ||
|
||
function maybeTruncateMtime(mtime, precision) { | ||
if (precision === 'ms') { | ||
return mtime; | ||
} | ||
|
||
const mtimeMs = Math.trunc(mtime.getTime() / 1000) * 1000; | ||
|
||
return new Date(mtimeMs); | ||
} | ||
|
||
function calculatePrecision(probedMtime) { | ||
return probedMtime.getTime() % 1000 === 0 ? 's' : 'ms'; | ||
} | ||
|
||
function createMtimeChecker() { | ||
let precision; | ||
|
||
return { | ||
check(issuedMtime, statMtime) { | ||
if (!precision) { | ||
precision = calculatePrecision(statMtime); | ||
} | ||
|
||
return maybeTruncateMtime(issuedMtime, precision).getTime() === maybeTruncateMtime(statMtime, precision).getTime(); | ||
}, | ||
getDate() { | ||
let mtime; | ||
|
||
// If we already calculated the precision, return Date.now() according to it | ||
if (precision) { | ||
mtime = maybeTruncateMtime(new Date(), precision); | ||
// Otherwise, return "not on the second" Date.now() in ms in order to probe | ||
} else { | ||
mtime = getMtimeToProbe(); | ||
} | ||
|
||
return mtime; | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = createMtimeChecker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters