70 lines
3.9 KiB
JavaScript
70 lines
3.9 KiB
JavaScript
"use strict";
|
|
|
|
// adpanel:backup-cap-daemon-enforced — ONE home for resolving the panel's
|
|
// per-server backup cap OUTSIDE the two monoliths. index.js
|
|
// (SCHEDULER_MAX_BACKUPS_PER_SERVER) and nodes.js (MAX_BACKUPS_PER_SERVER)
|
|
// each keep their own IIFE over the same env override — both are pinned in
|
|
// lockstep by tests/backup-create-cap-refusal.test.js and must NOT be
|
|
// replaced by this module (their source is slice-evaluated by that test).
|
|
// This resolver exists for the modules that transmit server resources to the
|
|
// node daemon (services/server-create.js, utils/dashboard-assistant-tools.js),
|
|
// which can require utils/ but can never require index.js. Parsing mirrors
|
|
// the two IIFEs exactly: SCHEDULER_MAX_BACKUPS_PER_SERVER must be a finite
|
|
// integer > 0, anything else keeps the built-in 50. A function (not a
|
|
// module-load constant) so tests can drive the env override per case.
|
|
//
|
|
// The resolved value is what the panel's count-then-create layers enforce
|
|
// (scheduler fire path, interactive relay) AND what this module's consumers
|
|
// transmit to the daemon as resources.backupsMax, so the daemon's op-lock
|
|
// bound and the panel's fast-fail bound agree. The daemon treats a MISSING
|
|
// or non-positive backupsMax as "no cap" (main.go: `if backupsMax > 0`),
|
|
// so a default of 0/undefined would silently disable the daemon layer —
|
|
// resolvePanelBackupsCap can never return either.
|
|
|
|
const PANEL_BACKUPS_CAP_DEFAULT = 50;
|
|
|
|
function resolvePanelBackupsCap() {
|
|
const configured = Number(process.env.SCHEDULER_MAX_BACKUPS_PER_SERVER);
|
|
return Number.isInteger(configured) && configured > 0 ? configured : PANEL_BACKUPS_CAP_DEFAULT;
|
|
}
|
|
|
|
// adpanel:backup-cap-parse-hardening — ONE home for turning a caller-supplied
|
|
// backupsMax into a daemon-safe payload value. The previous per-site pattern
|
|
// `parseInt(x, 10) || 0` mapped garbage ("abc" → 0) and negatives ("-5" → -5)
|
|
// both onto the daemon's "no cap" branch (`if backupsMax > 0`), silently
|
|
// REMOVING the bound. Here: absent → the panel cap; garbage/non-finite or
|
|
// negative → the panel cap (degrade to the bound, never to unlimited); a
|
|
// finite number >= 0 passes through truncated toward zero. The deliberate-
|
|
// zero set is EXACTLY: a number 0, or an empty/whitespace-only string — the
|
|
// settings UI's zero field — either means deliberate unlimited; every other
|
|
// garbage shape degrades to the panel cap (Number([]) === Number(false) ===
|
|
// 0, so coercible-to-zero must never read as a deliberate zero). Numeric
|
|
// strings ("12", "0", " 7 ", "0.5") take Number()'s value with the same
|
|
// finite/≥0 gate ("0.5" truncates to 0 — the trunc floor, not a gate
|
|
// bypass). index.js's resources PATCH route keeps an inline copy of the same
|
|
// classification ONLY because the monolith's cap constant is slice-pinned to
|
|
// its own IIFE and must not resolve through this module (see the header note
|
|
// above) — the parity is pinned by tests/backup-cap-daemon-wiring.test.js.
|
|
function resolveBackupsMaxFromInput(rawValue) {
|
|
let parsed = NaN;
|
|
if (typeof rawValue === "number") {
|
|
// A finite number IS the caller's explicit value — accept it as-is
|
|
// (0 = the deliberate "no daemon cap"); non-finite (NaN/±Infinity) → cap.
|
|
if (Number.isFinite(rawValue)) parsed = Math.trunc(rawValue);
|
|
} else if (typeof rawValue === "string") {
|
|
// Empty/whitespace-only = the settings UI's zero field (deliberate 0).
|
|
// Any other string takes Number()'s value; garbage ("abc") stays NaN.
|
|
if (rawValue.trim() === "") parsed = 0;
|
|
else if (Number.isFinite(Number(rawValue))) parsed = Math.trunc(Number(rawValue));
|
|
}
|
|
// Everything else (arrays, booleans, objects; null/undefined handled
|
|
// upstream) stays NaN → the panel cap.
|
|
return Number.isFinite(parsed) && parsed >= 0 ? parsed : resolvePanelBackupsCap();
|
|
}
|
|
|
|
module.exports = {
|
|
resolvePanelBackupsCap,
|
|
resolveBackupsMaxFromInput,
|
|
PANEL_BACKUPS_CAP_DEFAULT,
|
|
};
|