@@ -2,7 +2,9 @@ import { v4 as uuidv4 } from "uuid";
22
33import { CloudEventV1 , validateV1 , CloudEventV1Attributes } from "./v1" ;
44import { CloudEventV03 , validateV03 , CloudEventV03Attributes } from "./v03" ;
5- import { ValidationError } from "./validation" ;
5+ import { ValidationError , isBinary , asBase64 } from "./validation" ;
6+ import CONSTANTS from "../constants" ;
7+ import { isString } from "util" ;
68
79export const enum Version {
810 V1 = "1.0" ,
@@ -23,7 +25,7 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
2325 dataschema ?: string ;
2426 subject ?: string ;
2527 #_time?: string | Date ;
26- data ?: Record < string , unknown | string | number | boolean > | string | number | boolean | null | unknown ;
28+ #_data ?: Record < string , unknown | string | number | boolean > | string | number | boolean | null | unknown ;
2729 data_base64 ?: string ;
2830
2931 // Extensions should not exist as it's own object, but instead
@@ -60,21 +62,21 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
6062 this . #_time = properties . time ;
6163 delete properties . time ;
6264
63- this . data = properties . data ;
64- delete properties . data ;
65+ this . datacontentencoding = properties . datacontentencoding as string ;
66+ delete properties . datacontentencoding ;
6567
6668 this . dataschema = properties . dataschema as string ;
6769 delete properties . dataschema ;
6870
6971 this . data_base64 = properties . data_base64 as string ;
7072 delete properties . data_base64 ;
7173
72- this . datacontentencoding = properties . datacontentencoding as string ;
73- delete properties . datacontentencoding ;
74-
7574 this . schemaurl = properties . schemaurl as string ;
7675 delete properties . schemaurl ;
7776
77+ this . _setData ( properties . data ) ;
78+ delete properties . data ;
79+
7880 // Make sure time has a default value and whatever is provided is formatted
7981 if ( ! this . #_time) {
8082 this . #_time = new Date ( ) . toISOString ( ) ;
@@ -103,9 +105,35 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
103105 this . #_time = new Date ( val ) . toISOString ( ) ;
104106 }
105107
108+ get data ( ) : unknown {
109+ if (
110+ this . datacontenttype === CONSTANTS . MIME_JSON &&
111+ ! ( this . datacontentencoding === CONSTANTS . ENCODING_BASE64 ) &&
112+ isString ( this . #_data)
113+ ) {
114+ return JSON . parse ( this . #_data as string ) ;
115+ } else if ( isBinary ( this . #_data) ) {
116+ return asBase64 ( this . #_data as Uint32Array ) ;
117+ }
118+ return this . #_data;
119+ }
120+
121+ set data ( value : unknown ) {
122+ this . _setData ( value ) ;
123+ }
124+
125+ private _setData ( value : unknown ) : void {
126+ if ( isBinary ( value ) ) {
127+ this . #_data = value ;
128+ this . data_base64 = asBase64 ( value as Uint32Array ) ;
129+ }
130+ this . #_data = value ;
131+ }
132+
106133 toJSON ( ) : Record < string , unknown > {
107134 const event = { ...this } ;
108135 event . time = this . time ;
136+ event . data = this . data ;
109137 return event ;
110138 }
111139
@@ -119,12 +147,19 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
119147 * @return {boolean } true if this event is valid
120148 */
121149 validate ( ) : boolean {
122- if ( this . specversion === Version . V1 ) {
123- return validateV1 ( this ) ;
124- } else if ( this . specversion === Version . V03 ) {
125- return validateV03 ( this ) ;
126- } else {
150+ try {
151+ if ( this . specversion === Version . V1 ) {
152+ return validateV1 ( this ) ;
153+ } else if ( this . specversion === Version . V03 ) {
154+ return validateV03 ( this ) ;
155+ }
127156 throw new ValidationError ( "invalid payload" ) ;
157+ } catch ( e ) {
158+ if ( e instanceof ValidationError ) {
159+ throw e ;
160+ } else {
161+ throw new ValidationError ( "invalid payload" , e ) ;
162+ }
128163 }
129164 }
130165
0 commit comments