From 3f13000e35d46f314a0c7102ae6761a7039cdb49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Fri, 21 Jun 2019 10:56:30 -0300 Subject: [PATCH] Add the responsability of parse the data attribute MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/receiver_binary_0_2.js | 32 ++++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/bindings/http/receiver_binary_0_2.js b/lib/bindings/http/receiver_binary_0_2.js index b5ab212..9634c8e 100644 --- a/lib/bindings/http/receiver_binary_0_2.js +++ b/lib/bindings/http/receiver_binary_0_2.js @@ -3,6 +3,11 @@ const Commons = require("./commons.js"); const Cloudevent = require("../../cloudevent.js"); const Spec02 = require("../../specs/spec_0_2.js"); +const JSONParser = require("../../formats/json/parser.js"); + +const parser_by_type = {}; +parser_by_type[Constants.MIME_JSON] = new JSONParser(); + const allowed_content_types = []; allowed_content_types.push(Constants.MIME_JSON); @@ -47,8 +52,11 @@ function validate_args(payload, attributes) { throw {message: "attributes is null or undefined"}; } - if((typeof payload) !== "object"){ - throw {message: "payload must be an object", erros: [typeof payload]}; + if((typeof payload) !== "object" && (typeof payload) !== "string"){ + throw { + message: "payload must be an object or a string", + errors: [typeof payload] + }; } } @@ -66,8 +74,10 @@ Receiver.prototype.check = function(payload, headers) { // Validation Level 1 if(!allowed_content_types .includes(sanity_headers[Constants.HEADER_CONTENT_TYPE])){ - throw {message: "invalid content type", - errors: [sanity_headers[Constants.HEADER_CONTENT_TYPE]]}; + throw { + message: "invalid content type", + errors: [sanity_headers[Constants.HEADER_CONTENT_TYPE]] + }; } for(i in required_headers){ @@ -77,9 +87,10 @@ Receiver.prototype.check = function(payload, headers) { } if(sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION] !== "0.2"){ - throw {message: "invalid spec version", - errors: - [sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]}; + throw { + message: "invalid spec version", + errors: [sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]] + }; } // No erros! Its contains the minimum required attributes @@ -103,8 +114,13 @@ Receiver.prototype.parse = function(payload, headers) { } } + // Parses the payload + var parsedPayload = + parser_by_type[sanity_headers[Constants.HEADER_CONTENT_TYPE]] + .parse(payload); + // Sets the data - cloudevent.data(payload); + cloudevent.data(parsedPayload); // Checks the event spec cloudevent.format();