{"_id":"@efvezombie/sproto-js","name":"@efvezombie/sproto-js","dist-tags":{"latest":"0.1.9"},"versions":{"0.1.9":{"name":"@efvezombie/sproto-js","version":"0.1.9","description":"TypeScript refactor of hanxi/sproto-js with ESM support","type":"module","main":"dist/index.js","types":"dist/index.d.ts","exports":{".":{"import":"./dist/index.js","types":"./dist/index.d.ts"}},"packageManager":"bun@1.0.0","scripts":{"prepare":"npm run build","build":"tsc -p tsconfig.json","lint":"eslint . --ext .ts","test":"vitest run","test:coverage":"vitest run --coverage","test:watch":"vitest","prepublishOnly":"npm run build"},"keywords":["sproto","sproto-js","typescript","bun","esm"],"author":{"name":"hanxi"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/EfveZombie/sproto-js.git"},"bugs":{"url":"https://github.com/EfveZombie/sproto-js/issues"},"homepage":"https://github.com/EfveZombie/sproto-js#readme","publishConfig":{"access":"public"},"devDependencies":{"@types/node":"^25.5.0","@typescript-eslint/eslint-plugin":"^6.0.0","@typescript-eslint/parser":"^6.0.0","@vitest/coverage-v8":"^3.0.0","eslint":"^8.0.0","typescript":"^5.0.0","vitest":"^3.0.0"},"_id":"@efvezombie/sproto-js@0.1.9","gitHead":"3379cbd5a924a9e9cbffc9e9157b797cba6dc743","_nodeVersion":"22.22.2","_npmVersion":"10.9.7","dist":{"integrity":"sha512-YWHT0T/RUkdBpKBqiZf3yZQBJUcIQPcDQrkWfonnruauoB7sK6fNaRAOWRE86kIbzUjwFCthLPPR+ISfy/l6UQ==","shasum":"933d17edd5357d20bdc04485e92ce5dc6da96d56","tarball":"https://registry.npmjs.org/@efvezombie/sproto-js/-/sproto-js-0.1.9.tgz","fileCount":10,"unpackedSize":157801,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDtq8PMMDFOiwd5NnZ0eIeVFbM/n0t91Wu4di7tnwt61gIgEl1uI2B9yhjE70v8i1ZPmn+3VSp1Ajvx3Mgie/L2BqI="}]},"_npmUser":{"name":"shenzhengopher","email":"lizh629@mail2.sysu.edu.cn"},"directories":{},"maintainers":[{"name":"shenzhengopher","email":"lizh629@mail2.sysu.edu.cn"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/sproto-js_0.1.9_1783581166523_0.2598636027749044"},"_hasShrinkwrap":false}},"time":{"created":"2026-07-09T07:12:46.275Z","0.1.9":"2026-07-09T07:12:46.661Z","modified":"2026-07-09T07:12:46.856Z"},"maintainers":[{"name":"shenzhengopher","email":"lizh629@mail2.sysu.edu.cn"}],"description":"TypeScript refactor of hanxi/sproto-js with ESM support","homepage":"https://github.com/EfveZombie/sproto-js#readme","keywords":["sproto","sproto-js","typescript","bun","esm"],"repository":{"type":"git","url":"git+https://github.com/EfveZombie/sproto-js.git"},"author":{"name":"hanxi"},"bugs":{"url":"https://github.com/EfveZombie/sproto-js/issues"},"license":"MIT","readme":"# sproto-js\r\n\r\n一个用于 JavaScript/TypeScript 的 sproto 协议库，这是原版 [zhangshiqian1214/sproto-js](https://github.com/zhangshiqian1214/sproto-js) 的 TypeScript 重构版本。\r\n\r\n## 简介\r\n\r\nsproto 是一个轻量级的二进制协议，类似于 Google Protocol Buffers，但更加简洁。本库提供了在 TypeScript 环境中使用 sproto 协议的完整实现。\r\n\r\n## 特性\r\n\r\n- 🚀 **TypeScript 支持**：完整的类型定义和类型安全\r\n- 📦 **轻量级**：无外部依赖，体积小巧\r\n- 🔧 **易于使用**：简单的 API 设计\r\n- 🎯 **高性能**：优化的编解码算法\r\n- 🌐 **跨平台**：支持 Node.js 和浏览器环境\r\n\r\n## 安装\r\n\r\n```bash\r\nbun add @imhanxi/sproto-js\r\n```\r\n\r\n或使用 npm：\r\n\r\n```bash\r\nnpm install @imhanxi/sproto-js\r\n```\r\n\r\n## 快速开始\r\n\r\n### 1. 定义协议文件\r\n\r\n创建 `.sproto` 文件定义你的协议结构：\r\n\r\n```sproto\r\n# proto/login.sproto\r\n.context {\r\n    rid 0 : integer\r\n    proto_checksum 1 : string\r\n}\r\n\r\nlogin 101 {\r\n    request {\r\n        token 0 : string\r\n        ctx 1 : context\r\n    }\r\n    response {\r\n        code 0 : integer\r\n        account 1 : string\r\n        gamenode 2 : string\r\n    }\r\n}\r\n```\r\n\r\n### 2. 编译协议文件\r\n\r\n将 `.sproto` 文件编译成 `.spb` 二进制文件（需要 sproto 编译器）。\r\n\r\n### 3. 使用示例\r\n\r\n```typescript\r\nimport fs from \"fs\";\r\nimport sproto from \"@imhanxi/sproto-js\";\r\n\r\n// 读取编译后的协议文件\r\nconst bundle = new Uint8Array(fs.readFileSync(\"./protocol.spb\"));\r\n\r\n// 创建 sproto 实例\r\nconst sp = sproto.createNew(bundle);\r\n\r\n// 创建主机实例\r\nconst client = sp.host(\"base.package\");\r\nconst clientRequest = client.attach(sp);\r\n\r\n// 编码请求数据\r\nconst data = {\r\n    token: \"your-jwt-token\",\r\n    ctx: {\r\n        proto_checksum: \"xxxxx\",\r\n    },\r\n};\r\n\r\nconst req = clientRequest(\"login.login\", data);\r\nconsole.log(\"编码后的请求数据长度:\", req.length);\r\n\r\n// 解码响应数据\r\nconst ret = client.dispatch(req);\r\nconsole.log(\"解码结果:\", ret);\r\n```\r\n\r\n## API 文档\r\n\r\n### sproto.createNew(bundle: Uint8Array)\r\n\r\n从编译后的协议文件创建 sproto 实例。\r\n\r\n**参数:**\r\n- `bundle`: 编译后的 `.spb` 文件内容\r\n\r\n**返回值:**\r\n- `SprotoInstance`: sproto 实例对象\r\n\r\n### instance.host(packageName?: string)\r\n\r\n创建协议主机实例。\r\n\r\n**参数:**\r\n- `packageName`: 包名（可选）\r\n\r\n**返回值:**\r\n- `SprotoHost`: 主机实例\r\n\r\n### host.attach(sp: SprotoInstance)\r\n\r\n创建请求编码函数。\r\n\r\n**参数:**\r\n- `sp`: sproto 实例\r\n\r\n**返回值:**\r\n- 编码函数：`(name: string, args: object) => Uint8Array`\r\n\r\n### host.dispatch(buffer: Uint8Array)\r\n\r\n解码接收到的数据包。\r\n\r\n**参数:**\r\n- `buffer`: 要解码的数据包\r\n\r\n**返回值:**\r\n- 解码后的对象\r\n\r\n## 支持的数据类型\r\n\r\n| 类型 | 描述 |\r\n|------|------|\r\n| **string** | 字符串类型 |\r\n| **binary** | 二进制字符串（字符串的子类型） |\r\n| **integer** | 整数，最大长度为有符号 52 位（符合 IEEE 754 标准） |\r\n| **double** | 双精度浮点数，符合 [IEEE 754 标准](https://en.wikipedia.org/wiki/Double-precision_floating-point_format) |\r\n| **boolean** | 布尔值：true 或 false |\r\n\r\n## 项目结构\r\n\r\n```\r\nsproto-js/\r\n├── src/\r\n│   └── sproto.ts          # 主要实现文件\r\n├── proto/\r\n│   ├── base.sproto        # 基础协议定义\r\n│   └── login.sproto       # 登录协议定义\r\n├── test.ts                # 使用示例\r\n├── package.json\r\n└── README.md\r\n```\r\n\r\n## 开发\r\n\r\n### 构建项目\r\n\r\n```bash\r\nbun run build\r\n```\r\n\r\n### 运行测试\r\n\r\n```bash\r\nbun run test.ts\r\n```\r\n\r\n### 代码检查\r\n\r\n```bash\r\nbunx tsc -p tsconfig.json\r\n```\r\n\r\n## 许可证\r\n\r\nMIT License\r\n\r\n## 贡献\r\n\r\n欢迎提交 Issue 和 Pull Request！\r\n\r\n## 相关链接\r\n\r\n- [GitHub 仓库](https://github.com/hanxi/sproto-js)\r\n- [问题反馈](https://github.com/hanxi/sproto-js/issues)\r\n- [sproto 协议规范](https://github.com/cloudwu/sproto)\r\n\r\n\r\n\r\n\r\n\r\n \r\n\r\n","readmeFilename":"README.md","_rev":"1-447fc42704bd22615e29b5e6309b0833"}