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
+56
-109
@@ -1,6 +1,7 @@
|
||||
import type { WebhookMode } from "../domains/seal/types.js";
|
||||
import type { MaterialPackFilePolicyPatch } from "../domains/seal/material-pack/policy.js";
|
||||
import type { ParsedArgs } from "./types.js";
|
||||
import { type DurationToken, expireSeconds, tokenizeDuration } from "../core/duration.js";
|
||||
import { usageError } from "./errors.js";
|
||||
|
||||
/**
|
||||
@@ -437,6 +438,42 @@ export function materialPackFilePolicyOptions(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an `--expire` literal through the shared tokenizer, raising the two
|
||||
* usage errors both link flags share. The format hint differs per flag only in
|
||||
* which example it recommends.
|
||||
*/
|
||||
function readExpireToken(
|
||||
value: string | boolean | number,
|
||||
options: { formatHint: string; unitlessHint?: string }
|
||||
): DurationToken {
|
||||
if (typeof value === "number") {
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
throw usageError(
|
||||
`Invalid --expire: ${value}。必须为大于 0 的有效时长${options.unitlessHint ? `,推荐使用 ${options.unitlessHint}` : ""}。`
|
||||
);
|
||||
}
|
||||
const token = tokenizeDuration(value);
|
||||
if (!token) throw usageError(`Invalid --expire: ${value}。`);
|
||||
return token;
|
||||
}
|
||||
|
||||
if (typeof value !== "string") {
|
||||
throw usageError(`Invalid --expire: ${String(value)}。`);
|
||||
}
|
||||
|
||||
const token = tokenizeDuration(value);
|
||||
if (!token) {
|
||||
throw usageError(`Invalid --expire: '${value}'。必须为有效时长格式${options.formatHint}`);
|
||||
}
|
||||
if (token.value <= 0) {
|
||||
throw usageError(
|
||||
`Invalid --expire: '${value}'。必须为大于 0 的有效时长${options.unitlessHint ? `,推荐使用 ${options.unitlessHint}` : ""}。`
|
||||
);
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
export function parseDelegationExpireHours(
|
||||
value: string | boolean | number | undefined,
|
||||
defaultHours = 24
|
||||
@@ -444,80 +481,30 @@ export function parseDelegationExpireHours(
|
||||
if (value === undefined || value === null || value === "") {
|
||||
return defaultHours;
|
||||
}
|
||||
let rawHours: number;
|
||||
let isFractionalOrTruncated = false;
|
||||
|
||||
if (typeof value === "number") {
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
throw usageError(`Invalid --expire: ${value}。必须为大于 0 的有效时长,推荐使用 '7d' 或 '24h'。`);
|
||||
}
|
||||
if (value > 168) {
|
||||
rawHours = value / 3600;
|
||||
if (value % 3600 !== 0) isFractionalOrTruncated = true;
|
||||
} else {
|
||||
rawHours = value;
|
||||
if (!Number.isInteger(value)) isFractionalOrTruncated = true;
|
||||
}
|
||||
} else if (typeof value === "string") {
|
||||
const trimmed = value.trim();
|
||||
const match = trimmed.match(
|
||||
/^(\d+(?:\.\d+)?)\s*(d|day|days|h|hr|hrs|hour|hours|m|min|mins|minute|minutes|s|sec|secs|second|seconds)?$/i
|
||||
);
|
||||
if (!match || !match[1]) {
|
||||
throw usageError(
|
||||
`Invalid --expire: '${value}'。必须为有效时长格式,推荐使用 '7d' 或 '24h'(支持 d/h/m/s 及秒数)。`
|
||||
);
|
||||
}
|
||||
const val = parseFloat(match[1]);
|
||||
if (!Number.isFinite(val) || val <= 0) {
|
||||
throw usageError(`Invalid --expire: '${value}'。必须为大于 0 的有效时长,推荐使用 '7d' 或 '24h'。`);
|
||||
}
|
||||
const unit = (match[2] ?? "").toLowerCase();
|
||||
|
||||
if (!unit) {
|
||||
if (val > 168) {
|
||||
rawHours = val / 3600;
|
||||
if (val % 3600 !== 0) isFractionalOrTruncated = true;
|
||||
} else {
|
||||
rawHours = val;
|
||||
if (!Number.isInteger(val)) isFractionalOrTruncated = true;
|
||||
}
|
||||
} else if (unit.startsWith("d")) {
|
||||
rawHours = val * 24;
|
||||
if (!Number.isInteger(rawHours)) isFractionalOrTruncated = true;
|
||||
} else if (unit.startsWith("h")) {
|
||||
rawHours = val;
|
||||
if (!Number.isInteger(rawHours)) isFractionalOrTruncated = true;
|
||||
} else if (unit.startsWith("m")) {
|
||||
rawHours = val / 60;
|
||||
if (val % 60 !== 0) isFractionalOrTruncated = true;
|
||||
} else if (unit.startsWith("s")) {
|
||||
rawHours = val / 3600;
|
||||
if (val % 3600 !== 0) isFractionalOrTruncated = true;
|
||||
} else {
|
||||
rawHours = val;
|
||||
}
|
||||
} else {
|
||||
throw usageError(`Invalid --expire: ${String(value)}。`);
|
||||
}
|
||||
const token = readExpireToken(value, {
|
||||
formatHint: ",推荐使用 '7d' 或 '24h'(支持 d/h/m/s 及秒数)",
|
||||
unitlessHint: ",推荐使用 '7d' 或 '24h'"
|
||||
});
|
||||
|
||||
// Seal delegation links are hourly and capped at 7 days.
|
||||
const rawHours = expireSeconds(token) / 3600;
|
||||
if (rawHours > 168) {
|
||||
throw usageError(
|
||||
`Invalid --expire: 计算得 ${rawHours > 168.1 ? Math.floor(rawHours) : rawHours} 小时。Seal delegation 授权有效期最多为 7 天 (168h)。建议使用 --expire 7d。`
|
||||
);
|
||||
}
|
||||
|
||||
let finalHours = Math.floor(rawHours);
|
||||
const finalHours = Math.floor(rawHours);
|
||||
|
||||
if (finalHours < 1) {
|
||||
finalHours = 1;
|
||||
console.error(
|
||||
`👉 提示: Seal 协助链接按整小时生效(最少 1 小时),输入值 ${String(value)} 已自动保底设置为 1 小时 (1h)。建议后续使用 --expire 1h。`
|
||||
);
|
||||
return finalHours;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (isFractionalOrTruncated) {
|
||||
if (!Number.isInteger(rawHours)) {
|
||||
console.error(
|
||||
`👉 提示: Seal 协助链接按整小时生效,输入值 ${String(value)} 已自动向下取整为 ${finalHours} 小时 (${finalHours}h)。建议后续使用 --expire ${finalHours}h 或 --expire 7d。`
|
||||
);
|
||||
@@ -533,59 +520,19 @@ export function parseHoseExpireSeconds(
|
||||
if (value === undefined || value === null || value === "") {
|
||||
return defaultSeconds;
|
||||
}
|
||||
let seconds: number;
|
||||
if (typeof value === "number") {
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
throw usageError(`Invalid --expire: ${value}。必须为大于 0 的有效时长。`);
|
||||
}
|
||||
if (value <= 168) {
|
||||
seconds = Math.round(value * 3600);
|
||||
console.error(
|
||||
`👉 提示: 检测到未加单位的数值 ${value},已按 ${value} 小时 (${seconds}s) 自动换算。建议明确使用 --expire ${value}h。`
|
||||
);
|
||||
} else {
|
||||
seconds = Math.round(value);
|
||||
}
|
||||
} else if (typeof value === "string") {
|
||||
const trimmed = value.trim();
|
||||
const match = trimmed.match(
|
||||
/^(\d+(?:\.\d+)?)\s*(d|day|days|h|hr|hrs|hour|hours|m|min|mins|minute|minutes|s|sec|secs|second|seconds)?$/i
|
||||
);
|
||||
if (!match || !match[1]) {
|
||||
throw usageError(
|
||||
`Invalid --expire: '${value}'。必须为有效时长,推荐使用 '7d' 或 '2h'(支持 d/h/m/s 及秒数)。`
|
||||
);
|
||||
}
|
||||
const val = parseFloat(match[1]);
|
||||
if (!Number.isFinite(val) || val <= 0) {
|
||||
throw usageError(`Invalid --expire: '${value}'。必须为大于 0 的有效时长。`);
|
||||
}
|
||||
const unit = (match[2] ?? "").toLowerCase();
|
||||
|
||||
if (!unit) {
|
||||
if (val <= 168) {
|
||||
seconds = Math.round(val * 3600);
|
||||
console.error(
|
||||
`👉 提示: 检测到未加单位的数值 ${value},已按 ${val} 小时 (${seconds}s) 自动换算。建议明确使用 --expire ${val}h。`
|
||||
);
|
||||
} else {
|
||||
seconds = Math.round(val);
|
||||
}
|
||||
} else if (unit.startsWith("d")) {
|
||||
seconds = Math.round(val * 86400);
|
||||
} else if (unit.startsWith("h")) {
|
||||
seconds = Math.round(val * 3600);
|
||||
} else if (unit.startsWith("m")) {
|
||||
seconds = Math.round(val * 60);
|
||||
} else if (unit.startsWith("s")) {
|
||||
seconds = Math.round(val);
|
||||
} else {
|
||||
seconds = Math.round(val);
|
||||
}
|
||||
} else {
|
||||
throw usageError(`Invalid --expire: ${String(value)}。`);
|
||||
const token = readExpireToken(value, {
|
||||
formatHint: ",推荐使用 '7d' 或 '2h'(支持 d/h/m/s 及秒数)"
|
||||
});
|
||||
|
||||
// A bare number is ambiguous: <=168 is read as hours, larger as seconds.
|
||||
if (token.unit === undefined && token.value <= 168) {
|
||||
console.error(
|
||||
`👉 提示: 检测到未加单位的数值 ${token.value},已按 ${token.value} 小时 (${Math.round(token.value * 3600)}s) 自动换算。建议明确使用 --expire ${token.value}h。`
|
||||
);
|
||||
}
|
||||
|
||||
const seconds = Math.round(expireSeconds(token));
|
||||
if (seconds <= 0) {
|
||||
throw usageError(`Invalid --expire: 计算得 ${seconds} 秒。必须为大于 0 的有效时长。`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user