@@ -4,22 +4,14 @@ const Commons = require("./commons.js");
4
4
const STRUCTURED = "structured" ;
5
5
const BINARY = "binary" ;
6
6
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
+ ] ;
10
11
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
+ ] ;
23
15
24
16
// Is it binary or structured?
25
17
function resolveBindingName ( payload , headers ) {
@@ -47,18 +39,22 @@ function resolveBindingName(payload, headers) {
47
39
}
48
40
}
49
41
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
+ }
58
46
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
+ }
60
55
61
56
// Validation level 1
57
+ const sanityHeaders = Commons . sanityAndClone ( headers ) ;
62
58
if ( ! sanityHeaders [ Constants . HEADER_CONTENT_TYPE ] ) {
63
59
throw new TypeError ( "content-type header not found" ) ;
64
60
}
@@ -69,10 +65,8 @@ Unmarshaller.prototype.unmarshall = function(payload, headers) {
69
65
. parse ( payload , sanityHeaders ) ;
70
66
71
67
resolve ( cloudevent ) ;
72
- } catch ( e ) {
73
- reject ( e ) ;
74
- }
75
- } ) ;
76
- } ;
68
+ } ) ;
69
+ }
70
+ }
77
71
78
72
module . exports = Unmarshaller ;
0 commit comments