refactor(web): extract the build marker into its own module
The version and the interface build date were computed inside the route file, where two copies of the same comment had already drifted apart. The status page needs the same answer, and a second copy would eventually give a different one. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e3ce266225
commit
d2256e2211
2 files changed
+58
-55
No files matched your search
+1
-55
@@ -1,9 +1,7 @@
|
|||||||
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
||||||
import path from 'node:path';
|
|
||||||
import { fileURLToPath } from 'node:url';
|
|
||||||
import { ACCESS_LEVELS } from '../../config/constants.js';
|
import { ACCESS_LEVELS } from '../../config/constants.js';
|
||||||
import { registerAuthRoutes } from '../auth/routes.js';
|
import { registerAuthRoutes } from '../auth/routes.js';
|
||||||
import { openEventStream } from '../events.js';
|
import { openEventStream } from '../events.js';
|
||||||
|
import { VERSION, buildAt } from '../services/build-info.js';
|
||||||
import { registerCommunityRoutes } from './community.js';
|
import { registerCommunityRoutes } from './community.js';
|
||||||
import { registerConfigurationRoutes } from './configuration.js';
|
import { registerConfigurationRoutes } from './configuration.js';
|
||||||
import { registerDeploymentRoutes } from './deployments.js';
|
import { registerDeploymentRoutes } from './deployments.js';
|
||||||
@@ -17,58 +15,6 @@ import { registerRepositoryRoutes } from './repositories.js';
|
|||||||
import { registerSettingsRoutes } from './settings.js';
|
import { registerSettingsRoutes } from './settings.js';
|
||||||
import { registerWikiRoutes } from './wiki.js';
|
import { registerWikiRoutes } from './wiki.js';
|
||||||
|
|
||||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
||||||
|
|
||||||
const VERSION = (() => {
|
|
||||||
try {
|
|
||||||
return JSON.parse(readFileSync(path.join(here, '../../../package.json'), 'utf8')).version;
|
|
||||||
} catch {
|
|
||||||
return 'inconnue';
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Newest file of the served interface.
|
|
||||||
*
|
|
||||||
* Deployments that ship an archive make it easy to look at a stale copy and
|
|
||||||
* wonder why a fix is missing; this gives the page something to state.
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Newest file of the served interface.
|
|
||||||
*
|
|
||||||
* Read from disk rather than frozen at startup: the front-end is served from
|
|
||||||
* disk on every request, so a marker captured at boot would keep announcing an
|
|
||||||
* old build after an upload — the exact confusion it exists to prevent. Cached
|
|
||||||
* briefly so a health check does not walk the tree each time.
|
|
||||||
*/
|
|
||||||
const BUILD_TTL_MS = 10_000;
|
|
||||||
let buildCache = { at: null, readAt: 0 };
|
|
||||||
|
|
||||||
function buildAt() {
|
|
||||||
if (buildCache.readAt && Date.now() - buildCache.readAt < BUILD_TTL_MS) return buildCache.at;
|
|
||||||
|
|
||||||
const root = path.join(here, '../public');
|
|
||||||
let newest = 0;
|
|
||||||
|
|
||||||
const walk = (directory) => {
|
|
||||||
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
||||||
const target = path.join(directory, entry.name);
|
|
||||||
if (entry.isDirectory()) walk(target);
|
|
||||||
else newest = Math.max(newest, statSync(target).mtimeMs);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
walk(root);
|
|
||||||
} catch {
|
|
||||||
buildCache = { at: null, readAt: Date.now() };
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
buildCache = { at: newest > 0 ? new Date(newest).toISOString() : null, readAt: Date.now() };
|
|
||||||
return buildCache.at;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route registration order matters only for readability: the router matches on
|
* Route registration order matters only for readability: the router matches on
|
||||||
* the full pattern, not on declaration order.
|
* the full pattern, not on declaration order.
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
||||||
|
import path from 'node:path';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What is actually being served (M16-T01, extracted in M19).
|
||||||
|
*
|
||||||
|
* Kept apart from the routes because the health probe, the status page and the
|
||||||
|
* sidebar all need the same answer, and a second copy would eventually give a
|
||||||
|
* different one.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
|
export const VERSION = (() => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(readFileSync(path.join(here, '../../../package.json'), 'utf8')).version;
|
||||||
|
} catch {
|
||||||
|
return 'inconnue';
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Newest file of the served interface.
|
||||||
|
*
|
||||||
|
* Read from disk rather than frozen at startup: the front-end is served from
|
||||||
|
* disk on every request, so a marker captured at boot would keep announcing an
|
||||||
|
* old build after an upload — the exact confusion it exists to prevent. Cached
|
||||||
|
* briefly so a health check does not walk the tree each time.
|
||||||
|
*/
|
||||||
|
const BUILD_TTL_MS = 10_000;
|
||||||
|
let cache = { at: null, readAt: 0 };
|
||||||
|
|
||||||
|
export function buildAt() {
|
||||||
|
if (cache.readAt && Date.now() - cache.readAt < BUILD_TTL_MS) return cache.at;
|
||||||
|
|
||||||
|
const root = path.join(here, '../public');
|
||||||
|
let newest = 0;
|
||||||
|
|
||||||
|
const walk = (directory) => {
|
||||||
|
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
||||||
|
const target = path.join(directory, entry.name);
|
||||||
|
if (entry.isDirectory()) walk(target);
|
||||||
|
else newest = Math.max(newest, statSync(target).mtimeMs);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
walk(root);
|
||||||
|
} catch {
|
||||||
|
cache = { at: null, readAt: Date.now() };
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
cache = { at: newest > 0 ? new Date(newest).toISOString() : null, readAt: Date.now() };
|
||||||
|
return cache.at;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user