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

@themainstack/communication

v1.2.0

Published

Unified gRPC framework for inter-service communication - auto-generates protos, creates servers, and provides type-safe clients

Readme

@themainstack/communication

A unified gRPC framework for inter-service communication at Mainstack.

Table of Contents

  1. Overview
  2. Installation
  3. Proto Generation
  4. Creating a gRPC Server
  5. Creating a gRPC Client
  6. Error Handling
  7. Full Example

Overview

The workflow is simple:

Developer writes TypeScript function
         ↓
Package auto-generates .proto file
         ↓
Server Factory exposes function as gRPC
         ↓
Client Factory in another service calls it
         ↓
Error Handler normalizes any errors

Installation

npm install @themainstack/communication
# or
yarn add @themainstack/communication

Proto Generation

Auto-generate .proto files from your TypeScript types.

import { generateProtoFromMethods } from '@themainstack/communication';

generateProtoFromMethods([
  {
    name: 'CalculateFee',
    requestSample: () => ({ merchantId: '', amount: 0, currency: '' }),
    responseSample: () => ({ fee: 0, total: 0 }),
  }
], {
  packageName: 'fee.v1',
  serviceName: 'FeeService',
  outputDir: './src/grpc',
});

Output: ./src/grpc/feeservice.proto is auto-generated!


Creating a gRPC Server

Expose existing functions as gRPC endpoints.

import { GrpcServerFactory } from '@themainstack/communication';

// Your existing business function
async function calculateFee(request) {
  return { fee: request.amount * 0.02, total: request.amount * 1.02 };
}

// Create and start the server
const server = await GrpcServerFactory.createServer({
  packageName: 'fee.v1',
  serviceName: 'FeeService',
  port: 50053,
}, [
  {
    name: 'CalculateFee',
    handler: calculateFee,  // Your existing function!
    requestSample: () => ({ merchantId: '', amount: 0, currency: '' }),
    responseSample: () => ({ fee: 0, total: 0 }),
  }
]);

await server.start();
// 🚀 gRPC Server running on 0.0.0.0:50053

Quick Expose (One-liner)

import { exposeAsGrpc } from '@themainstack/communication';

const server = await exposeAsGrpc(
  'CalculateFee',
  calculateFee,
  { requestSample: () => ({...}), responseSample: () => ({...}) },
  { packageName: 'fee.v1', serviceName: 'FeeService', port: 50053 }
);

Creating a gRPC Client

Call gRPC services from other services.

import { GrpcClientFactory } from '@themainstack/communication';

const client = GrpcClientFactory.createClient({
  serviceName: 'FeeService',
  packageName: 'fee.v1',
  protoPath: './src/grpc/fee.proto',
  url: process.env.FEE_SERVICE_URL || 'localhost:50053',
});

// Make a call
client.CalculateFee(
  { merchantId: 'merchant_123', amount: 1000, currency: 'USD' },
  (err, response) => {
    if (err) {
      handleGrpcError(err);
      return;
    }
    console.log('Fee:', response.fee);
  }
);

Error Handling

Standardized gRPC error translation.

import { handleGrpcError } from '@themainstack/communication';

client.SomeMethod(request, (err, response) => {
  if (err) {
    try {
      handleGrpcError(err); // Throws normalized error
    } catch (normalizedError) {
      console.error(normalizedError.message);
      // Handle based on error type
    }
    return;
  }
  // Process response
});

Full Example

See examples/full-grpc-demo.ts in the repository for a complete working example.


Environment Variables

| Variable | Description | Default | |----------|-------------|---------| | GRPC_PORT | Port for gRPC server | 50051 | | FEE_SERVICE_URL | Fee service gRPC address | localhost:50053 |


API Reference

Proto Generation

  • generateProtoFromMethods(methods, options) - Generate proto with service definition
  • generateProtoFromFunction(fn, name) - Generate proto for a single message
  • AnyType - Marker symbol for google.protobuf.Any dynamic fields

Server

  • GrpcServerFactory.createServer(options, handlers) - Create a gRPC server
  • exposeAsGrpc(name, handler, samples, options) - Quick one-liner

Client

  • GrpcClientFactory.createClient(options) - Create a gRPC client

Error Handling

  • handleGrpcError(err) - Translate gRPC errors to application errors