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 🙏

© 2024 – Pkg Stats / Ryan Hefner

grpc-kit

v0.2.0

Published

use grpc more simply

Downloads

13,555

Readme

grpc-kit

npm version

Use grpc more simply on Node.js.

quick start

install

$ npm install grpc @grpc/proto-loader grpc-kit

proto

syntax="proto3";

package greeter;

service Greeter {
  rpc Hello (RequestGreet) returns (ResponseGreet) {}
  rpc Goodbye (RequestGreet) returns (ResponseGreet) {}
}

message RequestGreet {
  string name = 1;
}

message ResponseGreet {
  string message = 1;
}

Server

const {createServer} = require("grpc-kit");
const server = createServer();

server.use({
  protoPath: "/path/to/greeter.proto",
  packageName: "greeter",
  serviceName: "Greeter",
  routes: {
    hello: (call, callback) => {
      callback(null, { message: `Hello, ${call.request.name}` });
    },
    goodbye: async (call) => {
      return { message: `Goodbye, ${call.request.name}` };
    }
  },
  options: {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true
  }
});
server.listen("0.0.0.0:50051");

Client

const {createClient} = require("grpc-kit");
const client = createClient({
  protoPath: "/path/to/greeter.proto",
  packageName: "greeter",
  serviceName: "Greeter",
  options: {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true
  }
}

}, "0.0.0.0:50051");

client.hello({ name: "Jack" }, (err, { message }) => {
  if(err) throw err;
  console.log(message);
});

client.goodbye({ name: "John" }, (err, { message }) => {
  if(err) throw err;
  console.log(message);
});

use stream

proto

syntax="proto3";

package stream_greeter;

service StreamGreeter {
  rpc ClientStreamHello(stream Message) returns(Message) {}
  rpc ServerStreamHello(Message) returns(stream Message) {}
  rpc MutualStreamHello (stream Message) returns (stream Message) {}
}

message Message {
  string message = 1;
}

Server

const {createServer} = require("grpc-kit");
const server = createServer();

server.use({
  protoPath: "/path/to/stream_greeter.proto"),
  packageName: "stream_greeter",
  serviceName: "StreamGreeter",
  routes: {
    clientStreamHello: (call, callback) => {
      call.on("data", (chunk) => {
        //exec when client wrote message
        console.log(chunk.message);
      });
      call.on("end", (chunk) => {
        callback(null, { message: "Hello! I'm fine, thank you!" })
      });
    },

    serverStreamHello: (call) => {
      console.log(call.request.message);
      call.write({ message: "Hello!" });
      call.write({ message: "I'm fine, thank you" });
      call.end();
    },

    mutualStreamHello: (call) => {
      call.on("data", (chunk) => {
        //exec when client wrote message
        console.log(chunk.message);
        if(chunk.message === "Hello!"){
          call.write({ message: "Hello!" });
        } else if(chunk.message === "How are you?"){
          call.write({ message: "I'm fine, thank you" });
        } else {
          call.write({ message: "pardon?" });
        }
      });
      call.on("end", (chunk) => {
        call.end();
      });
    }
  }
});

server.listen("0.0.0.0:50051");

Client

const {createClient} = require("grpc-kit");
const client = createClient({
  protoPath: "/path/to/stream_greeter.proto",
  packageName: "stream_greeter",
  serviceName: "StreamGreeter"
}, "0.0.0.0:50051");

client stream

const call = client.clientStreamHello((err, res) => {
  if(err) throw err;
  console.log(res.message);
});
call.write({ message: "Hello!" });
call.write({ message: "How are you?" });
call.end();

server stream

const call = client.serverStreamHello({ message: "Hello! How are you?" });
call.on("data", (chunk) => {
  console.log(chunk.message);
});
call.on("end", () => {
  //exec when server streaming ended.
});

mutual stream

const call = client.mutualStreamHello();
call.on("data", (chunk) => {
  console.log(chunk.message);
});
call.on("end", () => {
  //exec when server streaming ended.
});
call.write({ message: "Hello!" });
call.write({ message: "How are you?" });
call.end();

api

createServer(): GrpcServer

Create GrpcServer instance. GrpcServer is a wrapper class of grpc.Server providing simplified api to register services.

GrpcServer.use({protoPath,packageName,serviceName,options,routes}): GrpcServer

Register a service to provide from a server.

|arg name|type|required/optional|description| |:-------|:---|:----------------|:----------| |protoPath|String|Required|path to .proto file| |packageName|String|Required|name of package| |serviceName|String|Required|name of service| |options|@grpc/proto-loader.Options|Optional|options for @grpc/proto-loader to load .proto file. In detail, please check here out. Default is null| |routes|{ [methodName]:(call, callback) => void | (call) => Promise }|Required|routing map consists of a set of pair of method name and handler. Both of sync function and async function are available as a handler|

GrpcServer.listen(address_port, credentials): GrpcServer

Start server. Alias to grpc.Server.bind() and grpc.Server.start().

|arg name|type|required/optional|description| |:-------|:---|:----------------|:----------| |address_port|String|Required|address and port of server to listen| |credentials|grpc.ServerCredentials|Optional|grpc server credentials. Default to insecure credentials generated by grpc.ServerCredentials.createInsecure()|

GrpcServer.close(force, callback): GrpcServer

Close server. Alias to grpc.Server.tryShutdown() and grpc.Server.forceShutdown.

|arg name|type|required/optional|description| |:-------|:---|:----------------|:----------| |force|Boolean|Optional|flag if force shutdown or not. Default to false| |callback|()=>{}|Optional|call when shutdown completed. available only when force is false|

createClient({protoPath,packageName,serviceName,options},address_port,credentials): grpc.Client

Create grpc.Client instance.

|arg name|type|required/optional|description| |:-------|:---|:----------------|:----------| |protoPath|String|Required|path to .proto file| |packageName|String|Required|name of package| |serviceName|String|Required|name of service| |options|@grpc/proto-loader.Options|Optional|options for @grpc/proto-loader to load .proto file. In detail, please check here out. Default is null| |address_port|String|Required|address and port of server to listen| |credentials|grpc.ChannelCredentials|Optional|grpc channel credentials. Default to insecure credentials generated by grpc.credentials.createInsecure()|