-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
outbound.ts
78 lines (69 loc) · 2.56 KB
/
outbound.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* istanbul ignore file */
import * as https from 'https';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Lambda, waitUntilFunctionActiveV2, InvocationResponse, InvokeCommandInput } from '@aws-sdk/client-lambda';
// eslint-disable-next-line import/no-extraneous-dependencies
import { SFN, StartExecutionInput, StartExecutionOutput } from '@aws-sdk/client-sfn';
// eslint-disable-next-line import/no-extraneous-dependencies
const FRAMEWORK_HANDLER_TIMEOUT = 900000; // 15 minutes
// In order to honor the overall maximum timeout set for the target process,
// the default 2 minutes from AWS SDK has to be overriden:
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#httpOptions-property
const awsSdkConfig = {
httpOptions: { timeout: FRAMEWORK_HANDLER_TIMEOUT },
};
async function defaultHttpRequest(options: https.RequestOptions, responseBody: string) {
return new Promise((resolve, reject) => {
try {
const request = https.request(options, resolve);
request.on('error', reject);
request.write(responseBody);
request.end();
} catch (e) {
reject(e);
}
});
}
let sfn: SFN;
let lambda: Lambda;
async function defaultStartExecution(req: StartExecutionInput): Promise<StartExecutionOutput> {
if (!sfn) {
sfn = new SFN(awsSdkConfig);
}
return sfn.startExecution(req);
}
async function defaultInvokeFunction(req: InvokeCommandInput): Promise<InvocationResponse> {
if (!lambda) {
lambda = new Lambda(awsSdkConfig);
}
try {
/**
* Try an initial invoke.
*
* When you try to invoke a function that is inactive, the invocation fails and Lambda sets
* the function to pending state until the function resources are recreated.
* If Lambda fails to recreate the resources, the function is set to the inactive state.
*
* We're using invoke first because `waitFor` doesn't trigger an inactive function to do anything,
* it just runs `getFunction` and checks the state.
*/
return await lambda.invoke(req);
} catch {
/**
* The status of the Lambda function is checked every second for up to 300 seconds.
* Exits the loop on 'Active' state and throws an error on 'Inactive' or 'Failed'.
*
* And now we wait.
*/
await waitUntilFunctionActiveV2({
client: lambda,
maxWaitTime: 300,
}, {
FunctionName: req.FunctionName,
});
return await lambda.invoke(req);
}
}
export let startExecution = defaultStartExecution;
export let invokeFunction = defaultInvokeFunction;
export let httpRequest = defaultHttpRequest;