パッケージの詳細

cloudevents

cloudevents4mApache-2.010.0.0

CloudEvents SDK for JavaScript

events, cloudevents, sdk, javascript

readme

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 receive CloudEvents with via different protocol bindings.

Note: Supports CloudEvent version 1.0

Installation

The CloudEvents SDK requires a current LTS version of Node.js. At the moment those are Node.js 16.x, and Node.js 18.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

The easiest way to send events is to use the built-in HTTP emitter.

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

// Create an emitter to send events to a receiver
const emit = emitterFor(httpTransport("https://my.receiver.com/endpoint"));

// Create a new CloudEvent
const ce = new CloudEvent({ type, source, data });

// Send it to the endpoint - encoded as HTTP binary by default
emit(ce);

If you prefer to use another transport mechanism for sending events over HTTP, you can use the HTTP binding to create a Message which has properties for headers and body, allowing greater flexibility and customization. For example, the axios module is used here to send a CloudEvent.

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 to send your CloudEvents.

const { emitterFor, httpTransport, Mode, CloudEvent, Emitter } = require("cloudevents");

// Create a CloudEvent emitter function to send events to our receiver
const emit = emitterFor(httpTransport("https://example.com/receiver"));

// Use the emit() function to send a CloudEvent to its endpoint when a "cloudevent" event is emitted
// (see: https://nodejs.org/api/events.html#class-eventemitter)
Emitter.on("cloudevent", emit);

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

// You can also have several listeners to send the event to several endpoints

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"});

You can create a CloudEvent object in many ways, for example, in TypeScript:

import { CloudEvent, CloudEventV1, CloudEventV1Attributes } from "cloudevents";
const ce: CloudEventV1<string> = {
  specversion: "1.0",
  source: "/some/source",
  type: "example",
  id: "1234"
};
const event = new CloudEvent(ce);
const ce2: CloudEventV1Attributes<string> = {
  specversion: "1.0",
  source: "/some/source",
  type: "example",
};
const event2 = new CloudEvent(ce2);
const event3 = new CloudEvent({
  source: "/some/source",
  type: "example",
});

A Note About Big Integers

When parsing JSON data, if a JSON field value is a number, and that number is really big, JavaScript loses precision. For example, the Twitter API exposes the Tweet ID. This is a large number that exceeds the integer space of Number.

In order to address this situation, you can set the environment variable CE_USE_BIG_INT to the string value "true" to enable the use of the json-bigint package. This package is not used by default due to the resulting slowdown in parse speed by a factor of 7x.

See for more information: https://github.com/cloudevents/sdk-javascript/issues/489

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 :white_check_mark: :white_check_mark:

Event Formats v0.3 v1.0
AVRO Event Format :x: :x:
JSON Event Format :white_check_mark: :white_check_mark:

Protocol Bindings v0.3 v1.0
AMQP Protocol Binding :x: :x:
HTTP Protocol Binding :white_check_mark: :white_check_mark:
Kafka Protocol Binding :x: :white_check_mark:
MQTT Protocol Binding :white_check_mark: :x:
NATS Protocol Binding :x: :x:

Content Modes v0.3 v1.0
HTTP Binary :white_check_mark: :white_check_mark:
HTTP Structured :white_check_mark: :white_check_mark:
HTTP Batch :white_check_mark: :white_check_mark:
Kafka Binary :white_check_mark: :white_check_mark:
Kafka Structured :white_check_mark: :white_check_mark:
Kafka Batch :white_check_mark: :white_check_mark:
MQTT Binary :white_check_mark: :white_check_mark:
MQTT Structured :white_check_mark: :white_check_mark:

Community

Maintainers

Currently active maintainers who may be found in the CNCF Slack.

  • Lance Ball (@lance)
  • Lucas Holmquist (@lholmquist)

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.

If there is a security concern with one of the CloudEvents specifications, or with one of the project's SDKs, please send an email to cncf-cloudevents-security@lists.cncf.io.

Additional SDK Resources

更新履歴

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

10.0.0 (2025-06-05)

