diff --git a/src/renderer/src/components/KeyList.vue b/src/renderer/src/components/KeyList.vue index e62b2d0..dc05747 100644 --- a/src/renderer/src/components/KeyList.vue +++ b/src/renderer/src/components/KeyList.vue @@ -4,7 +4,7 @@ import { useVirtualizer } from '@tanstack/vue-virtual' import { useKeyStore } from '@renderer/stores/key' import { useConnectionStore } from '@renderer/stores/connection' import { useI18n } from '@renderer/i18n' -import { formatKeyName } from '@renderer/utils/binary' +import { formatKeyName, strToHex, hexToStr, binaryToHex, hexToBinary } from '@renderer/utils/binary' import { ElMessage, ElMessageBox } from 'element-plus' const keyStore = useKeyStore() @@ -166,8 +166,8 @@ async function exportKeys() { try { const dumpResult = await window.electronAPI.redis.execute(connStore.activeId, 'DUMP', [key]) const ttl = await window.electronAPI.redis.getKeyTTL(connStore.activeId, key) - const hexKey = Buffer.from(key).toString('hex') - const hexValue = Buffer.from(dumpResult, 'binary').toString('hex') + const hexKey = strToHex(key) + const hexValue = binaryToHex(dumpResult) rows.push(`${hexKey},${hexValue},${ttl}`) exported++ } catch { @@ -229,8 +229,8 @@ async function importKeys() { const ttl = parseInt(parts[2], 10) try { - const key = Buffer.from(hexKey, 'hex').toString('utf-8') - const value = Buffer.from(hexValue, 'hex').toString('binary') + const key = hexToStr(hexKey) + const value = hexToBinary(hexValue) await window.electronAPI.redis.execute(connStore.activeId, 'RESTORE', [key, String(ttl), value, 'REPLACE']) success++ } catch { diff --git a/src/renderer/src/utils/binary.ts b/src/renderer/src/utils/binary.ts index efec2b6..793d7a3 100644 --- a/src/renderer/src/utils/binary.ts +++ b/src/renderer/src/utils/binary.ts @@ -14,3 +14,36 @@ export function formatKeyName(key: string): string { } return key } + +/** UTF-8 string → hex (equivalent to Buffer.from(str).toString('hex')) */ +export function strToHex(str: string): string { + const bytes = new TextEncoder().encode(str) + return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('') +} + +/** hex → UTF-8 string (equivalent to Buffer.from(hex, 'hex').toString('utf-8')) */ +export function hexToStr(hex: string): string { + const bytes = new Uint8Array(hex.length / 2) + for (let i = 0; i < hex.length; i += 2) { + bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16) + } + return new TextDecoder().decode(bytes) +} + +/** binary string (Latin-1) → hex (equivalent to Buffer.from(str, 'binary').toString('hex')) */ +export function binaryToHex(str: string): string { + let hex = '' + for (let i = 0; i < str.length; i++) { + hex += (str.charCodeAt(i) & 0xff).toString(16).padStart(2, '0') + } + return hex +} + +/** hex → binary string (Latin-1) (equivalent to Buffer.from(hex, 'hex').toString('binary')) */ +export function hexToBinary(hex: string): string { + let str = '' + for (let i = 0; i < hex.length; i += 2) { + str += String.fromCharCode(parseInt(hex.slice(i, i + 2), 16)) + } + return str +}