Skip to content

Commit 3f13000

Browse files
committed
Add the responsability of parse the data attribute
Signed-off-by: Fabio José <[email protected]>
1 parent fb92f71 commit 3f13000

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

lib/bindings/http/receiver_binary_0_2.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ const Commons = require("./commons.js");
33
const Cloudevent = require("../../cloudevent.js");
44
const Spec02 = require("../../specs/spec_0_2.js");
55

6+
const JSONParser = require("../../formats/json/parser.js");
7+
8+
const parser_by_type = {};
9+
parser_by_type[Constants.MIME_JSON] = new JSONParser();
10+
611
const allowed_content_types = [];
712
allowed_content_types.push(Constants.MIME_JSON);
813

@@ -47,8 +52,11 @@ function validate_args(payload, attributes) {
4752
throw {message: "attributes is null or undefined"};
4853
}
4954

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+
};
5260
}
5361
}
5462

@@ -66,8 +74,10 @@ Receiver.prototype.check = function(payload, headers) {
6674
// Validation Level 1
6775
if(!allowed_content_types
6876
.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+
};
7181
}
7282

7383
for(i in required_headers){
@@ -77,9 +87,10 @@ Receiver.prototype.check = function(payload, headers) {
7787
}
7888

7989
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+
};
8394
}
8495

8596
// No erros! Its contains the minimum required attributes
@@ -103,8 +114,13 @@ Receiver.prototype.parse = function(payload, headers) {
103114
}
104115
}
105116

117+
// Parses the payload
118+
var parsedPayload =
119+
parser_by_type[sanity_headers[Constants.HEADER_CONTENT_TYPE]]
120+
.parse(payload);
121+
106122
// Sets the data
107-
cloudevent.data(payload);
123+
cloudevent.data(parsedPayload);
108124

109125
// Checks the event spec
110126
cloudevent.format();

0 commit comments

Comments
 (0)