refactor(gateway): split server helpers
This commit is contained in:
76
src/gateway/server-discovery.ts
Normal file
76
src/gateway/server-discovery.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { getTailnetHostname } from "../infra/tailscale.js";
|
||||
import { runExec } from "../process/exec.js";
|
||||
|
||||
export type ResolveBonjourCliPathOptions = {
|
||||
env?: NodeJS.ProcessEnv;
|
||||
argv?: string[];
|
||||
execPath?: string;
|
||||
cwd?: string;
|
||||
statSync?: (path: string) => fs.Stats;
|
||||
};
|
||||
|
||||
export function formatBonjourInstanceName(displayName: string) {
|
||||
const trimmed = displayName.trim();
|
||||
if (!trimmed) return "Clawdis";
|
||||
if (/clawdis/i.test(trimmed)) return trimmed;
|
||||
return `${trimmed} (Clawdis)`;
|
||||
}
|
||||
|
||||
export function resolveBonjourCliPath(
|
||||
opts: ResolveBonjourCliPathOptions = {},
|
||||
): string | undefined {
|
||||
const env = opts.env ?? process.env;
|
||||
const envPath = env.CLAWDIS_CLI_PATH?.trim();
|
||||
if (envPath) return envPath;
|
||||
|
||||
const statSync = opts.statSync ?? fs.statSync;
|
||||
const isFile = (candidate: string) => {
|
||||
try {
|
||||
return statSync(candidate).isFile();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const execPath = opts.execPath ?? process.execPath;
|
||||
const execDir = path.dirname(execPath);
|
||||
const siblingCli = path.join(execDir, "clawdis");
|
||||
if (isFile(siblingCli)) return siblingCli;
|
||||
|
||||
const argv = opts.argv ?? process.argv;
|
||||
const argvPath = argv[1];
|
||||
if (argvPath && isFile(argvPath)) {
|
||||
const base = path.basename(argvPath);
|
||||
if (!base.includes("gateway-daemon")) return argvPath;
|
||||
}
|
||||
|
||||
const cwd = opts.cwd ?? process.cwd();
|
||||
const distCli = path.join(cwd, "dist", "index.js");
|
||||
if (isFile(distCli)) return distCli;
|
||||
const binCli = path.join(cwd, "bin", "clawdis.js");
|
||||
if (isFile(binCli)) return binCli;
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export async function resolveTailnetDnsHint(opts?: {
|
||||
env?: NodeJS.ProcessEnv;
|
||||
exec?: typeof runExec;
|
||||
}): Promise<string | undefined> {
|
||||
const env = opts?.env ?? process.env;
|
||||
const envRaw = env.CLAWDIS_TAILNET_DNS?.trim();
|
||||
const envValue = envRaw && envRaw.length > 0 ? envRaw.replace(/\.$/, "") : "";
|
||||
if (envValue) return envValue;
|
||||
|
||||
const exec =
|
||||
opts?.exec ??
|
||||
((command, args) =>
|
||||
runExec(command, args, { timeoutMs: 1500, maxBuffer: 200_000 }));
|
||||
try {
|
||||
return await getTailnetHostname(exec);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user