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:
@@ -165,7 +165,8 @@ function _renderSystemResources(ps) {
|
||||
}
|
||||
const o = ps.os;
|
||||
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 = `
|
||||
<div style="margin-bottom:14px;">
|
||||
@@ -178,9 +179,9 @@ function _renderSystemResources(ps) {
|
||||
<div style="margin-bottom:14px;">
|
||||
<div style="font-size:12px;color:var(--text3);margin-bottom:8px;">CPU Load Average</div>
|
||||
<div style="display:flex;gap:8px;flex-wrap:wrap;">
|
||||
${_statChip('1 min', o.loadAvg[0]?.toFixed(2) ?? '—', loadColors[0])}
|
||||
${_statChip('5 min', o.loadAvg[1]?.toFixed(2) ?? '—', loadColors[1])}
|
||||
${_statChip('15 min', o.loadAvg[2]?.toFixed(2) ?? '—', loadColors[2])}
|
||||
${_statChip('1 min', loadAvg[0]?.toFixed(2) ?? '—', loadColors[0])}
|
||||
${_statChip('5 min', loadAvg[1]?.toFixed(2) ?? '—', loadColors[1])}
|
||||
${_statChip('15 min', loadAvg[2]?.toFixed(2) ?? '—', loadColors[2])}
|
||||
${_statChip('Cores', o.cpuCount, 'var(--text2)')}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -356,7 +356,7 @@ async function handleMessageAsync(send, msg) {
|
||||
let arch = '';
|
||||
let totalMemory = 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 { platform = os.platform(); } catch (_) {}
|
||||
try { arch = os.arch(); } catch (_) {}
|
||||
|
||||
Reference in New Issue
Block a user