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

@lucaconlaq/grape-cola

v1.0.2

Published

A thin wrapper that gives grpc-js a better DX

Downloads

96

Readme

Grape Cola

npm biome vitest

What it is — a thin wrapper that gives grpc-js a better DX.

What it isn't — a new protocol. Any gRPC client connects as usual.

Quick start

1. Create a handler

A handler is a factory that carries shared context and middleware. Every RPC you define goes through it.

import {
  createHandler,
  GrpcCall,
  injectGRPCLoggingContext,
  withClock,
  withGRPCLogging,
  withLogger,
} from "@lucaconlaq/grape-cola";

// inject functions extract values from the raw gRPC call into context
const injectAuthContext = (call?: GrpcCall) => ({
  authorization: call?.metadata.get("authorization")[0]?.toString(),
});

const handler = createHandler({
  injectInContext: [injectGRPCLoggingContext, injectAuthContext],
  defaultMiddleware: [withLogger(), withClock(), withGRPCLogging()],
});

2. Define RPCs

Use the builder chain to set request/reply types and pick the RPC kind:

const sayHello = handler()
  .request(HelloRequest)
  .reply(HelloReply)
  .unary(async ({ req }) => {
    const reply = new HelloReply();
    reply.setMessage(`hello, ${req.getName()}!`);
    return reply;
  });

const chatHello = handler()
  .request(HelloRequest)
  .reply(HelloReply)
  .bidiStream(async ({ ctx, call }) => {
    for await (const req of call) {
      const reply = new HelloReply();
      reply.setMessage(`${ctx.message}, ${req.getName()}!`);
      call.write(reply);
    }
  });

All four gRPC patterns are supported: .unary(), .serverStream(), .clientStream(), .bidiStream().

3. Group RPCs into services

createService validates your handler map against the generated ServiceDefinition at compile time — missing methods, wrong message types, or mismatched streaming kinds are type errors.

Given a proto file like:

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
  rpc SayHelloReversed (HelloRequest) returns (HelloReply);
  rpc StreamHello (HelloRequest) returns (stream HelloReply);
  rpc CollectHellos (stream HelloRequest) returns (HelloReply);
  rpc ChatHello (stream HelloRequest) returns (stream HelloReply);
}

You wire up all the handlers:

import { createService } from "@lucaconlaq/grape-cola";
import { GreeterService } from "./gen/greeter_grpc_pb";

const greeterService = createService<typeof GreeterService>({
  sayHello,
  sayHelloReversed,
  streamHello,
  collectHellos,
  chatHello,
});

4. Start the server

import { createServer } from "@lucaconlaq/grape-cola";

const server = createServer({
  handler,
  onStart: ({ ctx, error }) => {
    if (error) throw error;
    ctx.logger.info("🚀 listening on 0.0.0.0:50051");
  },
});

server.addService(GreeterService, greeterService);
server.addService(AuthService, authService);
await server.listen("0.0.0.0", 50051);

🔌 Context & middleware

Grape Cola builds a typed context that flows through every request. The context is assembled in two layers:

Inject functions

injectInContext functions run first. They receive the raw gRPC call and extract values (metadata headers, path, etc.) into the base context:

const injectAuthContext = (call?: GrpcCall) => {
  const authorization = call?.metadata.get("authorization")[0]?.toString();
  return { authorization };
};

Middleware

Default middleware runs next, sequentially. Each middleware receives the accumulated context and returns new fields to merge into it. TypeScript tracks the context shape at every step — if a middleware requires { logger: Logger }, the compiler ensures a previous middleware provides it.

import { middleware } from "@lucaconlaq/grape-cola";

const withLogger = () =>
  middleware<{ logger?: Logger }>()<{ logger: Logger }>(async ({ ctx }) => ({
    logger: ctx.logger ?? pino(),
  }));

Per-handler middleware

Individual RPCs can add extra middleware with .use(). This is useful for capabilities only some endpoints need (e.g. auth, database):

const login = handler()
  .use(withDb())                    // adds { db } to context
  .request(LoginRequest)
  .reply(LoginReply)
  .unary(async ({ req, ctx }) => {
    const user = ctx.db.findUser(req.getUsername());
    // ...
  });

const saySecretGreeting = handler()
  .use(withDb())                    // adds { db }
  .use(withAuth())                  // adds { user } — requires { db, authorization }
  .request(SecretRequest)
  .reply(SecretReply)
  .unary(async ({ req, ctx }) => {
    // ctx.user is fully typed here
  });

After functions ♻️

middlewareWithAfter lets a middleware return a function that runs after the handler completes, in reverse middleware order. Perfect for logging, tracing, or resource cleanup:

import { middlewareWithAfter, withAfter } from "@lucaconlaq/grape-cola";

const withLogging = () =>
  middlewareWithAfter<{ clock: Clock; logger: Logger }>()(async ({ ctx }) => {
    const startedAt = ctx.clock.now();
    return withAfter({}, () => {
      const duration = ctx.clock.now() - startedAt;
      ctx.logger.info(`completed in ${duration}ms`);
    });
  });

🧩 Built-in middleware

| Middleware | Adds to context | Peer deps | |---|---|---| | withClock() | clock — injectable sleep + now abstraction. Use immediateClock in tests. | — | | withLogger() | logger — pino logger instance | pino, pino-pretty | | withGRPCLogging() | logs request duration and path | pino |

All three preserve existing context values, so tests can inject fakes. They compose together like this:

import {
  createHandler,
  injectGRPCLoggingContext,
  withClock,
  withGRPCLogging,
  withLogger,
} from "@lucaconlaq/grape-cola";

const handler = createHandler({
  injectInContext: [injectGRPCLoggingContext],
  defaultMiddleware: [withLogger(), withClock(), withGRPCLogging()],
});

🧪 Testing

Grape Cola ships two utilities that let you run handlers and middleware without starting a gRPC server.

testHandler

Runs a resolved handler in isolation. Pass a baseCtx to satisfy middleware requirements, and a req (or streamInput for streaming RPCs):

import { testHandler } from "@lucaconlaq/grape-cola";

// unary
const { reply } = await testHandler(greeterService.sayHello, {
  baseCtx: createBaseCtx(),
  req: makeHelloRequest("world"),
});
expect(reply.getMessage()).toBe("hello, world!");

// server stream
const { output } = await testHandler(greeterService.streamHello, {
  baseCtx: createBaseCtx(),
  req: makeHelloRequest("world"),
});
expect(output).toHaveLength(3);

Since middleware reads from context, you can swap in test doubles by overriding baseCtx. For example, an immediateClock that resolves sleep() instantly:

const { output } = await testHandler(greeterService.streamHello, {
  baseCtx: createBaseCtx({ clock: immediateClock }),
  req: makeHelloRequest("world"),
});
// no actual waiting — the handler runs instantly

testMiddleware

Tests a single middleware in isolation. Returns the merged ctx and, for middlewareWithAfter, the after cleanup function:

import { testMiddleware } from "@lucaconlaq/grape-cola";

const { ctx } = await testMiddleware(withLogger(), {
  baseCtx: {},
});
expect(ctx.logger).toBeDefined();

// with after function
const { after } = await testMiddleware(withLogging(), {
  baseCtx: createBaseCtx({ logger: spyLogger }),
});
after!();
expect(spyLogger.messages).toHaveLength(1);

Typical test helper

A small helper that pre-fills the base context keeps tests concise:

const createBaseCtx = (overrides?: Record<string, unknown>) => ({
  logger: pino({ level: "silent" }),
  clock: immediateClock,
  message: "hello",
  request: { id: "test-123", path: "/TestService/testMethod" },
  ...overrides,
});