openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
45 lines (44 loc) • 1.5 kB
JavaScript
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { n as runExec } from "./exec-aRIvS9s4.mjs";
import fs from "node:fs/promises";
import os from "node:os";
//#region src/infra/host-account-name.ts
let cachedName;
async function readAccountCommand(e, t) {
try {
const { stdout: n } = await runExec(e, t, {
timeoutMs: 1e3,
maxBuffer: 16384,
logOutput: false
});
return n;
} catch {
return null;
}
}
async function readHostAccountName() {
if (process.platform !== "darwin" && process.platform !== "linux") return null;
const { username: e } = os.userInfo();
const normalizeName = (t) => {
const n = t?.trim();
return n && n.toLowerCase() !== e.toLowerCase() ? truncateUtf16Safe(n, 256) : null;
};
if (process.platform === "darwin") {
const t = normalizeName(await readAccountCommand("/usr/bin/id", ["-F"]));
if (t) return t;
return normalizeName((await readAccountCommand("/usr/bin/dscl", [
".",
"-read",
`/Users/${e}`,
"RealName"
]))?.replace(/^RealName:\s*/u, ""));
}
const t = (await readAccountCommand("getent", ["passwd", e]) ?? await fs.readFile("/etc/passwd", "utf8")).split("\n").find((t) => t.split(":", 1)[0] === e)?.split(":");
return normalizeName(t?.[4]?.split(",", 1)[0]);
}
/** Best-effort human name for the gateway host account; never seeds a login name. */
function resolveHostAccountName() {
return cachedName ??= readHostAccountName().catch(() => null);
}
//#endregion
export { resolveHostAccountName as t };