A Yaak plugin
这是一个 Yaak 插件,提供了 hex.encode 和 hex.decode 两个模板函数,用于在 Yaak 中进行 16 进制编码和解码操作。
0x 前缀hexEncode 和 hexDecode 别名npm install
npm run build
如果你已经获得了构建好的插件文件,直接将其放入 Yaak 的插件目录即可。
在 Yaak 的模板中使用以下语法:
// 编码
${[ hex.encode("要编码的字符串") ]}
// 解码
${[ hex.decode("要解码的16进制字符串") ]}
// 使用别名
${[ hexEncode("字符串") ]}
${[ hexDecode("16进制字符串") ]}
${[ hex.encode("hello") ]} → "68656c6c6f"0x 前缀)${[ hex.decode("68656c6c6f") ]} → "hello"// 编码英文
${[ hex.encode("hello world") ]}
// 输出: 68656c6c6f20776f726c64
// 解码英文
${[ hex.decode("68656c6c6f20776f726c64") ]}
// 输出: hello world
// 编码中文
${[ hex.encode("你好,世界!") ]}
// 输出: e4bda0e5a5bd239e4b896e7958c21
// 解码中文
${[ hex.decode("e4bda0e5a5bd239e4b896e7958c21") ]}
// 输出: 你好,世界!
// 带空格的 16 进制
${[ hex.decode("68 65 6c 6c 6f") ]}
// 输出: hello
// 带 0x 前缀的 16 进制
${[ hex.decode("0x68656c6c6f") ]}
// 输出: hello
// 混合格式
${[ hex.decode("0x68 65 6c 6c 6f") ]}
// 输出: hello
# 在 Authorization 头中使用
GET /api/data HTTP/1.1
Host: example.com
Authorization: Basic ${[ hex.encode("admin:password123") ]}
Content-Type: application/json
# 在请求体中使用
POST /api/encode HTTP/1.1
Host: example.com
Content-Type: application/json
{
"data": "${[ hex.encode("敏感信息") ]}",
"timestamp": "${[ $timestamp ]}"
}
// 假设响应体是 16 进制编码的数据
const hexData = "${[ response.body ]}";
const decodedData = "${[ hex.decode(hexData) ]}";
// 或者直接解码
const data = "${[ hex.decode(response.body) ]}";
插件提供了完善的错误处理机制:
无效的 16 进制字符串
${[ hex.decode("xyz123") ]}
// 错误: 无效的16进制字符串
奇数长度的 16 进制字符串
${[ hex.decode("68656c6c6") ]}
// 错误: 16进制字符串长度必须为偶数
空输入
${[ hex.encode("") ]}
// 输出: "" (空字符串)
${[ hex.decode("") ]}
// 输出: "" (空字符串)
所有错误都会通过 Yaak 的 toast 通知显示,不会中断模板的渲染流程。
TextEncoder 将字符串转换为 UTF-8 字节数组0x 前缀TextDecoder 将字节数组解码为 UTF-8 字符串hex/
├── src/
│ ├── index.ts # 插件主文件
│ └── index.test.ts # 单元测试
├── build/ # 构建输出目录
├── package.json # 项目配置
├── tsconfig.json # TypeScript 配置
└── examples.md # 使用示例文档
# 运行所有测试
npm test
# 以监听模式运行测试
npx vitest
npm run build
构建后的文件将输出到 build/ 目录。
要添加新的模板函数,请参考现有实现:
src/index.ts 中添加新的 TemplateFunctionPlugin 对象onRender 方法plugin.templateFunctions 数组中A: 插件完全支持 UTF-8 编码,可以正确处理 ASCII、中文、日文、韩文等所有 Unicode 字符。
A: 输出为小写 16 进制字符串,这是大多数系统的标准做法。
A: 可以。插件可以将任意字节数据编码为 16 进制,也可以将 16 进制解码回原始数据。
A: 对于常规使用场景性能足够。如果需要处理大量数据(超过 1MB),建议在外部处理。
A: 请通过项目的 Issue 页面提交问题或功能请求。
hex.encode 和 hex.decode 函数本项目采用 MIT 许可证。详情请参阅 LICENSE 文件。
欢迎提交 Issue 和 Pull Request 来改进这个插件。
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)如果你在使用过程中遇到任何问题,或者有改进建议,请通过以下方式联系:
感谢使用 Hex 编码/解码插件!希望这个插件能让你的 Yaak 使用体验更加愉快。