Skip to content

Commit

Permalink
Add sysinfo module. Update deps.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed May 24, 2024
1 parent 0cef796 commit 09a33fb
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 4 deletions.
9 changes: 5 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/utils",
"version": "0.12.0",
"version": "0.13.0",
"exports": {
".": "./mod.ts",
"./ansi": "./utils/ansi.ts",
Expand All @@ -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"]
Expand Down
8 changes: 8 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
112 changes: 112 additions & 0 deletions utils/sysinfo.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 09a33fb

Please sign in to comment.