fix(discord): guard cid decode to avoid URIError

This commit is contained in:
李肖然
2026-02-28 20:32:18 +08:00
committed by Peter Steinberger
parent 9aba8422ca
commit e95f96f77a

View File

@@ -423,9 +423,26 @@ function parseAgentComponentData(data: ComponentData): {
: (data as Record<string, unknown>).componentId) ??
(data as Record<string, unknown>).componentId;
const decodeSafe = (value: string): string => {
// `cid` values may be raw (not URI-encoded). Guard against malformed % sequences.
// Only attempt decoding when it looks like it contains percent-encoding.
if (!value.includes("%")) {
return value;
}
// If it has a % but not a valid %XX sequence, skip decode.
if (!/%[0-9A-Fa-f]{2}/.test(value)) {
return value;
}
try {
return decodeURIComponent(value);
} catch {
return value;
}
};
const componentId =
typeof raw === "string"
? decodeURIComponent(raw)
? decodeSafe(raw)
: typeof raw === "number"
? String(raw)
: null;