Class: CloudEvent

CloudEvent(idnon-null, typenon-null, sourcenon-null, datanullable, optionsopt, extensionsnullable)

CloudEvent implementation.

Constructor

new CloudEvent(idnon-null, typenon-null, sourcenon-null, datanullable, optionsopt, extensionsnullable)

Create a new instance of a CloudEvent object.

Create a new instance of a CloudEvent object.
Parameters:
Name Type Attributes Default Description
id string the ID of the event (unique), mandatory
type string the type of the event (usually prefixed with a reverse-DNS name), mandatory
source uri the source uri of the event (use '/' if empty), mandatory
data object | Map | Set | string <nullable>
the real event data
options object <optional>
{} optional attributes of the event; some has default values chosen here: - time (timestamp in string ISO representation, from a date, default now), - datainbase64 (string) base64 encoded value for the data (data attribute must not be present when this is defined), - datacontenttype (string, default 'application/json') is the content type of the data attribute, - dataschema (uri) optional, reference to the schema that data adheres to, - subject (string) optional, describes the subject of the event in the context of the event producer (identified by source), - strict (boolean, default false) tell if object instance will be validated in a more strict way
extensions object <nullable>
optional, contains extension properties (each extension as a key/value property, and no nested objects) but if given any object must contain at least 1 property
Source:
See:
Throws:
  • if strict is true and id or type is undefined or null
    Type
    Error
  • if data and data_base64 are defined
    Type
    Error

Members

(static) standardProps :Array

Getter method to return the list of standard property names, as an array of strings.

Getter method to return the list of standard property names, as an array of strings.
Type:
  • Array
Source:

dataType :string

Getter method to tell if CloudEvent data is text or binary, or unknown if not clear.

Getter method to tell if CloudEvent data is text or binary, or unknown if not clear.
Type:
  • string
Source:

extensions :object

Getter method to return a copy of CloudEvent extensions.

Getter method to return a copy of CloudEvent extensions. See CloudEvent.getExtensionsOfEvent.
Type:
  • object
Source:

isDatacontenttypeJSON :boolean

Getter method to tell if data content type is a JSON-derived format, so data must be encoded/decoded accordingly.

Getter method to tell if data content type is a JSON-derived format, so data must be encoded/decoded accordingly. See CloudEvent.isDatacontenttypeJSONEvent.
Type:
  • boolean
Source:

isStrict :boolean

Getter method to tell if the object has the strict flag enabled.

Getter method to tell if the object has the strict flag enabled. See CloudEvent.isStrictEvent.
Type:
  • boolean
Source:

payload :object|Map|Set|Array|string|boolean|number

Getter method to return a copy of CloudEvent data attribute (or data_base64 if defined), but transformed/decoded if possible.

Getter method to return a copy of CloudEvent data attribute (or data_base64 if defined), but transformed/decoded if possible. See CloudEvent.data, CloudEvent.data_base64.
Type:
  • object | Map | Set | Array | string | boolean | number
Source:

schema :object

Getter method to return JSON Schema for a CloudEvent.

Getter method to return JSON Schema for a CloudEvent. See CloudEvent.getJSONSchema.
Type:
  • object
Source:

timeAsDate :Date

Getter method to return the CloudEvent time but as a Date object.

Getter method to return the CloudEvent time but as a Date object. See CloudEvent.time.
Type:
  • Date
Source:

Methods

(static) datacontenttypeDefault() → {string}

Return the default data content Type for a CloudEvent.

Return the default data content Type for a CloudEvent
Source:
Returns:
the value
Type
string

(static) deserializeEvent(sernon-null, optionsopt) → {object}

Deserialize/parse the given CloudEvent from JSON format.

Deserialize/parse the given CloudEvent from JSON format. Note that here standard parse from JSON is used (no additional libraries). Note that the result of decoder function is assigned to decoded data.
Parameters:
Name Type Attributes Default Description
ser string the serialized CloudEvent to parse/deserialize
options object <optional>
{} optional deserialization attributes: - decoder (function, no default) a function that takes data and returns decoder data as a string, - decodedData (string, no default) already decoded data (but consistency with the datacontenttype is not checked), - onlyValid (boolean, default false) to deserialize only if it's a valid instance, - onlyIfLessThan64KB (boolean, default false) to return the deserialized string only if it's less than 64 KB, - printDebugInfo (boolean, default false) to print some debug info to the console, - timezoneOffset (number, default 0) to apply a different timezone offset
Source:
Throws:
  • if ser is undefined or null, or an option is undefined/null/wrong
    Type
    Error
  • in case of JSON parsing error
    Type
    Error
