quidproquo-actionprocessor-awslambda 0.0.30 → 0.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/awsLambdaUtils.d.ts +0 -1
- package/lib/awsLambdaUtils.js +1 -18
- package/lib/getActionProcessor/core/system/getExecuteStoryActionProcessor.d.ts +2 -2
- package/lib/getActionProcessor/core/system/getExecuteStoryActionProcessor.js +4 -5
- package/lib/getActionProcessor/core/system/index.d.ts +2 -2
- package/lib/getActionProcessor/core/system/index.js +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/lambdas/index.d.ts +3 -0
- package/lib/lambdas/index.js +19 -0
- package/lib/lambdas/lambdaAPIGatewayEvent.d.ts +13 -0
- package/lib/lambdas/lambdaAPIGatewayEvent.js +101 -0
- package/lib/lambdas/lambdaConfig.d.ts +4 -0
- package/lib/lambdas/lambdaConfig.js +4 -0
- package/lib/lambdas/lambdaEventBridgeEvent.d.ts +5 -0
- package/lib/lambdas/lambdaEventBridgeEvent.js +60 -0
- package/lib/types/DynamicLoader.d.ts +1 -0
- package/lib/types/DynamicLoader.js +2 -0
- package/package.json +3 -2
- package/src/awsLambdaUtils.ts +22 -0
- package/src/getActionProcessor/core/config/getConfigGetParameterActionProcessor.ts +23 -0
- package/src/getActionProcessor/core/config/getConfigGetParametersActionProcessor.ts +27 -0
- package/src/getActionProcessor/core/config/getConfigGetSecretActionProcessor.ts +23 -0
- package/src/getActionProcessor/core/config/index.ts +11 -0
- package/src/getActionProcessor/core/event/getAPIGatewayEventActionProcessor.ts +134 -0
- package/src/getActionProcessor/core/event/getEventBridgeEventActionProcessor.ts +66 -0
- package/src/getActionProcessor/core/file/getFileDeleteActionProcessor.ts +31 -0
- package/src/getActionProcessor/core/file/getFileExistsActionProcessor.ts +16 -0
- package/src/getActionProcessor/core/file/getFileListDirectoryActionProcessor.ts +28 -0
- package/src/getActionProcessor/core/file/getFileReadTextContentsActionProcessor.ts +18 -0
- package/src/getActionProcessor/core/file/getFileWriteTextContentsActionProcessor.ts +23 -0
- package/src/getActionProcessor/core/file/index.ts +15 -0
- package/src/getActionProcessor/core/index.ts +7 -0
- package/src/getActionProcessor/core/system/getExecuteStoryActionProcessor.ts +67 -0
- package/src/getActionProcessor/core/system/index.ts +6 -0
- package/src/getActionProcessor/index.ts +1 -0
- package/src/index.ts +6 -0
- package/src/lambdas/index.ts +4 -0
- package/src/lambdas/lambdaAPIGatewayEvent.ts +105 -0
- package/src/lambdas/lambdaConfig.ts +11 -0
- package/src/lambdas/lambdaEventBridgeEvent.ts +60 -0
- package/src/logic/parametersManager/getParameter.ts +13 -0
- package/src/logic/parametersManager/getParameters.ts +15 -0
- package/src/logic/s3/deleteFiles.ts +17 -0
- package/src/logic/s3/listFiles.ts +66 -0
- package/src/logic/s3/objectExists.ts +18 -0
- package/src/logic/s3/readTextFile.ts +14 -0
- package/src/logic/s3/s3Client.ts +5 -0
- package/src/logic/s3/s3Utils.ts +5 -0
- package/src/logic/s3/writeTextFile.ts +17 -0
- package/src/logic/secretsManager/getSecret.ts +13 -0
- package/src/runtimeConfig/QPQAWSLambdaConfig.ts +15 -0
- package/src/runtimeConfig/qpqAwsLambdaRuntimeConfigUtils.ts +19 -0
- package/src/types/DynamicLoader.ts +1 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EventActionType,
|
|
3
|
+
MatchStoryResult,
|
|
4
|
+
EventMatchStoryActionProcessor,
|
|
5
|
+
EventTransformEventParamsActionProcessor,
|
|
6
|
+
EventTransformResponseResultActionProcessor,
|
|
7
|
+
EventAutoRespondActionProcessor,
|
|
8
|
+
actionResult,
|
|
9
|
+
actionResultError,
|
|
10
|
+
ErrorTypeEnum,
|
|
11
|
+
ScheduledEventParams,
|
|
12
|
+
} from 'quidproquo-core';
|
|
13
|
+
|
|
14
|
+
import { QPQAWSLambdaConfig, LambdaRuntimeConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
|
|
15
|
+
|
|
16
|
+
import { EventBridgeEvent, Context, APIGatewayProxyResult } from 'aws-lambda';
|
|
17
|
+
|
|
18
|
+
const getProcessTransformEventParams = (): EventTransformEventParamsActionProcessor<
|
|
19
|
+
[EventBridgeEvent<any, any>, Context],
|
|
20
|
+
ScheduledEventParams<any>
|
|
21
|
+
> => {
|
|
22
|
+
return async ({ eventParams: [eventBridgeEvent, context] }) => {
|
|
23
|
+
return actionResult({
|
|
24
|
+
time: eventBridgeEvent.time,
|
|
25
|
+
correlation: context.awsRequestId,
|
|
26
|
+
detail: eventBridgeEvent.detail,
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// No transform
|
|
32
|
+
const getProcessTransformResponseResult = (): EventTransformResponseResultActionProcessor<any> => {
|
|
33
|
+
return async ({ response }) => actionResult<any>(response);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// never early exit (maybe add validation?)
|
|
37
|
+
const getProcessAutoRespond = (): EventAutoRespondActionProcessor<ScheduledEventParams<any>> => {
|
|
38
|
+
return async () => actionResult(null);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const getProcessMatchStory = (
|
|
42
|
+
lambdaRuntimeConfig?: LambdaRuntimeConfig,
|
|
43
|
+
): EventMatchStoryActionProcessor<ScheduledEventParams<any>> => {
|
|
44
|
+
return async (payload) => {
|
|
45
|
+
console.log('Trying to match story');
|
|
46
|
+
console.log(JSON.stringify(lambdaRuntimeConfig));
|
|
47
|
+
if (!lambdaRuntimeConfig) {
|
|
48
|
+
return actionResultError(ErrorTypeEnum.NotFound, 'event runtime not found');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return actionResult<MatchStoryResult>({
|
|
52
|
+
src: lambdaRuntimeConfig.src,
|
|
53
|
+
runtime: lambdaRuntimeConfig.runtime,
|
|
54
|
+
options: {},
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default (runtimeConfig: QPQAWSLambdaConfig) => {
|
|
60
|
+
return {
|
|
61
|
+
[EventActionType.TransformEventParams]: getProcessTransformEventParams(),
|
|
62
|
+
[EventActionType.TransformResponseResult]: getProcessTransformResponseResult(),
|
|
63
|
+
[EventActionType.AutoRespond]: getProcessAutoRespond(),
|
|
64
|
+
[EventActionType.MatchStory]: getProcessMatchStory(runtimeConfig.lambdaRuntimeConfig),
|
|
65
|
+
};
|
|
66
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
|
|
2
|
+
import { resolveResourceName } from '../../../runtimeConfig/qpqAwsLambdaRuntimeConfigUtils';
|
|
3
|
+
import {
|
|
4
|
+
FileDeleteActionProcessor,
|
|
5
|
+
actionResult,
|
|
6
|
+
actionResultError,
|
|
7
|
+
FileActionType,
|
|
8
|
+
ErrorTypeEnum,
|
|
9
|
+
} from 'quidproquo-core';
|
|
10
|
+
import { deleteFiles } from '../../../logic/s3/s3Utils';
|
|
11
|
+
|
|
12
|
+
const getProcessFileDelete = (runtimeConfig: QPQAWSLambdaConfig): FileDeleteActionProcessor => {
|
|
13
|
+
return async ({ drive, filepaths }) => {
|
|
14
|
+
const s3BucketName = resolveResourceName(drive, runtimeConfig);
|
|
15
|
+
const errored = await deleteFiles(s3BucketName, filepaths);
|
|
16
|
+
|
|
17
|
+
// errored deletes are a graceful success ~ Retry
|
|
18
|
+
// if (errored.length > 0) {
|
|
19
|
+
// return actionResultError(
|
|
20
|
+
// ErrorTypeEnum.GenericError,
|
|
21
|
+
// `Could not delete files ${errored.length}`,
|
|
22
|
+
// );
|
|
23
|
+
// }
|
|
24
|
+
|
|
25
|
+
return actionResult(errored);
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export default (runtimeConfig: QPQAWSLambdaConfig) => ({
|
|
30
|
+
[FileActionType.Delete]: getProcessFileDelete(runtimeConfig),
|
|
31
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
|
|
2
|
+
import { resolveResourceName } from '../../../runtimeConfig/qpqAwsLambdaRuntimeConfigUtils';
|
|
3
|
+
import { FileExistsActionProcessor, actionResult, FileActionType } from 'quidproquo-core';
|
|
4
|
+
import { objectExists } from '../../../logic/s3/s3Utils';
|
|
5
|
+
|
|
6
|
+
const getProcessFileExists = (runtimeConfig: QPQAWSLambdaConfig): FileExistsActionProcessor => {
|
|
7
|
+
return async ({ drive, filepath }) => {
|
|
8
|
+
const s3BucketName = resolveResourceName(drive, runtimeConfig);
|
|
9
|
+
|
|
10
|
+
return actionResult(await objectExists(s3BucketName, filepath));
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default (runtimeConfig: QPQAWSLambdaConfig) => ({
|
|
15
|
+
[FileActionType.Exists]: getProcessFileExists(runtimeConfig),
|
|
16
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
|
|
2
|
+
import { resolveResourceName } from '../../../runtimeConfig/qpqAwsLambdaRuntimeConfigUtils';
|
|
3
|
+
import { FileListDirectoryActionProcessor, actionResult, FileActionType } from 'quidproquo-core';
|
|
4
|
+
import { listFiles } from '../../../logic/s3/s3Utils';
|
|
5
|
+
|
|
6
|
+
const getProcessFileListDirectory = (
|
|
7
|
+
runtimeConfig: QPQAWSLambdaConfig,
|
|
8
|
+
): FileListDirectoryActionProcessor => {
|
|
9
|
+
return async ({ drive, folderPath, maxFiles, pageToken }) => {
|
|
10
|
+
const s3BucketName = resolveResourceName(drive, runtimeConfig);
|
|
11
|
+
const s3FileList = await listFiles(s3BucketName, folderPath, maxFiles, pageToken);
|
|
12
|
+
|
|
13
|
+
// Add the drive onto the list
|
|
14
|
+
const fileInfos = s3FileList.fileInfos.map((s3fi) => ({
|
|
15
|
+
...s3fi,
|
|
16
|
+
drive: drive,
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
return actionResult({
|
|
20
|
+
fileInfos,
|
|
21
|
+
pageToken: s3FileList.pageToken,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export default (runtimeConfig: QPQAWSLambdaConfig) => ({
|
|
27
|
+
[FileActionType.ListDirectory]: getProcessFileListDirectory(runtimeConfig),
|
|
28
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
|
|
2
|
+
import { resolveResourceName } from '../../../runtimeConfig/qpqAwsLambdaRuntimeConfigUtils';
|
|
3
|
+
import { FileReadTextContentsActionProcessor, actionResult, FileActionType } from 'quidproquo-core';
|
|
4
|
+
import { readTextFile } from '../../../logic/s3/s3Utils';
|
|
5
|
+
|
|
6
|
+
const getProcessFileReadTextContents = (
|
|
7
|
+
runtimeConfig: QPQAWSLambdaConfig,
|
|
8
|
+
): FileReadTextContentsActionProcessor => {
|
|
9
|
+
return async ({ drive, filepath }) => {
|
|
10
|
+
const s3BucketName = resolveResourceName(drive, runtimeConfig);
|
|
11
|
+
|
|
12
|
+
return actionResult(await readTextFile(s3BucketName, filepath));
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default (runtimeConfig: QPQAWSLambdaConfig) => ({
|
|
17
|
+
[FileActionType.ReadTextContents]: getProcessFileReadTextContents(runtimeConfig),
|
|
18
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
|
|
2
|
+
import { resolveResourceName } from '../../../runtimeConfig/qpqAwsLambdaRuntimeConfigUtils';
|
|
3
|
+
import {
|
|
4
|
+
FileWriteTextContentsActionProcessor,
|
|
5
|
+
actionResult,
|
|
6
|
+
FileActionType,
|
|
7
|
+
} from 'quidproquo-core';
|
|
8
|
+
import { writeTextFile } from '../../../logic/s3/s3Utils';
|
|
9
|
+
|
|
10
|
+
const getProcessFileWriteTextContents = (
|
|
11
|
+
runtimeConfig: QPQAWSLambdaConfig,
|
|
12
|
+
): FileWriteTextContentsActionProcessor => {
|
|
13
|
+
return async ({ drive, filepath, data }) => {
|
|
14
|
+
const s3BucketName = resolveResourceName(drive, runtimeConfig);
|
|
15
|
+
await writeTextFile(s3BucketName, filepath, data);
|
|
16
|
+
|
|
17
|
+
return actionResult(void 0);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default (runtimeConfig: QPQAWSLambdaConfig) => ({
|
|
22
|
+
[FileActionType.WriteTextContents]: getProcessFileWriteTextContents(runtimeConfig),
|
|
23
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
|
|
2
|
+
|
|
3
|
+
import getFileDeleteActionProcessor from './getFileDeleteActionProcessor';
|
|
4
|
+
import getFileExistsActionProcessor from './getFileExistsActionProcessor';
|
|
5
|
+
import getFileListDirectoryActionProcessor from './getFileListDirectoryActionProcessor';
|
|
6
|
+
import getFileReadTextContentsActionProcessor from './getFileReadTextContentsActionProcessor';
|
|
7
|
+
import getFileWriteTextContentsActionProcessor from './getFileWriteTextContentsActionProcessor';
|
|
8
|
+
|
|
9
|
+
export default (runtimeConfig: QPQAWSLambdaConfig) => ({
|
|
10
|
+
...getFileDeleteActionProcessor(runtimeConfig),
|
|
11
|
+
...getFileExistsActionProcessor(runtimeConfig),
|
|
12
|
+
...getFileListDirectoryActionProcessor(runtimeConfig),
|
|
13
|
+
...getFileReadTextContentsActionProcessor(runtimeConfig),
|
|
14
|
+
...getFileWriteTextContentsActionProcessor(runtimeConfig),
|
|
15
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as getConfigGetSecretActionProcessor } from './config/getConfigGetSecretActionProcessor';
|
|
2
|
+
export { default as getConfigGetParameterActionProcessor } from './config/getConfigGetParameterActionProcessor';
|
|
3
|
+
export { default as getConfigGetParametersActionProcessor } from './config/getConfigGetParametersActionProcessor';
|
|
4
|
+
export { default as getAPIGatewayEventActionProcessor } from './event/getAPIGatewayEventActionProcessor';
|
|
5
|
+
export { default as getEventBridgeEventActionProcessor } from './event/getEventBridgeEventActionProcessor';
|
|
6
|
+
export { default as getSystemActionProcessor } from './system';
|
|
7
|
+
export { default as getFileActionProcessor } from './file';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SystemActionType,
|
|
3
|
+
QPQConfig,
|
|
4
|
+
qpqCoreUtils,
|
|
5
|
+
SystemExecuteStoryActionPayload,
|
|
6
|
+
StorySession,
|
|
7
|
+
createRuntime,
|
|
8
|
+
SystemExecuteStoryActionProcessor,
|
|
9
|
+
actionResultError,
|
|
10
|
+
ErrorTypeEnum,
|
|
11
|
+
actionResult,
|
|
12
|
+
ActionProcessorList,
|
|
13
|
+
} from 'quidproquo-core';
|
|
14
|
+
|
|
15
|
+
import { randomGuid } from './../../../awsLambdaUtils';
|
|
16
|
+
import { DynamicModuleLoader } from '../../../types/DynamicLoader';
|
|
17
|
+
|
|
18
|
+
export const getDateNow = () => new Date().toISOString();
|
|
19
|
+
|
|
20
|
+
const getProcessExecuteStory = <T extends Array<any>>(
|
|
21
|
+
dynamicModuleLoader: DynamicModuleLoader,
|
|
22
|
+
): SystemExecuteStoryActionProcessor<T> => {
|
|
23
|
+
return async (
|
|
24
|
+
payload: SystemExecuteStoryActionPayload<T>,
|
|
25
|
+
session: StorySession,
|
|
26
|
+
actionProcessors: ActionProcessorList,
|
|
27
|
+
): Promise<any> => {
|
|
28
|
+
let module = await dynamicModuleLoader(payload.src);
|
|
29
|
+
if (module === null) {
|
|
30
|
+
return actionResultError(ErrorTypeEnum.NotFound, `Module not found [${payload.src}]`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const story = module[payload.runtime];
|
|
34
|
+
if (!story) {
|
|
35
|
+
return actionResultError(
|
|
36
|
+
ErrorTypeEnum.NotFound,
|
|
37
|
+
`[${payload.runtime}] not found in module [${payload.src}]`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const logger = async (result: any) => {
|
|
42
|
+
// return await addResult(service, getDateNow(), payload.params[0][0].path, 'user-route', payload.src, payload.runtime, result);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const resolveStory = createRuntime(session, actionProcessors, getDateNow, logger, randomGuid);
|
|
46
|
+
const storyResult = await resolveStory(story, payload.params);
|
|
47
|
+
|
|
48
|
+
if (storyResult.error) {
|
|
49
|
+
return actionResultError(
|
|
50
|
+
storyResult.error.errorType,
|
|
51
|
+
`story error! in ${payload.src}::${payload.runtime} -> [${storyResult.error.errorText}]`,
|
|
52
|
+
storyResult.error.errorStack,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return actionResult({
|
|
57
|
+
result: storyResult.result,
|
|
58
|
+
session: storyResult.session,
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export default (dynamicModuleLoader: DynamicModuleLoader) => {
|
|
64
|
+
return {
|
|
65
|
+
[SystemActionType.ExecuteStory]: getProcessExecuteStory(dynamicModuleLoader),
|
|
66
|
+
};
|
|
67
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import getExecuteStoryActionProcessor from './getExecuteStoryActionProcessor';
|
|
2
|
+
import { DynamicModuleLoader } from '../../../types/DynamicLoader';
|
|
3
|
+
|
|
4
|
+
export default (dynamicModuleLoader: DynamicModuleLoader) => ({
|
|
5
|
+
...getExecuteStoryActionProcessor(dynamicModuleLoader),
|
|
6
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './core';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { APIGatewayEvent, Context } from 'aws-lambda';
|
|
2
|
+
import { qpqWebServerUtils } from 'quidproquo-webserver';
|
|
3
|
+
import { createRuntime, askProcessEvent, ErrorTypeEnum } from 'quidproquo-core';
|
|
4
|
+
import { coreActionProcessor, webserverActionProcessor } from 'quidproquo-actionprocessor-node';
|
|
5
|
+
|
|
6
|
+
import { DynamicModuleLoader } from '../types/DynamicLoader';
|
|
7
|
+
import * as awsLambdaUtils from '../awsLambdaUtils';
|
|
8
|
+
import {
|
|
9
|
+
getAPIGatewayEventActionProcessor,
|
|
10
|
+
getSystemActionProcessor,
|
|
11
|
+
getFileActionProcessor,
|
|
12
|
+
getConfigGetSecretActionProcessor,
|
|
13
|
+
getConfigGetParameterActionProcessor,
|
|
14
|
+
getConfigGetParametersActionProcessor,
|
|
15
|
+
} from '../getActionProcessor';
|
|
16
|
+
|
|
17
|
+
import { lambdaRuntimeConfig, ActionProcessorListResolver } from './lambdaConfig';
|
|
18
|
+
|
|
19
|
+
// @ts-ignore - Special webpack loader
|
|
20
|
+
import qpqDynamicModuleLoader from 'qpq-dynamic-loader!';
|
|
21
|
+
|
|
22
|
+
// TODO: Make this a util or something based on server time or something..
|
|
23
|
+
const getDateNow = () => new Date().toISOString();
|
|
24
|
+
|
|
25
|
+
const ErrorTypeHttpResponseMap = {
|
|
26
|
+
[ErrorTypeEnum.BadRequest]: 400,
|
|
27
|
+
[ErrorTypeEnum.Unauthorized]: 401,
|
|
28
|
+
[ErrorTypeEnum.PaymentRequired]: 402,
|
|
29
|
+
[ErrorTypeEnum.Forbidden]: 403,
|
|
30
|
+
[ErrorTypeEnum.NotFound]: 404,
|
|
31
|
+
[ErrorTypeEnum.TimeOut]: 408,
|
|
32
|
+
[ErrorTypeEnum.UnsupportedMediaType]: 415,
|
|
33
|
+
[ErrorTypeEnum.OutOfResources]: 500,
|
|
34
|
+
[ErrorTypeEnum.GenericError]: 500,
|
|
35
|
+
[ErrorTypeEnum.NotImplemented]: 501,
|
|
36
|
+
[ErrorTypeEnum.NoContent]: 204,
|
|
37
|
+
[ErrorTypeEnum.Invalid]: 422,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const getAPIGatewayEventExecutor = (
|
|
41
|
+
dynamicModuleLoader: DynamicModuleLoader,
|
|
42
|
+
getCustomActionProcessors: ActionProcessorListResolver = () => ({}),
|
|
43
|
+
) => {
|
|
44
|
+
return async (event: APIGatewayEvent, context: Context) => {
|
|
45
|
+
// Build a processor for the session and stuff
|
|
46
|
+
// Remove the non route ones ~ let the story execute action add them
|
|
47
|
+
const storyActionProcessor = {
|
|
48
|
+
...coreActionProcessor,
|
|
49
|
+
...webserverActionProcessor,
|
|
50
|
+
|
|
51
|
+
...getAPIGatewayEventActionProcessor(lambdaRuntimeConfig.qpqConfig),
|
|
52
|
+
...getConfigGetSecretActionProcessor(lambdaRuntimeConfig),
|
|
53
|
+
...getConfigGetParameterActionProcessor(lambdaRuntimeConfig),
|
|
54
|
+
...getConfigGetParametersActionProcessor(lambdaRuntimeConfig),
|
|
55
|
+
...getSystemActionProcessor(dynamicModuleLoader),
|
|
56
|
+
...getFileActionProcessor(lambdaRuntimeConfig),
|
|
57
|
+
|
|
58
|
+
...getCustomActionProcessors(lambdaRuntimeConfig),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const logger = async (result: any) => {
|
|
62
|
+
// addResult(
|
|
63
|
+
// envConfig.appName,
|
|
64
|
+
// getDateNow(),
|
|
65
|
+
// doWeCarePath,
|
|
66
|
+
// "route-infrastructure",
|
|
67
|
+
// "infrastructure",
|
|
68
|
+
// "askRoute",
|
|
69
|
+
// result
|
|
70
|
+
// );
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// // Run the callback
|
|
74
|
+
|
|
75
|
+
const resolveStory = createRuntime(
|
|
76
|
+
{},
|
|
77
|
+
storyActionProcessor,
|
|
78
|
+
getDateNow,
|
|
79
|
+
logger,
|
|
80
|
+
awsLambdaUtils.randomGuid,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const result = await resolveStory(askProcessEvent, [event, context]);
|
|
84
|
+
|
|
85
|
+
// Run the callback
|
|
86
|
+
if (!result.error) {
|
|
87
|
+
return {
|
|
88
|
+
statusCode: result.result.statusCode,
|
|
89
|
+
body: JSON.stringify(result.result.body),
|
|
90
|
+
headers: result.result.headers,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
console.log(JSON.stringify(result));
|
|
95
|
+
const code = ErrorTypeHttpResponseMap[result.error.errorType];
|
|
96
|
+
return {
|
|
97
|
+
statusCode: code || 500,
|
|
98
|
+
body: JSON.stringify(result.error),
|
|
99
|
+
headers: qpqWebServerUtils.getCorsHeaders(lambdaRuntimeConfig.qpqConfig, {}, event.headers),
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// Default executor
|
|
105
|
+
export const executeAPIGatewayEvent = getAPIGatewayEventExecutor(qpqDynamicModuleLoader);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ActionProcessorList } from 'quidproquo-core';
|
|
2
|
+
|
|
3
|
+
import { QPQAWSLambdaConfig } from '../runtimeConfig/QPQAWSLambdaConfig';
|
|
4
|
+
|
|
5
|
+
export const lambdaRuntimeConfig = JSON.parse(
|
|
6
|
+
process.env.lambdaRuntimeConfig || '{}',
|
|
7
|
+
) as QPQAWSLambdaConfig;
|
|
8
|
+
|
|
9
|
+
export type ActionProcessorListResolver = (
|
|
10
|
+
lambdaRuntimeConfig: QPQAWSLambdaConfig,
|
|
11
|
+
) => ActionProcessorList;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { EventBridgeEvent, Context } from 'aws-lambda';
|
|
2
|
+
import { createRuntime, askProcessEvent } from 'quidproquo-core';
|
|
3
|
+
import { coreActionProcessor, webserverActionProcessor } from 'quidproquo-actionprocessor-node';
|
|
4
|
+
|
|
5
|
+
import { DynamicModuleLoader } from '../types/DynamicLoader';
|
|
6
|
+
import * as awsLambdaUtils from '../awsLambdaUtils';
|
|
7
|
+
import {
|
|
8
|
+
getEventBridgeEventActionProcessor,
|
|
9
|
+
getSystemActionProcessor,
|
|
10
|
+
getFileActionProcessor,
|
|
11
|
+
getConfigGetSecretActionProcessor,
|
|
12
|
+
getConfigGetParameterActionProcessor,
|
|
13
|
+
getConfigGetParametersActionProcessor,
|
|
14
|
+
} from '../getActionProcessor';
|
|
15
|
+
|
|
16
|
+
import { lambdaRuntimeConfig, ActionProcessorListResolver } from './lambdaConfig';
|
|
17
|
+
|
|
18
|
+
// @ts-ignore - Special webpack loader
|
|
19
|
+
import qpqDynamicModuleLoader from 'qpq-dynamic-loader!';
|
|
20
|
+
|
|
21
|
+
// TODO: Make this a util or something based on server time or something..
|
|
22
|
+
const getDateNow = () => new Date().toISOString();
|
|
23
|
+
|
|
24
|
+
export const getEventBridgeEventExecutor = (
|
|
25
|
+
dynamicModuleLoader: DynamicModuleLoader,
|
|
26
|
+
getCustomActionProcessors: ActionProcessorListResolver = () => ({}),
|
|
27
|
+
) => {
|
|
28
|
+
return async (event: EventBridgeEvent<string, void>, context: Context) => {
|
|
29
|
+
// Build a processor for the session and stuff
|
|
30
|
+
// Remove the non event ones
|
|
31
|
+
const storyActionProcessor = {
|
|
32
|
+
...coreActionProcessor,
|
|
33
|
+
...webserverActionProcessor,
|
|
34
|
+
|
|
35
|
+
...getEventBridgeEventActionProcessor(lambdaRuntimeConfig),
|
|
36
|
+
...getSystemActionProcessor(dynamicModuleLoader),
|
|
37
|
+
...getFileActionProcessor(lambdaRuntimeConfig),
|
|
38
|
+
...getConfigGetSecretActionProcessor(lambdaRuntimeConfig),
|
|
39
|
+
...getConfigGetParameterActionProcessor(lambdaRuntimeConfig),
|
|
40
|
+
...getConfigGetParametersActionProcessor(lambdaRuntimeConfig),
|
|
41
|
+
|
|
42
|
+
...getCustomActionProcessors(lambdaRuntimeConfig),
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const logger = async (result: any) => {};
|
|
46
|
+
|
|
47
|
+
// Run the callback
|
|
48
|
+
const resolveStory = createRuntime(
|
|
49
|
+
{},
|
|
50
|
+
storyActionProcessor,
|
|
51
|
+
getDateNow,
|
|
52
|
+
logger,
|
|
53
|
+
awsLambdaUtils.randomGuid,
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
await resolveStory(askProcessEvent, [event, context]);
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const executeEventBridgeEvent = getEventBridgeEventExecutor(qpqDynamicModuleLoader);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm';
|
|
2
|
+
|
|
3
|
+
const smClient = new SSMClient({});
|
|
4
|
+
|
|
5
|
+
export const getParameter = async (parameterName: string): Promise<string> => {
|
|
6
|
+
const response = await smClient.send(
|
|
7
|
+
new GetParameterCommand({
|
|
8
|
+
Name: parameterName,
|
|
9
|
+
}),
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
return response.Parameter?.Value || '';
|
|
13
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SSMClient, GetParametersCommand } from '@aws-sdk/client-ssm';
|
|
2
|
+
|
|
3
|
+
const smClient = new SSMClient({});
|
|
4
|
+
|
|
5
|
+
export const getParameters = async (parameterNames: string[]): Promise<string[]> => {
|
|
6
|
+
const response = await smClient.send(
|
|
7
|
+
new GetParametersCommand({
|
|
8
|
+
Names: parameterNames,
|
|
9
|
+
}),
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
const resolvedParams = response.Parameters || [];
|
|
13
|
+
|
|
14
|
+
return parameterNames.map((pn) => resolvedParams.find((rp) => rp.Name == pn)?.Value || '');
|
|
15
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DeleteObjectsCommand, DeleteObjectsCommandInput } from '@aws-sdk/client-s3';
|
|
2
|
+
|
|
3
|
+
import s3Client from './s3Client';
|
|
4
|
+
|
|
5
|
+
export const deleteFiles = async (drive: string, filepaths: string[]): Promise<string[]> => {
|
|
6
|
+
const bucketParams: DeleteObjectsCommandInput = {
|
|
7
|
+
Bucket: drive,
|
|
8
|
+
Delete: {
|
|
9
|
+
Quiet: true,
|
|
10
|
+
Objects: filepaths.map((fp) => ({ Key: fp })),
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const response = await s3Client.send(new DeleteObjectsCommand(bucketParams));
|
|
15
|
+
|
|
16
|
+
return (response.Errors || []).map((e) => e.Key || '');
|
|
17
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ListObjectsV2Command, ListObjectsV2CommandInput } from '@aws-sdk/client-s3';
|
|
2
|
+
import { filePathDelimiter } from 'quidproquo-core';
|
|
3
|
+
import s3Client from './s3Client';
|
|
4
|
+
|
|
5
|
+
export interface S3FileInfo {
|
|
6
|
+
filepath: string;
|
|
7
|
+
isDir: boolean;
|
|
8
|
+
hashMd5?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface S3FileList {
|
|
12
|
+
fileInfos: S3FileInfo[];
|
|
13
|
+
pageToken?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const listFiles = async (
|
|
17
|
+
drive: string,
|
|
18
|
+
folder: string = '',
|
|
19
|
+
maxKeys: number = 1000,
|
|
20
|
+
pageToken?: string,
|
|
21
|
+
): Promise<S3FileList> => {
|
|
22
|
+
const validatedPrefix = `${folder}${
|
|
23
|
+
folder.endsWith(filePathDelimiter) || !folder ? '' : filePathDelimiter
|
|
24
|
+
}`;
|
|
25
|
+
const bucketParams: ListObjectsV2CommandInput = {
|
|
26
|
+
Bucket: drive,
|
|
27
|
+
Delimiter: filePathDelimiter,
|
|
28
|
+
Prefix: validatedPrefix,
|
|
29
|
+
ContinuationToken: pageToken,
|
|
30
|
+
MaxKeys: maxKeys,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Declare truncated as a flag that the while loop is based on.
|
|
34
|
+
let files: S3FileInfo[] = [];
|
|
35
|
+
|
|
36
|
+
const response = await s3Client.send(new ListObjectsV2Command(bucketParams));
|
|
37
|
+
|
|
38
|
+
if (response.CommonPrefixes && !bucketParams.ContinuationToken) {
|
|
39
|
+
files = [
|
|
40
|
+
...files,
|
|
41
|
+
...response.CommonPrefixes.filter((cp) => !!cp.Prefix).map((cp) => ({
|
|
42
|
+
filepath: cp.Prefix!,
|
|
43
|
+
drive: drive,
|
|
44
|
+
isDir: true,
|
|
45
|
+
})),
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
files = [
|
|
50
|
+
...files,
|
|
51
|
+
...(response.Contents || [])
|
|
52
|
+
.filter((c) => !!c.Key && c.Key != folder)
|
|
53
|
+
.map(
|
|
54
|
+
(item): S3FileInfo => ({
|
|
55
|
+
filepath: item.Key!,
|
|
56
|
+
isDir: item.Key!.endsWith(filePathDelimiter),
|
|
57
|
+
hashMd5: item.ETag,
|
|
58
|
+
}),
|
|
59
|
+
),
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
fileInfos: files,
|
|
64
|
+
pageToken: response.NextContinuationToken,
|
|
65
|
+
};
|
|
66
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { HeadObjectCommand } from '@aws-sdk/client-s3';
|
|
2
|
+
|
|
3
|
+
import s3Client from './s3Client';
|
|
4
|
+
|
|
5
|
+
export const objectExists = async (bucketName: string, key: string): Promise<boolean> => {
|
|
6
|
+
try {
|
|
7
|
+
const response = await s3Client.send(
|
|
8
|
+
new HeadObjectCommand({
|
|
9
|
+
Key: key,
|
|
10
|
+
Bucket: bucketName,
|
|
11
|
+
}),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
return true;
|
|
15
|
+
} catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { GetObjectCommand } from '@aws-sdk/client-s3';
|
|
2
|
+
|
|
3
|
+
import s3Client from './s3Client';
|
|
4
|
+
|
|
5
|
+
export const readTextFile = async (bucketName: string, key: string): Promise<string> => {
|
|
6
|
+
const response = await s3Client.send(
|
|
7
|
+
new GetObjectCommand({
|
|
8
|
+
Key: key,
|
|
9
|
+
Bucket: bucketName,
|
|
10
|
+
}),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
return (await response.Body?.transformToString()) || '';
|
|
14
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PutObjectCommand } from '@aws-sdk/client-s3';
|
|
2
|
+
|
|
3
|
+
import s3Client from './s3Client';
|
|
4
|
+
|
|
5
|
+
export const writeTextFile = async (
|
|
6
|
+
bucketName: string,
|
|
7
|
+
key: string,
|
|
8
|
+
data: string,
|
|
9
|
+
): Promise<void> => {
|
|
10
|
+
await s3Client.send(
|
|
11
|
+
new PutObjectCommand({
|
|
12
|
+
Key: key,
|
|
13
|
+
Bucket: bucketName,
|
|
14
|
+
Body: Buffer.from(data),
|
|
15
|
+
}),
|
|
16
|
+
);
|
|
17
|
+
};
|