feat(redis): support renaming keys
This commit is contained in:
21 files changed
+297
-2
No files matched your search
@@ -443,6 +443,7 @@ export const redisGetStreamConsumers = forward("redisGetStreamConsumers");
|
||||
export const redisGetStreamPending = forward("redisGetStreamPending");
|
||||
export const redisSetString = forward("redisSetString");
|
||||
export const redisDeleteKey = forward("redisDeleteKey");
|
||||
export const redisRenameKey = forward("redisRenameKey");
|
||||
export const redisHashSet = forward("redisHashSet");
|
||||
export const redisHashDel = forward("redisHashDel");
|
||||
export const redisHashFieldSetTtl = forward("redisHashFieldSetTtl");
|
||||
|
||||
@@ -2535,6 +2535,10 @@ export async function redisDeleteKey(connectionId: string, db: number, keyRaw: s
|
||||
return post("/api/redis/delete-key", { connectionId, db, keyRaw });
|
||||
}
|
||||
|
||||
export async function redisRenameKey(connectionId: string, db: number, keyRaw: string, newKeyRaw: string): Promise<void> {
|
||||
return post("/api/redis/rename-key", { connectionId, db, keyRaw, newKeyRaw });
|
||||
}
|
||||
|
||||
export async function redisHashSet(connectionId: string, db: number, keyRaw: string, field: string, value: string, ttl?: number): Promise<void> {
|
||||
return post("/api/redis/hash-set", {
|
||||
connectionId,
|
||||
|
||||
@@ -2342,6 +2342,10 @@ export async function redisDeleteKey(connectionId: string, db: number, keyRaw: s
|
||||
return invoke("redis_delete_key", { connectionId, db, keyRaw });
|
||||
}
|
||||
|
||||
export async function redisRenameKey(connectionId: string, db: number, keyRaw: string, newKeyRaw: string): Promise<void> {
|
||||
return invoke("redis_rename_key", { connectionId, db, keyRaw, newKeyRaw });
|
||||
}
|
||||
|
||||
export async function redisHashSet(connectionId: string, db: number, keyRaw: string, field: string, value: string, ttl?: number): Promise<void> {
|
||||
return invoke("redis_hash_set", {
|
||||
connectionId,
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { redisKeyRawToText, redisKeyTextToDisplay, redisKeyTextToRaw } from "./redisCommandSession";
|
||||
|
||||
describe("Redis key text encoding", () => {
|
||||
it("round-trips UTF-8 keys without treating backslashes as display escapes", () => {
|
||||
const key = "cache\\session";
|
||||
const raw = redisKeyTextToRaw(key);
|
||||
|
||||
expect(redisKeyRawToText(raw)).toBe(key);
|
||||
expect(redisKeyTextToDisplay(key)).toBe("cache\\\\session");
|
||||
});
|
||||
|
||||
it("does not coerce binary key bytes into a text key name", () => {
|
||||
expect(redisKeyRawToText("/w==")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -20,3 +20,17 @@ export function redisKeyTextToRaw(key: string): string {
|
||||
}
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
/** Returns null when a Redis key contains bytes that cannot be represented in a text input. */
|
||||
export function redisKeyRawToText(keyRaw: string): string | null {
|
||||
try {
|
||||
const bytes = Uint8Array.from(atob(keyRaw), (character) => character.charCodeAt(0));
|
||||
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function redisKeyTextToDisplay(key: string): string {
|
||||
return key.replaceAll("\\", "\\\\");
|
||||
}
|
||||
Reference in new issue
Block a user