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

@anfema/grpc-code-generator

v3.0.1

Published

A code generator for gRPC

Downloads

152

Readme

grpc-code-generator

A code generator for gRPC/protobuf .proto files. Contains templates for generating Typescript definitions for the plain callback/streams based API for grpc-node and protobuf message types.

Running

$ yarn/npm run grpc-code-generator [options] path/to/main.proto [path/to/another.proto]

Options can be specified on the command line or in a config file. If both are present, the command line options take precedence.

Options:

-o  --out <out_dir>

    Output directory (default: src-gen/)

-I  --proto_path <include_dir>     
	
    Root path for resolving imports (may be specified multiple times, default: current working dir)

-t  --templates <template1> [<template2> …]

    Path to template modules used for generating code (default: builtin templates)

-c  --config <file>

    Path to JSON config file.

Config file

{
	"out": "<out_dir>",
	"proto_paths": [
		"<dir1>",
		"<dir2>"
	],
	"files": [
		"path/to/main.proto",
		"path/to/another.proto",
	]
}

Generated files

| File | Content | |------------------------------------------|-----------------------------------| |/<package>/index.d.ts | Interfaces for message types | |/<package>/<ServiceName>/grpc-node.d.ts | Client/server typings with standard grpc-node interface | |/message-base.d.ts | Base message type with constructor | |/grpc.d.ts | Object with constructor functions for messages and service descriptions (what grpc.load() returns) |

Usage

Parametrize your grpc-node's load() function with the generated type description:

import { load } from 'grpc';
// Service type description for grpc.load()
import Description from './gen/grpc';

const grpc = load<Description>('src/tests/proto/test.proto');

Server side usage:

class TestService implements TestService {
	unaryCall(call: ServerUnaryCall<Request>, callback: sendUnaryData<Response>): void {
		callback(null, new grpc.Response());
	}

	streamResponse(call: ServerWriteableStream<Request>): void {
		for (let i = 0; i < request.count; i++) {
			call.write(new grpc.Response());
		}
		call.end();
	}

	streamRequest(call: ServerReadableStream<Request>, callback: sendUnaryData<Response>): void {
		call
			.on('data', (data: Request) => {
				/* */
			})
			.on('error', (error) => {
				/* */
			})
			.on('end', () => {
				callback(null, new grpc.Response());
			});
	}

	streamBidi(call: ServerDuplexStream<Request, Response>): void {
		call
			.on('data', (data: Request) => {
				call.write(new grpc.Response());
			})
			.on('error', (error) => {
				/* */
			})
			.on('end', () => {
				call.end();
			});
	}
}

const server = new Server();

server.addService(grpc.TestService.service, new TestService());
server.bind('0.0.0.0:3000', ServerCredentials.createInsecure());
server.start();

Client side usage:

import { credentials } from 'grpc';
// message types
import { Request, Response } from './gen';
// adapter types
import { Client } from './gen/TestService/grpc-node';

const client = new grpc.TestService('0.0.0.0:3000', credentials.createInsecure());

// unary request
client.unaryCall(new grpc.Request({ mode: 'normal' }), (err, response) => {
	/* */
});

// streaming response
const stream1 = client.streamResponse(new grpc.Request())
	.on('data', (response) => {
		/* */
	})
	.on('error', (error) => {
		/* */
	})
	.on('end', () => {
		/* */
	});

// streaming request
const requestStream = client.streamRequest(cb);
for (let i = 0; i < 10; i++) {
	requestStream.write(new grpc.Request({ mode: 'normal' }));
}
requestStream.end();

const stream = client.streamBidi()
	.on('data', (response) => {
		/* */
	})
	.on('error', (error) => {
		/* */
	})
	.on('end', () => {
		/* */
	});

for (let i = 0; i < 10; i++) {
	stream.write(new grpc.Request({ mode: 'normal' }));
}
// wait for responses, then
stream.end();

Development

Getting the sources

$ git clone https://github.com/anfema/grpc-code-generator.git
# or
$ git clone [email protected]:anfema/grpc-code-generator.git

Install dependencies & build project

$ yarn
#or 
$ npm

Tasks:

  • Build once: $ yarn/npm run build
  • Build, watch files: $ yarn/npm run dev
  • Remove generated files: $ yarn/npm run clean