-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
84 lines (79 loc) · 2.72 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
77
78
79
80
81
82
83
84
/**
* @description The module appendix template. Never import this,
* only copy-paste from here to transpiler/index.js
* @param {*} moduleexp
* @param {*} [exp]
*/
module.exports = () => {
// START PASTE
/**
* Start Hyperform wrapper
* It provides some simple usability features
* Amazon:
* Google:
* - Send pre-flight headers
* - console.error on error
*/
global.alreadyWrappedNames = [];
function wrapExs(me, platform) {
const newmoduleexports = { ...me };
const expkeys = Object.keys(me);
for (let i = 0; i < expkeys.length; i += 1) {
const expkey = expkeys[i];
const userfunc = newmoduleexports[expkey];
// it should be idempotent
// TODO fix code so this doesn't happen
if (global.alreadyWrappedNames.includes(expkey)) {
continue;
}
global.alreadyWrappedNames.push(expkey);
let wrappedfunc;
if (platform === 'amazon') {
wrappedfunc = async function handler(event, context, callback) {
/// ////////////////////////////////
// Invoke user function ///////
/// ////////////////////////////////
const res = await userfunc(event, context, callback);
context.succeed(res);
// throwing will call context.fail automatically
};
}
if (platform === 'google') {
wrappedfunc = async function handler(req, resp, ...rest) {
// allow to be called from anywhere (also localhost)
// resp.header('Content-Type', 'application/json');
resp.header('Access-Control-Allow-Origin', '*');
resp.header('Access-Control-Allow-Headers', '*');
resp.header('Access-Control-Allow-Methods', '*');
resp.header('Access-Control-Max-Age', 30);
// respond to CORS preflight requests
if (req.method === 'OPTIONS') {
resp.status(204).send('');
} else {
// Invoke user function
// (user must .json or .send himself)
try {
await userfunc(req, resp, ...rest);
} catch (e) {
console.error(e);
resp.status(500).send(''); // TODO generate URL to logs (similar to GH)
}
}
};
}
newmoduleexports[expkey] = wrappedfunc;
}
return newmoduleexports;
}
const curr = { ...exports, ...module.exports };
const isInAmazon = !!(process.env.LAMBDA_TASK_ROOT || process.env.AWS_EXECUTION_ENV);
const isInGoogle = (/google/.test(process.env._) === true);
if (isInAmazon === true) {
return wrapExs(curr, 'amazon');
}
if (isInGoogle === true) {
return wrapExs(curr, 'google');
}
return curr; // Export unchanged (local, fallback)
// END PASTE
};