JavaScript/TypeScript SDK for CloudEvents
Go to file
Snyk bot 25077a9b43
fix: upgrade uuid from 8.0.0 to 8.1.0 (#220)
Snyk has created this PR to upgrade uuid from 8.0.0 to 8.1.0.

See this package in NPM:
https://www.npmjs.com/package/uuid

See this project in Snyk:
https://app.snyk.io/org/lance/project/37afc620-45ad-41a3-9acc-1ac155caebc7?utm_source=github&utm_medium=upgrade-pr

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
2020-06-30 11:34:16 -04:00
.github/workflows docs: generate api documentation as a GitHub workflow (#217) 2020-06-08 18:25:27 -04:00
examples BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
plugins lib!: change CloudEvent to use direct object notation and get/set properties (#172) 2020-05-22 13:03:36 -04:00
src BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
test BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
.eslintrc BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
.gitignore BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
.npmignore Igore files for npm publish 2019-06-16 20:53:55 -03:00
.prettierrc.js BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
CHANGELOG.md chore(release): 2.0.2 2020-06-08 14:00:56 -04:00
CONTRIBUTING.md docs: add instructions and details to contributors guide (#105) 2020-05-11 20:08:45 -04:00
LICENSE Initial commit 2018-09-20 15:54:57 -04:00
README.md BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
maintainer_guidelines.md chore: minor typos in guidance docs (#196) 2020-05-29 14:03:33 -04:00
package-lock.json fix: upgrade uuid from 8.0.0 to 8.1.0 (#220) 2020-06-30 11:34:16 -04:00
package.json fix: upgrade uuid from 8.0.0 to 8.1.0 (#220) 2020-06-30 11:34:16 -04:00
pr_guidelines.md chore: minor typos in guidance docs (#196) 2020-05-29 14:03:33 -04:00
tsconfig.browser.json BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
tsconfig.json BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
webpack.config.js chore: webpack should publish to bundles not _bundles (#227) 2020-06-17 16:05:31 -04:00

README.md

JavaScript SDK for CloudEvents

Codacy Badge Codacy Badge Node.js CI npm version vulnerabilities

The CloudEvents SDK for JavaScript.

Features

  • Represent CloudEvents in memory
  • Serialize and deserialize CloudEvents in different event formats.
  • Send and recieve CloudEvents with via different protocol bindings.

Note: Supports CloudEvent versions 0.3, 1.0

Installation

The CloudEvents SDK requires a current LTS version of Node.js. At the moment those are Node.js 10.x and Node.js 12.x. To install in your Node.js project:

npm install cloudevents-sdk

Receiving and Emitting Events

Receiving Events

You can choose almost any popular web framework for port binding. Use an HTTPReceiver to process the incoming HTTP request. The receiver accepts binary and structured events in either the 1.0 or 0.3 protocol formats.

const {
  CloudEvent,
  Receiver
} = require("cloudevents-sdk");

// Create a receiver to accept events over HTTP
const receiver = new Receiver();

// body and headers come from an incoming HTTP request, e.g. express.js
const receivedEvent = receiver.accept(req.headers, req.body);
console.log(receivedEvent);

Emitting Events

You can send events over HTTP in either binary or structured format.

By default, the Emitter will emit events over HTTP POST using the binary transport protocol. The Emitter will examine the specversion of the event being sent, and use the appropriate protocol version. To send structured events, add Protocol.HTTPStructured as a parameter to emitter.send().

const { CloudEvent, Emitter, Protocol, Version } = require("cloudevents-sdk");

// With only an endpoint URL, this creates a v1 emitter
const emitter = new Emitter({
  url: "https://cloudevents.io/example"
});
const event = new CloudEvent({
  type, source, data
});

// By default, the emitter will send binary events
emitter.send(event).then((response) => {
    // handle the response
  }).catch(console.error);

// To send a structured event, just add that as an option
emitter.send(event, { protocol: Protocol.HTTPStructured })
  .then((response) => {
    // handle the response
  }).catch(console.error);

// To send an event to an alternate URL, add that as an option
emitter.send(event, { url: "https://alternate.com/api" })
  .then((response) => {
    // handle the response
  }).catch(console.error);

// Sending a v0.3 event works the same, If your event has a
// specversion property of Version.V03, then it will be sent
// using the 0.3 transport protocol
emitter.send(new CloudEvent({ specversion: Version.V03, source, type }))
  .then((response) => {
    // handle the response
  }).catch(console.error);

Example Applications

There are a few trivial example applications in the examples folder. There you will find Express.js, TypeScript and Websocket examples.

Supported specification features

v0.3 v1.0
CloudEvents Core ✔️ ✔️
AMQP Protocol Binding
AVRO Event Format
HTTP Protocol Binding ✔️ ✔️
JSON Event Format ✔️ ✔️
Kafka Protocol Binding
NATS Protocol Binding
STAN Protocol Binding

Community

Contributing

We love contributions from the community! Please check the Contributor's Guide for information on how to get involved.