-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchild.js
56 lines (49 loc) · 1.53 KB
/
child.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
/* eslint-disable no-console */
'use strict';
process.on('message', async params => {
let res = '';
try {
const functions = require(params.script);
if (params.function.length > 0) {
if (typeof functions[params.function] === 'function') {
res = await functions[params.function](params.parameters);
} else {
throw new Error(
`'${params.function}' is not a function. Make sure you do 'module.exports.${params.function} = function_name' in '${params.script}'.`
);
}
} else {
if (typeof functions === 'function') {
res = await functions(params.parameters);
} else {
throw new Error(`function not found. Make sure you do 'module.exports = function_name' in '${params.script}'.`);
}
}
if (res) process.send(res);
process.exit();
} catch (err) {
if (params.debug) console.error(err);
process.send(serializeError(err));
process.exit(1);
}
});
process.on('uncaughtException', err => {
process.send(serializeError(err));
process.exit(1);
});
process.on('unhandledRejection', (reason, p) => {
process.send(serializeError(reason));
process.exit(1);
});
process.stdin.resume();
function exitHandler() {
process.send('Process has been killed.');
process.exit(1);
}
process.on('SIGINT', exitHandler.bind());
process.on('SIGTERM', exitHandler.bind());
function serializeError(error) {
return typeof error === 'object'
? JSON.stringify({ name: error.name, message: error.message, stack: error.stack, ...error })
: error;
}