-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathindex.ts
60 lines (50 loc) · 1.82 KB
/
index.ts
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
/* eslint-disable no-console */
/**
* Copyright 2019 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.
*/
"use strict";
import { HandlerFunction } from "./Common";
import * as Errors from "./Errors";
import RuntimeClient from "./RuntimeClient";
import Runtime from "./Runtime";
import BeforeExitListener from "./Runtime/BeforeExitListener";
import LogPatch from "./utils/LogPatch";
import * as UserFunction from "./utils/UserFunction";
LogPatch.patchConsole();
export async function run(appRoot: string, handler: string): Promise<void> {
if (!process.env.AWS_LAMBDA_RUNTIME_API) {
throw new Error("Missing Runtime API Server configuration.");
}
const client = new RuntimeClient(process.env.AWS_LAMBDA_RUNTIME_API);
const errorCallbacks = {
uncaughtException: (error: Error) => {
client.postInitError(error, () => process.exit(129));
},
unhandledRejection: (error: Error) => {
client.postInitError(error, () => process.exit(128));
},
};
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception", Errors.toFormatted(error));
errorCallbacks.uncaughtException(error);
});
process.on("unhandledRejection", (reason, promise) => {
const error = new Errors.UnhandledPromiseRejection(
reason?.toString(),
promise
);
console.error("Unhandled Promise Rejection", Errors.toFormatted(error));
errorCallbacks.unhandledRejection(error);
});
BeforeExitListener.reset();
process.on("beforeExit", BeforeExitListener.invoke);
const handlerFunc = (await UserFunction.load(
appRoot,
handler
)) as HandlerFunction;
const runtime = new Runtime(client, handlerFunc, errorCallbacks);
runtime.scheduleIteration();
}