refactor: share gateway timeout parsing

This commit is contained in:
Peter Steinberger
2026-03-14 00:56:40 +00:00
parent d1fda7b8f2
commit 4e055d8df2
4 changed files with 83 additions and 34 deletions

View File

@@ -1,5 +1,6 @@
import type { GatewayBonjourBeacon } from "../../infra/bonjour-discovery.js";
import { colorize, theme } from "../../terminal/theme.js";
import { parseTimeoutMsWithFallback } from "../parse-timeout.js";
export type GatewayDiscoverOpts = {
timeout?: string;
@@ -7,26 +8,7 @@ export type GatewayDiscoverOpts = {
};
export function parseDiscoverTimeoutMs(raw: unknown, fallbackMs: number): number {
if (raw === undefined || raw === null) {
return fallbackMs;
}
const value =
typeof raw === "string"
? raw.trim()
: typeof raw === "number" || typeof raw === "bigint"
? String(raw)
: null;
if (value === null) {
throw new Error("invalid --timeout");
}
if (!value) {
return fallbackMs;
}
const parsed = Number.parseInt(value, 10);
if (!Number.isFinite(parsed) || parsed <= 0) {
throw new Error(`invalid --timeout: ${value}`);
}
return parsed;
return parseTimeoutMsWithFallback(raw, fallbackMs, { invalidType: "error" });
}
export function pickBeaconHost(beacon: GatewayBonjourBeacon): string | null {