Merge pull request #214 from yunyingk/mac15/unify-expire-duration-parsing

Unify assistance link duration parsing with hour floor and stderr hints
This commit is contained in:
yunyingk authored and GitHub committed 2026-10-10 15:10:24 +08:00
commit 042d223704
4 files changed
+133 -39

No files matched your search

+15
View File
@@ -156,6 +156,14 @@ describe("auth links", () => {
expiresInHours: 168
});
// Test fractional duration: floors to 1h and prints stderr guidance
const floorResult = await runCli(["auth", "link", "--corp", "native-seal", "--expire", "90m"]);
expect(floorResult.isError).toBe(false);
expect(recordedPayload).toMatchObject({
expiresInHours: 1
});
expect(floorResult.stderr).toContain("已自动向下取整为 1 小时 (1h)");
// Test reject exceeding 7 days (168 hours)
const invalidResult = await runCli(["auth", "link", "--corp", "native-seal", "--expire", "8d"]);
expect(invalidResult.isError).toBe(true);
@@ -300,6 +308,13 @@ describe("auth links", () => {
expect(recordedHoseBody).toMatchObject({
expireDate: 604800
});
const result2 = await runCli(["auth", "hose-link", "--corp", "hose-corp", "--expire", "2"]);
expect(result2.isError).toBe(false);
expect(recordedHoseBody).toMatchObject({
expireDate: 7200
});
expect(result2.stderr).toContain("已按 2 小时 (7200s) 自动换算");
});
test("runs auth check and hidden aliases (status, diagnose, test)", async () => {
+3 -3
View File
@@ -840,7 +840,7 @@ export const HELP_REGISTRY: AreaHelp[] = [
],
options: [
{ flag: "--note <note>", description: "委托授权备注说明(如:演示测试、外部审计)" },
{ flag: "--expire, --expires-in <duration>", description: "委托授权有效期(1~168 小时,即最大 7 天),默认 24h,支持 7d、168h、24h 等" },
{ flag: "--expire, --expires-in <duration>", description: "委托授权有效期,推荐 '7d' 或 '24h'(最大 7 天,兼容秒数并自动向下取整)" },
{ flag: "--from-link <url>", description: "直接由合思协助链接换取 Seal 委托直达链接" }
],
description:
@@ -862,7 +862,7 @@ export const HELP_REGISTRY: AreaHelp[] = [
summary: "【合思外部主站】生成免密登录/第三方协助入口链接",
usage: ["seal-use auth hose-link [--corp <corpId>] [--expire <duration>] [--force]"],
options: [
{ flag: "--expire, --expires-in <duration>", description: "链接有效时间,默认 7200 秒(2h),支持 7d、2h、86400 等" },
{ flag: "--expire, --expires-in <duration>", description: "链接有效时间,推荐 '7d' 或 '2h'(默认 2h,兼容秒数如 7200)" },
{ flag: "--force, --refresh, -f", description: "强制向合思 OpenAPI 申请全新临时凭证" }
],
description:
@@ -916,7 +916,7 @@ export const HELP_REGISTRY: AreaHelp[] = [
usage: ["seal-use users delegation <userIdOrPhone|name> [--note <note>] [--expire <duration>] [--corp <corpId>]"],
options: [
{ flag: "--note <note>", description: "委托授权备注说明" },
{ flag: "--expire, --expires-in <duration>", description: "委托授权有效期(1~168 小时,即最大 7 天),默认 24h,支持 7d、168h、24h 等" }
{ flag: "--expire, --expires-in <duration>", description: "委托授权有效期,推荐 '7d' 或 '24h'(最大 7 天,兼容秒数并自动向下取整)" }
]
},
{
+19 -7
View File
@@ -119,7 +119,7 @@ describe("option helpers", () => {
expect(splitOption(true)).toBeUndefined();
});
test("parseDelegationExpireHours parses days, hours, and limits to 1..168", () => {
test("parseDelegationExpireHours parses days, hours, rounds down with stderr notice, and clamps to 1..168", () => {
expect(parseDelegationExpireHours(undefined)).toBe(24);
expect(parseDelegationExpireHours("7d")).toBe(168);
expect(parseDelegationExpireHours("1d")).toBe(24);
@@ -128,13 +128,20 @@ describe("option helpers", () => {
expect(parseDelegationExpireHours("168")).toBe(168);
expect(parseDelegationExpireHours(168)).toBe(168);
expect(parseDelegationExpireHours("86400")).toBe(24);
expect(parseDelegationExpireHours("7200s")).toBe(2);
expect(() => parseDelegationExpireHours("8d")).toThrow("1 到 168 小时(最大 7 天)");
expect(() => parseDelegationExpireHours("0h")).toThrow("1 到 168 小时(最大 7 天)");
expect(() => parseDelegationExpireHours("invalid")).toThrow("Must be a valid duration");
// Floor fractional hours and clamp minimum to 1h
expect(parseDelegationExpireHours("90m")).toBe(1);
expect(parseDelegationExpireHours("1.5h")).toBe(1);
expect(parseDelegationExpireHours("30m")).toBe(1);
expect(parseDelegationExpireHours("1800s")).toBe(1);
expect(() => parseDelegationExpireHours("8d")).toThrow("最多为 7 天 (168h)");
expect(() => parseDelegationExpireHours("0h")).toThrow("必须为大于 0 的有效时长");
expect(() => parseDelegationExpireHours("invalid")).toThrow("必须为有效时长格式");
});
test("parseHoseExpireSeconds parses days, hours, minutes, and raw seconds", () => {
test("parseHoseExpireSeconds parses days, hours, minutes, raw seconds, and converts unitless small numbers to hours", () => {
expect(parseHoseExpireSeconds(undefined)).toBe(7200);
expect(parseHoseExpireSeconds("7200")).toBe(7200);
expect(parseHoseExpireSeconds(7200)).toBe(7200);
@@ -142,8 +149,13 @@ describe("option helpers", () => {
expect(parseHoseExpireSeconds("7d")).toBe(604800);
expect(parseHoseExpireSeconds("30m")).toBe(1800);
expect(() => parseHoseExpireSeconds("0s")).toThrow("Must be a positive number of seconds");
expect(() => parseHoseExpireSeconds("invalid")).toThrow("Must be a valid duration");
// Unitless small numbers (<= 168) convert to hours automatically
expect(parseHoseExpireSeconds(2)).toBe(7200);
expect(parseHoseExpireSeconds("2")).toBe(7200);
expect(parseHoseExpireSeconds("24")).toBe(86400);
expect(() => parseHoseExpireSeconds("0s")).toThrow("必须为大于 0 的有效时长");
expect(() => parseHoseExpireSeconds("invalid")).toThrow("必须为有效时长");
});
});
+96 -29
View File
@@ -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;
}