Skip to content

chore: es6 unmarshaller #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 23 additions & 29 deletions lib/bindings/http/unmarshaller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -47,18 +39,22 @@ function resolveBindingName(payload, headers) {
}
}

const Unmarshaller = function(receiverByBinding) {
this.receiverByBinding = receiverByBinding;
};

Unmarshaller.prototype.unmarshall = function(payload, headers) {
return new Promise((resolve, reject) => {
try {
validateArgs(payload, headers);
class Unmarshaller {
constructor(receiverByBinding) {
this.receiverByBinding = receiverByBinding;
}

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");
}
Expand All @@ -69,10 +65,8 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) {
.parse(payload, sanityHeaders);

resolve(cloudevent);
} catch (e) {
reject(e);
}
});
};
});
}
}

module.exports = Unmarshaller;