JavaScript/TypeScript SDK for CloudEvents
Go to file
Fabio José af7f7fe2ab Filter to process just the availble attributes
Signed-off-by: Fabio José <fabiojose@gmail.com>
2019-11-04 09:12:42 -03:00
examples ts example with binary emitter 2019-10-29 21:43:21 -03:00
ext Json schema for spec v1 2019-10-29 08:55:42 -03:00
http/unmarshaller Export of http unmarshaller 0.2 2019-06-20 21:45:49 -03:00
lib Filter to process just the availble attributes 2019-11-04 09:12:42 -03:00
test Comment pendeing stuff 2019-11-02 07:06:38 -03:00
v1 Fixing headers arg type 2019-11-02 07:16:01 -03:00
v02 Fix the unmarshaller import 2019-08-05 15:10:29 -03:00
v03 Exporting the HTTPUnmarshaller 2019-08-05 09:22:15 -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 Fixing the coverage issue when PR 2019-06-26 12:49:29 -03:00
CHANGELOG.md Document the changelog of v1.0.0 2019-10-29 16:28:39 -03:00
CONTRIBUTING.md Add changelog too 2019-08-22 09:14:09 -03:00
LICENSE Initial commit 2018-09-20 15:54:57 -04:00
README.md Update the versioning section 2019-09-01 10:44:48 -03:00
index.js double quotes 2018-12-15 11:19:48 -02:00
package.json fix An implementation cannot be declared in ambient contexts 2019-10-29 09:46:20 -03:00

README.md

licence Codacy Badge Codacy Badge Build Status downloads npm version dependencies vulnerabilities

sdk-javascript

Official CloudEvents' SDK for JavaScript.

CloudEvents logo

NOTE: This SDK is still considered work in progress, things might (and will) break with every update.

Checkout the changelog to see what's going on!

Contributing

Before create an awesome PR, please read our guidelines.

Examples

To see working examples, point to examples.

📰 Newsletter 📰

all the API developed before, for 0.1 and 0.2, works as the same.

Checkout the new expressive additions.

New way to import the specifications stuff

// Import the v0.3 stuff
const v03 = require("cloudevents-sdk/v03");

// Access the spec
v03.Spec;

// Access the structured http event emitter
v03.StructuredHTTPEmitter;

// Access the binary http event emitter
v03.BinaryHTTPEmitter;

// Access http unmarshaller to process incoming events, Binary or Structured
v03.HTTPUnmarshaller;

An easy way to create events

// Import the v0.3 stuff
const v03 = require("cloudevents-sdk/v03");

// Creates an event using the v0.3 spec
let ce =
  v03.event()
       .type("com.github.pull.create")
       .source("urn:event:from:myapi/resourse/123");

Versioning

Before Spec reaches 1.0

  • 0.x.p: where x relates to spec version and p relates to fixes, releases and breaking changes

After Spec reaches 1.0

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

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.1 v0.2 v0.3
CloudEvents yes yes yes
HTTP Transport Binding - Structured yes yes yes
HTTP Transport Binding - Binary yes yes yes
JSON Event Format yes yes yes

What we can do

What v0.1 v0.2 v0.3
Create events yes yes yes
Emit Structured events over HTTP yes yes yes
Emit Binary events over HTTP yes yes yes
JSON Event Format yes yes yes
Receice Structured events over HTTP no yes yes
Receice Binary events over HTTP no yes yes

How to use

The Cloudevent constructor arguments.

/*
 * spec  : if is null, set the spec 0.1 impl
 * format: if is null, set the JSON Format 0.1 impl
 */
Cloudevent(spec, format);

Usage

var Cloudevent = require("cloudevents-sdk");

var Spec02 = require("cloudevents-sdk/v02");

/*
 * Constructs a default instance with:
 *   - Spec 0.1
 *   - JSON Format 0.1
 */
var cloudevent01 = new Cloudevent();

/*
 * Implemented using Builder Design Pattern
 */
cloudevent01
  .type("com.github.pull.create")
  .source("urn:event:from:myapi/resourse/123");

/*
 * Backward compatibility to spec 0.1 by injecting methods from spec
 * implementation to Cloudevent
 */
cloudevent01
  .eventTypeVersion("1.0");

/*
 * Constructs an instance with:
 *   - Spec 0.2
 *   - JSON Format 0.1
 */
var cloudevent02 = new Cloudevent(Cloudevent.specs["0.2"]);

/*
 * Different specs, but the same API.
 */
cloudevent02
  .type("com.github.pull.create")
  .source("urn:event:from:myapi/resourse/123");

Formatting

var Cloudevent = require("cloudevents-sdk");

/*
 * Creates an instance with default spec and format
 */
var cloudevent =
  new Cloudevent()
        .type("com.github.pull.create")
        .source("urn:event:from:myapi/resourse/123");

/*
 * Format the payload and return it
 */
var formatted = cloudevent.format();

Emitting

var Cloudevent = require("cloudevents-sdk");

// The event
var cloudevent =
  new Cloudevent()
    .type("com.github.pull.create")
    .source("urn:event:from:myapi/resourse/123");

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

/*
 * To use HTTP Binary:
 *   Cloudevent.bindings["http-binary0.2"](config);
 */

// The binding instance
var binding = new Cloudevent.bindings["http-structured0.1"](config);

// Emit the event using Promise
binding.emit(cloudevent)
  .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 Unmarshaller to process the HTTP Payload and HTTP Headers, extracting the CloudEvents.

The Unmarshaller will parse the HTTP Request and decides if it is a binary or a structured version of transport binding.

😃 Checkout the full working example: here.

// some parts were removed //

const v02 = require("cloudevents-sdk/v02");
const unmarshaller = new v02.HTTPUnmarshaller();

// some parts were removed //

app.post('/', function (req, res) {
  unmarshaller.unmarshall(req.body, req.headers)
    .then(cloudevent => {

      // TODO use the cloudevent

      res.status(201)
            .send("Event Accepted");
  })
  .catch(err => {
    console.error(err);
    res.status(415)
          .header("Content-Type", "application/json")
          .send(JSON.stringify(err));
  });
});


Repository Structure

├── index.js
├── ext
├── lib
│   ├── bindings
│   │   └── http
│   ├── cloudevent.js
│   ├── formats
│   │   └── json
│   └── specs
├── LICENSE
├── package.json
├── README.md
  • index.js: library exports
  • ext: external stuff, e.g, Cloud Events JSONSchema
  • lib/bindings: every binding implementation goes here
  • lib/bindings/http: every http binding implementation goes here
  • lib/cloudevent.js: implementation of Cloudevent, an interface
  • lib/formats/: every format implementation goes here
  • lib/specs/: every spec implementation goes here

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()

/*
 * Create a Cloudevent instance from String.
 */
Cloudevent Cloudevent.fromString(String)

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)

Unmarshaller classes

The Unmarshaller classes uses the receiver API, abstracting the formats:

  • structured
  • binary

Choosing the right implementation based on the headers map.

/*
 * Constructor without arguments
 */
Unmarshaller()

/*
 * The method to unmarshall the payload.
 * @arg payload could be a string or a object
 * @arg headers a map of headers
 */
Promise Unmarshaller.unmarshall(payload, headers)

See how to implement the method injection here

Learn about Builder Design Pattern

Check out the produced event payload using this tool