Unify assistance link duration parsing with hour floor and stderr hints
This commit is contained in:
1 parent
c094980ae5
commit
0296c2bff2
4 files changed
+133
-39
No files matched your search
+96
-29
@@ -437,90 +437,157 @@ export function materialPackFilePolicyOptions(
|
||||
};
|
||||
}
|
||||
|
||||
export function parseDelegationExpireHours(value: string | boolean | number | undefined, defaultHours = 24): number {
|
||||
export function parseDelegationExpireHours(
|
||||
value: string | boolean | number | undefined,
|
||||
defaultHours = 24
|
||||
): number {
|
||||
if (value === undefined || value === null || value === "") {
|
||||
return defaultHours;
|
||||
}
|
||||
let hours: number;
|
||||
let rawHours: number;
|
||||
let isFractionalOrTruncated = false;
|
||||
|
||||
if (typeof value === "number") {
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
throw usageError(`Invalid --expire: ${value}. Must be a positive duration (e.g. '7d', '24h', or 1..168).`);
|
||||
throw usageError(`Invalid --expire: ${value}。必须为大于 0 的有效时长,推荐使用 '7d' 或 '24h'。`);
|
||||
}
|
||||
if (value > 168 && value % 3600 === 0 && value / 3600 <= 168) {
|
||||
hours = Math.round(value / 3600);
|
||||
if (value > 168) {
|
||||
rawHours = value / 3600;
|
||||
if (value % 3600 !== 0) isFractionalOrTruncated = true;
|
||||
} else {
|
||||
hours = Math.round(value);
|
||||
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);
|
||||
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}'. Must be a valid duration (e.g. '7d', '24h', or 1..168).`);
|
||||
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 && val % 3600 === 0 && val / 3600 <= 168) {
|
||||
hours = Math.round(val / 3600);
|
||||
if (val > 168) {
|
||||
rawHours = val / 3600;
|
||||
if (val % 3600 !== 0) isFractionalOrTruncated = true;
|
||||
} else {
|
||||
hours = Math.round(val);
|
||||
rawHours = val;
|
||||
if (!Number.isInteger(val)) isFractionalOrTruncated = true;
|
||||
}
|
||||
} else if (unit.startsWith("d")) {
|
||||
hours = Math.round(val * 24);
|
||||
rawHours = val * 24;
|
||||
if (!Number.isInteger(rawHours)) isFractionalOrTruncated = true;
|
||||
} else if (unit.startsWith("h")) {
|
||||
hours = Math.round(val);
|
||||
rawHours = val;
|
||||
if (!Number.isInteger(rawHours)) isFractionalOrTruncated = true;
|
||||
} else if (unit.startsWith("m")) {
|
||||
hours = Math.round(val / 60);
|
||||
rawHours = val / 60;
|
||||
if (val % 60 !== 0) isFractionalOrTruncated = true;
|
||||
} else if (unit.startsWith("s")) {
|
||||
hours = Math.round(val / 3600);
|
||||
rawHours = val / 3600;
|
||||
if (val % 3600 !== 0) isFractionalOrTruncated = true;
|
||||
} else {
|
||||
hours = Math.round(val);
|
||||
rawHours = val;
|
||||
}
|
||||
} else {
|
||||
throw usageError(`Invalid --expire: ${String(value)}.`);
|
||||
throw usageError(`Invalid --expire: ${String(value)}。`);
|
||||
}
|
||||
|
||||
if (hours < 1 || hours > 168) {
|
||||
throw usageError(`Invalid --expire: computed ${hours} hours. Seal delegation 有效期必须在 1 到 168 小时(最大 7 天)之间。例如:--expire 7d 或 --expire 24h。`);
|
||||
if (rawHours > 168) {
|
||||
throw usageError(
|
||||
`Invalid --expire: 计算得 ${rawHours > 168.1 ? Math.floor(rawHours) : rawHours} 小时。Seal delegation 授权有效期最多为 7 天 (168h)。建议使用 --expire 7d。`
|
||||
);
|
||||
}
|
||||
return hours;
|
||||
|
||||
let finalHours = Math.floor(rawHours);
|
||||
|
||||
if (finalHours < 1) {
|
||||
finalHours = 1;
|
||||
console.error(
|
||||
`👉 提示: Seal 协助链接按整小时生效(最少 1 小时),输入值 ${String(value)} 已自动保底设置为 1 小时 (1h)。建议后续使用 --expire 1h。`
|
||||
);
|
||||
return finalHours;
|
||||
}
|
||||
|
||||
if (isFractionalOrTruncated) {
|
||||
console.error(
|
||||
`👉 提示: Seal 协助链接按整小时生效,输入值 ${String(value)} 已自动向下取整为 ${finalHours} 小时 (${finalHours}h)。建议后续使用 --expire ${finalHours}h 或 --expire 7d。`
|
||||
);
|
||||
}
|
||||
|
||||
return finalHours;
|
||||
}
|
||||
|
||||
export function parseHoseExpireSeconds(value: string | boolean | number | undefined, defaultSeconds = 7200): number {
|
||||
export function parseHoseExpireSeconds(
|
||||
value: string | boolean | number | undefined,
|
||||
defaultSeconds = 7200
|
||||
): number {
|
||||
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}. Must be a positive duration.`);
|
||||
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);
|
||||
}
|
||||
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);
|
||||
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}'. Must be a valid duration (e.g. '7d', '2h', '7200').`);
|
||||
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 || unit.startsWith("s")) {
|
||||
seconds = Math.round(val);
|
||||
|
||||
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)}.`);
|
||||
throw usageError(`Invalid --expire: ${String(value)}。`);
|
||||
}
|
||||
|
||||
if (seconds <= 0) {
|
||||
throw usageError(`Invalid --expire: computed ${seconds} seconds. Must be a positive number of seconds.`);
|
||||
throw usageError(`Invalid --expire: 计算得 ${seconds} 秒。必须为大于 0 的有效时长。`);
|
||||
}
|
||||
return seconds;
|
||||
}
|
||||
Reference in new issue
Block a user