question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A CloudEvent should be readonly but provide a way to augment itself

See original GitHub issue

With 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

  1. At the bottom of the CloudEvent constructor, call this.validate()
  2. Make the CloudEvent object read only just after validation with Object.freeze(this);
  3. 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 on CloudEvent 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:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
lholmquistcommented, Jul 1, 2020

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.

0reactions
lholmquistcommented, Jul 1, 2020

Create a CloudEvent from a CloudEvent

Yup. Doing something now. Probably sticking to the “cloneWith” method

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cloud Pub/Sub Tutorial 1st gen
After you finish these steps, you can delete the project, removing all resources associated with the project. Go to project selector. Make sure...
Read more >
Class: CloudEvents::Event::V0
This object is immutable, and Ractor-shareable on Ruby 3. The data and attribute values can be retrieved but not modified. To obtain an...
Read more >
PubSub+ Cloud Releases - Solace
Enterprise customers can now deploy PubSub+ Cloudevent broker services in AWS ... This provides a way to explore historical data, investigate anomalies, ...
Read more >
All configuration options - Quarkus
AWS Lambda Type Default AWS Lambda Common Type Default AWS Lambda Gateway REST API Type Default Agroal ‑ Database connection pool Type Default
Read more >
Serverless OpenShift Container Platform 4.11
This document provides information on how to use OpenShift Serverless in ... can be used for development and testing purposes, but does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found