JavaScript/TypeScript SDK for CloudEvents
Go to file
Grant Timmerman 42652819f3
feat: add types to package.json (#216)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
2020-06-08 15:27:31 -07:00
.github/workflows docs: generate api documentation as a GitHub workflow (#217) 2020-06-08 18:25:27 -04:00
examples docs: Update references of specific versions to use Latest Supported. (#211) 2020-06-08 15:47:30 -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 docs: generate api documentation as a GitHub workflow (#217) 2020-06-08 18:25:27 -04:00
test fix: add correct types to improve TypeScript behavior (#202) 2020-06-04 14:35:51 -04:00
test-ts fix: add correct types to improve TypeScript behavior (#202) 2020-06-04 14:35:51 -04:00
.eslintrc docs: add JSDocs for top level API objects (#140) 2020-05-12 17:27:11 -04:00
.gitignore fix: fix references to constants - remove .js extension (#200) 2020-06-01 17:55:54 -04:00
.npmignore Igore files for npm publish 2019-06-16 20:53:55 -03: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 docs: Update references of specific versions to use Latest Supported. (#211) 2020-06-08 15:47:30 -04:00
maintainer_guidelines.md chore: minor typos in guidance docs (#196) 2020-05-29 14:03:33 -04:00
package-lock.json docs: generate api documentation as a GitHub workflow (#217) 2020-06-08 18:25:27 -04:00
package.json feat: add types to package.json (#216) 2020-06-08 15:27:31 -07:00
pr_guidelines.md chore: minor typos in guidance docs (#196) 2020-05-29 14:03:33 -04:00
tsconfig.json docs: generate api documentation as a GitHub workflow (#217) 2020-06-08 18:25:27 -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,
  HTTPReceiver
} = require("cloudevents-sdk");

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

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

Emitting Events

To emit events, you'll need to decide whether the event should be sent in binary or structured format, and determine what version of the CloudEvents specification you want to send the event as.

By default, the HTTPEmitter will emit events over HTTP POST using the latest supported specification version, in binary mode. You can emit version specific events by providing the specication version in the constructor to HTTPEmitter. To send structured events, add that string as a parameter to emitter.send().

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

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

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

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

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

// Sending a v0.3 event works the same, just let the emitter know when
// you create it that you are working with the 0.3 spec
const v03Emitter = new HTTPEmitter({
  url: "https://cloudevents.io/example",
  version: "0.3"
});

// Again, the default is to send binary events
// To send a structured event or to an alternate URL, provide those
// as parameters in a options object as above
v3Emitter.send(event)
  .then((response) => {
    // handle the response
  }).catch(console.error);

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.