JavaScript/TypeScript SDK for CloudEvents
Go to file
Lance Ball 54f242b79e
feat: expose a mode and version agnostic event receiver (#120)
Event receivers in the wild may not always know what version or mode an
incoming event is. Instead of requiring developers to inspect the headers
themselves, the SDK should provide an HTTP receiver that is capable of
figuring out what the version and mode (structured/binary) of an incoming
event is and handle it appropriately.

In determining the best way to expose this, I chose to modify the API a
little bit. Now, instead of `const CloudEvent = require('cloudevents-sdk');`
users need to destructure it.

```js
const { HTTPReceiver, CloudEvent } = require('cloudevents-sdk');
```

This change should not be backported to 1.x.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/93

Signed-off-by: Lance Ball <lball@redhat.com>
2020-05-06 13:25:16 -04:00
examples feat: use CloudEvents not cloudevents everywhere (#101) 2020-04-30 16:14:13 -07:00
ext src,fix: drop support for v0.2 and clean up contenttype 2020-04-28 17:37:49 -03:00
lib feat: expose a mode and version agnostic event receiver (#120) 2020-05-06 13:25:16 -04:00
test feat: expose a mode and version agnostic event receiver (#120) 2020-05-06 13:25:16 -04:00
v1 feat: use CloudEvents not cloudevents everywhere (#101) 2020-04-30 16:14:13 -07:00
v03 feat: use CloudEvents not cloudevents everywhere (#101) 2020-04-30 16:14:13 -07:00
.eslintrc chore: update eslint rules to disallow var usage 2020-04-30 12:36:04 -03:00
.gitignore Ignore package-lock 2019-10-29 20:49:43 -03:00
.npmignore Igore files for npm publish 2019-06-16 20:53:55 -03:00
.travis.yml deps: update old and remove unused dependencies 2020-04-20 14:53:53 -04:00
CHANGELOG.md chore: add standard-version and release script 2020-04-29 08:50:52 -03:00
CONTRIBUTING.md chore: add standard-version and release script 2020-04-29 08:50:52 -03:00
LICENSE Initial commit 2018-09-20 15:54:57 -04:00
README.md docs: organize README badges and remove TS example (#112) 2020-05-04 10:33:40 -07:00
index.js feat: expose a mode and version agnostic event receiver (#120) 2020-05-06 13:25:16 -04:00
package-lock.json chore: Update uuid dependency 2020-04-30 12:34:30 -03:00
package.json chore: Update uuid dependency 2020-04-30 12:34:30 -03:00

README.md

JavaScript SDK for CloudEvents

Codacy Badge Codacy Badge Build Status downloads npm version vulnerabilities licence

The CloudEvents SDK for JavaScript.

Status

This SDK is still considered a work in progress.

This SDK current supports the following versions of CloudEvents:

  • v1.0

Checkout the changelog to see what's going on!

Installation

This CloudEvents SDK requires nodejs 6.11+

Nodejs

npm install cloudevents-sdk

Specification Support

These are the supported specifications by this version.

Specifications v0.3 v1.0
CloudEvents yes yes
HTTP Transport Binding - Structured yes yes
HTTP Transport Binding - Binary yes yes
JSON Event Format yes yes

What we can do

What v0.3 v1.0
Create events yes yes
Emit Structured events over HTTP yes yes
Emit Binary events over HTTP yes yes
JSON Event Format yes yes
Receive Structured events over HTTP yes yes
Receive Binary events over HTTP yes yes

How to use

Usage

const v1 = require("cloudevents-sdk/v1");

/*
 * Creating an event
 */
let myevent = v1.event()
  .type("com.github.pull.create")
  .source("urn:event:from:myapi/resource/123");

Formatting

const v1 = require("cloudevents-sdk/v1");

/*
 * Creating an event
 */
let myevent = v1.event()
  .type("com.github.pull.create")
  .source("urn:event:from:myapi/resource/123");

/*
 * Format the payload and return it
 */
let formatted = myevent.format();

Emitting

const v1 = require("cloudevents-sdk/v1");

/*
 * Creating an event
 */
let myevent = v1.event()
  .type("com.github.pull.create")
  .source("urn:event:from:myapi/resource/123");

// The binding configuration using POST
let config = {
  method: "POST",
  url   : "https://myserver.com"
};

// The binding instance
let binding = new v1.StructuredHTTPEmitter(config);

// Emit the event using Promise
binding.emit(myevent)
  .then(response => {
    // Treat the response
    console.log(response.data);

  }).catch(err => {
    // Deal with errors
    console.error(err);
  });

Receiving Events

You can choose any framework for port binding. But, use the StructuredHTTPReceiver or BinaryHTTPReceiver to process the HTTP Payload and HTTP Headers, extracting the CloudEvents.

😃 Checkout the full working example: here.

// some parts were removed //

const v1 = require("cloudevents-sdk/v1");

const receiver = new v1.StructuredHTTPReceiver();

// some parts were removed //

app.post("/", (req, res) => {
  try {
    let myevent = receiver.parse(req.body, req.headers);

    // TODO use the event

    res.status(201).send("Event Accepted");

  } catch(err) {
    // TODO deal with errors
    console.error(err);
    res.status(415)
          .header("Content-Type", "application/json")
          .send(JSON.stringify(err));
  }
});

Unit Testing

The unit test checks the result of formatted payload and the constraints.

npm test

The API

CloudEvent class

/*
 * Format the payload and return an Object.
 */
Object CloudEvent.format()

/*
 * Format the payload as String.
 */
String CloudEvent.toString()

Formatter classes

Every formatter class must implement these methods to work properly.

/*
 * Format the CloudEvent payload argument and return an Object.
 */
Object Formatter.format(Object)

/*
 * Format the CloudEvent payload as String.
 */
String Formatter.toString(Object)

Parser classes

Every Parser class must implement these methods to work properly.

/*
 * The default constructor with Parser as decorator
 */
Parser(Parser)

/*
 * Try to parse the payload to some event format
 */
Object Parser.parse(payload)

Spec classes

Every Spec class must implement these methods to work properly.

/*
 * The constructor must receives the CloudEvent type.
 */
Spec(CloudEvent)

/*
 * Checks the spec constraints, throwing an error if do not pass.
 * @throws Error when it is an invalid event
 */
Spec.check()

/*
 * Checks if the argument pass through the spec constraints
 * @throws Error when it is an invalid event
 */
Spec.check(Object)

Binding classes

Every Binding class must implement these methods to work properly.

Emitter Binding

Following we have the signature for the binding to emit CloudEvents.

/*
 * The constructor must receives the map of configurations.
 */
Binding(config)

/*
 * Emits the event using an instance of CloudEvent.
 */
Binding.emit(cloudEvent)

Receiver Binding

Following we have the signature for the binding to receive CloudEvents.

/*
 * The constructor must receives the map of configurations.
 */
Receiver(config)

/*
 * Checks if some Object and a Map of headers
 * follows the binding definition, throwing an error if did not follow
 */
Receiver.check(Object, Map)

/*
 * Checks and parse as CloudEvent
 */
CloudEvent Receiver.parse(Object, Map)

Versioning

  • x.M.p: where x relates to spec version, M relates to minor and p relates to fixes. See semver

Community