fix(metrics): simplify macOS memory to use Node.js os module#42
fix(metrics): simplify macOS memory to use Node.js os module#42
Conversation
Co-authored-by: Garbee <868301+Garbee@users.noreply.github.com>
…ction Co-authored-by: Garbee <868301+Garbee@users.noreply.github.com>
Co-authored-by: Garbee <868301+Garbee@users.noreply.github.com>
…racy Co-authored-by: Garbee <868301+Garbee@users.noreply.github.com>
|
|
||
| function getLinuxMemory(): MemoryInfo { | ||
| // Read /proc/meminfo for accurate memory statistics | ||
| const meminfo = execSync("cat /proc/meminfo", { encoding: "utf-8" }); |
Check warning
Code scanning / CodeQL
Unnecessary use of `cat` process Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 23 days ago
To fix this, replace the use of execSync("cat /proc/meminfo", ...) with a direct file read via Node’s fs.readFileSync. This removes the need to spawn a shell and execute cat, while preserving the current behavior of returning the file contents as a UTF‑8 string.
Concretely, in src/system-info.ts, within getLinuxMemory(), change the line:
const meminfo = execSync("cat /proc/meminfo", { encoding: "utf-8" });to:
const meminfo = readFileSync("/proc/meminfo", { encoding: "utf-8" });You then need to import readFileSync from the Node fs module at the top of the file. No other logic needs to change; the parsing of meminfo as a string remains the same and the function signature and return type are unaffected.
| @@ -1,5 +1,6 @@ | ||
| import { execSync } from "node:child_process"; | ||
| import { freemem, totalmem } from "node:os"; | ||
| import { readFileSync } from "node:fs"; | ||
|
|
||
| /** | ||
| * Native system information collector that replaces systeminformation package. | ||
| @@ -86,7 +87,7 @@ | ||
|
|
||
| function getLinuxMemory(): MemoryInfo { | ||
| // Read /proc/meminfo for accurate memory statistics | ||
| const meminfo = execSync("cat /proc/meminfo", { encoding: "utf-8" }); | ||
| const meminfo = readFileSync("/proc/meminfo", { encoding: "utf-8" }); | ||
|
|
||
| let memTotal = 0; | ||
| let memAvailable = 0; |
Co-authored-by: Garbee <868301+Garbee@users.noreply.github.com>
The vm_stat-based macOS memory calculation was inaccurate. Simplified to use Node.js native
os.freemem()andos.totalmem(), matching the Windows approach.Changes:
/proc/meminfofor MemAvailable/Active fieldsBefore (vm_stat parsing):
After (Node.js native):