Constant usage

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-06-11 11:36:04 -03:00
parent f1e1f942e1
commit 7839b733d6
1 changed files with 9 additions and 10 deletions

View File

@ -1,10 +1,8 @@
var Spec02 = require("../../specs/spec_0_2.js"); var Spec02 = require("../../specs/spec_0_2.js");
var JSONParser = require("../../formats/json/parser.js"); var JSONParser = require("../../formats/json/parser.js");
const json_mime = "application/json"; const Constants = require("./constants.js");
const ce_json_mime = "application/cloudevents+json";
const content_type_header = "content-type";
const ce_structured_content_type = "application/cloudevents"; const ce_structured_content_type = "application/cloudevents";
const structured = "structured"; const structured = "structured";
@ -12,14 +10,14 @@ const binary = "binary";
const jsonParserSpec02 = new JSONParser(new Spec02()); const jsonParserSpec02 = new JSONParser(new Spec02());
const parsers = {}; const parsers = {};
parsers[json_mime] = jsonParserSpec02; parsers[Constants.MIME_JSON] = jsonParserSpec02;
parsers[ce_json_mime] = jsonParserSpec02; parsers[Constants.MIME_CE_JSON] = jsonParserSpec02;
const allowed_binary_content_types = []; const allowed_binary_content_types = [];
allowed_binary_content_types.push(json_mime); allowed_binary_content_types.push(Constants.MIME_JSON);
const allowed_structured_content_types = []; const allowed_structured_content_types = [];
allowed_structured_content_types.push(ce_json_mime); allowed_structured_content_types.push(Constants.MIME_CE_JSON);
function validate_args(payload, headers) { function validate_args(payload, headers) {
if(!payload){ if(!payload){
@ -30,7 +28,7 @@ function validate_args(payload, headers) {
throw {message: "headers is null or undefined"}; throw {message: "headers is null or undefined"};
} }
if(!headers[content_type_header]){ if(!headers[Constants.HEADER_CONTENT_TYPE]){
throw {message: "content-type header not found"}; throw {message: "content-type header not found"};
} }
} }
@ -38,9 +36,10 @@ function validate_args(payload, headers) {
// Is it binary or structured? // Is it binary or structured?
function resolve_binding_name(payload, headers) { function resolve_binding_name(payload, headers) {
var contentType = headers[content_type_header]; var contentType = headers[Constants.HEADER_CONTENT_TYPE];
if(contentType.startsWith(ce_structured_content_type)){ if(contentType.startsWith(ce_structured_content_type)){
// Structured // Structured
console.log(allowed_structured_content_types);
if(allowed_structured_content_types.includes(contentType)){ if(allowed_structured_content_types.includes(contentType)){
return structured; return structured;
} else { } else {
@ -65,7 +64,7 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) {
// Resolve the binding // Resolve the binding
var bindingName = resolve_binding_name(payload, headers); var bindingName = resolve_binding_name(payload, headers);
var contentType = headers[content_type_header]; var contentType = headers[Constants.HEADER_CONTENT_TYPE];
if(bindingName === structured){ if(bindingName === structured){
var parser = parsers[contentType]; var parser = parsers[contentType];