93 lines
2.5 KiB
JavaScript
93 lines
2.5 KiB
JavaScript
const Constants = require("./constants.js");
|
|
const Cloudevent = require("../../cloudevent.js");
|
|
const Spec02 = require("../../specs/spec_0_2.js");
|
|
|
|
const required_headers = [];
|
|
required_headers.push(Constants.BINARY_HEADERS_02.TYPE);
|
|
required_headers.push(Constants.BINARY_HEADERS_02.SPEC_VERSION);
|
|
required_headers.push(Constants.BINARY_HEADERS_02.SOURCE);
|
|
required_headers.push(Constants.BINARY_HEADERS_02.ID);
|
|
|
|
const setter_reflections = {};
|
|
setter_reflections[Constants.BINARY_HEADERS_02.TYPE] = {
|
|
name : "type",
|
|
parser : (v) => v
|
|
};
|
|
setter_reflections[Constants.BINARY_HEADERS_02.SOURCE] = {
|
|
name : "source",
|
|
parser: (v) => v
|
|
};
|
|
setter_reflections[Constants.BINARY_HEADERS_02.ID] = {
|
|
name : "id",
|
|
parser : (v) => v
|
|
};
|
|
setter_reflections[Constants.BINARY_HEADERS_02.TIME] = {
|
|
name : "time",
|
|
parser : (v) => new Date(Date.parse(v))
|
|
};
|
|
setter_reflections[Constants.BINARY_HEADERS_02.SCHEMA_URL] = {
|
|
name: "schemaurl",
|
|
parser: (v) => v
|
|
};
|
|
|
|
function validate_args(payload, attributes) {
|
|
if(!payload){
|
|
throw {message: "payload is null or undefined"};
|
|
}
|
|
|
|
if(!attributes) {
|
|
throw {message: "attributes is null or undefined"};
|
|
}
|
|
|
|
if((typeof payload) !== "object"){
|
|
throw {message: "payload must be an object", erros: [typeof payload]};
|
|
}
|
|
}
|
|
|
|
function Receiver(configuration) {
|
|
|
|
}
|
|
|
|
Receiver.prototype.check = function(payload, headers) {
|
|
// Validation Level 0
|
|
validate_args(payload, headers);
|
|
|
|
// Validation Level 1
|
|
for(i in required_headers){
|
|
if(!headers[required_headers[i]]){
|
|
throw {message: "header '" + required_headers[i] + "' not found"};
|
|
}
|
|
}
|
|
|
|
if(headers[Constants.BINARY_HEADERS_02.SPEC_VERSION] !== "0.2"){
|
|
throw {message: "invalid spec version",
|
|
errors: [headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]};
|
|
}
|
|
|
|
// No erros! Its contains the minimum required attributes
|
|
}
|
|
|
|
Receiver.prototype.parse = function(payload, headers) {
|
|
this.check(payload, headers);
|
|
|
|
var cloudevent = new Cloudevent(Spec02);
|
|
for(header in setter_reflections) {
|
|
// dont worry, check() have seen what was required or not
|
|
if(headers[header]){
|
|
var setter_name = setter_reflections[header].name;
|
|
var parser_fn = setter_reflections[header].parser;
|
|
console.log(typeof parser_fn(headers[header]));
|
|
// invoke the setter function
|
|
cloudevent[setter_name](parser_fn(headers[header]));
|
|
}
|
|
}
|
|
|
|
// Checks the event spec
|
|
cloudevent.format();
|
|
|
|
// return the result
|
|
return cloudevent;
|
|
}
|
|
|
|
module.exports = Receiver;
|