/** * Duration tokenizing shared by every flag that accepts `7d` / `1.5h` / `300`. * * The tokenizer owns exactly one job: turning the written form into a magnitude * plus a canonical unit. Interpretation stays with each caller, because the * three consumers genuinely disagree — Seal delegation links take whole hours * in 1..168, Hose assistance links take seconds, simulation windows take * milliseconds. Sharing the grammar (and the unitless heuristic) is what stops * `--expire 300` from parsing two different ways in two files. */ export type DurationUnit = "ms" | "s" | "m" | "h" | "d"; export interface DurationToken { /** The magnitude exactly as written, before any unit conversion. */ value: number; /** `undefined` when the input carried no unit, e.g. `300`. */ unit: DurationUnit | undefined; } const UNIT_ALIASES: Record = { ms: "ms", s: "s", sec: "s", secs: "s", second: "s", seconds: "s", m: "m", min: "m", mins: "m", minute: "m", minutes: "m", h: "h", hr: "h", hrs: "h", hour: "h", hours: "h", d: "d", day: "d", days: "d" }; const DURATION_PATTERN = /^(\d+(?:\.\d+)?)\s*([a-z]+)?$/i; export interface TokenizeDurationOptions { /** * Units this caller accepts. Restricting the set keeps a shared tokenizer * from quietly widening a flag's grammar: sub-second input was a usage error * for `--expire` before the parsers were unified, and must stay one. */ units?: readonly DurationUnit[]; } /** * Parse a duration literal. Returns `undefined` when the input cannot be read * at all — callers decide whether that is a usage error or a default. * * A unitless number keeps whatever magnitude it had; pair it with * {@link expireSeconds} or an explicit `unitless` argument to resolve it. */ export function tokenizeDuration( value: string | number, options: TokenizeDurationOptions = {} ): DurationToken | undefined { if (typeof value === "number") { return Number.isFinite(value) ? { value, unit: undefined } : undefined; } if (typeof value !== "string") { return undefined; } const match = value.trim().match(DURATION_PATTERN); if (!match || !match[1]) { return undefined; } const magnitude = parseFloat(match[1]); if (!Number.isFinite(magnitude)) { return undefined; } const rawUnit = match[2]?.toLowerCase(); if (!rawUnit) { return { value: magnitude, unit: undefined }; } const unit = UNIT_ALIASES[rawUnit]; if (!unit) { return undefined; } if (options.units && !options.units.includes(unit)) { return undefined; } return { value: magnitude, unit }; } /** * Units the `--expire` flags accept. Sub-second precision is meaningless for * an assistance link and was rejected before the parsers were shared. */ export const EXPIRE_UNITS: readonly DurationUnit[] = ["d", "h", "m", "s"]; /** Convert a token to seconds, resolving a unitless value as `unitless`. */ export function durationSeconds(token: DurationToken, unitless: DurationUnit): number { switch (token.unit ?? unitless) { case "ms": return token.value / 1000; case "m": return token.value * 60; case "h": return token.value * 3600; case "d": return token.value * 86400; case "s": default: return token.value; } } /** * Unitless `--expire` heuristic shared by the Seal and Hose link flags: * a bare number at most 168 reads as hours (7 days of them), a larger one * reads as seconds. Without this, `--expire 300` would mean 300 hours on one * path and 5 minutes on another. */ export function expireSeconds(token: DurationToken): number { return durationSeconds(token, token.value <= 168 ? "h" : "s"); } /** Default used by {@link parseDurationMs} when the literal is unreadable. */ export const DEFAULT_DURATION_MS = 600_000; /** * Simulation windows and poll intervals: milliseconds, unitless input read as * seconds, and an unreadable literal falls back to the default instead of * failing the call — a poll interval is not worth aborting a batch over. */ export function parseDurationMs(duration: string, fallbackMs = DEFAULT_DURATION_MS): number { const token = tokenizeDuration(duration); if (!token || token.value < 0) { return fallbackMs; } const milliseconds = Math.round(durationSeconds(token, "s") * 1000); return Number.isFinite(milliseconds) ? milliseconds : fallbackMs; }