-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathsandbox-child.mjs
53 lines (45 loc) · 1.13 KB
/
sandbox-child.mjs
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
// @ts-check
import { getLogger } from 'lambda-local'
import { loadFunction } from './lambda-helpers.mjs'
getLogger().level = 'alert'
/**
* @type {import('./lambda-helpers.mjs').InvokeFunction | undefined}
*/
let invokeFunctionImpl
process.on(
'message',
/**
* @param {any} msg
*/
async (msg) => {
if (msg?.action === 'exit') {
process.exit(0)
} else if (msg?.action === 'loadFunction') {
const [ctx, options] = msg.args
invokeFunctionImpl = await loadFunction(ctx, options)
if (process.send) {
process.send({
action: 'loadedFunction',
})
}
} else if (msg?.action === 'invokeFunction') {
try {
const [ctx, options] = msg.args
if (!invokeFunctionImpl) {
throw new Error('Function not loaded')
}
const result = await invokeFunctionImpl(options)
if (process.send) {
process.send({
action: 'invokeFunctionResult',
operationId: msg.operationId,
result,
})
}
} catch (e) {
console.log('error', e)
process.exit(1)
}
}
},
)