fix(redis): 修复 Redis set 类型的值中有乱码 (#2554)

This commit is contained in:
onenewcode authored and GitHub committed 2026-07-04 23:03:29 +08:00
1 parent 842dcc6cab
commit f48e751d90
3 files changed
+72 -20

No files matched your search

@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { formatRedisMemberDetail, formatRedisStringValue, getRedisMemberSelectionKey, sanitizeRedisDisplayText } from "@/lib/redis/redisValuePresentation";
describe("redisValuePresentation", () => {
it("strips control bytes from display without mutating raw member text", () => {
const raw = "send_message_to_esb\x06\x16\x06\x16send_message_to_esb";
const detail = formatRedisMemberDetail(raw);
expect(detail.text).toBe("send_message_to_esbsend_message_to_esb");
expect(detail.rawText).toBe(raw);
});
it("preserves common whitespace in display text", () => {
expect(sanitizeRedisDisplayText("line1\nline2\tvalue\r\n")).toBe("line1\nline2\tvalue\r\n");
});
it("strips utf8 c1 control bytes for display", () => {
expect(sanitizeRedisDisplayText("before\u0085after")).toBe("beforeafter");
});
it("uses raw member text for selection keys", () => {
const raw = "send_message_to_esb\x06\x16";
expect(getRedisMemberSelectionKey("member", raw)).toBe(`member\n${raw}`);
});
it("formats string values for display without changing plain text", () => {
expect(formatRedisStringValue("plain-text")).toBe("plain-text");
});
});
@@ -30,7 +30,7 @@ export function clampRedisMemberDetailSheetWidth(width: number, viewportWidth: n
export function formatRedisMemberDetail(value: unknown): RedisMemberDetail {
if (typeof value === "string") {
const json = parseRedisJsonDetail(value);
return json ? { text: json.formattedText, rawText: value, format: "json", json } : { text: value, rawText: value, format: "text" };
return json ? { text: json.formattedText, rawText: value, format: "json", json } : { text: sanitizeRedisDisplayText(value), rawText: value, format: "text" };
}
try {
@@ -49,7 +49,7 @@ export function formatRedisMemberDetail(value: unknown): RedisMemberDetail {
export function formatRedisStringValue(value: unknown): string {
if (typeof value !== "string") return String(value ?? "");
return formatRedisJsonString(value) ?? value;
return formatRedisJsonString(value) ?? sanitizeRedisDisplayText(value);
}
export function formatRedisCommandResult(value: unknown): string {
@@ -117,7 +117,26 @@ function isJsonContainer(value: unknown): boolean {
}
export function getRedisMemberSelectionKey(title: string, value: unknown): string {
return `${title}\n${formatRedisMemberDetail(value).text}`;
const detail = formatRedisMemberDetail(value);
return `${title}\n${detail.format === "json" ? detail.text : detail.rawText}`;
}
export function sanitizeRedisDisplayText(value: string): string {
let output = "";
for (const ch of value) {
if (ch === "\n" || ch === "\r" || ch === "\t") {
output += ch;
continue;
}
if (ch >= " " && ch !== "\u007f" && !isUtf8ControlCharacter(ch)) {
output += ch;
}
}
return output;
}
function isUtf8ControlCharacter(ch: string): boolean {
return /\p{Cc}/u.test(ch);
}
export function highlightRedisJsonDetail(json: string): string {