quidproquo-actionprocessor-awslambda 0.0.35 → 0.0.36

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.
Files changed (89) hide show
  1. package/package.json +1 -4
  2. package/src/awsLambdaUtils.ts +22 -0
  3. package/src/getActionProcessor/core/config/getConfigGetParameterActionProcessor.ts +23 -0
  4. package/src/getActionProcessor/core/config/getConfigGetParametersActionProcessor.ts +27 -0
  5. package/src/getActionProcessor/core/config/getConfigGetSecretActionProcessor.ts +23 -0
  6. package/src/getActionProcessor/core/config/index.ts +11 -0
  7. package/src/getActionProcessor/core/event/getAPIGatewayEventActionProcessor.ts +134 -0
  8. package/src/getActionProcessor/core/event/getEventBridgeEventActionProcessor.ts +66 -0
  9. package/src/getActionProcessor/core/file/getFileDeleteActionProcessor.ts +31 -0
  10. package/src/getActionProcessor/core/file/getFileExistsActionProcessor.ts +16 -0
  11. package/src/getActionProcessor/core/file/getFileListDirectoryActionProcessor.ts +28 -0
  12. package/src/getActionProcessor/core/file/getFileReadTextContentsActionProcessor.ts +18 -0
  13. package/src/getActionProcessor/core/file/getFileWriteTextContentsActionProcessor.ts +23 -0
  14. package/src/getActionProcessor/core/file/index.ts +15 -0
  15. package/{lib/getActionProcessor/core/index.d.ts → src/getActionProcessor/core/index.ts} +0 -0
  16. package/src/getActionProcessor/core/system/getExecuteStoryActionProcessor.ts +67 -0
  17. package/src/getActionProcessor/core/system/index.ts +6 -0
  18. package/{lib/getActionProcessor/index.d.ts → src/getActionProcessor/index.ts} +0 -0
  19. package/{lib/index.d.ts → src/index.ts} +2 -0
  20. package/src/logic/parametersManager/getParameter.ts +13 -0
  21. package/src/logic/parametersManager/getParameters.ts +15 -0
  22. package/src/logic/s3/deleteFiles.ts +17 -0
  23. package/src/logic/s3/listFiles.ts +66 -0
  24. package/src/logic/s3/objectExists.ts +18 -0
  25. package/src/logic/s3/readTextFile.ts +14 -0
  26. package/{lib/logic/s3/s3Client.d.ts → src/logic/s3/s3Client.ts} +3 -1
  27. package/{lib/logic/s3/s3Utils.d.ts → src/logic/s3/s3Utils.ts} +0 -0
  28. package/src/logic/s3/writeTextFile.ts +17 -0
  29. package/src/logic/secretsManager/getSecret.ts +13 -0
  30. package/src/runtimeConfig/QPQAWSLambdaConfig.ts +15 -0
  31. package/src/runtimeConfig/qpqAwsLambdaRuntimeConfigUtils.ts +19 -0
  32. package/{lib/types/DynamicLoader.d.ts → src/types/DynamicLoader.ts} +0 -0
  33. package/tsconfig.json +8 -0
  34. package/lib/awsLambdaUtils.d.ts +0 -6
  35. package/lib/awsLambdaUtils.js +0 -19
  36. package/lib/getActionProcessor/core/config/getConfigGetParameterActionProcessor.d.ts +0 -6
  37. package/lib/getActionProcessor/core/config/getConfigGetParameterActionProcessor.js +0 -26
  38. package/lib/getActionProcessor/core/config/getConfigGetParametersActionProcessor.d.ts +0 -6
  39. package/lib/getActionProcessor/core/config/getConfigGetParametersActionProcessor.js +0 -26
  40. package/lib/getActionProcessor/core/config/getConfigGetSecretActionProcessor.d.ts +0 -6
  41. package/lib/getActionProcessor/core/config/getConfigGetSecretActionProcessor.js +0 -26
  42. package/lib/getActionProcessor/core/config/index.d.ts +0 -7
  43. package/lib/getActionProcessor/core/config/index.js +0 -9
  44. package/lib/getActionProcessor/core/event/getAPIGatewayEventActionProcessor.d.ts +0 -10
  45. package/lib/getActionProcessor/core/event/getAPIGatewayEventActionProcessor.js +0 -95
  46. package/lib/getActionProcessor/core/event/getEventBridgeEventActionProcessor.d.ts +0 -10
  47. package/lib/getActionProcessor/core/event/getEventBridgeEventActionProcessor.js +0 -51
  48. package/lib/getActionProcessor/core/file/getFileDeleteActionProcessor.d.ts +0 -6
  49. package/lib/getActionProcessor/core/file/getFileDeleteActionProcessor.js +0 -31
  50. package/lib/getActionProcessor/core/file/getFileExistsActionProcessor.d.ts +0 -6
  51. package/lib/getActionProcessor/core/file/getFileExistsActionProcessor.js +0 -23
  52. package/lib/getActionProcessor/core/file/getFileListDirectoryActionProcessor.d.ts +0 -6
  53. package/lib/getActionProcessor/core/file/getFileListDirectoryActionProcessor.js +0 -29
  54. package/lib/getActionProcessor/core/file/getFileReadTextContentsActionProcessor.d.ts +0 -6
  55. package/lib/getActionProcessor/core/file/getFileReadTextContentsActionProcessor.js +0 -23
  56. package/lib/getActionProcessor/core/file/getFileWriteTextContentsActionProcessor.d.ts +0 -6
  57. package/lib/getActionProcessor/core/file/getFileWriteTextContentsActionProcessor.js +0 -24
  58. package/lib/getActionProcessor/core/file/index.d.ts +0 -9
  59. package/lib/getActionProcessor/core/file/index.js +0 -11
  60. package/lib/getActionProcessor/core/index.js +0 -20
  61. package/lib/getActionProcessor/core/system/getExecuteStoryActionProcessor.d.ts +0 -7
  62. package/lib/getActionProcessor/core/system/getExecuteStoryActionProcessor.js +0 -45
  63. package/lib/getActionProcessor/core/system/index.d.ts +0 -5
  64. package/lib/getActionProcessor/core/system/index.js +0 -7
  65. package/lib/getActionProcessor/index.js +0 -17
  66. package/lib/index.js +0 -33
  67. package/lib/logic/parametersManager/getParameter.d.ts +0 -1
  68. package/lib/logic/parametersManager/getParameter.js +0 -22
  69. package/lib/logic/parametersManager/getParameters.d.ts +0 -1
  70. package/lib/logic/parametersManager/getParameters.js +0 -22
  71. package/lib/logic/s3/deleteFiles.d.ts +0 -1
  72. package/lib/logic/s3/deleteFiles.js +0 -29
  73. package/lib/logic/s3/listFiles.d.ts +0 -10
  74. package/lib/logic/s3/listFiles.js +0 -56
  75. package/lib/logic/s3/objectExists.d.ts +0 -1
  76. package/lib/logic/s3/objectExists.js +0 -30
  77. package/lib/logic/s3/readTextFile.d.ts +0 -1
  78. package/lib/logic/s3/readTextFile.js +0 -26
  79. package/lib/logic/s3/s3Client.js +0 -5
  80. package/lib/logic/s3/s3Utils.js +0 -21
  81. package/lib/logic/s3/writeTextFile.d.ts +0 -1
  82. package/lib/logic/s3/writeTextFile.js +0 -25
  83. package/lib/logic/secretsManager/getSecret.d.ts +0 -1
  84. package/lib/logic/secretsManager/getSecret.js +0 -21
  85. package/lib/runtimeConfig/QPQAWSLambdaConfig.d.ts +0 -12
  86. package/lib/runtimeConfig/QPQAWSLambdaConfig.js +0 -2
  87. package/lib/runtimeConfig/qpqAwsLambdaRuntimeConfigUtils.d.ts +0 -4
  88. package/lib/runtimeConfig/qpqAwsLambdaRuntimeConfigUtils.js +0 -15
  89. package/lib/types/DynamicLoader.js +0 -2
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "quidproquo-actionprocessor-awslambda",
3
- "version": "0.0.35",
3
+ "version": "0.0.36",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.js",
7
- "files": [
8
- "lib/**/*"
9
- ],
10
7
  "scripts": {
11
8
  "test": "echo \"Error: no test specified\" && exit 1",
12
9
  "clean": "npx rimraf lib",
@@ -0,0 +1,22 @@
1
+ import { randomUUID } from 'crypto';
2
+ import { match } from 'node-match-path';
3
+
4
+ export const randomGuid = () => {
5
+ return randomUUID();
6
+ };
7
+
8
+ export interface UrlMatch {
9
+ didMatch: boolean;
10
+ params: Record<string, string> | null;
11
+ }
12
+
13
+ export const matchUrl = (path: string, url: string): UrlMatch => {
14
+ // /attempt/{attemptUuid}/result/{test} => /attempt/:attemptUuid/result/:test
15
+ const modifiedPath = path.replaceAll(/{(.+?)}/g, (m, g) => `:${g}`);
16
+
17
+ const matchResult = match(modifiedPath, url);
18
+ return {
19
+ didMatch: matchResult.matches,
20
+ params: matchResult.params,
21
+ };
22
+ };
@@ -0,0 +1,23 @@
1
+ import { ConfigActionType, ConfigGetParameterActionProcessor, actionResult } from 'quidproquo-core';
2
+
3
+ import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
4
+ import { resolveParameterKey } from '../../../runtimeConfig/qpqAwsLambdaRuntimeConfigUtils';
5
+
6
+ import { getParameter } from '../../../logic/parametersManager/getParameter';
7
+
8
+ const getProcessConfigGetParameter = (
9
+ runtimeConfig: QPQAWSLambdaConfig,
10
+ ): ConfigGetParameterActionProcessor => {
11
+ return async ({ parameterName }) => {
12
+ const awsParameterKey = resolveParameterKey(parameterName, runtimeConfig);
13
+ const parameterValue = await getParameter(awsParameterKey);
14
+
15
+ return actionResult(parameterValue);
16
+ };
17
+ };
18
+
19
+ export default (runtimeConfig: QPQAWSLambdaConfig) => {
20
+ return {
21
+ [ConfigActionType.GetParameter]: getProcessConfigGetParameter(runtimeConfig),
22
+ };
23
+ };
@@ -0,0 +1,27 @@
1
+ import {
2
+ ConfigActionType,
3
+ ConfigGetParametersActionProcessor,
4
+ actionResult,
5
+ } from 'quidproquo-core';
6
+
7
+ import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
8
+ import { resolveParameterKey } from '../../../runtimeConfig/qpqAwsLambdaRuntimeConfigUtils';
9
+
10
+ import { getParameters } from '../../../logic/parametersManager/getParameters';
11
+
12
+ const getProcessConfigGetParameters = (
13
+ runtimeConfig: QPQAWSLambdaConfig,
14
+ ): ConfigGetParametersActionProcessor => {
15
+ return async ({ parameterNames }) => {
16
+ const awsParameterKeys = parameterNames.map((pn) => resolveParameterKey(pn, runtimeConfig));
17
+ const parameterValues = await getParameters(awsParameterKeys);
18
+
19
+ return actionResult(parameterValues);
20
+ };
21
+ };
22
+
23
+ export default (runtimeConfig: QPQAWSLambdaConfig) => {
24
+ return {
25
+ [ConfigActionType.GetParameters]: getProcessConfigGetParameters(runtimeConfig),
26
+ };
27
+ };
@@ -0,0 +1,23 @@
1
+ import { ConfigActionType, ConfigGetSecretActionProcessor, actionResult } from 'quidproquo-core';
2
+
3
+ import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
4
+ import { resolveSecretKey } from '../../../runtimeConfig/qpqAwsLambdaRuntimeConfigUtils';
5
+
6
+ import { getSecret } from '../../../logic/secretsManager/getSecret';
7
+
8
+ const getProcessConfigActionType = (
9
+ runtimeConfig: QPQAWSLambdaConfig,
10
+ ): ConfigGetSecretActionProcessor => {
11
+ return async ({ secretName }) => {
12
+ const awsSecretKey = resolveSecretKey(secretName, runtimeConfig);
13
+ const secretValue = await getSecret(awsSecretKey);
14
+
15
+ return actionResult(secretValue);
16
+ };
17
+ };
18
+
19
+ export default (runtimeConfig: QPQAWSLambdaConfig) => {
20
+ return {
21
+ [ConfigActionType.GetSecret]: getProcessConfigActionType(runtimeConfig),
22
+ };
23
+ };
@@ -0,0 +1,11 @@
1
+ import { QPQAWSLambdaConfig } from '../../../runtimeConfig/QPQAWSLambdaConfig';
2
+
3
+ import getConfigGetParameterActionProcessor from './getConfigGetParameterActionProcessor';
4
+ import getConfigGetParametersActionProcessor from './getConfigGetParametersActionProcessor';
5
+ import getConfigGetSecretActionProcessor from './getConfigGetSecretActionProcessor';
6
+
7
+ export default (runtimeConfig: QPQAWSLambdaConfig) => ({
8
+ ...getConfigGetParameterActionProcessor(runtimeConfig),
9
+ ...getConfigGetParametersActionProcessor(runtimeConfig),
10
+ ...getConfigGetSecretActionProcessor(runtimeConfig),
11
+ });
@@ -0,0 +1,134 @@
1
+ import {
2
+ EventActionType,
3
+ QPQConfig,
4
+ qpqCoreUtils,
5
+ MatchStoryResult,
6
+ EventMatchStoryActionProcessor,
7
+ EventTransformEventParamsActionProcessor,
8
+ EventTransformResponseResultActionProcessor,
9
+ EventAutoRespondActionProcessor,
10
+ actionResult,
11
+ actionResultError,
12
+ ErrorTypeEnum,
13
+ } from 'quidproquo-core';
14
+
15
+ import {
16
+ RouteQPQWebServerConfigSetting,
17
+ HTTPEventParams,
18
+ qpqWebServerUtils,
19
+ } from 'quidproquo-webserver';
20
+
21
+ import { APIGatewayEvent, Context, APIGatewayProxyResult } from 'aws-lambda';
22
+
23
+ import { matchUrl } from '../../../awsLambdaUtils';
24
+
25
+ const getProcessTransformEventParams = (
26
+ appName: string,
27
+ ): EventTransformEventParamsActionProcessor<[APIGatewayEvent, Context], HTTPEventParams<any>> => {
28
+ return async ({ eventParams: [apiGatewayEvent, context] }) => {
29
+ const path = (apiGatewayEvent.path || '').replace(new RegExp(`^(\/${appName})/`), '/');
30
+ console.log('getProcessTransformEventParams', JSON.stringify(apiGatewayEvent));
31
+
32
+ return actionResult({
33
+ path,
34
+ query: {
35
+ ...(apiGatewayEvent.multiValueQueryStringParameters || {}),
36
+ ...(apiGatewayEvent.queryStringParameters || {}),
37
+ } as { [key: string]: undefined | string | string[] },
38
+ body: apiGatewayEvent.body ? JSON.parse(apiGatewayEvent.body) : undefined,
39
+ headers: apiGatewayEvent.headers,
40
+ method: apiGatewayEvent.httpMethod as 'GET' | 'POST',
41
+ correlation: context.awsRequestId,
42
+ sourceIp: apiGatewayEvent.requestContext.identity.sourceIp,
43
+ });
44
+ };
45
+ };
46
+
47
+ const getProcessTransformResponseResult = (
48
+ configs: QPQConfig,
49
+ ): EventTransformResponseResultActionProcessor<APIGatewayProxyResult> => {
50
+ // We might need to JSON.stringify the body.
51
+ return async (payload) => {
52
+ // Validate response
53
+ // if !valid actionResultError
54
+
55
+ console.log('getProcessTransformResponseResult', JSON.stringify(payload));
56
+
57
+ return actionResult<APIGatewayProxyResult>({
58
+ statusCode: payload.response.result.statusCode,
59
+ body: payload.response.result.body,
60
+ headers: {
61
+ 'Content-Type': 'application/json',
62
+ ...qpqWebServerUtils.getCorsHeaders(configs, {}, payload.transformedEventParams.headers),
63
+ ...(payload.response.headers || {}),
64
+ },
65
+ });
66
+ };
67
+ };
68
+
69
+ const getProcessAutoRespond = (
70
+ configs: QPQConfig,
71
+ ): EventAutoRespondActionProcessor<HTTPEventParams<any>> => {
72
+ return async (payload) => {
73
+ if (payload.transformedEventParams.method === 'OPTIONS') {
74
+ return actionResult({
75
+ result: {
76
+ statusCode: 200,
77
+ headers: qpqWebServerUtils.getCorsHeaders(
78
+ configs,
79
+ {},
80
+ payload.transformedEventParams.headers,
81
+ ),
82
+ },
83
+ });
84
+ }
85
+
86
+ return actionResult(null);
87
+ };
88
+ };
89
+
90
+ const getProcessMatchStory = (
91
+ routes: RouteQPQWebServerConfigSetting[],
92
+ ): EventMatchStoryActionProcessor<HTTPEventParams<any>> => {
93
+ return async (payload) => {
94
+ // Sort the routes by string length
95
+ // Note: We may need to filter variable routes out {} as the variables are length independent
96
+ const sortedRoutes = routes
97
+ .filter((r: any) => r.method === payload.transformedEventParams.method)
98
+ .sort((a: any, b: any) => {
99
+ if (a.path.length < b.path.length) return -1;
100
+ if (a.path.length > b.path.length) return 1;
101
+ return 0;
102
+ });
103
+
104
+ // Find the most relevant match
105
+ const matchedRoute = sortedRoutes
106
+ .map((r) => ({
107
+ match: matchUrl(r.path, payload.transformedEventParams.path),
108
+ route: r,
109
+ }))
110
+ .find((m) => m.match.didMatch);
111
+
112
+ if (!matchedRoute) {
113
+ return actionResultError(ErrorTypeEnum.NotFound, 'route not found');
114
+ }
115
+
116
+ return actionResult<MatchStoryResult>({
117
+ src: matchedRoute.route.src,
118
+ runtime: matchedRoute.route.runtime,
119
+ options: matchedRoute.match.params || {},
120
+ });
121
+ };
122
+ };
123
+
124
+ export default (config: QPQConfig) => {
125
+ const routes = qpqWebServerUtils.getAllRoutes(config);
126
+ const appName = qpqCoreUtils.getAppName(config);
127
+
128
+ return {
129
+ [EventActionType.TransformEventParams]: getProcessTransformEventParams(appName),
130
+ [EventActionType.TransformResponseResult]: getProcessTransformResponseResult(config),
131
+ [EventActionType.AutoRespond]: getProcessAutoRespond(config),
132
+ [EventActionType.MatchStory]: getProcessMatchStory(routes),
133
+ };
134
+ };
@@ -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,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
+ });
@@ -1,4 +1,6 @@
1
1
  export * from './getActionProcessor';
2
2
  export * as awsLambdaUtils from './awsLambdaUtils';
3
+
3
4
  export * from './runtimeConfig/QPQAWSLambdaConfig';
5
+
4
6
  export * from './types/DynamicLoader';
@@ -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
+ };