openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
90 lines (89 loc) • 2.76 kB
JavaScript
import { a as extractErrorCode } from "./error-coercion-hDrHOSGy.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { s as raceWithTimeout } from "./src-BPmQBLJp.mjs";
import { c as spawnCommand } from "./exec-spawn-DMKoejF-.mjs";
import { n as runExec } from "./exec-aRIvS9s4.mjs";
//#region src/gateway/server-methods/open-path.ts
const OPEN_PATH_TIMEOUT_MS = 5e3;
const XDG_OPEN_STARTUP_OBSERVATION_MS = 5e3;
const XDG_OPEN_STDERR_MAX_CHARS = 4096;
function resolveOpenPathCommand(e, t = process.platform) {
if (t === "win32") return {
command: "powershell.exe",
args: [
"-NoProfile",
"-NonInteractive",
"-Command",
`Start-Process -FilePath '${e.replaceAll("'", "''")}'`
]
};
return {
command: t === "darwin" ? "open" : "xdg-open",
args: [e]
};
}
async function observeXdgOpenStartup(e) {
const t = spawnCommand([e.command, ...e.args], {
buffer: false,
cleanup: false,
detached: true,
reject: true,
stdio: [
"ignore",
"ignore",
"pipe"
]
});
t.nodeChildProcess.unref();
let n = "";
const r = t.stderr;
r?.setEncoding("utf8");
const onStderr = (e) => {
if (n.length >= XDG_OPEN_STDERR_MAX_CHARS) return;
n += truncateUtf16Safe(String(e), XDG_OPEN_STDERR_MAX_CHARS - n.length);
};
r?.on("data", onStderr);
const releaseStderr = (e) => {
r?.off("data", onStderr);
if (e) {
r?.destroy();
return;
}
r?.resume();
r?.unref?.();
};
await raceWithTimeout(() => t.then(() => releaseStderr(true), (e) => {
releaseStderr(true);
const t = e instanceof Error ? e : new Error(String(e));
const r = n.trim();
throw r ? new Error(`${t.message}: ${r}`, { cause: t }) : t;
}), XDG_OPEN_STARTUP_OBSERVATION_MS, () => releaseStderr(false));
}
async function execOpenPath(e, t = process.platform) {
if (t === "linux") {
await observeXdgOpenStartup(e);
return;
}
await runExec(e.command, e.args, {
logOutput: false,
timeoutMs: OPEN_PATH_TIMEOUT_MS
});
}
function formatOpenPathError(e) {
if (typeof e === "object" && e && "message" in e && typeof e.message === "string") return e.message;
return String(e);
}
function isHeadlessOpenPathError(e, t, n = process.platform) {
if (n === "linux" && t.command === "xdg-open" && extractErrorCode(e) === "ENOENT") return true;
const r = formatOpenPathError(e);
return r.includes("xdg-open") && r.includes("no method available");
}
function sanitizePathForLog(e) {
const t = Array.from(e, (e) => {
const t = e.charCodeAt(0);
return t < 32 || t === 127 ? "?" : e;
}).join("");
return t.length > 120 ? `${truncateUtf16Safe(t, 117)}...` : t;
}
//#endregion
export { sanitizePathForLog as a, resolveOpenPathCommand as i, formatOpenPathError as n, isHeadlessOpenPathError as r, execOpenPath as t };