JavaScript/TypeScript SDK for CloudEvents
Go to file
Lance Ball 2118488a14
chore: use git submodules for conformance tests (#427)
I don't think downloading to `/tmp` for each `npm test` is such a great
idea. This does mean that contributors to this repo will need to run the
following command once on their clone after this commit lands.

```
git submodule init
git submodule update
```

Signed-off-by: Lance Ball <lball@redhat.com>
2021-08-05 09:47:43 -04:00
.github chore: use git submodules for conformance tests (#427) 2021-08-05 09:47:43 -04:00
.vscode chore: add vscode task JSON and GitHub issue/pr templates (#268) 2020-07-24 15:10:03 -04:00
conformance@eddc279339 chore: use git submodules for conformance tests (#427) 2021-08-05 09:47:43 -04:00
examples fix: examples/typescript-ex/package.json to reduce vulnerabilities (#395) 2021-04-06 10:58:06 -04:00
src chore: update eslint and prettier dependencies (#424) 2021-08-04 15:51:37 -04:00
test chore: update eslint and prettier dependencies (#424) 2021-08-04 15:51:37 -04:00
.eslintrc chore: update eslint and prettier dependencies (#424) 2021-08-04 15:51:37 -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 chore: use git submodules for conformance tests (#427) 2021-08-05 09:47:43 -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
.remarkrc chore: externalize remark-lint config (#406) 2021-04-21 10:12:31 -04:00
API_TRANSITION_GUIDE.md chore: add a transition guide. fixes #360 (#363) 2020-12-07 11:21:30 -05:00
CHANGELOG.md chore: release 4.0.3 (#412) 2021-07-06 14: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 chore: update codacy badges (#409) 2021-04-21 17:13:05 -04:00
RELEASE_GUIDELINES.md chore: Update references of master to main (#316) 2020-08-12 19:00:02 -04:00
cucumber.js chore: add copyrights header and lint rules (#418) 2021-05-14 09:28:49 -04:00
maintainer_guidelines.md chore: Update references of master to main (#316) 2020-08-12 19:00:02 -04:00
package-lock.json chore: use git submodules for conformance tests (#427) 2021-08-05 09:47:43 -04:00
package.json chore: use git submodules for conformance tests (#427) 2021-08-05 09:47:43 -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 docs: fix 'npm run generate-docs' (#398) 2021-04-05 10:18:59 -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 any popular web framework for port binding. A CloudEvent object can be created by simply providing the HTTP protocol binding the incoming headers and request body.

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

app.post("/", (req, res) => {
  // body and headers come from an incoming HTTP request, e.g. express.js
  const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
  console.log(receivedEvent);
});

Emitting Events

You can send events over HTTP in either binary or structured format using the HTTP binding to create a Message which has properties for headers and body.

const axios = require("axios").default;
const { HTTP, CloudEvent } = require("cloudevents");

const ce = new CloudEvent({ type, source, data });
const message = HTTP.binary(ce); // Or HTTP.structured(ce)

axios({
  method: "post",
  url: "...",
  data: message.body,
  headers: message.headers,
});

You may also use the emitterFor() function as a convenience.

const axios = require("axios").default;
const { emitterFor, Mode, CloudEvent } = require("cloudevents");

function sendWithAxios(message) {
  // Do what you need with the message headers
  // and body in this function, then send the
  // event
  axios({
    method: "post",
    url: "...",
    data: message.body,
    headers: message.headers,
  });
}

const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
emit(new CloudEvent({ type, source, data }));

You may also use the Emitter singleton

const axios = require("axios").default;
const { emitterFor, Mode, CloudEvent, Emitter } = require("cloudevents");

function sendWithAxios(message) {
  // Do what you need with the message headers
  // and body in this function, then send the
  // event
  axios({
    method: "post",
    url: "...",
    data: message.body,
    headers: message.headers,
  });
}

const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
// Set the emit
Emitter.on("cloudevent", emit);

...
// In any part of the code will send the event
new CloudEvent({ type, source, data }).emit();

// You can also have several listener to send the event to several endpoint

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.

API Transition Guide

Guide Link

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.