parsersByEnconding and checkDecorator
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
701c7ec77b
commit
3cdd7b9280
|
@ -2,6 +2,7 @@ const Constants = require("./constants.js");
|
||||||
const Spec = require("../../specs/spec_0_3.js");
|
const Spec = require("../../specs/spec_0_3.js");
|
||||||
|
|
||||||
const JSONParser = require("../../formats/json/parser.js");
|
const JSONParser = require("../../formats/json/parser.js");
|
||||||
|
const Base64Parser = require("../../formats/base64.js");
|
||||||
|
|
||||||
const BinaryHTTPReceiver = require("./receiver_binary.js");
|
const BinaryHTTPReceiver = require("./receiver_binary.js");
|
||||||
|
|
||||||
|
@ -16,10 +17,25 @@ parserByType[Constants.MIME_OCTET_STREAM] = {
|
||||||
parse(payload) { return payload; }
|
parse(payload) { return payload; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const parsersByEncoding = {};
|
||||||
|
parsersByEncoding[null] = parserByType;
|
||||||
|
parsersByEncoding[undefined] = parserByType;
|
||||||
|
|
||||||
|
// base64
|
||||||
|
parsersByEncoding[Constants.ENCODING_BASE64] = {};
|
||||||
|
parsersByEncoding[Constants.ENCODING_BASE64][Constants.MIME_JSON] =
|
||||||
|
new JSONParser(new Base64Parser());
|
||||||
|
parsersByEncoding[Constants.ENCODING_BASE64][Constants.MIME_OCTET_STREAM] = {
|
||||||
|
parse(payload) { return payload; }
|
||||||
|
};
|
||||||
|
|
||||||
const allowedContentTypes = [];
|
const allowedContentTypes = [];
|
||||||
allowedContentTypes.push(Constants.MIME_JSON);
|
allowedContentTypes.push(Constants.MIME_JSON);
|
||||||
allowedContentTypes.push(Constants.MIME_OCTET_STREAM);
|
allowedContentTypes.push(Constants.MIME_OCTET_STREAM);
|
||||||
|
|
||||||
|
const allowedEncodings = [];
|
||||||
|
allowedEncodings.push(Constants.ENCODING_BASE64);
|
||||||
|
|
||||||
const requiredHeaders = [];
|
const requiredHeaders = [];
|
||||||
requiredHeaders.push(Constants.BINARY_HEADERS_03.TYPE);
|
requiredHeaders.push(Constants.BINARY_HEADERS_03.TYPE);
|
||||||
requiredHeaders.push(Constants.BINARY_HEADERS_03.SPEC_VERSION);
|
requiredHeaders.push(Constants.BINARY_HEADERS_03.SPEC_VERSION);
|
||||||
|
@ -64,15 +80,29 @@ setterByHeader[Constants.BINARY_HEADERS_03.SUBJECT] = {
|
||||||
parser: (v) => v
|
parser: (v) => v
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function checkDecorator(payload, headers) {
|
||||||
|
Object.keys(headers)
|
||||||
|
.map((header) => header.toLocaleLowerCase("en-US"))
|
||||||
|
.filter((header) =>
|
||||||
|
header === Constants.BINARY_HEADERS_03.CONTENT_ENCONDING)
|
||||||
|
.filter((header) => !allowedEncodings.includes(headers[header]))
|
||||||
|
.forEach((header) => {
|
||||||
|
throw {message : "unsupported datacontentencoding", errors: [
|
||||||
|
headers[header]
|
||||||
|
]};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function Receiver(configuration) {
|
function Receiver(configuration) {
|
||||||
this.receiver = new BinaryHTTPReceiver(
|
this.receiver = new BinaryHTTPReceiver(
|
||||||
parserByType,
|
parsersByEncoding,
|
||||||
setterByHeader,
|
setterByHeader,
|
||||||
allowedContentTypes,
|
allowedContentTypes,
|
||||||
requiredHeaders,
|
requiredHeaders,
|
||||||
Spec,
|
Spec,
|
||||||
Constants.SPEC_V03,
|
Constants.SPEC_V03,
|
||||||
Constants.BINARY_HEADERS_03.EXTENSIONS_PREFIX
|
Constants.BINARY_HEADERS_03.EXTENSIONS_PREFIX,
|
||||||
|
checkDecorator
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,6 +111,9 @@ Receiver.prototype.check = function(payload, headers) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Receiver.prototype.parse = function(payload, headers) {
|
Receiver.prototype.parse = function(payload, headers) {
|
||||||
|
// firstly specific local checks
|
||||||
|
this.check(payload, headers);
|
||||||
|
|
||||||
return this.receiver.parse(payload, headers);
|
return this.receiver.parse(payload, headers);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue