forked from aws/aws-lambda-nodejs-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·53 lines (45 loc) · 1.68 KB
/
index.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
/**
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This module is the bootstrap entrypoint. It establishes the top-level event
* listeners and loads the user's code.
*/
const RAPIDClient = require('./RAPIDClient.js');
const Runtime = require('./Runtime.js');
const UserFunction = require('./UserFunction.js');
const Errors = require('./Errors.js');
const BeforeExitListener = require('./BeforeExitListener.js');
const LogPatch = require('./LogPatch');
export async function run(appRootOrHandler, handler = '') {
LogPatch.patchConsole();
const client = new RAPIDClient(process.env.AWS_LAMBDA_RUNTIME_API);
let errorCallbacks = {
uncaughtException: (error) => {
client.postInitError(error, () => process.exit(129));
},
unhandledRejection: (error) => {
client.postInitError(error, () => process.exit(128));
},
};
process.on('uncaughtException', (error) => {
LogPatch.structuredConsole.logError('Uncaught Exception', error);
errorCallbacks.uncaughtException(error);
});
process.on('unhandledRejection', (reason, promise) => {
let error = new Errors.UnhandledPromiseRejection(reason, promise);
LogPatch.structuredConsole.logError('Unhandled Promise Rejection', error);
errorCallbacks.unhandledRejection(error);
});
BeforeExitListener.reset();
process.on('beforeExit', BeforeExitListener.invoke);
const handlerFunc = UserFunction.isHandlerFunction(appRootOrHandler)
? appRootOrHandler
: await UserFunction.load(appRootOrHandler, handler);
const metadata = UserFunction.getHandlerMetadata(handlerFunc);
new Runtime(
client,
handlerFunc,
metadata,
errorCallbacks,
).scheduleIteration();
}