修复 Rawproto 5 字节 varint 限制

Author: dannisliangCreated Sep 28, 2026Updated Sep 28, 2026

一、

rawproto.js

1.1 问题根因 项目原先依赖第三方 npm 包 [email protected] 来解析原始 protobuf 线格式(getData),在以下两处使用:

src/util.js 的 isProtobuf() 判断是否 protobuf 数据 src/components/viewers/ViewerProtobuf.vue 的 Rawproto 模式解码展示 rawproto 内部 readVarint 的循环上限为 5 字节(for (let i = 0; i < 5; i++))且使用 32 位位运算。 而 protobuf 的 varint 最多可达 10 字节(覆盖完整 64 位)。因此:

含 64 位整数(int64/uint64 等)的字段 长度需要超过 5 字节编码的 length-delimited 字段 都会导致解析失败或数值溢出(原实现会抛错,表现为 isProtobuf 返回 false、ViewerProtobuf 显示 Protobuf Decode Failed!)。

由于 node_modules 不随仓库持久化、且无法直接稳定补丁第三方包,采用仓库内本地替换方案。

1.2 修复方案 新增本地模块 src/util/rawproto.js,作为 rawproto 的 getData 的 drop-in 替换:

varint 读取循环改为 10 字节,覆盖完整 64 位 使用 BigInt 进行位运算,保持 64 位精度 返回值策略(保持与原输出结构一致,且保证 JSON 可序列化): 数值落在 Number 安全整数范围内 → 返回 Number(与原行为一致) 超出安全整数范围 → 返回十进制 String(原生 BigInt 无法被 JSON.stringify,字符串可安全序列化) 输出结构完全沿用 rawproto:返回数组,每个元素为 { [tag]: value };fixed64(wire type 1)/ fixed32(wire type 5)返回字节数组;length-delimited(wire type 2)递归解码,失败则回退为原始字节数组。

Source: qishibo/AnotherRedisDesktopManager