sdk-javascript icon indicating copy to clipboard operation
sdk-javascript copied to clipboard

Serialize and deserialize custom extensions

Open gradybarrett opened this issue 4 months ago • 7 comments

Is your feature request related to a problem? Please describe.

I work in a large enterprise, and we have a few custom extensions defined. With the given structure, we can just extend the CloudEventV1 interface, add our extensions, hand that to devs, and they can just create CloudEvent's with that options interface. Super lightweight. Awesome. 🥳

What I don't love is that there's no clean way to inject our custom extensions into the transport layer, specifically for deserialize.

For example, we define a tags extension, which is simply a [key: string]: string object, that we encode. Easy to kinda handle for serialize via a toJSON() implementation. But for deserialize (and proper serialize support - binary + structured) to decode tags, we're going to have to, for example, wrap the HTTP deserialize function, then provide our own Binding for HTTP and have folks use that custom binding. Doable, but I don't love having to wrap the existing functions and then maintain our own Bindings, all for each transport.

Describe the solution you would like to see

It would be cool to be able to define an Extension, something like this, patterned after the Binding interface:

interface Extension {
  // CE Extension's key/name
  key: string;
  deserialize: ExtensionDeserializer;
  serializeStructured: ExtensionSerializer;
  serializeBinary: ExtensionSerializer;
}

Then be able to "register" those extensions with the transport code, then have the transport code exec the correct supplied extension code when it sees an extension key that matches a "registered" extension.

I realize the interface above is a bit naive. We'd want a way to be able to implement that interface per-transport type, but hopefully that's enough for you to kinda see where I'm headed with this.

Is that sort of interface something you would entertain?

gradybarrett avatar Oct 01 '25 23:10 gradybarrett

Ping @lance

duglin avatar Oct 07 '25 19:10 duglin

Adding to this thread since we've had have more conversations since. We'll also want to talk through how to handle multi-attribute extentions such as tracing and data classification. The Java SDK implements, for example, data classification as a single extension with multiple keys, then serializes that extension into multiple attributes on the cloud event. That leads to the existing readFrom and keySet interface. Not sure I'm a huge fan of how that was done, but I get why it was done.

gradybarrett avatar Oct 09 '25 16:10 gradybarrett

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] avatar Nov 09 '25 00:11 github-actions[bot]

Bump

gradybarrett avatar Nov 09 '25 01:11 gradybarrett

ping @lance @lholmquist @fabiojose @danbev

duglin avatar Nov 09 '25 16:11 duglin

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] avatar Dec 11 '25 00:12 github-actions[bot]

Bump

gradybarrett avatar Dec 11 '25 00:12 gradybarrett