-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (68 loc) · 2.49 KB
/
index.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
'use strict';
let events;
module.exports = () => {
return (req, res, next) => {
return events ? handleRequest() : compileHandler();
function compileHandler() {
req.webtaskContext.compiler.nodejsCompiler(req.webtaskContext.compiler.script, (e, func) => {
if (e) return next(e);
events = {};
function on(spec, handler) {
if (!Array.isArray(spec)) spec = [ spec ];
if (typeof handler !== 'function') {
let error = new Error('Second argument to the `on` call must be a function.');
error.statusCode = 400;
return next(error);
}
for (var i = 0; i < spec.length; i++) {
if (typeof spec[i] !== 'string') {
let error = new Error('First argument to the `on` call must be a string or array of strings.');
error.statusCode = 400;
return next(error);
}
events[spec[i]] = handler;
}
}
func({ on });
return handleRequest();
});
}
function handleRequest() {
let auth;
if (req.webtaskContext.secrets.BASIC_AUTH) {
auth = new Buffer(req.webtaskContext.secrets.BASIC_AUTH).toString('base64');
}
if (auth) {
let match = (req.headers.authorization || '').match(/^\s*Basic\s+([^\s]+)\s*$/);
if (!match || match[1] !== auth) {
let error = new Error('Unauthorized.');
error.statusCode = 403;
return next(error);
}
}
let method = req.body.eventType;
if (!method) {
let error = new Error(`Malformed CloudEvent message. The required 'eventType' property is not specified.`);
error.statusCode = 400;
return next(error);
}
if (typeof events[method] !== 'function') {
let error = new Error(`Unuspported eventType: ${method}.`);
error.statusCode = 501;
return next(error);
}
else {
if (events[method].length === 1) {
res.writeHead(201)
res.end();
}
req.webtaskContext.body = req.body;
return events[method](req.webtaskContext, (e,d) => {
if (e) return next(e);
res.writeHead(200, { 'Content-Type': 'application/json' });
return res.end(JSON.stringify(d));
});
}
}
};
};