npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@imhanxi/sproto-js

v0.1.9

Published

TypeScript refactor of hanxi/sproto-js with ESM support

Readme

sproto-js

一个用于 JavaScript/TypeScript 的 sproto 协议库,这是原版 zhangshiqian1214/sproto-js 的 TypeScript 重构版本。

简介

sproto 是一个轻量级的二进制协议,类似于 Google Protocol Buffers,但更加简洁。本库提供了在 TypeScript 环境中使用 sproto 协议的完整实现。

特性

  • 🚀 TypeScript 支持:完整的类型定义和类型安全
  • 📦 轻量级:无外部依赖,体积小巧
  • 🔧 易于使用:简单的 API 设计
  • 🎯 高性能:优化的编解码算法
  • 🌐 跨平台:支持 Node.js 和浏览器环境

安装

bun add @imhanxi/sproto-js

或使用 npm:

npm install @imhanxi/sproto-js

快速开始

1. 定义协议文件

创建 .sproto 文件定义你的协议结构:

# proto/login.sproto
.context {
    rid 0 : integer
    proto_checksum 1 : string
}

login 101 {
    request {
        token 0 : string
        ctx 1 : context
    }
    response {
        code 0 : integer
        account 1 : string
        gamenode 2 : string
    }
}

2. 编译协议文件

.sproto 文件编译成 .spb 二进制文件(需要 sproto 编译器)。

3. 使用示例

import fs from "fs";
import sproto from "@imhanxi/sproto-js";

// 读取编译后的协议文件
const bundle = new Uint8Array(fs.readFileSync("./protocol.spb"));

// 创建 sproto 实例
const sp = sproto.createNew(bundle);

// 创建主机实例
const client = sp.host("base.package");
const clientRequest = client.attach(sp);

// 编码请求数据
const data = {
    token: "your-jwt-token",
    ctx: {
        proto_checksum: "xxxxx",
    },
};

const req = clientRequest("login.login", data);
console.log("编码后的请求数据长度:", req.length);

// 解码响应数据
const ret = client.dispatch(req);
console.log("解码结果:", ret);

API 文档

sproto.createNew(bundle: Uint8Array)

从编译后的协议文件创建 sproto 实例。

参数:

  • bundle: 编译后的 .spb 文件内容

返回值:

  • SprotoInstance: sproto 实例对象

instance.host(packageName?: string)

创建协议主机实例。

参数:

  • packageName: 包名(可选)

返回值:

  • SprotoHost: 主机实例

host.attach(sp: SprotoInstance)

创建请求编码函数。

参数:

  • sp: sproto 实例

返回值:

  • 编码函数:(name: string, args: object) => Uint8Array

host.dispatch(buffer: Uint8Array)

解码接收到的数据包。

参数:

  • buffer: 要解码的数据包

返回值:

  • 解码后的对象

支持的数据类型

| 类型 | 描述 | |------|------| | string | 字符串类型 | | binary | 二进制字符串(字符串的子类型) | | integer | 整数,最大长度为有符号 52 位(符合 IEEE 754 标准) | | double | 双精度浮点数,符合 IEEE 754 标准 | | boolean | 布尔值:true 或 false |

项目结构

sproto-js/
├── src/
│   └── sproto.ts          # 主要实现文件
├── proto/
│   ├── base.sproto        # 基础协议定义
│   └── login.sproto       # 登录协议定义
├── test.ts                # 使用示例
├── package.json
└── README.md

开发

构建项目

bun run build

运行测试

bun run test.ts

代码检查

bunx tsc -p tsconfig.json

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

相关链接