fix: 移除渲染进程中的 Buffer 调用, 改用浏览器兼容的 hex 转换
- binary.ts 新增 strToHex/hexToStr/binaryToHex/hexToBinary 四个函数 - KeyList.vue 导入导出功能用这些函数替代 Buffer.from() - 渲染进程无 Node.js 访问权限, 不应使用 Buffer 全局对象
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user