Returns:
the deserialized event as a CloudEvent instance
Type
object

(static) dumpValidationResults(ce, optionsopt, nameopt) → {string}

Utility function that return a dump of validation results on the given CloudEvent.

Utility function that return a dump of validation results on the given CloudEvent.
Parameters:
Name Type Attributes Default Description
ce object the CloudEvent object to dump
options object <optional>
{} optional validation options
name string <optional>
'noname' the name to assign in the returned string, or 'noname' as default value
Source:
Returns:
the dump of the object or a message when obj is undefined/null/not a CloudEvent
Type
string

(static) ensureTypeOfDataIsRight(cenon-null, optionsopt, nameopt) → {string|null}

Tell the type of data of the CloudEvent, if it's right (depending even on related datacontenttype), from the validator point of view.

Tell the type of data of the CloudEvent, if it's right (depending even on related datacontenttype), from the validator point of view.
Parameters:
Name Type Attributes Default Description
ce object the CloudEvent to validate
options object <optional>
{} optional validation options
name string <optional>
'data' the name to assign in the returned error string (if any), or 'data' as default value
Source:
Throws:
  • if event is not a CloudEvent instance or subclass
    Type
    TypeError
  • if event is undefined or null
    Type
    Error
Returns:
error message if the given data type is not right, otherwise null
Type
string | null

(static) getJSONSchema() → {object}

Get the JSON Schema for a CloudEvent.

Get the JSON Schema for a CloudEvent. Note that it's not used in standard serialization to JSON, but only in some serialization libraries. Note that schema definitions for data and extensions are right, but I need to keep them commented here and to set the flag additionalProperties to true, or when used both data and extensions will be empty in JSON output. See JSON Schema.
Source:
Returns:
the JSON Schema
Type
object

(static) isCloudEvent(eventnon-null) → {boolean}

Tell the given CloudEvent, if it's instance of the CloudEvent class or a subclass of it.

Tell the given CloudEvent, if it's instance of the CloudEvent class or a subclass of it.
Parameters:
Name Type Description
event object the CloudEvent to check
Source:
Throws:
if event is undefined or null
Type
Error
Returns:
true if it's an instance (or a subclass), otherwise false
Type
boolean

(static) isDatacontenttypeJSONEvent(eventnon-null) → {boolean}

Tell the data content Type for a CloudEvent, if is a JSON-derived format, so data must be encoded/decoded accordingly.

Tell the data content Type for a CloudEvent, if is a JSON-derived format, so data must be encoded/decoded accordingly.
Parameters:
Name Type Description
event object the CloudEvent to validate
Source:
Throws:
  • if event is not a CloudEvent instance or subclass
    Type
    TypeError
  • if event is undefined or null
    Type
    Error
Returns:
true if data content type is JSON-like, otherwise false
Type
boolean

(static) isExtensionProperty(propertynon-null) → {boolean}

Tell the given property, if it's an extension CloudEvent property/attribute.

Tell the given property, if it's an extension CloudEvent property/attribute.
Parameters:
Name Type Description
property string the property/attribute to check
Source:
Returns:
true if it's an extension (not standard) otherwise false
Type
boolean

(static) isStandardProperty(propertynon-null) → {boolean}

Tell the given property, if it's a standard CloudEvent property/attribute.

Tell the given property, if it's a standard CloudEvent property/attribute.
Parameters:
Name Type Description
property string the property/attribute to check
Source:
Returns:
true if it's standard otherwise false
Type
boolean

(static) isStrictEvent(eventnon-null) → {boolean}

Tell if the object has the strict flag enabled.

Tell if the object has the strict flag enabled.
Parameters:
Name Type Description
event object the CloudEvent to validate
Source:
Throws:
  • if event is not a CloudEvent instance or subclass
    Type
    TypeError
  • if event is undefined or null
    Type
    Error
Returns:
true if strict, otherwise false
Type
boolean

(static) isValidEvent(eventnon-null, optionsopt) → {boolean}

Tell the given CloudEvent, if it's valid.

