A CloudEvent should be readonly but provide a way to augment itself
See original GitHub issueWith recent API changes, a CloudEvent
is nicely represented as a plain old JS object. For example, for most purposes, this is actually a valid CE.
const ce = {
source: '/',
type: 'example',
specversion: 'v1.0',
id: 'asd0aan3412juv'
}
However, because we want to have spec compliance checks, and provide default values (e.g. a UUID or timestamp), we have a constructor and a class. The example above can be written as:
const ce = new CloudEvent({
source: '/',
type: 'example',
});
Here, code in the constructor is providing default values for id
and specversion
.
However this introduces some tricky edge cases. For example, extensions. According to the spec, extensions must exist as siblings of the CE properties. And JS being as loosey goosey as it is, this means a user can do some things outside of the spec boundaries. For example
const ce = new CloudEvent({ source: '/', type: 'example' });
ce[extension-1] = // <- note invalid extension name
{
foo: 'bar' // <-- note invalid extension value
};
A solution to this is to
- At the bottom of the
CloudEvent
constructor, callthis.validate()
- Make the
CloudEvent
object read only just after validation withObject.freeze(this);
- Provide a “builder” of some kind that enables cloning an event with new properties/extensions, e.g.
myEvent.cloneWith( { extension: 'value' });
(this could be a function onCloudEvent
or a class unto itself.
Note that both #228 and #229 should be implemented and added to the validation step as a part of this.
Related: https://github.com/cloudevents/sdk-javascript/issues/29
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (10 by maintainers)
After doing a bit of research on Proxies and a WIP PR #234 , it might be easier/more efficient to have a method an a cloudEvent that takes fields to update and merges then with the existing CloudEvents properties and then returns a new CloudEvent.
sort of what @lance suggested with
myEvent.cloneWith( { extension: 'value' });
😃While i do like the proxy approach, the implementation was starting to get a bit convoluted.
Yup. Doing something now. Probably sticking to the “cloneWith” method