JavaScript/TypeScript SDK for CloudEvents
Go to file
Lance Ball e334b6eceb
feat: add emitterFactory and friends (#342)
* feat: add emitterFactory and friends

This commit adds an emitterFactory function that returns an EmitterFunction
object. The EmitterFunction may be used to emit events over a supported
network transport layer. Currently, only HTTP is supported.

Parameters provided to the emitterFactory are the transport Binding (only
HTTP supported), the encoding mode (Mode.BINARY or Mode.STRUCTURED), and
a TransportFunction.

The implementation for emitBinary and emitStructured has been replaced
with this simple pattern and those two functions have been removed.

Example:

```js
// The endpoint URL that will receive the event
const sink = 'https://my-event-sink';

// A function that uses Axios to send a message over HTTP
function axiosEmitter(message: Message, options?: Options): Promise<unknown> {
  return axios.post(sink, message.body, { headers: message.headers, ...options });
}

// Create an event emitter
const emit = emitterFactory(HTTP, Mode.BINARY, axiosEmitter);

// Emit an event, sending it to the endpoint URL
emit(new CloudEvent{ source: '/example', type: 'example' });
```

Signed-off-by: Lance Ball <lball@redhat.com>
2020-09-25 17:25:15 -04:00
.github chore: add an automated GH action for releases (#329) 2020-09-17 14:03:48 -04:00
.vscode chore: add vscode task JSON and GitHub issue/pr templates (#268) 2020-07-24 15:10:03 -04:00
examples fix: upgrade cloudevents from 3.0.1 to 3.1.0 (#335) 2020-09-03 14:52:50 -04:00
src feat: add emitterFactory and friends (#342) 2020-09-25 17:25:15 -04:00
test feat: add emitterFactory and friends (#342) 2020-09-25 17:25:15 -04:00
.eslintrc BREAKING CHANGE(lib): rewrite in TypeScript (#226) 2020-06-29 14:46:20 -04:00
.gitignore chore: add cucumber.js to list of files to lint and /docs to .gitignore (#327) 2020-08-25 16:38:17 -04:00
.gitmodules test: inplement the cucumber conformance tests from cloudevents/spec (#238) 2020-07-13 09:47:02 -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): 3.1.0" 2020-08-11 09:34:19 -04:00
CONTRIBUTING.md chore: Update references of master to main (#316) 2020-08-12 19:00:02 -04:00
LICENSE Add Copyright Date and Holder name (#257) 2020-07-21 10:06:01 -04:00
README.md docs: update README with maintainer names (#337) 2020-09-09 09:45:47 -04:00
RELEASE_GUIDELINES.md chore: Update references of master to main (#316) 2020-08-12 19:00:02 -04:00
cucumber.js test: inplement the cucumber conformance tests from cloudevents/spec (#238) 2020-07-13 09:47:02 -04:00
maintainer_guidelines.md chore: Update references of master to main (#316) 2020-08-12 19:00:02 -04:00
package-lock.json feat: add emitterFactory and friends (#342) 2020-09-25 17:25:15 -04:00
package.json feat: add emitterFactory and friends (#342) 2020-09-25 17:25:15 -04:00
pr_guidelines.md chore: Update references of master to main (#316) 2020-08-12 19:00:02 -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 fix: update browser name to cloudevents. (#292) 2020-07-30 13:55:57 -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

Receiving and Emitting Events

Receiving Events

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

const app = require("express")();
const {Receiver} = require("cloudevents");

app.post("/", (req, res) => {
  // 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");

// 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);

CloudEvent Objects

All created CloudEvent objects are read-only. If you need to update a property or add a new extension to an existing cloud event object, you can use the cloneWith method. This will return a new CloudEvent with any update or new properties. For example:

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

// Create a new CloudEvent
const ce = new CloudEvent({...});

// Add a new extension to an existing CloudEvent
const ce2 = ce.cloneWith({extension: "Value"});

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

Core Specification v0.3 v1.0
CloudEvents Core ✔️ ✔️

Event Formats v0.3 v1.0
AVRO Event Format
JSON Event Format ✔️ ✔️

Transport Protocols v0.3 v1.0
AMQP Protocol Binding
HTTP Protocol Binding ✔️ ✔️
Kafka Protocol Binding
MQTT Protocol Binding
NATS Protocol Binding

Community

Contributing

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

Each SDK may have its own unique processes, tooling and guidelines, common governance related material can be found in the CloudEvents community directory. In particular, in there you will find information concerning how SDK projects are managed, guidelines for how PR reviews and approval, and our Code of Conduct information.