37 lines
771 B
TypeScript
37 lines
771 B
TypeScript
import fs from "node:fs";
|
|
|
|
export function resolveCacheTtlMs(params: {
|
|
envValue: string | undefined;
|
|
defaultTtlMs: number;
|
|
}): number {
|
|
const { envValue, defaultTtlMs } = params;
|
|
if (envValue) {
|
|
const parsed = Number.parseInt(envValue, 10);
|
|
if (Number.isFinite(parsed) && parsed >= 0) {
|
|
return parsed;
|
|
}
|
|
}
|
|
return defaultTtlMs;
|
|
}
|
|
|
|
export function isCacheEnabled(ttlMs: number): boolean {
|
|
return ttlMs > 0;
|
|
}
|
|
|
|
export type FileStatSnapshot = {
|
|
mtimeMs: number;
|
|
sizeBytes: number;
|
|
};
|
|
|
|
export function getFileStatSnapshot(filePath: string): FileStatSnapshot | undefined {
|
|
try {
|
|
const stats = fs.statSync(filePath);
|
|
return {
|
|
mtimeMs: stats.mtimeMs,
|
|
sizeBytes: stats.size,
|
|
};
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|