From 16175a4635219c97b59fa1b51943cd2595203f39 Mon Sep 17 00:00:00 2001 From: Jokul Date: Sat, 11 Jul 2026 15:09:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E6=B8=B2=E6=9F=93?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E4=B8=AD=E7=9A=84=20Buffer=20=E8=B0=83?= =?UTF-8?q?=E7=94=A8,=20=E6=94=B9=E7=94=A8=E6=B5=8F=E8=A7=88=E5=99=A8?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E7=9A=84=20hex=20=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - binary.ts 新增 strToHex/hexToStr/binaryToHex/hexToBinary 四个函数 - KeyList.vue 导入导出功能用这些函数替代 Buffer.from() - 渲染进程无 Node.js 访问权限, 不应使用 Buffer 全局对象 --- src/renderer/src/components/KeyList.vue | 10 ++++---- src/renderer/src/utils/binary.ts | 33 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) 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 +}