chore: es6 unmarshaller (#108)
* chore: es6 unmarshaller Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
This commit is contained in:
parent
e329d9ac00
commit
79ec3ef126
|
@ -4,22 +4,14 @@ const Commons = require("./commons.js");
|
|||
const STRUCTURED = "structured";
|
||||
const BINARY = "binary";
|
||||
|
||||
const allowedBinaryContentTypes = [];
|
||||
allowedBinaryContentTypes.push(Constants.MIME_JSON);
|
||||
allowedBinaryContentTypes.push(Constants.MIME_OCTET_STREAM);
|
||||
const allowedBinaryContentTypes = [
|
||||
Constants.MIME_JSON,
|
||||
Constants.MIME_OCTET_STREAM
|
||||
];
|
||||
|
||||
const allowedStructuredContentTypes = [];
|
||||
allowedStructuredContentTypes.push(Constants.MIME_CE_JSON);
|
||||
|
||||
function validateArgs(payload, headers) {
|
||||
if (!payload) {
|
||||
throw new TypeError("payload is null or undefined");
|
||||
}
|
||||
|
||||
if (!headers) {
|
||||
throw new TypeError("headers is null or undefined");
|
||||
}
|
||||
}
|
||||
const allowedStructuredContentTypes = [
|
||||
Constants.MIME_CE_JSON
|
||||
];
|
||||
|
||||
// Is it binary or structured?
|
||||
function resolveBindingName(payload, headers) {
|
||||
|
@ -47,18 +39,22 @@ function resolveBindingName(payload, headers) {
|
|||
}
|
||||
}
|
||||
|
||||
const Unmarshaller = function(receiverByBinding) {
|
||||
this.receiverByBinding = receiverByBinding;
|
||||
};
|
||||
class Unmarshaller {
|
||||
constructor(receiverByBinding) {
|
||||
this.receiverByBinding = receiverByBinding;
|
||||
}
|
||||
|
||||
Unmarshaller.prototype.unmarshall = function(payload, headers) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
validateArgs(payload, headers);
|
||||
|
||||
const sanityHeaders = Commons.sanityAndClone(headers);
|
||||
unmarshall(payload, headers) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!payload) {
|
||||
return reject(new TypeError("payload is null or undefined"));
|
||||
}
|
||||
if (!headers) {
|
||||
return reject(new TypeError("headers is null or undefined"));
|
||||
}
|
||||
|
||||
// Validation level 1
|
||||
const sanityHeaders = Commons.sanityAndClone(headers);
|
||||
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
|
||||
throw new TypeError("content-type header not found");
|
||||
}
|
||||
|
@ -69,10 +65,8 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) {
|
|||
.parse(payload, sanityHeaders);
|
||||
|
||||
resolve(cloudevent);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Unmarshaller;
|
||||
|
|
Loading…
Reference in New Issue