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

nestjs-protobuf-es

v0.1.2

Published

Protobuf-ES integration for NestJS

Readme

nestjs-protobuf-es

npm license

@bufbuild/protobuf integration for NestJS

Features

  • Generates interfaces for both client and controller.
  • Generates decorators that unifies @GrpcMethod and @GrpcStreamMethod.
  • Handles serialization of JS Date into google.protobuf.Timestamp.
  • Provides a method to convert google.protobuf.Timestamp to JS Date in messages (including nested messages, maps, lists and oneofs).
  • Properly typed.

Installation

npm install nestjs-protobuf-es @bufbuild/protobuf @grpc/grpc-js @nestjs/microservices rxjs
npm install --save-dev @bufbuild/buf @bufbuild/protoc-gen-es

# Or with yarn
yarn add nestjs-protobuf-es @bufbuild/protobuf @grpc/grpc-js @nestjs/microservices rxjs
yarn add -D @bufbuild/buf @bufbuild/protoc-gen-es

Configuration

Add a new configuration file to the root of your project buf.gen.yaml:

version: v2
inputs:
  - directory: proto
plugins:
  - local: protoc-gen-es
    opt: target=ts
    out: src/gen
  - local: protoc-gen-nestjs
    opt: target=ts
    out: src/gen

Available protoc-gen-nestjs options:

  • valid_types - use generated protobuf-es valid types in generated NestJS interfaces.
  • js_dates - type google.protobuf.Timestamp fields as JS Date for controller requests and client responses. Use with generateGrpcServices(..., { jsDates: true }).
  • strict_init - use StrictMessageInit for controller responses and client requests.
  • export_file - generate a {name}.ts barrel file that exports {name}_pb.ts and {name}_nestjs.ts.

Boolean options are enabled when present. Use false or 0 to disable an option explicitly, for example js_dates=false.

To generate the code, simply run:

npx buf generate

Usage

For example, we have the following definition:

syntax = "proto3";

package eliza;

import "google/protobuf/timestamp.proto";

service ElizaService {
  rpc Say(SayRequest) returns (SayResponse) {}
}

message SayRequest {
  string sentence = 1;
}
message SayResponse {
  string sentence = 1;

  google.protobuf.Timestamp time = 2;
}

ElizaServiceClient, ElizaServiceController and ElizaServiceMethods will be generated.

You can then configure the grpc service:

import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { generateGrpcServices } from 'nestjs-protobuf-es';
import { file_eliza, ElizaService } from './gen/eliza_pb';

const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
  transport: Transport.GRPC,
  options: {
    package: ['eliza'],
    packageDefinition: generateGrpcServices(file_eliza),
    // Or load only selected services:
    // packageDefinition: generateGrpcServices(ElizaService),
  },
});

And implement the controller like this:

import { Controller } from '@nestjs/common';
import { ElizaServiceMethods, ElizaServiceController } from './gen/eliza_nestjs';
import { SayRequest } from './gen/eliza_pb';

@Controller()
@ElizaServiceMethods()
export class ElizaController implements ElizaServiceController {
  async say(request: SayRequest) {
    return {
      sentence: request.sentence,
      time: new Date(), // The runtime serializer converts Date to Timestamp.
    };
  }
}

Additional utilities

generateGrpcServices(input: DescFile | DescService | (DescFile | DescService)[], options?: Options)

Returns: Record<string, ServiceDefinition>

Builds @grpc/grpc-js package definitions from protobuf-es file or service descriptors.
The same Options interface below is used for deserializing incoming messages. If generated interfaces use js_dates, pass { jsDates: true } here too.

packageDefinition: generateGrpcServices(file_eliza, { jsDates: true });

initMessage(schema: DescMessage, init?: MessageInit<Message>)

Returns: Message

Creates a protobuf-es message and converts JS Date values to google.protobuf.Timestamp, including nested messages, maps, lists and oneofs.

This method normalizes nested message values in place before passing them to protobuf-es create().

populate(schema: DescMessage, message: Message, options?: Options)

Returns: PopulatedMessage<Message>

Creates a deep copy of the message and populates message fields with defaults and/or converts Timestamp to JS Date.
Method handles all the nested messages, maps, lists and oneofs.

populateInPlace(schema: DescMessage, message: Message, options?: Options)

Returns: PopulatedMessage<Message>

Like populate(), but mutates the original message.

interface Options {
  /**
   * Populate all the required fields with defaults if they are missing (default `true`).
   *
   * This ensures runtime message types are the same as generated valid types (https://github.com/bufbuild/protobuf-es/blob/main/MANUAL.md#valid-types).
   */
  validTypes?: boolean;
  /**
   * Populate scalar fields with defaults (default `false`).
   */
  scalars?: boolean;
  /**
   * Populate message fields with defaults (default `false`).
   */
  messages?: boolean;
  /**
   * Convert `google.protobuf.Timestamp` into JS `Date` (default `false`).
   *
   * NOTE: Probably you want to use this in conjunction with `js_dates` option for `protoc-gen-nestjs` to generate proper types for client and controller.
   */
  jsDates?: boolean;
}