fix: guard against non-array os.loadavg() in stats page
CI / Build & Test (push) Successful in 3m9s

Normalize loadAvg to [0,0,0] fallback in both the native host handler
and the dashboard renderer to prevent TypeError when bare-node-os returns
a non-array value for loadavg().
This commit is contained in:
Raven Scott
2026-03-01 22:43:56 -05:00
parent 98ecb695f4
commit f9c651c6f5
2 changed files with 6 additions and 5 deletions
+5 -4
View File
@@ -165,7 +165,8 @@ function _renderSystemResources(ps) {
} }
const o = ps.os; const o = ps.os;
const usedMem = o.totalMemory - o.freeMemory; const usedMem = o.totalMemory - o.freeMemory;
const loadColors = o.loadAvg.map(l => l > (o.cpuCount * 0.8) ? 'var(--red)' : l > (o.cpuCount * 0.5) ? 'var(--amber)' : 'var(--green)'); const loadAvg = Array.isArray(o.loadAvg) ? o.loadAvg : [0, 0, 0];
const loadColors = loadAvg.map(l => l > (o.cpuCount * 0.8) ? 'var(--red)' : l > (o.cpuCount * 0.5) ? 'var(--amber)' : 'var(--green)');
const content = ` const content = `
<div style="margin-bottom:14px;"> <div style="margin-bottom:14px;">
@@ -178,9 +179,9 @@ function _renderSystemResources(ps) {
<div style="margin-bottom:14px;"> <div style="margin-bottom:14px;">
<div style="font-size:12px;color:var(--text3);margin-bottom:8px;">CPU Load Average</div> <div style="font-size:12px;color:var(--text3);margin-bottom:8px;">CPU Load Average</div>
<div style="display:flex;gap:8px;flex-wrap:wrap;"> <div style="display:flex;gap:8px;flex-wrap:wrap;">
${_statChip('1 min', o.loadAvg[0]?.toFixed(2) ?? '—', loadColors[0])} ${_statChip('1 min', loadAvg[0]?.toFixed(2) ?? '—', loadColors[0])}
${_statChip('5 min', o.loadAvg[1]?.toFixed(2) ?? '—', loadColors[1])} ${_statChip('5 min', loadAvg[1]?.toFixed(2) ?? '—', loadColors[1])}
${_statChip('15 min', o.loadAvg[2]?.toFixed(2) ?? '—', loadColors[2])} ${_statChip('15 min', loadAvg[2]?.toFixed(2) ?? '—', loadColors[2])}
${_statChip('Cores', o.cpuCount, 'var(--text2)')} ${_statChip('Cores', o.cpuCount, 'var(--text2)')}
</div> </div>
</div> </div>
+1 -1
View File
@@ -356,7 +356,7 @@ async function handleMessageAsync(send, msg) {
let arch = ''; let arch = '';
let totalMemory = 0; let totalMemory = 0;
let freeMemory = 0; let freeMemory = 0;
try { loadAvg = os.loadavg(); } catch (_) {} try { const la = os.loadavg(); loadAvg = Array.isArray(la) ? la : [0, 0, 0]; } catch (_) {}
try { cpuCount = os.cpus().length; } catch (_) {} try { cpuCount = os.cpus().length; } catch (_) {}
try { platform = os.platform(); } catch (_) {} try { platform = os.platform(); } catch (_) {}
try { arch = os.arch(); } catch (_) {} try { arch = os.arch(); } catch (_) {}