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

@aptre/protobuf-es-lite

v0.4.8

Published

Lightweight Protobuf codegen for TypeScript and JavaScript.

Downloads

635

Readme

protobuf-es-lite

npm version npm downloads

protobuf-es-lite is a TypeScript and JavaScript protobuf implementation.

It uses protoplugin to implement protoc-gen-es-lite which generates js and/or ts code.

See protobuf-es for information about protoplugin.

Ecosystem

Lightweight Protobuf 3 RPCs are implemented in StaRPC for Go and TypeScript.

protoc-gen-doc is recommended for generating documentation.

protobuf-go-lite is recommended for lightweight Go protobufs.

Purpose

protobuf-es generates a class for each message with interfaces for partial or plain messages. It is significantly easier to construct and pass around plain and partial messages as opposed to using classes, and plain messages work better when used in React props/state. This ends up looking like:

const myMessage: PlainMessage<MyMessage> = {body: "Hello world"}
const myMessageBin = new MyMessage(myMessage).toBinary()

ts-proto generates an interface for each protobuf message and a const message declaration object containing the marshal and unmarshal functions. This allows using interfaces and plain messages everywhere:

// Create a partial MyMessage with just one field sets.
const myMessage: MyMessage = {body: "Hello world"}
// Creates a version of MyMessage filled with zeros.
const myCompleteMessage: MyMessage = MyMessage.create(myMessage)
// Convert MyMessage to binary.
const myMessageBin = MyMessage.toBinary(myCompleteMessage)

This fork generates the ts-proto style with the protobuf-es code generator.

Installation

protoc-gen-es generates base types - messages and enumerations - from your Protocol Buffer schema.

To install the plugin and the runtime library, run:

npm install @aptre/protoc-gen-es-lite

We use peer dependencies to ensure that code generator and runtime library are compatible with each other. Note that npm installs them automatically, but yarn and pnpm do not.

Generating code

With buf

npm install --save-dev @bufbuild/buf

Add a new configuration file buf.gen.yaml:

# buf.gen.yaml defines a local generation template.
# For details, see https://docs.buf.build/configuration/v1/buf-gen-yaml
version: v1
plugins:
  # This will invoke protoc-gen-es-lite and write output to src/gen
  - plugin: es-lite
    out: src/gen
    opt:
      # Add more plugin options here
      - target=ts

To generate code for all protobuf files within your project, simply run:

npx buf generate

Note that buf can generate from various inputs, not just local protobuf files.

With protoc

PATH=$PATH:$(pwd)/node_modules/.bin \
  protoc -I . \
  --es-lite_out src/gen \
  --es-lite_opt target=ts \
  a.proto b.proto c.proto

Note that we are adding node_modules/.bin to the $PATH, so that the protocol buffer compiler can find them. This happens automatically with npm scripts.

Since yarn v2 and above does not use a node_modules directory, you need to change the variable a bit:

PATH=$(dirname $(yarn bin protoc-gen-es-lite)):$PATH

Plugin options

target

This option controls whether the plugin generates JavaScript, TypeScript, or TypeScript declaration files.

Possible values:

  • target=js - generates a .pb.js file for every .proto input file.
  • target=ts - generates a .pb.ts file for every .proto input file.
  • target=dts - generates a .pb.d.ts file for every .proto input file.

Multiple values can be given by separating them with +, for example target=js+dts.

By default, we generate JavaScript and TypeScript declaration files, which produces the smallest code size and is the most compatible with various bundler configurations. If you prefer to generate TypeScript, use target=ts.

import_extension=.js

By default, protoc-gen-es (and all other plugins based on protoplugin) uses a .js file extensions in import paths, even in TypeScript files.

This is unintuitive, but necessary for ECMAScript modules in Node.js. Unfortunately, not all bundlers and tools have caught up yet, and Deno requires .ts. With this plugin option, you can replace .js extensions in import paths with the given value. For example, set

  • import_extension=none to remove the .js extension.
  • import_extension=.ts to replace the .js extension with .ts.

js_import_style

By default, protoc-gen-es (and all other plugins based on protoplugin) generates ECMAScript import and export statements. For use cases where CommonJS is difficult to avoid, this option can be used to generate CommonJS require() calls.

Possible values:

  • js_import_style=module generate ECMAScript import / export statements - the default behavior.
  • js_import_style=legacy_commonjs generate CommonJS require() calls.

keep_empty_files=true

By default, protoc-gen-es (and all other plugins based on protoplugin) omits empty files from the plugin output. This option disables pruning of empty files, to allow for smooth interoperation with Bazel and similar tooling that requires all output files to be declared ahead of time. Unless you use Bazel, it is very unlikely that you need this option.

ts_nocheck=false

By default, protoc-gen-es (and all other plugins based on protoplugin) generates an annotation at the top of each file: // @ts-nocheck.

We generate the annotation to support a wide range of compiler configurations and future changes to the language. But there can be situations where the annotation shadows an underlying problem, for example an unresolvable import. To remove the annotation and to enable type checks, set the plugin option ts_nocheck=false.

Developing on MacOS

On MacOS, some homebrew packages are required for yarn gen:

brew install bash make coreutils gnu-sed findutils protobuf
brew link --overwrite protobuf

Add to your .bashrc or .zshrc:

export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
export PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"
export PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
export PATH="/opt/homebrew/opt/make/libexec/gnubin:$PATH"

Support

Please open a GitHub issue with any questions / issues.

... or feel free to reach out on Matrix Chat or Discord.

License

Apache-2.0