forked from cloudevents/sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver_binary.js
139 lines (114 loc) · 4.04 KB
/
receiver_binary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const Constants = require("./constants.js");
const Commons = require("./commons.js");
const CloudEvent = require("../../cloudevent.js");
const {
isDefinedOrThrow,
isStringOrObjectOrThrow
} = require("../../utils/fun.js");
function validateArgs(payload, attributes) {
Array.of(payload)
.filter((p) => isDefinedOrThrow(p,
{ message: "payload is null or undefined" }))
.filter((p) => isStringOrObjectOrThrow(p,
{ message: "payload must be an object or a string" }))
.shift();
Array.of(attributes)
.filter((a) => isDefinedOrThrow(a,
{ message: "attributes is null or undefined" }))
.shift();
}
function BinaryHTTPReceiver(
parsersByEncoding,
setterByHeader,
allowedContentTypes,
requiredHeaders,
Spec,
specversion,
extensionsPrefix,
checkDecorator) {
this.parsersByEncoding = parsersByEncoding;
this.setterByHeader = setterByHeader;
this.allowedContentTypes = allowedContentTypes;
this.requiredHeaders = requiredHeaders;
this.Spec = Spec;
this.spec = new Spec();
this.specversion = specversion;
this.extensionsPrefix = extensionsPrefix;
this.checkDecorator = checkDecorator;
}
BinaryHTTPReceiver.prototype.check = function(payload, headers) {
// Validation Level 0
validateArgs(payload, headers);
if (this.checkDecorator) {
this.checkDecorator(payload, headers);
}
// Clone and low case all headers names
const sanityHeaders = Commons.sanityAndClone(headers);
// If no content type is provided, default to application/json
if (!sanityHeaders[Constants.HEADER_CONTENT_TYPE]) {
sanityHeaders[Constants.HEADER_CONTENT_TYPE] = Constants.MIME_JSON;
}
// Validation Level 1
if (!this.allowedContentTypes
.includes(sanityHeaders[Constants.HEADER_CONTENT_TYPE])) {
const err = new TypeError("invalid content type");
err.errors = [sanityHeaders[Constants.HEADER_CONTENT_TYPE]];
throw err;
}
this.requiredHeaders
.filter((required) => !sanityHeaders[required])
.forEach((required) => {
throw new TypeError(`header '${required}' not found`);
});
if (sanityHeaders[Constants.DEFAULT_SPEC_VERSION_HEADER] !==
this.specversion) {
const err = new TypeError("invalid spec version");
err.errors = [sanityHeaders[Constants.DEFAULT_SPEC_VERSION_HEADER]];
throw err;
}
// No erros! Its contains the minimum required attributes
};
function parserFor(parsersByEncoding, cloudevent, headers) {
const encoding = cloudevent.spec.payload.datacontentencoding;
return parsersByEncoding[encoding][headers[Constants.HEADER_CONTENT_TYPE]];
}
BinaryHTTPReceiver.prototype.parse = function(payload, headers) {
this.check(payload, headers);
// Clone and low case all headers names
const sanityHeaders = Commons.sanityAndClone(headers);
const processedHeaders = [];
const cloudevent = new CloudEvent(this.Spec);
// dont worry, check() have seen what was required or not
Array.from(Object.keys(this.setterByHeader))
.filter((header) => sanityHeaders[header])
.forEach((header) => {
const setterName = this.setterByHeader[header].name;
const parserFun = this.setterByHeader[header].parser;
// invoke the setter function
cloudevent[setterName](parserFun(sanityHeaders[header]));
// to use ahead, for extensions processing
processedHeaders.push(header);
});
// Parses the payload
const parsedPayload =
parserFor(this.parsersByEncoding, cloudevent, sanityHeaders)
.parse(payload);
// Every unprocessed header can be an extension
Array.from(Object.keys(sanityHeaders))
.filter((value) => !processedHeaders.includes(value))
.filter((value) =>
value.startsWith(this.extensionsPrefix))
.map((extension) =>
extension.substring(this.extensionsPrefix.length)
).forEach((extension) =>
cloudevent.addExtension(extension,
sanityHeaders[this.extensionsPrefix + extension])
);
// Sets the data
cloudevent.data(parsedPayload);
// Checks the event spec
cloudevent.format();
// return the result
return cloudevent;
};
module.exports = BinaryHTTPReceiver;