sdk-javascript/docs/cloudevent.js.html

433 lines
14 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
cloudevent.js - Documentation
</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<link type="text/css" rel="stylesheet" href="styles/collapse.css">
</head>
<body>
<svg style="display: none;">
<defs>
<symbol id="linkIcon" fill="#706d77" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/>
</symbol>
</defs>
</svg>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<div class="top-nav-wrapper">
<ul>
<li >
<a href="index.html">
<svg fill="#6D6D6D" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>
<path d="M0 0h24v24H0z" fill="none"/>
</svg>
</a>
</li>
</ul>
</div>
<nav>
<h3 class="reference-title">
cloudevents-sdk
</h3>
<h3>
Resources
</h3>
<a href="concepts.html">Concepts</a>
<h3>Classes</h3><ul><li id="BinaryHTTPEmitter-nav"><a href="BinaryHTTPEmitter.html">BinaryHTTPEmitter</a><ul class='methods'><li data-type="method" id="BinaryHTTPEmitter-emit-nav"><a href="BinaryHTTPEmitter.html#emit">emit</a></li></ul></li><li id="BinaryHTTPReceiver-nav"><a href="BinaryHTTPReceiver.html">BinaryHTTPReceiver</a><ul class='methods'><li data-type="method" id="BinaryHTTPReceiver-check-nav"><a href="BinaryHTTPReceiver.html#check">check</a></li><li data-type="method" id="BinaryHTTPReceiver-parse-nav"><a href="BinaryHTTPReceiver.html#parse">parse</a></li></ul></li><li id="CloudEvent-nav"><a href="CloudEvent.html">CloudEvent</a><ul class='methods'><li data-type="method" id="CloudEvent-addExtension-nav"><a href="CloudEvent.html#addExtension">addExtension</a></li><li data-type="method" id="CloudEvent-format-nav"><a href="CloudEvent.html#format">format</a></li><li data-type="method" id="CloudEvent-getExtensions-nav"><a href="CloudEvent.html#getExtensions">getExtensions</a></li><li data-type="method" id="CloudEvent-toString-nav"><a href="CloudEvent.html#toString">toString</a></li></ul></li><li id="HTTPEmitter-nav"><a href="HTTPEmitter.html">HTTPEmitter</a><ul class='methods'><li data-type="method" id="HTTPEmitter-headers-nav"><a href="HTTPEmitter.html#headers">headers</a></li><li data-type="method" id="HTTPEmitter-send-nav"><a href="HTTPEmitter.html#send">send</a></li></ul></li><li id="HTTPReceiver-nav"><a href="HTTPReceiver.html">HTTPReceiver</a><ul class='methods'><li data-type="method" id="HTTPReceiver-accept-nav"><a href="HTTPReceiver.html#accept">accept</a></li></ul></li><li id="StructuredHTTPEmitter-nav"><a href="StructuredHTTPEmitter.html">StructuredHTTPEmitter</a><ul class='methods'><li data-type="method" id="StructuredHTTPEmitter-emit-nav"><a href="StructuredHTTPEmitter.html#emit">emit</a></li></ul></li><li id="StructuredHTTPReceiver-nav"><a href="StructuredHTTPReceiver.html">StructuredHTTPReceiver</a><ul class='methods'><li data-type="method" id="StructuredHTTPReceiver-check-nav"><a href="StructuredHTTPReceiver.html#check">check</a></li><li data-type="method" id="StructuredHTTPReceiver-parse-nav"><a href="StructuredHTTPReceiver.html#parse">parse</a></li></ul></li><li id="ValidationError-nav"><a href="ValidationError.html">ValidationError</a></li></ul><h3 id="global-nav">Global</h3><ul><li><a href="global.html#headerMap">headerMap</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">
cloudevent.js
</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const Spec1 = require("./bindings/http/v1/spec_1.js");
const Spec03 = require("./bindings/http/v03/spec_0_3.js");
const Formatter = require("./formats/json/formatter.js");
const { SPEC_V1, SPEC_V03 } = require("./bindings/http/constants.js");
const { isBinary } = require("./bindings/http/validation/fun.js");
/**
* An CloudEvent describes event data in common formats to provide
* interopability across services, platforms and systems.
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md
*/
class CloudEvent {
/**
* Creates a new CloudEvent instance
* @param {object} options CloudEvent properties as a simple object
* @param {string} options.source Identifies the context in which an event happened as a URI reference
* @param {string} options.type Describes the type of event related to the originating occurrence
* @param {string} [options.id] A unique ID for this event - if not supplied, will be autogenerated
* @param {string} [options.time] A timestamp for this event. May also be provided as a Date
* @param {string} [options.subject] Describes the subject of the event in the context of the event producer
* @param {string} [options.dataContentType] The mime content type for the event data
* @param {string} [options.dataSchema] The URI of the schema that the event data adheres to (v1.0 events)
* @param {string} [options.schemaURL] The URI of the schema that the event data adheres to (v0.3 events)
* @param {string} [options.dataContentEncoding] The content encoding for the event data (v0.3 events)
* @param {string} [options.specversion] The CloudEvent specification version for this event - default: 1.0
* @param {*} [options.data] The event payload
*/
constructor({
id,
source,
type,
dataContentType,
time,
subject,
dataSchema,
schemaURL,
dataContentEncoding,
data,
specversion = SPEC_V1 } = {
id: undefined,
source: undefined,
type: undefined,
dataContentType: undefined,
time: undefined,
subject: undefined,
dataSchema: undefined,
schemaURL: undefined,
dataContentEncoding: undefined,
data: undefined
}) {
if (!type || !source) {
throw new TypeError("event type and source are required");
}
switch (specversion) {
case SPEC_V1:
this.spec = new Spec1();
break;
case SPEC_V03:
this.spec = new Spec03();
break;
default:
throw new TypeError(`unknown specification version ${specversion}`);
}
this.source = source;
this.type = type;
this.dataContentType = dataContentType;
this.data = data;
this.subject = subject;
if (dataSchema) {
this.dataSchema = dataSchema;
}
// TODO: Deprecated in 1.0
if (dataContentEncoding) {
this.dataContentEncoding = dataContentEncoding;
}
// TODO: Deprecated in 1.0
if (schemaURL) {
this.schemaURL = schemaURL;
}
if (id) {
this.id = id;
}
if (time) {
this.time = time;
}
this.formatter = new Formatter();
}
/**
* Gets or sets the event id. Source + id must be unique for each distinct event.
* @see https://github.com/cloudevents/spec/blob/master/spec.md#id
* @type {string}
*/
get id() {
return this.spec.id;
}
set id(id) {
this.spec.id = id;
}
/**
* Gets or sets the origination source of this event as a URI.
* @type {string}
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
*/
get source() {
return this.spec.source;
}
set source(source) {
this.spec.source = source;
}
/**
* Gets the CloudEvent specification version
* @type {string}
* @see https://github.com/cloudevents/spec/blob/master/spec.md#specversion
*/
get specversion() {
return this.spec.specversion;
}
/**
* Gets or sets the event type
* @type {string}
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
*/
get type() {
return this.spec.type;
}
set type(type) {
this.spec.type = type;
}
/**
* Gets or sets the content type of the data value for this event
* @type {string}
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
*/
get dataContentType() {
return this.spec.dataContentType;
}
set dataContentType(contenttype) {
this.spec.dataContentType = contenttype;
}
/**
* Gets or sets the event's data schema
* @type {string}
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md#dataschema
*/
get dataSchema() {
if (this.spec instanceof Spec1) {
return this.spec.dataSchema;
}
throw new TypeError("cannot get dataSchema from version 0.3 event");
}
set dataSchema(dataschema) {
if (this.spec instanceof Spec1) {
this.spec.dataSchema = dataschema;
} else {
throw new TypeError("cannot set dataSchema on version 0.3 event");
}
}
/**
* Gets or sets the event's data content encoding
* @type {string}
* @see https://github.com/cloudevents/spec/blob/v0.3/spec.md#datacontentencoding
*/
get dataContentEncoding() {
if (this.spec instanceof Spec03) {
return this.spec.dataContentEncoding;
}
throw new TypeError("cannot get dataContentEncoding from version 1.0 event");
}
set dataContentEncoding(dataContentEncoding) {
if (this.spec instanceof Spec03) {
this.spec.dataContentEncoding = dataContentEncoding;
} else {
throw new TypeError("cannot set dataContentEncoding on version 1.0 event");
}
}
/**
* Gets or sets the event subject
* @type {string}
* @see https://github.com/cloudevents/spec/blob/v1.0/spec.md#subject
*/
get subject() {
return this.spec.subject;
}
set subject(subject) {
this.spec.subject = subject;
}
/**
* Gets or sets the timestamp for this event as an ISO formatted date string
* @type {string}
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
*/
get time() {
return this.spec.time;
}
set time(time) {
this.spec.time = new Date(time).toISOString();
}
/**
* DEPRECATED: Gets or sets the schema URL for this event. Throws {TypeError}
* if this is a version 1.0 event.
* @type {string}
* @see https://github.com/cloudevents/spec/blob/v0.3/spec.md#schemaurl
*/
get schemaURL() {
if (this.spec instanceof Spec03) {
return this.spec.schemaURL;
}
throw new TypeError("cannot get schemaURL from version 1.0 event");
}
// TODO: Deprecated in 1.0
set schemaURL(schemaurl) {
if (schemaurl &amp;&amp; (this.spec instanceof Spec03)) {
this.spec.schemaURL = schemaurl;
} else if (schemaurl) {
throw new TypeError("cannot set schemaURL on version 1.0 event");
}
}
/**
* Gets or sets the data for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
* @type {*}
*/
get data() {
return this.spec.data;
}
set data(data) {
this.spec.data = data;
}
/**
* Formats the CloudEvent as JSON. Validates the event according
* to the CloudEvent specification and throws an exception if
* it's invalid.
* @returns {JSON} the CloudEvent in JSON form
* @throws {ValidationError} if this event cannot be validated against the specification
*/
format() {
this.spec.check();
const payload = {
data: undefined,
data_base64: undefined,
...this.spec.payload
};
// Handle when is binary, creating the data_base64
if (isBinary(payload.data)) {
// TODO: The call to this.spec.data formats the binary data
// I think having a side effect like this is an anti-pattern.
// FIXIT
payload.data_base64 = this.spec.data;
delete payload.data;
} else {
delete payload.data_base64;
}
return this.formatter.format(payload);
}
/**
* Formats the CloudEvent as JSON. No specification validation is performed.
* @returns {string} the CloudEvent as a JSON string
*/
toString() {
return this.formatter.toString(this.spec.payload);
}
/**
* Adds an extension attribute to this CloudEvent
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
* @param {string} key the name of the extension attribute
* @param {*} value the value of the extension attribute
* @returns {void}
*/
addExtension(key, value) {
this.spec.addExtension(key, value);
this.extensions = { [key]: value, ...this.extensions };
}
/**
* Gets the extension attributes, if any, associated with this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
* @returns {Object} the extensions attributes - if none exist will will be {}
*/
getExtensions() {
return this.extensions;
}
}
module.exports = CloudEvent;
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.4</a>
</footer>
<script src="scripts/linenumber.js"></script>
<script src="scripts/pagelocation.js"></script>
</body>
</html>