refactor(src): split oversized modules

This commit is contained in:
Peter Steinberger
2026-01-14 01:08:15 +00:00
parent b2179de839
commit bcbfb357be
675 changed files with 91476 additions and 73453 deletions

112
src/daemon/launchd-plist.ts Normal file
View File

@@ -0,0 +1,112 @@
import fs from "node:fs/promises";
const plistEscape = (value: string): string =>
value
.replaceAll("&", "&")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&apos;");
const plistUnescape = (value: string): string =>
value
.replaceAll("&apos;", "'")
.replaceAll("&quot;", '"')
.replaceAll("&gt;", ">")
.replaceAll("&lt;", "<")
.replaceAll("&amp;", "&");
const renderEnvDict = (
env: Record<string, string | undefined> | undefined,
): string => {
if (!env) return "";
const entries = Object.entries(env).filter(
([, value]) => typeof value === "string" && value.trim(),
);
if (entries.length === 0) return "";
const items = entries
.map(
([key, value]) =>
`\n <key>${plistEscape(key)}</key>\n <string>${plistEscape(value?.trim() ?? "")}</string>`,
)
.join("");
return `\n <key>EnvironmentVariables</key>\n <dict>${items}\n </dict>`;
};
export async function readLaunchAgentProgramArgumentsFromFile(
plistPath: string,
): Promise<{
programArguments: string[];
workingDirectory?: string;
environment?: Record<string, string>;
sourcePath?: string;
} | null> {
try {
const plist = await fs.readFile(plistPath, "utf8");
const programMatch = plist.match(
/<key>ProgramArguments<\/key>\s*<array>([\s\S]*?)<\/array>/i,
);
if (!programMatch) return null;
const args = Array.from(
programMatch[1].matchAll(/<string>([\s\S]*?)<\/string>/gi),
).map((match) => plistUnescape(match[1] ?? "").trim());
const workingDirMatch = plist.match(
/<key>WorkingDirectory<\/key>\s*<string>([\s\S]*?)<\/string>/i,
);
const workingDirectory = workingDirMatch
? plistUnescape(workingDirMatch[1] ?? "").trim()
: "";
const envMatch = plist.match(
/<key>EnvironmentVariables<\/key>\s*<dict>([\s\S]*?)<\/dict>/i,
);
const environment: Record<string, string> = {};
if (envMatch) {
for (const pair of envMatch[1].matchAll(
/<key>([\s\S]*?)<\/key>\s*<string>([\s\S]*?)<\/string>/gi,
)) {
const key = plistUnescape(pair[1] ?? "").trim();
if (!key) continue;
const value = plistUnescape(pair[2] ?? "").trim();
environment[key] = value;
}
}
return {
programArguments: args.filter(Boolean),
...(workingDirectory ? { workingDirectory } : {}),
...(Object.keys(environment).length > 0 ? { environment } : {}),
sourcePath: plistPath,
};
} catch {
return null;
}
}
export function buildLaunchAgentPlist({
label,
comment,
programArguments,
workingDirectory,
stdoutPath,
stderrPath,
environment,
}: {
label: string;
comment?: string;
programArguments: string[];
workingDirectory?: string;
stdoutPath: string;
stderrPath: string;
environment?: Record<string, string | undefined>;
}): string {
const argsXml = programArguments
.map((arg) => `\n <string>${plistEscape(arg)}</string>`)
.join("");
const workingDirXml = workingDirectory
? `\n <key>WorkingDirectory</key>\n <string>${plistEscape(workingDirectory)}</string>`
: "";
const commentXml = comment?.trim()
? `\n <key>Comment</key>\n <string>${plistEscape(comment.trim())}</string>`
: "";
const envXml = renderEnvDict(environment);
return `<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0">\n <dict>\n <key>Label</key>\n <string>${plistEscape(label)}</string>\n ${commentXml}\n <key>RunAtLoad</key>\n <true/>\n <key>KeepAlive</key>\n <true/>\n <key>ProgramArguments</key>\n <array>${argsXml}\n </array>\n ${workingDirXml}\n <key>StandardOutPath</key>\n <string>${plistEscape(stdoutPath)}</string>\n <key>StandardErrorPath</key>\n <string>${plistEscape(stderrPath)}</string>${envXml}\n </dict>\n</plist>\n`;
}

