fix(redis): preserve control bytes in copied values
This commit is contained in:
1 parent
330d27d718
commit
72997b88d8
4 files changed
+56
-2
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
getRedisMemberSelectionKey,
|
||||
normalizeRedisJsonDraft,
|
||||
preferredRedisValueFormat,
|
||||
redisClipboardSafeText,
|
||||
redisJsonValueText,
|
||||
redisMemberCopyText,
|
||||
redisValueCopyText,
|
||||
@@ -29,6 +30,10 @@ describe("redisValuePresentation", () => {
|
||||
expect(sanitizeRedisDisplayText("line1\nline2\tvalue\r\n")).toBe("line1\nline2\tvalue\r\n");
|
||||
});
|
||||
|
||||
it("escapes clipboard-unsafe controls without changing normal UTF-8 whitespace", () => {
|
||||
expect(redisClipboardSafeText("普通文本\n下一行\t值\x00\x06\u0085结束")).toBe("普通文本\n下一行\t值\\x00\\x06\\x85结束");
|
||||
});
|
||||
|
||||
it("strips utf8 c1 control bytes for display", () => {
|
||||
expect(sanitizeRedisDisplayText("before\u0085after")).toBe("beforeafter");
|
||||
});
|
||||
@@ -39,6 +44,10 @@ describe("redisValuePresentation", () => {
|
||||
expect(getRedisMemberSelectionKey("member", raw)).toBe(`member\n${raw}`);
|
||||
});
|
||||
|
||||
it("copies the complete Redis member when UTF-8 text contains NUL", () => {
|
||||
expect(redisMemberCopyText("before\x00after")).toBe("before\\x00after");
|
||||
});
|
||||
|
||||
it("can disambiguate duplicate stream fields with an explicit identity", () => {
|
||||
expect(getRedisMemberSelectionKey("event", "login", "stream:1:0")).not.toBe(getRedisMemberSelectionKey("event", "login", "stream:1:1"));
|
||||
});
|
||||
|
||||
@@ -249,7 +249,24 @@ export function canRenderRedisValueFormat(detail: RedisMemberDetail, format: Red
|
||||
}
|
||||
|
||||
export function redisMemberCopyText(value: unknown): string {
|
||||
return isRedisBlob(value) ? redisBlobRawText(value) : formatRedisMemberDetail(value).rawText;
|
||||
const text = isRedisBlob(value) ? redisBlobRawText(value) : formatRedisMemberDetail(value).rawText;
|
||||
return redisClipboardSafeText(text);
|
||||
}
|
||||
|
||||
export function redisClipboardSafeText(value: string): string {
|
||||
let output = "";
|
||||
for (const ch of value) {
|
||||
if (ch === "\n" || ch === "\r" || ch === "\t" || !isUtf8ControlCharacter(ch)) {
|
||||
output += ch;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Native clipboard text backends may treat embedded controls such as NUL as string terminators.
|
||||
// Keep ordinary whitespace intact, but copy other controls as visible byte-style escapes.
|
||||
const codePoint = ch.codePointAt(0)!;
|
||||
output += codePoint <= 0xff ? `\\x${codePoint.toString(16).padStart(2, "0")}` : `\\u{${codePoint.toString(16)}}`;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
export function getRedisMemberSelectionKey(title: string, value: unknown, identity = title): string {
|
||||
|
||||
Reference in new issue
Block a user