From 09a33fb156d51e436f263d48c5d954a2c775680f Mon Sep 17 00:00:00 2001 From: Hexagon Date: Fri, 24 May 2024 23:58:39 +0200 Subject: [PATCH] Add sysinfo module. Update deps. --- deno.json | 9 ++-- mod.ts | 8 ++++ utils/sysinfo.ts | 112 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 utils/sysinfo.ts diff --git a/deno.json b/deno.json index 772d5b7..ba64948 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@cross/utils", - "version": "0.12.0", + "version": "0.13.0", "exports": { ".": "./mod.ts", "./ansi": "./utils/ansi.ts", @@ -9,13 +9,14 @@ "./pid": "./utils/pid.ts", "./spawn": "./utils/spawn.ts", "./table": "./utils/table.ts", - "./execpath": "./utils/execpath.ts" + "./execpath": "./utils/execpath.ts", + "./sysinfo": "./utils/sysinfo.ts" }, "imports": { - "@cross/fs": "jsr:@cross/fs@^0.0.8", + "@cross/fs": "jsr:@cross/fs@^0.1.11", "@cross/runtime": "jsr:@cross/runtime@^1.0.0", "@cross/test": "jsr:@cross/test@^0.0.9", - "@std/assert": "jsr:@std/assert@^0.221.0" + "@std/assert": "jsr:@std/assert@^0.225.3" }, "publish": { "exclude": [".github", "*.test.ts"] diff --git a/mod.ts b/mod.ts index c4a5abc..489099b 100644 --- a/mod.ts +++ b/mod.ts @@ -5,3 +5,11 @@ export { Colors, Cursor, stripAnsi } from "./utils/ansi.ts"; export { spawn } from "./utils/spawn.ts"; export type { SpawnResult } from "./utils/spawn.ts"; export { execPath, resolvedExecPath } from "./utils/execpath.ts"; +export { + loadAvg, + memoryUsage, + type ProcessMemoryInfo, + type SystemMemoryInfo, + systemMemoryInfo, + uptime, +} from "./utils/sysinfo.ts"; diff --git a/utils/sysinfo.ts b/utils/sysinfo.ts new file mode 100644 index 0000000..a99f6f4 --- /dev/null +++ b/utils/sysinfo.ts @@ -0,0 +1,112 @@ +import { CurrentRuntime, Runtime } from "@cross/runtime"; +import { freemem, loadavg, totalmem, uptime as nodeUptime } from "node:os"; + +export interface ProcessMemoryInfo { + external?: number; + heapTotal: number; + heapUsed: number; + rss: number; +} +/** Based on Deno.SystemMemoryInfo */ +export interface SystemMemoryInfo { + /** Total installed memory in bytes. */ + total: number; + /** Unused memory in bytes. */ + free: number; + /** Estimation of how much memory, in bytes, is available for starting new + * applications, without swapping. Unlike the data provided by the cache or + * free fields, this field takes into account page cache and also that not + * all reclaimable memory will be reclaimed due to items being in use. + */ + available: number; + /** Memory used by kernel buffers. */ + buffers: number; + /** Memory used by the page cache and slabs. */ + cached: number; + /** Total swap memory. */ + swapTotal: number; + /** Unused swap memory. */ + swapFree: number; +} + +export function memoryUsage(): ProcessMemoryInfo { + let memoryUsageResult: ProcessMemoryInfo; + if (CurrentRuntime === Runtime.Deno) { + //@ts-ignore cross-runtime + const { external, heapTotal, heapUsed, rss } = Deno.memoryUsage(); + memoryUsageResult = { external, heapTotal, heapUsed, rss }; + } else if ( + CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun + ) { + //@ts-ignore cross-runtime + const { external = 0, heapTotal, heapUsed, rss } = process.memoryUsage(); + memoryUsageResult = { external, heapTotal, heapUsed, rss }; + } else { + memoryUsageResult = { external: 0, heapTotal: 0, heapUsed: 0, rss: 0 }; + } + return memoryUsageResult; +} + +export function loadAvg(): number[] { + let loadAvgResult: number[]; + if (CurrentRuntime === Runtime.Deno) { + loadAvgResult = Deno.loadavg(); + } else if ( + CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun + ) { + // Node.js and Bun provide os module for loadAvg + loadAvgResult = loadavg(); + } else { + // Unsupported runtime + loadAvgResult = []; + } + return loadAvgResult; +} + +export function uptime(): number { + let uptimeResult: number; + if (CurrentRuntime === Runtime.Deno) { + uptimeResult = Deno.osUptime(); + } else if ( + CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun + ) { + // Node.js and Bun provide os module for uptime + uptimeResult = nodeUptime(); + } else { + uptimeResult = -1; + } + return uptimeResult; +} + +export function systemMemoryInfo(): SystemMemoryInfo { + let memoryInfoResult: SystemMemoryInfo; + if (CurrentRuntime === Runtime.Deno) { + memoryInfoResult = Deno.systemMemoryInfo(); + } else if ( + CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun + ) { + // Node.js and Bun don't have a direct equivalent to Deno.systemMemoryInfo + // We can try to approximate values using os module (limited information) + memoryInfoResult = { + total: totalmem(), + free: freemem(), + available: -1, // Not directly available + buffers: -1, // Not directly available + cached: -1, // Not directly available + swapTotal: -1, // Approximate swap total + swapFree: -1, // Not directly available + }; + } else { + // Unsupported runtime + memoryInfoResult = { + total: -1, + free: -1, + available: -1, + buffers: -1, + cached: -1, + swapTotal: -1, + swapFree: -1, + }; + } + return memoryInfoResult; +}