Unify duration parsing behind a core/duration tokenizer
The two --expire flags each carried a byte-identical copy of the same regex and unit-conversion switch, and a third parser (parseDurationMs) lived in capabilities/helpers.ts with yet another regex. Because the copies were independent, --expire 300 could be read differently on each path without anyone noticing. core/duration.ts now owns the grammar (tokenizeDuration) and the unitless heuristic (expireSeconds: a bare number <= 168 reads as hours, larger as seconds). Each flag keeps only what genuinely differs: Seal delegation links floor to whole hours and cap at 168h, Hose links round to seconds, simulation windows are milliseconds and fall back instead of failing the batch. Adds a cross-flag regression test asserting both --expire parsers read the same literal the same way.
This commit is contained in:
1 parent
df44791779
commit
6e3d4634f1
6 files changed
+270
-125
No files matched your search
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* 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<string, DurationUnit> = {
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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): 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 rawUnit = match[2]?.toLowerCase();
|
||||
if (!rawUnit) {
|
||||
return { value: parseFloat(match[1]), unit: undefined };
|
||||
}
|
||||
|
||||
const unit = UNIT_ALIASES[rawUnit];
|
||||
if (!unit) {
|
||||
return undefined;
|
||||
}
|
||||
return { value: parseFloat(match[1]), unit };
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
return Math.round(durationSeconds(token, "s") * 1000);
|
||||
}
|
||||
Reference in new issue
Block a user