Tell the given CloudEvent, if it's valid. See CloudEvent.validateEvent.
Parameters:
Name Type Attributes Default Description
event object the CloudEvent to validate
options object <optional>
{} containing: - strict (boolean, default null so no override) to validate it in a more strict way (if null it will be used strict mode in the given event), - dataschemavalidator (function(data, dataschema) boolean, optional) a function to validate data of current CloudEvent instance with its dataschema - printDebugInfo (boolean, default false) to print some debug info to the console, - timezoneOffset (number, default 0) to apply a different timezone offset
Source:
Returns:
true if valid, otherwise false
Type
boolean

(static) mediaType() → {string}

Return the MIME Type for a CloudEvent.

Return the MIME Type for a CloudEvent
Source:
Returns:
the value
Type
string

(static) serializeEvent(eventnon-null, optionsopt) → {string}

Serialize the given CloudEvent in JSON format.

Serialize the given CloudEvent in JSON format. Note that here standard serialization to JSON is used (no additional libraries). Note that the result of encoder function is assigned to encoded data.
Parameters:
Name Type Attributes Default Description
event object the CloudEvent to serialize
options object <optional>
{} optional serialization attributes: - encoder (function, no default) a function that takes data and returns encoded data as a string, - encodedData (string, no default) already encoded data (but consistency with the datacontenttype is not checked), - onlyValid (boolean, default false) to serialize only if it's a valid instance, - onlyIfLessThan64KB (boolean, default false) to return the serialized string only if it's less than 64 KB, - printDebugInfo (boolean, default false) to print some debug info to the console, - timezoneOffset (number, default 0) to apply a different timezone offset
Source:
Throws:
if event is undefined or null, or an option is undefined/null/wrong
Type
Error
Returns:
the serialized event, as a string
Type
string

(static) validateEvent(eventnon-null, optionsopt) → {Array.<object>}

Validate the given CloudEvent.

Validate the given CloudEvent.
Parameters:
Name Type Attributes Default Description
event object the CloudEvent to validate
options object <optional>
{} containing: - strict (boolean, default null so no override) to validate it in a more strict way (if null it will be used strict mode in the given event), - dataschemavalidator (function(data, dataschema) boolean, optional) a function to validate data of current CloudEvent instance with its dataschema - timezoneOffset (number, default 0) to apply a different timezone offset
Source:
Returns:
an array of (non null) validation errors, or at least an empty array
Type
Array.<object>

(static) version() → {string}

Return the version of the CloudEvent Specification implemented here.

Return the version of the CloudEvent Specification implemented here
Source:
Returns:
the value
Type
string

isValid(optionsopt) → {boolean}

Tell the current CloudEvent, if it's valid.

Tell the current CloudEvent, if it's valid. See CloudEvent.isValidEvent.
Parameters:
Name Type Attributes Default Description
options object <optional>
{} containing: - strict (boolean, default null so no override) to validate it in a more strict way (if null it will be used strict mode in the given event), - dataschemavalidator (function(data, dataschema) boolean, optional) a function to validate data of current CloudEvent instance with its dataschema - printDebugInfo (boolean, default false) to print some debug info to the console, - timezoneOffset (number, default 0) to apply a different timezone offset
Source:
Returns:
true if valid, otherwise false
Type
boolean

serialize(optionsopt) → {string}

Serialize the current CloudEvent.

Serialize the current CloudEvent. See CloudEvent.serializeEvent.
Parameters:
Name Type Attributes Default Description
options object <optional>
{} optional serialization attributes: - encoder (function, default null) a function that takes data and returns encoded data, - encodedData (string, default null) already encoded data (but consistency with the datacontenttype is not checked),
Source:
Returns:
the serialized event, as a string
Type
string

toString() → {string}

Override the usual toString method, to show a summary (only some info) on current instance.

Override the usual toString method, to show a summary (only some info) on current instance. Note that the representation of the 'data' attribute is limited to 1024 chars (arbitrary limit, set here including the trim marker), to avoid too much overhead with instances with a big 'data' attribute. See Object.toString.
Source:
Returns:
a string representation for object instance
Type
string

validate(optionsopt) → {Array.<object>}

Validate the current CloudEvent.

Validate the current CloudEvent. See CloudEvent.validateEvent.
Parameters:
Name Type Attributes Default Description
options object <optional>
{} containing: - strict (boolean, default null so no override) to validate it in a more strict way (if null it will be used strict mode in the given event), - dataschemavalidator (function(data, dataschema) boolean, optional) a function to validate data of current CloudEvent instance with its dataschema
Source:
Returns:
an array of (non null) validation errors, or at least an empty array
Type
Array.<object>