Skip to content

Registries

Registries are collections of descriptors keyed by fully qualified name. Use them when you need to resolve types dynamically, parse or serialize google.protobuf.Any, or work with extensions in JSON.

A Registry exposes typed accessors and is iterable. Look up a descriptor by its fully qualified name:

import type { Registry } from "@bufbuild/protobuf";
declare const registry: Registry;
registry.getMessage("example.User");
registry.getEnum("example.PhoneType");
registry.getService("example.UserService");
registry.getExtension("example.sensitive");
for (const type of registry) {
type.kind;
}

Create a registry from descriptors with createRegistry():

import { createRegistry } from "@bufbuild/protobuf";
import { UserSchema, file_example } from "./gen/example_pb";
const registry = createRegistry(UserSchema, file_example, otherRegistry);

Use createMutableRegistry() if descriptors need to be added or removed over time.

import { createMutableRegistry } from "@bufbuild/protobuf";
import { UserSchema } from "./gen/example_pb";
const mutable = createMutableRegistry();
mutable.add(UserSchema);
mutable.remove(UserSchema);

If you have a google.protobuf.FileDescriptorSet, create a file registry with createFileRegistry().

Compile a descriptor set with the Buf CLI:

Terminal window
buf build proto --output set.binpb

Read it in JavaScript:

import { readFileSync } from "node:fs";
import { createFileRegistry, fromBinary } from "@bufbuild/protobuf";
import { FileDescriptorSetSchema } from "@bufbuild/protobuf/wkt";
const fileDescriptorSet = fromBinary(FileDescriptorSetSchema, readFileSync("set.binpb"));
const registry = createFileRegistry(fileDescriptorSet);
for (const file of registry.files) {
file.name;
}