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 {

View File

@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { parseTimeoutMs, parseTimeoutMsWithFallback } from "./parse-timeout.js";
describe("parseTimeoutMs", () => {
it("parses positive string values", () => {
expect(parseTimeoutMs("1500")).toBe(1500);
});
it("returns undefined for empty or invalid values", () => {
expect(parseTimeoutMs(undefined)).toBeUndefined();
expect(parseTimeoutMs("")).toBeUndefined();
expect(parseTimeoutMs("nope")).toBeUndefined();
});
});
describe("parseTimeoutMsWithFallback", () => {
it("returns the fallback for missing or empty values", () => {
expect(parseTimeoutMsWithFallback(undefined, 3000)).toBe(3000);
expect(parseTimeoutMsWithFallback(null, 3000)).toBe(3000);
expect(parseTimeoutMsWithFallback(" ", 3000)).toBe(3000);
});
it("parses positive numbers and strings", () => {
expect(parseTimeoutMsWithFallback(2500, 3000)).toBe(2500);
expect(parseTimeoutMsWithFallback(2500n, 3000)).toBe(2500);
expect(parseTimeoutMsWithFallback("2500", 3000)).toBe(2500);
});
it("falls back on unsupported types by default", () => {
expect(parseTimeoutMsWithFallback({}, 3000)).toBe(3000);
});
it("throws on unsupported types when requested", () => {
expect(() => parseTimeoutMsWithFallback({}, 3000, { invalidType: "error" })).toThrow(
"invalid --timeout",
);
});
it("throws on non-positive parsed values", () => {
expect(() => parseTimeoutMsWithFallback("0", 3000)).toThrow("invalid --timeout: 0");
expect(() => parseTimeoutMsWithFallback("-1", 3000)).toThrow("invalid --timeout: -1");
});
});

View File

@ -16,3 +16,39 @@ export function parseTimeoutMs(raw: unknown): number | undefined {
}
return Number.isFinite(value) ? value : undefined;
}
export function parseTimeoutMsWithFallback(
raw: unknown,
fallbackMs: number,
options: {
invalidType?: "fallback" | "error";
} = {},
): 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) {
if (options.invalidType === "error") {
throw new Error("invalid --timeout");
}
return fallbackMs;
}
if (!value) {
return fallbackMs;
}
const parsed = Number.parseInt(value, 10);
if (!Number.isFinite(parsed) || parsed <= 0) {
throw new Error(`invalid --timeout: ${value}`);
}
return parsed;
}

View File

@ -1,3 +1,4 @@
import { parseTimeoutMsWithFallback } from "../../cli/parse-timeout.js";
import { resolveGatewayPort } from "../../config/config.js";
import type { OpenClawConfig, ConfigFileSnapshot } from "../../config/types.js";
import { hasConfiguredSecretInput } from "../../config/types.secrets.js";
@ -64,20 +65,7 @@ function parseIntOrNull(value: unknown): number | null {
}
export function parseTimeoutMs(raw: unknown, fallbackMs: number): number {
const value =
typeof raw === "string"
? raw.trim()
: typeof raw === "number" || typeof raw === "bigint"
? String(raw)
: "";
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);
}
function normalizeWsUrl(value: string): string | null {