@elgato/utils 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -200,6 +200,19 @@ import { parseNumber } from "@elgato/utils";
200
200
  const number = parseNumber("13"); // 13
201
201
  ```
202
202
 
203
+ ## Processes
204
+
205
+ ### `getProcesses()`
206
+
207
+ Gets the running processes in a cross-platform way.
208
+
209
+ ```js
210
+ import { getProcesses } from "@elgato/utils";
211
+
212
+ const processes = await getProcesses();
213
+ console.log(processes);
214
+ ```
215
+
203
216
  ## Promises
204
217
 
205
218
  ### `withResolvers()`
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from "./json.js";
5
5
  export * from "./lazy.js";
6
6
  export * from "./objects.js";
7
7
  export * from "./parsers.js";
8
+ export * from "./processes.js";
8
9
  export * from "./promises.js";
9
10
  export * from "./strings.js";
10
11
  export * from "./timers.js";
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export * from "./json.js";
5
5
  export * from "./lazy.js";
6
6
  export * from "./objects.js";
7
7
  export * from "./parsers.js";
8
+ export * from "./processes.js";
8
9
  export * from "./promises.js";
9
10
  export * from "./strings.js";
10
11
  export * from "./timers.js";
@@ -0,0 +1,6 @@
1
+ import type { ProcessInfo } from "./process-info.js";
2
+ /**
3
+ * Uses `ps` to list running processes on POSIX-compliant systems (Linux, macOS, BSD, etc.).
4
+ * @returns A promise that resolves to an array of `ProcessInfo` objects representing the currently running processes on the system. Each object contains the PID and the full command line of the process. If the `ps` command fails for any reason, it returns an empty array.
5
+ */
6
+ export declare function getPosixProcesses(): Promise<ProcessInfo[]>;
@@ -0,0 +1,26 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ const execFileAsync = promisify(execFile);
4
+ /**
5
+ * Uses `ps` to list running processes on POSIX-compliant systems (Linux, macOS, BSD, etc.).
6
+ * @returns A promise that resolves to an array of `ProcessInfo` objects representing the currently running processes on the system. Each object contains the PID and the full command line of the process. If the `ps` command fails for any reason, it returns an empty array.
7
+ */
8
+ export async function getPosixProcesses() {
9
+ try {
10
+ const { stdout } = await execFileAsync("ps", ["-ax", "-ww", "-o", "pid=,command="], {
11
+ maxBuffer: 10 * 1024 * 1024,
12
+ });
13
+ return stdout
14
+ .split(/\r?\n/)
15
+ .map((line) => line.trim())
16
+ .filter(Boolean)
17
+ .map((line) => {
18
+ const match = line.match(/^(\d+)\s+(.*)$/);
19
+ return match ? { pid: Number(match[1]), commandLine: match[2] } : undefined;
20
+ })
21
+ .filter((row) => !!row && Number.isFinite(row.pid) && row.pid > 0);
22
+ }
23
+ catch {
24
+ return [];
25
+ }
26
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Represents a running process with its PID and command line.
3
+ */
4
+ export type ProcessInfo = {
5
+ /**
6
+ * The process ID (PID) of the running process. This is a positive integer that uniquely identifies the process on the system.
7
+ */
8
+ pid: number;
9
+ /**
10
+ * The full command line used to start the process, including the executable name and any arguments. This is a string that may contain spaces and should be treated as a single command line.
11
+ */
12
+ commandLine: string;
13
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { ProcessInfo } from "./process-info.js";
2
+ /**
3
+ * Uses PowerShell's `Get-CimInstance` to retrieve process information on Windows. This method is more robust than parsing `tasklist` output and handles command lines with spaces correctly.
4
+ * @returns A promise that resolves to an array of `ProcessInfo` objects representing the currently running processes on a Windows system. Each object contains the PID and the full command line of the process. If the PowerShell command fails for any reason, it returns an empty array.
5
+ */
6
+ export declare function getWindowsProcesses(): Promise<ProcessInfo[]>;
@@ -0,0 +1,32 @@
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+ const execFileAsync = promisify(execFile);
4
+ /**
5
+ * Uses PowerShell's `Get-CimInstance` to retrieve process information on Windows. This method is more robust than parsing `tasklist` output and handles command lines with spaces correctly.
6
+ * @returns A promise that resolves to an array of `ProcessInfo` objects representing the currently running processes on a Windows system. Each object contains the PID and the full command line of the process. If the PowerShell command fails for any reason, it returns an empty array.
7
+ */
8
+ export async function getWindowsProcesses() {
9
+ const script = "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance Win32_Process | Select-Object ProcessId,CommandLine,Name | ConvertTo-Json -Compress";
10
+ try {
11
+ const { stdout } = await execFileAsync("powershell", ["-NoProfile", "-Command", script], {
12
+ maxBuffer: 10 * 1024 * 1024,
13
+ encoding: "utf8",
14
+ });
15
+ // Normalize output: remove any leading UTF-8 BOM to avoid JSON.parse issues.
16
+ const normalizedStdout = stdout.replace(/^\uFEFF/, "");
17
+ if (!normalizedStdout.trim()) {
18
+ return [];
19
+ }
20
+ const parsed = JSON.parse(normalizedStdout);
21
+ const rows = Array.isArray(parsed) ? parsed : [parsed];
22
+ return rows
23
+ .map((row) => ({
24
+ pid: typeof row.ProcessId === "number" ? row.ProcessId : Number(row.ProcessId),
25
+ commandLine: row.CommandLine ?? row.Name ?? "",
26
+ }))
27
+ .filter((row) => Number.isFinite(row.pid) && row.pid > 0);
28
+ }
29
+ catch {
30
+ return [];
31
+ }
32
+ }
@@ -0,0 +1,6 @@
1
+ import type { ProcessInfo } from "./processes/process-info.js";
2
+ /**
3
+ * Gets the running processes in a cross-platform way.
4
+ * @returns A promise that resolves to an array of `ProcessInfo` objects representing the currently running processes on the system.
5
+ */
6
+ export declare function getProcesses(): Promise<ProcessInfo[]>;
@@ -0,0 +1,22 @@
1
+ import { getPosixProcesses } from "./processes/posix.js";
2
+ import { getWindowsProcesses } from "./processes/win32.js";
3
+ /**
4
+ * Gets the running processes in a cross-platform way.
5
+ * @returns A promise that resolves to an array of `ProcessInfo` objects representing the currently running processes on the system.
6
+ */
7
+ export async function getProcesses() {
8
+ switch (process.platform) {
9
+ case "win32":
10
+ return getWindowsProcesses();
11
+ case "linux":
12
+ case "darwin":
13
+ case "freebsd":
14
+ case "openbsd":
15
+ case "netbsd":
16
+ case "aix":
17
+ case "sunos":
18
+ return getPosixProcesses();
19
+ default:
20
+ throw new Error(`Unsupported platform: ${process.platform}`);
21
+ }
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elgato/utils",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Utilities used throughout the Elgato ecosystem.",
5
5
  "type": "module",
6
6
  "files": [