protobuf-es icon indicating copy to clipboard operation
protobuf-es copied to clipboard

Request: Global Registry of generated types

Open dnsco opened this issue 3 years ago • 2 comments

I see that there is createRegistry that works with a FileDescriptorSet.

Since I'm already Generating the code, is there a way that it could be registered at load time like how the google protobufs attatch to window.proto?

I'm trying to do some stuff with runtime reflection, right now, I'm hand coding a Record<string, MessageType> so as to not have to figure out how to load a filedescriptorset.bin with create-react-app's webpack configuration.

Thanks again for the library! Love where it's going!

dnsco avatar Sep 01 '22 21:09 dnsco

Hey Dennis,

it is very unlikely that we will ever add global registration at load time for the following reasons:

  1. it is a side effect that hampers tree-shaking
  2. it leads to runtime errors for legitimate use cases

To elaborate on point 2: Protobuf for Go uses a global registry, and users often run into issues caused by it. There is even a FAQ entry about protocol buffer namespace conflicts.

Instead of handwriting a Record, perhaps you could also use createRegistry()?

import { createRegistry } from "@bufbuild/protobuf";
import { MessageA, MessageB } from "./generated"

const registry = createRegistry(
  MessageA, 
  MessageB,
);

This is obviously still a manual setup. You could easily write a plugin that generates this code for you, though. Take a look at protoc-gen-connect-web for an example for a simple plugin.

While we think that a global registry is not a good fit, especially for ECMAScript, we absolutely are interested to hear about use cases and and finding a better approach. Let's keep this open, and thank you for the request!

timostamm avatar Sep 05 '22 11:09 timostamm

Thanks for pointing me in the right direction!

dnsco avatar Sep 07 '22 00:09 dnsco