@@ -3,6 +3,11 @@ const Commons = require("./commons.js");
3
3
const Cloudevent = require ( "../../cloudevent.js" ) ;
4
4
const Spec02 = require ( "../../specs/spec_0_2.js" ) ;
5
5
6
+ const JSONParser = require ( "../../formats/json/parser.js" ) ;
7
+
8
+ const parser_by_type = { } ;
9
+ parser_by_type [ Constants . MIME_JSON ] = new JSONParser ( ) ;
10
+
6
11
const allowed_content_types = [ ] ;
7
12
allowed_content_types . push ( Constants . MIME_JSON ) ;
8
13
@@ -47,8 +52,11 @@ function validate_args(payload, attributes) {
47
52
throw { message : "attributes is null or undefined" } ;
48
53
}
49
54
50
- if ( ( typeof payload ) !== "object" ) {
51
- throw { message : "payload must be an object" , erros : [ typeof payload ] } ;
55
+ if ( ( typeof payload ) !== "object" && ( typeof payload ) !== "string" ) {
56
+ throw {
57
+ message : "payload must be an object or a string" ,
58
+ errors : [ typeof payload ]
59
+ } ;
52
60
}
53
61
}
54
62
@@ -66,8 +74,10 @@ Receiver.prototype.check = function(payload, headers) {
66
74
// Validation Level 1
67
75
if ( ! allowed_content_types
68
76
. includes ( sanity_headers [ Constants . HEADER_CONTENT_TYPE ] ) ) {
69
- throw { message : "invalid content type" ,
70
- errors : [ sanity_headers [ Constants . HEADER_CONTENT_TYPE ] ] } ;
77
+ throw {
78
+ message : "invalid content type" ,
79
+ errors : [ sanity_headers [ Constants . HEADER_CONTENT_TYPE ] ]
80
+ } ;
71
81
}
72
82
73
83
for ( i in required_headers ) {
@@ -77,9 +87,10 @@ Receiver.prototype.check = function(payload, headers) {
77
87
}
78
88
79
89
if ( sanity_headers [ Constants . BINARY_HEADERS_02 . SPEC_VERSION ] !== "0.2" ) {
80
- throw { message : "invalid spec version" ,
81
- errors :
82
- [ sanity_headers [ Constants . BINARY_HEADERS_02 . SPEC_VERSION ] ] } ;
90
+ throw {
91
+ message : "invalid spec version" ,
92
+ errors : [ sanity_headers [ Constants . BINARY_HEADERS_02 . SPEC_VERSION ] ]
93
+ } ;
83
94
}
84
95
85
96
// No erros! Its contains the minimum required attributes
@@ -103,8 +114,13 @@ Receiver.prototype.parse = function(payload, headers) {
103
114
}
104
115
}
105
116
117
+ // Parses the payload
118
+ var parsedPayload =
119
+ parser_by_type [ sanity_headers [ Constants . HEADER_CONTENT_TYPE ] ]
120
+ . parse ( payload ) ;
121
+
106
122
// Sets the data
107
- cloudevent . data ( payload ) ;
123
+ cloudevent . data ( parsedPayload ) ;
108
124
109
125
// Checks the event spec
110
126
cloudevent . format ( ) ;
0 commit comments