⚠ BREAKING CHANGES

  • remove Node 18 support (#616)

Features

9.0.0 (2025-04-03)

⚠ BREAKING CHANGES

  • remove node 16 (#610)

Features

8.0.3 (2025-04-02)

Bug Fixes

8.0.2 (2024-07-22)

Bug Fixes

  • creating an event does not error when the event attribute name is too long (#593) (6977113)

8.0.1 (2024-06-12)

Bug Fixes

Miscellaneous

  • Update compatible node version (#573) (245bae9)
  • updated check mark symbol to show some green checkboxes (#582) (c65afe9)

8.0.0 (2023-07-24)

⚠ BREAKING CHANGES

  • use string instead of enum for Version (#561) (15f6505) TypeScript does not consider enum values equivalent, even if the string representation is the same. So, when a module imports cloudevents and also has a dependency on cloudevents this can cause conflicts where the CloudEvent.version attribute is not considered equal when, in fact, it is.

Miscellaneous

  • add npm run build:schema to the doc generation action (#557) (fa388f7)
  • modify release-please to use Signed-Off-By on commits (#559) (089520a)
  • release 8.0.0 (#563) (1ed43c8)

7.0.2 (2023-07-05)

Miscellaneous

  • add the provenance flag when publishing to npm (#556) (a0d8682)
  • fix the release-please automation script. (#554) (023171d)

7.0.1 (2023-05-30)

Bug Fixes

  • handle big integers in incoming events (#495) (43c3584)

Miscellaneous

7.0.0 (2023-05-03)

⚠ BREAKING CHANGES

  • remove node 12 and node 14 (#545)

Features

Miscellaneous

6.0.3 (2023-02-16)

Bug Fixes

  • improve validation on extension attribute (#502) (ea94a4d)
  • Make CloudEvent data field immutable and enumerable using Object.keys() (#515) (#516) (2d5fab1)
  • This fixes bug #525 where the browser version was breaking becuase of process not being found. (#526) (e5ee836)

Miscellaneous

6.0.3 (2022-11-01)

Bug Fixes

  • improve validation on extension attribute (#502) (ea94a4d)
  • Make CloudEvent data field immutable and enumerable using Object.keys() (#515) (#516) (2d5fab1)

Miscellaneous

6.0.2 (2022-06-21)

Bug Fixes

  • allow TypedArray for binary data (#494) (921e273)
  • HTTP headers for extensions with false values (#493) (d6f52ca)
  • package.json & package-lock.json to reduce vulnerabilities (ed63f14)

Miscellaneous

6.0.1 (2022-03-21)

Miscellaneous

6.0.0 (2022-03-21)

⚠ BREAKING CHANGES

  • add http transport and remove axios (#481)

Features

Miscellaneous

Documentation

  • update readme to include http builtin transport (#483) (4ab6356)

5.3.2 (2022-02-11)

Bug Fixes

  • use isolatedModules: true in tsconfig.json (#469) (b5c0b56)

Miscellaneous

5.3.1 (2022-02-02)

Bug Fixes

  • improve binary data detection in HTTP transport (#468) (cd4dea9)
  • package.json & package-lock.json to reduce vulnerabilities (#462) (ae8fa79)

Documentation

Miscellaneous

  • update readme with current Node LTS versions and add Node 16 to the testing matrix(#465) (8abbc11)

5.3.0 (2022-01-14)

Features

Miscellaneous

  • refactor: prefer interfaces over concrete classes (#457) (2ac731e)
  • update cucumber dependency and remove prettier (#453) (320354f)

5.2.0 (2021-12-07)

Features

5.1.0 (2021-12-01)

Features

  • use generic type for CloudEvent data (#446) (d941e2d)

Bug Fixes

  • do not assume an empty content-type header is JSON (#444) (52ea7de)
  • package.json & package-lock.json to reduce vulnerabilities (#439) (0f5a4c0)

Miscellaneous

5.0.0 (2021-10-04)

⚠ BREAKING CHANGES

  • remove support for 0.3 events (#425)

Features

  • add native logging with headers and body to CloudEvent (#437) (b38a48f)

Bug Fixes

  • update express example with framework features. (#429) (272bcea), closes #379
  • ensure source property has min length of 1 (#438) (2ff7852)
  • package.json & package-lock.json to reduce vulnerabilities (#433) (cf47248)
  • package.json & package-lock.json to reduce vulnerabilities (#434) (8814919)
  • package.json & package-lock.json to reduce vulnerabilities (#436) (2dc846c)

Miscellaneous

4.0.3 (2021-07-06)

Bug Fixes

  • do not modify incoming event's specversion (#419) (22e42dd)
  • do not modify incoming event's specversion (#419) (7c05ade)
  • throw on validation if extensions are improperly named (#420) (7f6b658)

Miscellaneous

  • add copyrights header and lint rules (#418) (80d987c)
  • add Lance Ball to maintainers in package.json (#411) (d68b85a)
  • be more forgiving parsing JSON as a string (#417) (db4be6b)

4.0.2 (2021-04-21)

Bug Fixes

  • defaults properly handled for emitterFor() (#399) (0f11b02)
  • ensure loose validation for isEvent and toEvent (#394) (efe466a)
  • examples/typescript-ex/package.json to reduce vulnerabilities (#395) (d359355)

Documentation

Miscellaneous

4.0.1 (2021-03-08)

Bug Fixes

  • cloudevents from 3.2.0 to 4.0.0 (#376) (6be3b27)
  • Emitter should not extend EventEmitter (1af3d43)
  • examples/websocket/package.json to reduce vulnerabilities (#375) (2b1e1ec)
  • package.json & package-lock.json to reduce vulnerabilities (#384) (39812f7)

Miscellaneous

  • docs: Fix minor import problems in README (#374) (ed81483)

Tests

  • add a test for extension names with all caps. (#389) (e7d99eb)

4.0.0 (2020-12-10)

⚠ BREAKING CHANGES

  • Remove All API's that are labeled "Remove in 4.0" (#362)
  • event: make the event's time property only a string (#330)

Features

  • add a constructor parameter for loose validation (#328) (1fa3a05)

  • add emitterFactory and friends (#342) (e334b6e)

  • add EventEmitter to Emitter and singleton paradigm (25f9c48)

  • allow ensureDelivery to be able to ensure delivery on emit (43d9e01)

  • introduce Message, Serializer, Deserializer and Binding interfaces (#324) (f3953a9)

  • Remove All API's that are labeled "Remove in 4.0" (#362) (875f700)

Bug Fixes

  • do not alter an event's data attribute (#344) (1446898)

  • extend Node.js IncomingHttpHeaders in our Headers type (#346) (f6be285)

  • improve error messages when validating extensions (9f86cfd)

  • package.json & package-lock.json to reduce vulnerabilities (132f052)

  • package.json & package-lock.json to reduce vulnerabilities (#338) (eca43d9)

  • upgrade cloudevents from 3.0.1 to 3.1.0 (#335) (7423acb)

  • upgrade uuid from 8.2.0 to 8.3.0 (#317) (6e2390e)

Tests

  • implement pending tests leftover from TS rewrite (#315) (b5cf886)

Documentation

Miscellaneous

  • add a transition guide. fixes #360 (#363) (79296a8)

  • package: Upgrade mocha from 7.1.2 to 8.2.0 (#354) (8205bc9)

  • add an automated GH action for releases (#329) (a9114b7)

  • tag v3.2.0 as release-v3.2.0 for release-please (#353) (765b81c)

  • ci,releases: bump release-please-action to 2.5.5 (#350) (c4afacb)

  • Remove commented version import. (#319) (0adcc35)

  • typo (#313) (81623ac)

  • update release please to the latest release(2.4.1) (#345) (76688c4)

  • event: make the event's time property only a string (#330) (6cd310c)

  • example: Replaced body parser with express JSON parser (#334) (4779d89)

  • add cucumber.js to list of files to lint and /docs to .gitignore (#327) (17d4bc8)

  • Update README with correct links for the support specification versions (#321) (73f0bec), closes #320

  • Update references of master to main (#316) (4bf2eb8)

  • validate cloudevent version agnostic (#311) (8ac3eb0)

3.1.0 (2020-08-11)

Bug Fixes

  • Add Correct Headers to emitted Binary Event (#302) (ad0c434), closes #301
  • ensure that data encoded as base64 is parsed as an object (#285) (ed9ea95)
  • update browser name to cloudevents. (#292) (48d182b), closes #286

Miscellaneous

  • fix promise tests to break the build when they fail (#305) (a5249de), closes #303
  • no import star (#297) (31c2005)
  • Update examples to use latest sdk changes (#282) (763838c)
  • Update readme with correct Receiver usage (#287) (e219a30)
  • update the release script to signoff the commit (#307) (f3cc2b4)

Documentation

3.0.1 (2020-07-29)

Bug Fixes

  • ensure that event data can be an array, number, boolean or null (#281) (b99f728)

Miscellaneous

3.0.0 (2020-07-27)

⚠ BREAKING CHANGES

  • This validates the value of the cloud event extension based on the spec, https://github.com/cloudevents/spec/blob/master/spec.md#type-system

  • This changes the modules name from cloudevents-sdk to cloudevents

  • feat: use npm name cloudevents

  • src: * Extension names are now validated during object creation. The values are defined by the specification, and can be lowercase(a-z) or digits(0-9) and must be no longer that 20 characters

  • src: * This change makes the CloudEvent Read-only and validates the input during object creation.

  • To augment an already created CloudEvent object, we have added a cloneWith method that takes attributes to add/update.

Features

Bug Fixes

  • do not require an HTTP body on incoming binary event messages (a7c326b)
  • ensure that the HTTP receiver sanitizes headers in accept() (#239) (51035dc)
  • package.json & package-lock.json to reduce vulnerabilities (#253) (2ed5f84)
  • parse method mutating its input (#231) (060b21b)
  • upgrade uuid from 8.0.0 to 8.1.0 (#220) (25077a9)
  • upgrade uuid from 8.1.0 to 8.2.0 (#250) (13bcdb4)

Tests

  • inplement the cucumber conformance tests from cloudevents/spec (#238) (dca2811)

Documentation

  • clean up spec compliance table on README.md (#252) (c496931)
  • README: fix wrong order of arguments in the accept example (#224) (850e893), closes #222
  • README: Update readme to mention that CloudEvents are read-only now (#248) (de6f0a2)
  • generate api documentation as a GitHub workflow (#217) (44b791b)
  • Update references of specific versions to use Latest Supported. (#211) (ed1d328), closes #160

lib

Miscellaneous

  • add vscode task JSON and GitHub issue/pr templates (#268) (1613595)
  • adds the return type for the extensions (#221) (5ab8164)
  • bump GH stale action to v3 (#243) (90a9984)
  • combine v03 and v1 event interfaces, specs and schemas into single files(#270) (129ec48)
  • consolidate HTTP parsers and header maps into single files (#267) (45850e3)
  • simplify ce version parsers (#274) (3d82fb6)
  • simplify parser logic and duplicated code (#269) (a6124cc)
  • actions: don't auto-close stale issues and pull requests (#235) (d65b013)
  • Update examples to use the latest sdk version(2.0.2) (#206) (dcb3c4e)
  • webpack should publish to bundles not _bundles (#227) (7012433)

2.0.2 (2020-06-08)

Bug Fixes

  • add correct types to improve TypeScript behavior (#202) (da365e0)
  • fix references to constants - remove .js extension (#200) (c757a2b)
  • use /lib in gitignore so src/lib is not ignored (#199) (fba3294)

Documentation

Miscellaneous

2.0.1 (2020-06-01)

Bug Fixes

  • initialize CloudEvent's extensions property (#192) (0710166)
  • introduce CloudEventV1 and CloudEventV03 interfaces (#194) (a5befbe)

Miscellaneous

  • CI workflow to only upload report if CODACY_PROJECT_TOKEN is set (#193) (aa320e7)
  • minor typos in guidance docs (#196) (15cd763)

2.0.0 (2020-05-27)

⚠ BREAKING CHANGES

  • change CloudEvent to use direct object notation and get/set properties (#172)
  • refactor HTTP bindings and specifications (#165)
  • expose a version agnostic event emitter (#141)
  • unmarshaller: remove asynchronous 0.3 unmarshaller API (#126)

Features

  • add ValidationError type extending TypeError (#151) (09b0c76)
  • expose a mode and version agnostic event receiver (#120) (54f242b)
  • expose a version agnostic event emitter (#141) (250a0a1)
  • unmarshaller: remove asynchronous 0.3 unmarshaller API (#126) (63ae1ad)
  • formatter.js es6 (#87) (c36f194)
  • use CloudEvents not cloudevents everywhere (#101) (05ecbde)

Bug Fixes

  • ensure binary events can handle no content-type header (#134) (72a87df)
  • Fix Express example installation (#77) (bb8e0f9)
  • make application/json the default content type in binary mode (#118) (d9e9ae6)
  • misspelled word (#113) (cd6a3ee)
  • misspelled word (#115) (53524ac)
  • protects the consts from being changed in other parts of the code. (fbcbcec)
  • remove d.ts types. Fixes #83 (#84) (6c223e2)
  • support mTLS in 1.0 Binary and Structured emitters (3a063d7)
  • throw "no cloud event detected" if one can't be read (#139) (ef7550d)

Tests

  • remove uuid require in spec_03_tests.js (#145) (c56c203)
  • use constants in spec_03_tests.js (#144) (2882aff)
  • use header constants in receiver tests (#131) (60bf05c)
  • use header constants in unmarshaller tests (#60) (e087805)

lib

  • change CloudEvent to use direct object notation and get/set properties (#172) (abc114b)
  • refactor HTTP bindings and specifications (#165) (6f0b5ea)

Documentation

  • add instructions and details to contributors guide (#105) (fd99cb1)
  • add JSDocs for top level API objects (#140) (b283583)
  • add maintainer guidelines for landing PRs (#177) (fdc79ae)
  • organize README badges and remove TS example (#112) (07323e0)
  • remove 0.1, 0.2 spec support from README (56036b0)
  • remove repo structure docs (#111) (223a7c6)
  • update README and examples with new API (#138) (b866edd)

Miscellaneous

1.0.0

Added

Removed

  • Unmarshaller docs from README, moving them to OLDOCS.md

0.3.2

Fixed

  • Fix the special data handling: issue #33

0.3.1

Fixed

  • Axios version to 0.18.1 due the CVE-2019-10742
  • Fix the subject attribute unmarshal error: issue #32