-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambdaSource.js
109 lines (98 loc) · 2.98 KB
/
lambdaSource.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
const path = require('path');
const { fork } = require('child_process');
const e2p = require('event-to-promise');
const log = require('logdown')('appsync-emulator:lambdaSource');
const Runner = path.join(__dirname, 'lambdaRunner');
const PythonRunner = path.join(__dirname, 'lambdaRunnerPython');
const RubyRunner = path.join(__dirname, 'lambdaRunnerRuby');
const GoRunner = path.join(__dirname, 'lambdaRunnerGo');
const lambdaSource = async (
{
dynamodbEndpoint,
dynamodbTables,
serverlessConfig: { functions = {}, custom = {}, provider = {} },
serverlessDirectory,
},
fn,
{ payload },
) => {
const fnConfig = functions[fn];
if (!fnConfig) {
throw new Error(`Cannot find function config for function : ${fn}`);
}
// Default to empty string, path.join will resolve this automatically
let buildPrefix = '';
// Check if the modulePrefix configuration is set
if (custom['appsync-emulator'] && custom['appsync-emulator'].buildPrefix) {
({ buildPrefix } = custom['appsync-emulator']);
}
const [handlerPath, handlerMethod] = fnConfig.handler.split('.');
const fullPath = path.join(serverlessDirectory, buildPrefix, handlerPath);
const dynamodbTableAliases = Object.entries(dynamodbTables).reduce(
(sum, [alias, tableName]) => ({
...sum,
[`DYNAMODB_TABLE_${alias}`]: tableName,
}),
{},
);
let child = null;
if (fnConfig.runtime && !fnConfig.runtime.includes('node')) {
let extHandlerMethod = '';
let runner = null;
if (fnConfig.runtime.indexOf('python') >= 0) {
extHandlerMethod = fn;
runner = PythonRunner;
} else if (fnConfig.runtime.indexOf('ruby') >= 0) {
extHandlerMethod = fn;
runner = RubyRunner;
} else if (fnConfig.runtime.indexOf('go') >= 0) {
extHandlerMethod = fnConfig.handler.split('/').pop();
runner = GoRunner;
}
child = fork(runner, [], {
env: {
...process.env,
...dynamodbTableAliases,
DYNAMODB_ENDPOINT: dynamodbEndpoint,
...provider.environment,
...fnConfig.environment,
},
stdio: [0, 1, 2, 'ipc'],
});
child.send({
serverlessDirectory,
handlerMethod: extHandlerMethod,
payload,
});
} else {
const childOptions = {
env: {
...process.env,
...dynamodbTableAliases,
DYNAMODB_ENDPOINT: dynamodbEndpoint,
...provider.environment,
...fnConfig.environment,
},
stdio: [0, 1, 2, 'ipc'],
};
if (process.env.SLS_DEBUG) childOptions.execArgv = ['--inspect'];
child = fork(Runner, [], childOptions);
child.send({
module: fullPath,
handlerPath,
handlerMethod,
payload,
});
}
const response = await e2p(child, 'message');
switch (response.type) {
case 'error':
throw response.error;
case 'success':
return response.output;
default:
log.error('unknown response type', response);
throw new Error('Unknown response type');
}
};
module.exports = lambdaSource;