Skip to content

Commit 79ec3ef

Browse files
authored
chore: es6 unmarshaller (cloudevents#108)
* chore: es6 unmarshaller Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
1 parent e329d9a commit 79ec3ef

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

lib/bindings/http/unmarshaller.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@ const Commons = require("./commons.js");
44
const STRUCTURED = "structured";
55
const BINARY = "binary";
66

7-
const allowedBinaryContentTypes = [];
8-
allowedBinaryContentTypes.push(Constants.MIME_JSON);
9-
allowedBinaryContentTypes.push(Constants.MIME_OCTET_STREAM);
7+
const allowedBinaryContentTypes = [
8+
Constants.MIME_JSON,
9+
Constants.MIME_OCTET_STREAM
10+
];
1011

11-
const allowedStructuredContentTypes = [];
12-
allowedStructuredContentTypes.push(Constants.MIME_CE_JSON);
13-
14-
function validateArgs(payload, headers) {
15-
if (!payload) {
16-
throw new TypeError("payload is null or undefined");
17-
}
18-
19-
if (!headers) {
20-
throw new TypeError("headers is null or undefined");
21-
}
22-
}
12+
const allowedStructuredContentTypes = [
13+
Constants.MIME_CE_JSON
14+
];
2315

2416
// Is it binary or structured?
2517
function resolveBindingName(payload, headers) {
@@ -47,18 +39,22 @@ function resolveBindingName(payload, headers) {
4739
}
4840
}
4941

50-
const Unmarshaller = function(receiverByBinding) {
51-
this.receiverByBinding = receiverByBinding;
52-
};
53-
54-
Unmarshaller.prototype.unmarshall = function(payload, headers) {
55-
return new Promise((resolve, reject) => {
56-
try {
57-
validateArgs(payload, headers);
42+
class Unmarshaller {
43+
constructor(receiverByBinding) {
44+
this.receiverByBinding = receiverByBinding;
45+
}
5846

59-
const sanityHeaders = Commons.sanityAndClone(headers);
47+
unmarshall(payload, headers) {
48+
return new Promise((resolve, reject) => {
49+
if (!payload) {
50+
return reject(new TypeError("payload is null or undefined"));
51+
}
52+
if (!headers) {
53+
return reject(new TypeError("headers is null or undefined"));
54+
}
6055

6156
// Validation level 1
57+
const sanityHeaders = Commons.sanityAndClone(headers);
6258
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
6359
throw new TypeError("content-type header not found");
6460
}
@@ -69,10 +65,8 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) {
6965
.parse(payload, sanityHeaders);
7066

7167
resolve(cloudevent);
72-
} catch (e) {
73-
reject(e);
74-
}
75-
});
76-
};
68+
});
69+
}
70+
}
7771

7872
module.exports = Unmarshaller;

0 commit comments

Comments
 (0)