Minify: Return `Date` objects from `statFile()`

pull/4765/head
Richard Hansen 2021-02-11 18:42:27 -05:00 committed by John McLear
parent aa11667ff7
commit 44e420b6c5
1 changed files with 5 additions and 10 deletions

View File

@ -152,7 +152,6 @@ const minify = async (req, res) => {
return; return;
} }
if (date) { if (date) {
date = new Date(date);
date.setMilliseconds(0); date.setMilliseconds(0);
res.setHeader('last-modified', date.toUTCString()); res.setHeader('last-modified', date.toUTCString());
res.setHeader('date', (new Date()).toUTCString()); res.setHeader('date', (new Date()).toUTCString());
@ -250,7 +249,7 @@ const statFile = async (filename, dirStatLimit) => {
// everything. // everything.
return [await lastModifiedDateOfEverything(), true]; return [await lastModifiedDateOfEverything(), true];
} else if (filename === 'js/require-kernel.js') { } else if (filename === 'js/require-kernel.js') {
return [requireLastModified(), true]; return [_requireLastModified, true];
} else { } else {
let stats; let stats;
try { try {
@ -263,13 +262,13 @@ const statFile = async (filename, dirStatLimit) => {
} }
throw err; throw err;
} }
return [stats.mtime.getTime(), stats.isFile()]; return [stats.mtime, stats.isFile()];
} }
}; };
const lastModifiedDateOfEverything = async () => { const lastModifiedDateOfEverything = async () => {
const folders2check = [`${ROOT_DIR}js/`, `${ROOT_DIR}css/`]; const folders2check = [`${ROOT_DIR}js/`, `${ROOT_DIR}css/`];
let latestModification = 0; let latestModification = null;
// go through this two folders // go through this two folders
await Promise.all(folders2check.map(async (path) => { await Promise.all(folders2check.map(async (path) => {
// read the files in the folder // read the files in the folder
@ -283,12 +282,9 @@ const lastModifiedDateOfEverything = async () => {
// get the stat data of this file // get the stat data of this file
const stats = await fs.stat(`${path}/${filename}`); const stats = await fs.stat(`${path}/${filename}`);
// get the modification time
const modificationTime = stats.mtime.getTime();
// compare the modification time to the highest found // compare the modification time to the highest found
if (modificationTime > latestModification) { if (latestModification == null || stats.mtime > latestModification) {
latestModification = modificationTime; latestModification = stats.mtime;
} }
})); }));
})); }));
@ -298,7 +294,6 @@ const lastModifiedDateOfEverything = async () => {
// This should be provided by the module, but until then, just use startup // This should be provided by the module, but until then, just use startup
// time. // time.
const _requireLastModified = new Date(); const _requireLastModified = new Date();
const requireLastModified = () => _requireLastModified.toUTCString();
const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`; const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`;
const getFileCompressed = async (filename, contentType) => { const getFileCompressed = async (filename, contentType) => {