fix(process): capture complete host probe output

This commit is contained in:
morluto
2026-09-29 17:36:04 +08:00
parent dfad09e3b5
commit 6c68ff565f
8 changed files with 68 additions and 24 deletions
+16
View File
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { execFileOutput } from "./ExecFileOutput.js";
describe("execFileOutput", () => {
it("captures output larger than Node's default execFile limit", async () => {
const marker = "tail-marker";
const result = await execFileOutput(process.execPath, [
"-e",
`process.stdout.write("x".repeat(2 * 1024 * 1024)); process.stdout.write(${JSON.stringify(marker)});`,
]);
expect(result.stdout).toHaveLength(2 * 1024 * 1024 + marker.length);
expect(result.stdout.endsWith(marker)).toBe(true);
});
});
+30
View File
@@ -0,0 +1,30 @@
import {
execFile,
type ExecFileOptionsWithStringEncoding,
} from "node:child_process";
type ExecFileOutputOptions = Omit<
ExecFileOptionsWithStringEncoding,
"encoding" | "maxBuffer"
>;
/** Run a shell-free command while capturing its complete UTF-8 output. */
export const execFileOutput = (
command: string,
arguments_: readonly string[],
options: ExecFileOutputOptions = {},
): Promise<{ readonly stdout: string; readonly stderr: string }> =>
new Promise((resolve, reject) => {
execFile(
command,
[...arguments_],
{ ...options, encoding: "utf8", maxBuffer: Number.POSITIVE_INFINITY },
(error, stdout, stderr) => {
if (error !== null) {
reject(error);
return;
}
resolve({ stdout, stderr });
},
);
});
+3 -2
View File
@@ -8,6 +8,7 @@ import {
} from "./ProcessOwnershipObservation.js";
import { launcherIdentityFailure } from "./ProcessOwnershipIdentity.js";
import { descendantsOf, liveProcesses } from "./ProcessOwnershipProcessTree.js";
import { execFileOutput } from "./ExecFileOutput.js";
const execFileAsync = promisify(execFile);
@@ -159,7 +160,7 @@ export const parseProcessEnvironment = (
const systemHost: ProcessOwnershipHost = {
async listProcesses() {
if (process.platform === "win32") return [];
const { stdout } = await execFileAsync("ps", [
const { stdout } = await execFileOutput("ps", [
"-axo",
"pid=,ppid=,pgid=,stat=,command=",
]);
@@ -181,7 +182,7 @@ const systemHost: ProcessOwnershipHost = {
await readFile(`/proc/${pid}/environ`, "utf8"),
);
}
const { stdout } = await execFileAsync("ps", ["eww", "-p", String(pid)]);
const { stdout } = await execFileOutput("ps", ["eww", "-p", String(pid)]);
const environment: Record<string, string> = {};
for (const match of stdout.matchAll(
/(?:^|\s)([A-Za-z_][A-Za-z0-9_]*)=([^\s]*)/gu,