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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@protogenjs/protogen

v0.1.0

Published

The protogen package makes it easy to write protoc plugins in JavaScript/Typescript

Readme

protogen

Package protogen makes writing protoc plugins easy. Working with the raw protobuf descriptor messages can be cumbersome. protogen resolves and links the dependencies and references between the raw Protobuf descriptors and turns them into their corresponding protogen classes that are easier to work with. It also provides mechanisms that are espacially useful to generate JavaScript/TypeScript code like dealing with module imports.

Installation

Package protogen is available via npm. To install run:

npm install @protogenjs/protogen

API

Most classes in protogen are simply replacements of their corresponding Protobuf descriptors: protogen.File represents a FileDescriptor, protogen.Message a Descriptor, protogen.Field a FieldDescriptor and so on. They should be self explanatory. You can read their docstrings for more information about them.

The classes protogen.Options, protogen.Plugin and protogen.GeneratedFile make up a framework to generate files. You can see these in action in the following example plugin:

#!/usr/bin/env node
/** An example plugin. */

import * as protogen from "@protogenjs/protogen";

new protogen.Options().run((gen: protogen.Plugin) => {
  for (let file of gen.filesToGenerate) {
    let g = gen.newGeneratedFile(
      file.name.replace(".proto", ".ts"),
      file.jsImportPath
    );
    g.P("# Generated code ahead.");
    g.P();
    g.printImports();
    g.P();
    for (let message of file.messages) {
      g.P("export class ", message.jsIdent.name, "{");
      for (let field of message.fields) {
        // TODO: generate code for the field
      }
      g.P("}");
      g.P();
    }
    for (let service of file.services) {
      g.P("export class ", service.jsIdent.name, "Client {");
      g.P("constructor (private host: string) {}");
      for (let method of service.methods) {
        // prettier-ignore
        g.P(method.jsName, "(request: ", method.input.jsIdent, "): ", method.output.jsIdent, "{");
        // TODO: generate the method implementation
        g.P("}");
        g.P();
      }
      g.P("}");
      g.P();
    }
  }
});

Misc

What is a protoc plugin anyway?

protoc, the Protobuf compiler, is used to generate code derived from Protobuf definitions (.proto files). Under the hood, protoc's job is to read and parse the definitions into their Descriptor types (see google/protobuf/descriptor.proto). When protoc is run (with a plugin) it creates a CodeGeneratorRequest (see google/protobuf/compiler/plugin.proto#L68) that contains the descriptors for the files to generate and everything they import and passes it to the plugin via stdin.

A protoc plugin is an executable. It reads the CodeGeneratorRequest from stdin and returns a CodeGeneratorResponse (see google/protobuf/compiler/plugin.proto#L99) via stdout. The plugin can use the descriptors from the CodeGeneratorRequest to create output files (in memory). It returns these output files (consisting of name and content as string) in the CodeGeneratorResponse to protoc.

protoc then writes these files to disk.

Run protoc with your plugin

Assume you have an executable plugin under path/to/plugin/main.js. You can invoke it via:

protoc 
    --plugin=protoc-gen-myplugin=path/to/plugin/main.js \
    --plugin_out=./output_root \
    myproto.proto myproto2.proto

Caveats:

  • you must use the --plugin=protoc-gen-<plugin_name> prefix, otherwise protoc fails with "plugin not executable"
  • your plugin must be executable (chmod +x path/to/plugin/main.js and put a #!/usr/bin/env node at the top of the file)

See also

Credits

This package is inspired by the google.golang.org/protobuf/compiler/protogen Golang package.