View File

@@ -10,6 +10,10 @@ import {
LEGACY_GATEWAY_LAUNCH_AGENT_LABELS,
resolveGatewayLaunchAgentLabel,
} from "./constants.js";
import {
buildLaunchAgentPlist as buildLaunchAgentPlistImpl,
readLaunchAgentProgramArgumentsFromFile,
} from "./launchd-plist.js";
import { parseKeyValueOutput } from "./runtime-parse.js";
import type { GatewayServiceRuntime } from "./service-runtime.js";
@@ -86,45 +90,6 @@ function resolveUserPathWithHome(input: string, home: string): string {
return path.resolve(trimmed);
}
function plistEscape(value: string): string {
return value
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&apos;");
}
function plistUnescape(value: string): string {
return value
.replaceAll("&apos;", "'")
.replaceAll("&quot;", '"')
.replaceAll("&gt;", ">")
.replaceAll("&lt;", "<")
.replaceAll("&amp;", "&");
}
function renderEnvDict(
env: Record<string, string | undefined> | undefined,
): string {
if (!env) return "";
const entries = Object.entries(env).filter(
([, value]) => typeof value === "string" && value.trim(),
);
if (entries.length === 0) return "";
const items = entries
.map(
([key, value]) => `
<key>${plistEscape(key)}</key>
<string>${plistEscape(value?.trim() ?? "")}</string>`,
)
.join("");
return `
<key>EnvironmentVariables</key>
<dict>${items}
</dict>`;
}
export async function readLaunchAgentProgramArguments(
env: Record<string, string | undefined>,
): Promise<{
@@ -134,44 +99,7 @@ export async function readLaunchAgentProgramArguments(
sourcePath?: string;
} | null> {
const plistPath = resolveLaunchAgentPlistPath(env);
try {
const plist = await fs.readFile(plistPath, "utf8");
const programMatch = plist.match(
/<key>ProgramArguments<\/key>\s*<array>([\s\S]*?)<\/array>/i,
);
if (!programMatch) return null;
const args = Array.from(
programMatch[1].matchAll(/<string>([\s\S]*?)<\/string>/gi),
).map((match) => plistUnescape(match[1] ?? "").trim());
const workingDirMatch = plist.match(
/<key>WorkingDirectory<\/key>\s*<string>([\s\S]*?)<\/string>/i,
);
const workingDirectory = workingDirMatch
? plistUnescape(workingDirMatch[1] ?? "").trim()
: "";
const envMatch = plist.match(
/<key>EnvironmentVariables<\/key>\s*<dict>([\s\S]*?)<\/dict>/i,
);
const environment: Record<string, string> = {};
if (envMatch) {
for (const pair of envMatch[1].matchAll(
/<key>([\s\S]*?)<\/key>\s*<string>([\s\S]*?)<\/string>/gi,
)) {
const key = plistUnescape(pair[1] ?? "").trim();
if (!key) continue;
const value = plistUnescape(pair[2] ?? "").trim();
environment[key] = value;
}
}
return {
programArguments: args.filter(Boolean),
...(workingDirectory ? { workingDirectory } : {}),
...(Object.keys(environment).length > 0 ? { environment } : {}),
sourcePath: plistPath,
};
} catch {
return null;
}
return readLaunchAgentProgramArgumentsFromFile(plistPath);
}
export function buildLaunchAgentPlist({
@@ -191,42 +119,15 @@ export function buildLaunchAgentPlist({
stderrPath: string;
environment?: Record<string, string | undefined>;
}): string {
const argsXml = programArguments
.map((arg) => `\n <string>${plistEscape(arg)}</string>`)
.join("");
const workingDirXml = workingDirectory
? `
<key>WorkingDirectory</key>
<string>${plistEscape(workingDirectory)}</string>`
: "";
const commentXml = comment?.trim()
? `
<key>Comment</key>
<string>${plistEscape(comment.trim())}</string>`
: "";
const envXml = renderEnvDict(environment);
return `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${plistEscape(label)}</string>
${commentXml}
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>ProgramArguments</key>
<array>${argsXml}
</array>
${workingDirXml}
<key>StandardOutPath</key>
<string>${plistEscape(stdoutPath)}</string>
<key>StandardErrorPath</key>
<string>${plistEscape(stderrPath)}</string>${envXml}
</dict>
</plist>
`;
return buildLaunchAgentPlistImpl({
label,
comment,
programArguments,
workingDirectory,
stdoutPath,
stderrPath,
environment,
});
}
async function execLaunchctl(

View File

@@ -0,0 +1,74 @@
import os from "node:os";
import { runCommandWithTimeout, runExec } from "../process/exec.js";
function resolveLoginctlUser(
env: Record<string, string | undefined>,
): string | null {
const fromEnv = env.USER?.trim() || env.LOGNAME?.trim();
if (fromEnv) return fromEnv;
try {
return os.userInfo().username;
} catch {
return null;
}
}
export type SystemdUserLingerStatus = {
user: string;
linger: "yes" | "no";
};
export async function readSystemdUserLingerStatus(
env: Record<string, string | undefined>,
): Promise<SystemdUserLingerStatus | null> {
const user = resolveLoginctlUser(env);
if (!user) return null;
try {
const { stdout } = await runExec(
"loginctl",
["show-user", user, "-p", "Linger"],
{ timeoutMs: 5_000 },
);
const line = stdout
.split("\n")
.map((entry) => entry.trim())
.find((entry) => entry.startsWith("Linger="));
const value = line?.split("=")[1]?.trim().toLowerCase();
if (value === "yes" || value === "no") {
return { user, linger: value };
}
} catch {
// ignore; loginctl may be unavailable
}
return null;
}
export async function enableSystemdUserLinger(params: {
env: Record<string, string | undefined>;
user?: string;
sudoMode?: "prompt" | "non-interactive";
}): Promise<{ ok: boolean; stdout: string; stderr: string; code: number }> {
const user = params.user ?? resolveLoginctlUser(params.env);
if (!user) {
return { ok: false, stdout: "", stderr: "Missing user", code: 1 };
}
const needsSudo =
typeof process.getuid === "function" ? process.getuid() !== 0 : true;
const sudoArgs =
needsSudo && params.sudoMode !== undefined
? ["sudo", ...(params.sudoMode === "non-interactive" ? ["-n"] : [])]
: [];
const argv = [...sudoArgs, "loginctl", "enable-linger", user];
try {
const result = await runCommandWithTimeout(argv, { timeoutMs: 30_000 });
return {
ok: result.code === 0,
stdout: result.stdout,
stderr: result.stderr,
code: result.code ?? 1,
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return { ok: false, stdout: "", stderr: message, code: 1 };
}
}

126
src/daemon/systemd-unit.ts Normal file
View File

@@ -0,0 +1,126 @@
function systemdEscapeArg(value: string): string {
if (!/[\\s"\\\\]/.test(value)) return value;
return `"${value.replace(/\\\\/g, "\\\\\\\\").replace(/"/g, '\\\\"')}"`;
}
function renderEnvLines(
env: Record<string, string | undefined> | undefined,
): string[] {
if (!env) return [];
const entries = Object.entries(env).filter(
([, value]) => typeof value === "string" && value.trim(),
);
if (entries.length === 0) return [];
return entries.map(
([key, value]) =>
`Environment=${systemdEscapeArg(`${key}=${value?.trim() ?? ""}`)}`,
);
}
export function buildSystemdUnit({
description,
programArguments,
workingDirectory,
environment,
}: {
description?: string;
programArguments: string[];
workingDirectory?: string;
environment?: Record<string, string | undefined>;
}): string {
const execStart = programArguments.map(systemdEscapeArg).join(" ");
const descriptionLine = `Description=${description?.trim() || "Clawdbot Gateway"}`;
const workingDirLine = workingDirectory
? `WorkingDirectory=${systemdEscapeArg(workingDirectory)}`
: null;
const envLines = renderEnvLines(environment);
return [
"[Unit]",
descriptionLine,
"After=network-online.target",
"Wants=network-online.target",
"",
"[Service]",
`ExecStart=${execStart}`,
"Restart=always",
"RestartSec=5",
// KillMode=process ensures systemd only waits for the main process to exit.
// Without this, podman's conmon (container monitor) processes block shutdown
// since they run as children of the gateway and stay in the same cgroup.
"KillMode=process",
workingDirLine,
...envLines,
"",
"[Install]",
"WantedBy=default.target",
"",
]
.filter((line) => line !== null)
.join("\n");
}
export function parseSystemdExecStart(value: string): string[] {
const args: string[] = [];
let current = "";
let inQuotes = false;
let escapeNext = false;
for (const char of value) {
if (escapeNext) {
current += char;
escapeNext = false;
continue;
}
if (char === "\\\\") {
escapeNext = true;
continue;
}
if (char === '"') {
inQuotes = !inQuotes;
continue;
}
if (!inQuotes && /\\s/.test(char)) {
if (current) {
args.push(current);
current = "";
}
continue;
}
current += char;
}
if (current) args.push(current);
return args;
}
export function parseSystemdEnvAssignment(
raw: string,
): { key: string; value: string } | null {
const trimmed = raw.trim();
if (!trimmed) return null;
const unquoted = (() => {
if (!(trimmed.startsWith('"') && trimmed.endsWith('"'))) return trimmed;
let out = "";
let escapeNext = false;
for (const ch of trimmed.slice(1, -1)) {
if (escapeNext) {
out += ch;
escapeNext = false;
continue;
}
if (ch === "\\\\") {
escapeNext = true;
continue;
}
out += ch;
}
return out;
})();
const eq = unquoted.indexOf("=");
if (eq <= 0) return null;
const key = unquoted.slice(0, eq).trim();
if (!key) return null;
const value = unquoted.slice(eq + 1);
return { key, value };
}

View File

@@ -1,9 +1,7 @@
import { execFile } from "node:child_process";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import { runCommandWithTimeout, runExec } from "../process/exec.js";
import { colorize, isRich, theme } from "../terminal/theme.js";
import {
formatGatewayServiceDescription,
@@ -12,6 +10,16 @@ import {
} from "./constants.js";
import { parseKeyValueOutput } from "./runtime-parse.js";
import type { GatewayServiceRuntime } from "./service-runtime.js";
import {
enableSystemdUserLinger,
readSystemdUserLingerStatus,
type SystemdUserLingerStatus,
} from "./systemd-linger.js";
import {
buildSystemdUnit,
parseSystemdEnvAssignment,
parseSystemdExecStart,
} from "./systemd-unit.js";
const execFileAsync = promisify(execFile);
@@ -66,171 +74,10 @@ export function resolveSystemdUserUnitPath(
return resolveSystemdUnitPath(env);
}
function resolveLoginctlUser(
env: Record<string, string | undefined>,
): string | null {
const fromEnv = env.USER?.trim() || env.LOGNAME?.trim();
if (fromEnv) return fromEnv;
try {
return os.userInfo().username;
} catch {
return null;
}
}
export { enableSystemdUserLinger, readSystemdUserLingerStatus };
export type { SystemdUserLingerStatus };
export type SystemdUserLingerStatus = {
user: string;
linger: "yes" | "no";
};
export async function readSystemdUserLingerStatus(
env: Record<string, string | undefined>,
): Promise<SystemdUserLingerStatus | null> {
const user = resolveLoginctlUser(env);
if (!user) return null;
try {
const { stdout } = await runExec(
"loginctl",
["show-user", user, "-p", "Linger"],
{ timeoutMs: 5_000 },
);
const line = stdout
.split("\n")
.map((entry) => entry.trim())
.find((entry) => entry.startsWith("Linger="));
const value = line?.split("=")[1]?.trim().toLowerCase();
if (value === "yes" || value === "no") {
return { user, linger: value };
}
} catch {
// ignore; loginctl may be unavailable
}
return null;
}
export async function enableSystemdUserLinger(params: {
env: Record<string, string | undefined>;
user?: string;
sudoMode?: "prompt" | "non-interactive";
}): Promise<{ ok: boolean; stdout: string; stderr: string; code: number }> {
const user = params.user ?? resolveLoginctlUser(params.env);
if (!user) {
return { ok: false, stdout: "", stderr: "Missing user", code: 1 };
}
const needsSudo =
typeof process.getuid === "function" ? process.getuid() !== 0 : true;
const sudoArgs =
needsSudo && params.sudoMode !== undefined
? ["sudo", ...(params.sudoMode === "non-interactive" ? ["-n"] : [])]
: [];
const argv = [...sudoArgs, "loginctl", "enable-linger", user];
try {
const result = await runCommandWithTimeout(argv, { timeoutMs: 30_000 });
return {
ok: result.code === 0,
stdout: result.stdout,
stderr: result.stderr,
code: result.code ?? 1,
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return { ok: false, stdout: "", stderr: message, code: 1 };
}
}
function systemdEscapeArg(value: string): string {
if (!/[\s"\\]/.test(value)) return value;
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
}
function renderEnvLines(
env: Record<string, string | undefined> | undefined,
): string[] {
if (!env) return [];
const entries = Object.entries(env).filter(
([, value]) => typeof value === "string" && value.trim(),
);
if (entries.length === 0) return [];
return entries.map(
([key, value]) =>
`Environment=${systemdEscapeArg(`${key}=${value?.trim() ?? ""}`)}`,
);
}
function buildSystemdUnit({
description,
programArguments,
workingDirectory,
environment,
}: {
description?: string;
programArguments: string[];
workingDirectory?: string;
environment?: Record<string, string | undefined>;
}): string {
const execStart = programArguments.map(systemdEscapeArg).join(" ");
const descriptionLine = `Description=${description?.trim() || "Clawdbot Gateway"}`;
const workingDirLine = workingDirectory
? `WorkingDirectory=${systemdEscapeArg(workingDirectory)}`
: null;
const envLines = renderEnvLines(environment);
return [
"[Unit]",
descriptionLine,
"After=network-online.target",
"Wants=network-online.target",
"",
"[Service]",
`ExecStart=${execStart}`,
"Restart=always",
"RestartSec=5",
// KillMode=process ensures systemd only waits for the main process to exit.
// Without this, podman's conmon (container monitor) processes block shutdown
// since they run as children of the gateway and stay in the same cgroup.
"KillMode=process",
workingDirLine,
...envLines,
"",
"[Install]",
"WantedBy=default.target",
"",
]
.filter((line) => line !== null)
.join("\n");
}
function parseSystemdExecStart(value: string): string[] {
const args: string[] = [];
let current = "";
let inQuotes = false;
let escapeNext = false;
for (const char of value) {
if (escapeNext) {
current += char;
escapeNext = false;
continue;
}
if (char === "\\") {
escapeNext = true;
continue;
}
if (char === '"') {
inQuotes = !inQuotes;
continue;
}
if (!inQuotes && /\s/.test(char)) {
if (current) {
args.push(current);
current = "";
}
continue;
}
current += char;
}
if (current) args.push(current);
return args;
}
// Unit file parsing/rendering: see systemd-unit.ts
export async function readSystemdServiceExecStart(
env: Record<string, string | undefined>,
@@ -272,39 +119,6 @@ export async function readSystemdServiceExecStart(
}
}
function parseSystemdEnvAssignment(
raw: string,
): { key: string; value: string } | null {
const trimmed = raw.trim();
if (!trimmed) return null;
const unquoted = (() => {
if (!(trimmed.startsWith('"') && trimmed.endsWith('"'))) return trimmed;
let out = "";
let escapeNext = false;
for (const ch of trimmed.slice(1, -1)) {
if (escapeNext) {
out += ch;
escapeNext = false;
continue;
}
if (ch === "\\") {
escapeNext = true;
continue;
}
out += ch;
}
return out;
})();
const eq = unquoted.indexOf("=");
if (eq <= 0) return null;
const key = unquoted.slice(0, eq).trim();
if (!key) return null;
const value = unquoted.slice(eq + 1);
return { key, value };
}
export type SystemdServiceInfo = {
activeState?: string;
subState?: string;