@elgato/utils 0.4.0 → 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 +25 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/processes/posix.d.ts +6 -0
- package/dist/processes/posix.js +26 -0
- package/dist/processes/process-info.d.ts +13 -0
- package/dist/processes/process-info.js +1 -0
- package/dist/processes/win32.d.ts +6 -0
- package/dist/processes/win32.js +32 -0
- package/dist/processes.d.ts +6 -0
- package/dist/processes.js +22 -0
- package/dist/strings.d.ts +11 -0
- package/dist/strings.js +26 -0
- package/package.json +1 -1
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()`
|
|
@@ -212,6 +225,18 @@ import { withResolvers } "@elgato/utils";
|
|
|
212
225
|
const { promise, resolve, reject } = withResolvers<string>();
|
|
213
226
|
```
|
|
214
227
|
|
|
228
|
+
## Strings
|
|
229
|
+
|
|
230
|
+
### `format(format, ...args)`
|
|
231
|
+
|
|
232
|
+
Formats the specified string, replacing placeholders with their associated arguments.
|
|
233
|
+
|
|
234
|
+
```js
|
|
235
|
+
import { format } from "@elgato/utils";
|
|
236
|
+
|
|
237
|
+
format("Hello {0}, from {1}", "world", "Elgato"); // Hello world, from Elgato
|
|
238
|
+
```
|
|
239
|
+
|
|
215
240
|
## Timers
|
|
216
241
|
|
|
217
242
|
### `debounce(fn, delay)`
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ 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";
|
|
10
|
+
export * from "./strings.js";
|
|
9
11
|
export * from "./timers.js";
|
|
10
12
|
export * from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,8 @@ 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";
|
|
10
|
+
export * from "./strings.js";
|
|
9
11
|
export * from "./timers.js";
|
|
10
12
|
export * from "./types.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
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats the specified string, replacing placeholders with their associated arguments.
|
|
3
|
+
* @example Formatting
|
|
4
|
+
* format("Hello {0}", "world") // "Hello world"
|
|
5
|
+
* @example Escaping braces
|
|
6
|
+
* format("Escaped {{0}}") // "Escaped {0}"
|
|
7
|
+
* @param format Source format string.
|
|
8
|
+
* @param args Arguments used to replace placeholders.
|
|
9
|
+
* @returns The formatted string.
|
|
10
|
+
*/
|
|
11
|
+
export declare function format(format: string, ...args: unknown[]): string;
|
package/dist/strings.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats the specified string, replacing placeholders with their associated arguments.
|
|
3
|
+
* @example Formatting
|
|
4
|
+
* format("Hello {0}", "world") // "Hello world"
|
|
5
|
+
* @example Escaping braces
|
|
6
|
+
* format("Escaped {{0}}") // "Escaped {0}"
|
|
7
|
+
* @param format Source format string.
|
|
8
|
+
* @param args Arguments used to replace placeholders.
|
|
9
|
+
* @returns The formatted string.
|
|
10
|
+
*/
|
|
11
|
+
export function format(format, ...args) {
|
|
12
|
+
return (format
|
|
13
|
+
// Transform escaped braces, e.g. {{0}}, {{1}}, etc.
|
|
14
|
+
.replace(/{{(\d+)}}/g, (match, index) => {
|
|
15
|
+
return `\uE000${index}\uE001`;
|
|
16
|
+
})
|
|
17
|
+
// Replace placeholders, e.g. {0}, {1}, etc.
|
|
18
|
+
.replace(/{(\d+)}/g, (match, index) => {
|
|
19
|
+
const value = args.at(Number(index));
|
|
20
|
+
return value === undefined ? match : `${value}`;
|
|
21
|
+
})
|
|
22
|
+
// Restore escaped braces
|
|
23
|
+
.replace(/\uE000(\d+)\uE001/g, (match, index) => {
|
|
24
|
+
return `{${index}}`;
|
|
25
|
+
}));
|
|
26
|
+
}
|