在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:cloudevents/sdk-javascript开源软件地址:https://github.com/cloudevents/sdk-javascript开源编程语言:TypeScript 99.4%开源软件介绍:JavaScript SDK for CloudEventsThe CloudEvents SDK for JavaScript. Features
Note: Supports CloudEvent version 1.0 InstallationThe CloudEvents SDK requires a current LTS version of Node.js. At the moment those are Node.js 12.x, Node.js 14.x and Node.js 16.x. To install in your Node.js project: npm install cloudevents Receiving and Emitting EventsReceiving EventsYou can choose any popular web framework for port binding. A 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 EventsThe 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 reciever
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 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 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 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 ObjectsAll created 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 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",
}); Example ApplicationsThere are a few trivial example applications in the examples folder. There you will find Express.js, TypeScript and Websocket examples. API Transition GuideSupported specification features
Community
MaintainersCurrently active maintainers who may be found in the CNCF Slack.
ContributingWe 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 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论