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 🙏

© 2025 – Pkg Stats / Ryan Hefner

rpch

v1.0.1

Published

rpch-node

Readme

介绍

rpch-node是rpch框架的node实现,更多详细信息可参考rpch-go

使用async以及await,服务端接口方法以及客户端调用支持全异步。

使用

启动一个最简单的rpc服务器:

1. 创建IDL文件:

新建math.gfj:

//math.gfj
service Math{
    int32 Add(int32,int32)
}

使用hgen编译器对其进行编译:hgen -dir . -lang node ./math.gfj

即会在当前目录下生成math.rpch.js文件,其中定义了服务端应该提供服务的接口,客户端的调用api。

hgen编译器的介绍以及IDL的语法见hgen

2. 服务端实现

server.js:

//引入此库
const rpch = require("rpch");
//引入编译器生成的文件
const mathRPCH = require("./math.rpch");

//继承Math服务接口,实现具体服务
class MathServiceImpl extends mathRPCH.MathInterface{
    //实现Add方法
	async Add(arg1, arg2) {
        return arg1 + arg2;
	}
}

//建立服务端
let svr = rpch.createServer();

//注册服务
mathRPCH.registerMathService(svr, new (MathServiceImpl));

//开始监听
svr.listen(8080, "127.0.0.1", () => {
    console.log("server is listening at 127.0.0.1:8080");
})

对用用户来说,只需要实现具体的服务并将其注册即可。

3. 客户端实现

client.js:

//引入此库
const rpch = require("rpch");
//引入编译器生成的文件
const mathRPCH = require("./math.rpch");

//发起rpc链接
let conn = rpch.dial(8080, "127.0.0.1");

conn.onError(err => {
    console.log(err);
})

//使用自动生成的函数将此rpc连接转化为请求Math服务的client对象
let client = new mathRPCH.MathClient(conn);

//发起异步请求
(async()=> {
    try {
        //调用异步Add方法
        let res = await client.Add(-1, 2);
        if (res != 1) {
            console.log(`want ${1} but got ${res}`);
            process.exit(1);
        }
        console.log("test success!");
    } catch (e) {
        console.log(e);
    } finally {
        conn.destroy();
    }
})()

客户端在得到rpch连接后,使用编译器生成的函数就可以将其转化为访问具体服务的对象,该对象绑定了所有访问这个服务的方法。

更多的案例见examples

注意事项

请勿在rpch-node中使用IDL的stream类型,stream目前仅对rpch-go实现。

如果一个服务返回值或者传递参数是一个对象时,且该对象具有整数成员,请务必保证nodejs服务端的实现中将所有整数成员使用parseInt将可能出现的浮点数转化为整数。否则会导致跨语言通讯时出现错误。

安装

npm i rpch

或者

直接下载本仓库的rpch.js文件,项目中引入即可。