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

@testurio/codec-protobuf

v0.7.3

Published

Protobuf codec for testurio Publisher/Subscriber — per-topic message-type dispatch via the codec dispatch key.

Downloads

321

Readme

@testurio/codec-protobuf

Protobuf codec for testurio Publisher / Subscriber — per-topic message-type dispatch via the codec dispatch key.

A single codec instance handles every topic, exact, RegExp, and predicate matchers can mix freely in the same bindings array. First match wins. Throws CodecError when no entry matches — no silent fallback.

Install

pnpm add -D @testurio/codec-protobuf

Peer-depends on testurio and depends on protobufjs directly.

Quick start

import { Publisher, Subscriber } from "testurio";
import { KafkaAdapter } from "@testurio/adapter-kafka";
import { ProtobufCodec } from "@testurio/codec-protobuf";

const codec = new ProtobufCodec({
  proto: "./events.proto",
  bindings: [
    { match: "orders.v1", type: "pkg.OrderEvent" },
    { match: /^events\.user\..+/, type: "pkg.UserEvent" },
    { match: (k) => k.startsWith("audit."), type: "pkg.AuditEvent" },
  ],
});

const adapter = new KafkaAdapter({ brokers: ["localhost:9092"] });
const pub = new Publisher<MyTopics>("p", { adapter, codec });
const sub = new Subscriber<MyTopics>("s", { adapter, codec });

Bindings

Each entry pairs a matcher with a fully-qualified protobuf type name.

| Matcher kind | Use for | | ------------ | ------------------------------------------------------------------------------------ | | string | Exact match — key === match. | | RegExp | Kafka RegExp subscribers, ad-hoc regex. Use ^…$ for strict single-segment matches. | | (key) => boolean | AMQP / glob wildcards (compose with adapter matcher utilities), runtime conditions. |

Entries evaluate in declaration order; the first match wins. Put more-specific matchers before catch-alls.

bindings: [
  { match: "events.orders.priority", type: "pkg.PriorityOrder" }, // specific first
  { match: /^events\.orders\..+$/,   type: "pkg.OrderEvent" },    // catch-all
];

Matcher utilities like matchAmqpTopic, matchGlobChannel, and matchRegex live in the adapter packages. Import them directly from @testurio/adapter-rabbitmq / @testurio/adapter-redis / @testurio/adapter-kafka when composing predicate matchers — @testurio/codec-protobuf has no runtime dependency on the adapter packages.

defineBindings typed helper

import { defineBindings, ProtobufCodec } from "@testurio/codec-protobuf";

type Registry = {
  "pkg.OrderEvent": OrderEvent;
  "pkg.UserEvent": UserEvent;
};
interface MyTopics {
  "orders.v1": OrderEvent;
  "users.v1": UserEvent;
}

const bindings = defineBindings<MyTopics, Registry>()([
  { match: "orders.v1", type: "pkg.OrderEvent" }, // ✅
  // { match: "users.v1", type: "pkg.OrderEvent" }, // ❌ TS error
  { match: /^audit\./, type: "pkg.OrderEvent" },  // ✅ (RegExp — type checked only)
]);

const codec = new ProtobufCodec({ proto: "./events.proto", bindings });

The helper constrains every entry's type to keyof Registry (catches FQN typos) and, for string-match entries, requires Registry[type] to be assignable to TopicMap[match] (catches topic ↔ wire-type mismatches).

The user-declared Registry is not validated against the actual .proto source — a codegen tool is the only way to close that gap fully.

Error behaviour

ProtobufCodec throws CodecError when no entry matches the dispatch key. The error message lists every configured entry's matcher + target type. If a predicate threw during the scan, the last error is attached to .cause.

CodecError: Failed to decode message with protobuf codec: No binding entry matched key='unmapped.v1' — ProtobufCodec entries: ["orders.v1" → pkg.OrderEvent, /^users\..+/ → pkg.UserEvent]

No silent fallback. Mixed-codec setups (JSON for some topics, protobuf for others) should use separate Publisher / Subscriber instances.

keepCase

protobufjs's keepCase parse option, controlling field naming for both decode output and encode input. Applied at .proto load time so encode and decode always agree.

| Value | Behaviour | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------ | | false (default) | protobufjs's native behaviour — field names are converted to camelCase ({ orderId: … }). | | true | Preserve the original .proto field names (conventionally snake_case). decode emits { order_id: … }; encode reads the same. |

// camelCase (default)
new ProtobufCodec({ proto: "./events.proto", bindings });
// → decode: { orderId: "o-1", amount: 1 }

// snake_case
new ProtobufCodec({ proto: "./events.proto", keepCase: true, bindings });
// → decode: { order_id: "o-1", amount: 1 }

decodeOptions

Defaults to { defaults: true, longs: String, enums: String }. bytes is intentionally omitted so binary fields round-trip as Uint8Array via protobufjs's native default. Override per the protobufjs.IConversionOptions shape.

Loading .proto files

Three patterns:

  1. Single fileproto: "./events.proto". Imports resolve next to the file.
  2. Multiple filesproto: ["./orders.proto", "./users.proto"]. Use when bindings span several top-level files.
  3. includePaths — mirror of protoc -I include/path:
new ProtobufCodec({
  proto: [path.resolve(__dirname, "schemas/events/orders.proto")],
  includePaths: [path.resolve(__dirname, "schemas")],
  bindings: [{ match: "orders.v1", type: "pkg.OrderEvent" }],
});

includePaths are searched in order; the default Root.resolvePath (next-to-origin + bundled well-known types) is still consulted if none match.