@xube/kit-aws-hooks-infrastructure 0.0.32 → 0.0.34

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.
@@ -0,0 +1,4 @@
1
+ import { Callback, Context, DynamoDBStreamEvent } from "aws-lambda";
2
+ export declare const tableName: string;
3
+ export declare const invertedIndexName: string;
4
+ export declare const handler: (event: DynamoDBStreamEvent, context: Context, callback: Callback) => Promise<void>;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handler = exports.invertedIndexName = exports.tableName = void 0;
4
+ const kit_aws_1 = require("@xube/kit-aws");
5
+ const kit_aws_hooks_1 = require("@xube/kit-aws-hooks");
6
+ exports.tableName = process.env.WEBHOOK_TABLE_NAME || "";
7
+ exports.invertedIndexName = process.env.WEBHOOK_TABLE_INVERTED_INDEX_NAME || "";
8
+ const handler = async (event, context, callback) => {
9
+ if (!exports.tableName) {
10
+ console.log("No table name found");
11
+ return;
12
+ }
13
+ if (!exports.invertedIndexName) {
14
+ console.log("No inverted index name found");
15
+ return;
16
+ }
17
+ const tableItems = [];
18
+ for (const record of event.Records) {
19
+ const newItem = record.dynamodb?.NewImage;
20
+ if (!newItem) {
21
+ console.log("No new item found");
22
+ continue;
23
+ }
24
+ const tableItem = (0, kit_aws_1.unmarshallItem)(newItem);
25
+ if (!(0, kit_aws_1.isTableItem)(tableItem)) {
26
+ console.log("Item is not a valid table item");
27
+ continue;
28
+ }
29
+ }
30
+ const sendDataResponse = await (0, kit_aws_hooks_1.sendDataToEndpoints)(exports.tableName, exports.invertedIndexName, tableItems);
31
+ if (sendDataResponse.hasFailed()) {
32
+ console.log(`Failed to send data to endpoints: ${sendDataResponse.error}`);
33
+ }
34
+ return callback(`Failed to send data to endpoints`, sendDataResponse.data);
35
+ };
36
+ exports.handler = handler;
@@ -1,13 +1,13 @@
1
1
  import { CorsOptions, IAuthorizer, IDomainName, RestApi } from "aws-cdk-lib/aws-apigateway";
2
2
  import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
3
- import { ITable } from "aws-cdk-lib/aws-dynamodb";
3
+ import { ITable, Table } from "aws-cdk-lib/aws-dynamodb";
4
4
  import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
5
5
  import { Construct } from "constructs";
6
6
  export declare const ADD_WEBHOOK_ENDPOINTS_FUNCTION_NAME = "add-webhook-endpoints";
7
7
  export declare const REMOVE_WEBHOOK_ENDPOINTS_FUNCTION_NAME = "remove-webhook-endpoints";
8
8
  export declare const GET_WEBHOOK_ENDPOINTS_FUNCTION_NAME = "get-webhook-endpoints";
9
+ export declare const HANDLE_STREAMS_FUNCTION_NAME = "handle-streams";
9
10
  export interface WebhookManagementProps {
10
- table?: ITable;
11
11
  name?: string;
12
12
  domainName?: IDomainName;
13
13
  certificate?: ICertificate;
@@ -15,12 +15,14 @@ export interface WebhookManagementProps {
15
15
  basePath?: string;
16
16
  stage?: string;
17
17
  corsOptions?: CorsOptions;
18
+ streams: ITable[];
18
19
  }
19
20
  export declare class WebhookManagement extends Construct {
20
- table: ITable;
21
+ table: Table;
21
22
  addWebhookEndpoints: NodejsFunction;
22
23
  removeWebhookEndpoint: NodejsFunction;
23
24
  getWebhookEndpoints: NodejsFunction;
25
+ handleStreams: NodejsFunction;
24
26
  webhookAPI: RestApi;
25
27
  constructor(scope: Construct, id: string, props: WebhookManagementProps);
26
28
  }
@@ -1,60 +1,75 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebhookManagement = exports.GET_WEBHOOK_ENDPOINTS_FUNCTION_NAME = exports.REMOVE_WEBHOOK_ENDPOINTS_FUNCTION_NAME = exports.ADD_WEBHOOK_ENDPOINTS_FUNCTION_NAME = void 0;
3
+ exports.WebhookManagement = exports.HANDLE_STREAMS_FUNCTION_NAME = exports.GET_WEBHOOK_ENDPOINTS_FUNCTION_NAME = exports.REMOVE_WEBHOOK_ENDPOINTS_FUNCTION_NAME = exports.ADD_WEBHOOK_ENDPOINTS_FUNCTION_NAME = void 0;
4
4
  const kit_aws_1 = require("@xube/kit-aws");
5
5
  const kit_aws_2 = require("@xube/kit-aws");
6
+ const kit_aws_hooks_1 = require("@xube/kit-aws-hooks");
6
7
  const kit_log_1 = require("@xube/kit-log");
8
+ const aws_cdk_lib_1 = require("aws-cdk-lib");
7
9
  const aws_apigateway_1 = require("aws-cdk-lib/aws-apigateway");
8
10
  const aws_dynamodb_1 = require("aws-cdk-lib/aws-dynamodb");
9
11
  const aws_lambda_1 = require("aws-cdk-lib/aws-lambda");
12
+ const aws_lambda_event_sources_1 = require("aws-cdk-lib/aws-lambda-event-sources");
10
13
  const aws_lambda_nodejs_1 = require("aws-cdk-lib/aws-lambda-nodejs");
11
14
  const constructs_1 = require("constructs");
12
15
  const path_1 = require("path");
13
16
  exports.ADD_WEBHOOK_ENDPOINTS_FUNCTION_NAME = "add-webhook-endpoints";
14
17
  exports.REMOVE_WEBHOOK_ENDPOINTS_FUNCTION_NAME = "remove-webhook-endpoints";
15
18
  exports.GET_WEBHOOK_ENDPOINTS_FUNCTION_NAME = "get-webhook-endpoints";
19
+ exports.HANDLE_STREAMS_FUNCTION_NAME = "handle-streams";
16
20
  class WebhookManagement extends constructs_1.Construct {
17
21
  table;
18
22
  addWebhookEndpoints;
19
23
  removeWebhookEndpoint;
20
24
  getWebhookEndpoints;
25
+ handleStreams;
21
26
  webhookAPI;
22
27
  constructor(scope, id, props) {
23
28
  super(scope, id);
24
- this.table =
25
- props.table ??
26
- new aws_dynamodb_1.Table(scope, id + "-table", {
27
- tableName: (props.name ?? `webhook-management`) + "-table",
28
- partitionKey: {
29
- name: kit_aws_2.PARTITION_KEY,
30
- type: aws_dynamodb_1.AttributeType.STRING,
31
- },
32
- sortKey: {
33
- name: kit_aws_1.SORT_KEY,
34
- type: aws_dynamodb_1.AttributeType.STRING,
35
- },
36
- });
29
+ this.table = new aws_dynamodb_1.Table(scope, id + "-table", {
30
+ tableName: (props.name ?? `webhook-management`) + "-table",
31
+ partitionKey: {
32
+ name: kit_aws_2.PARTITION_KEY,
33
+ type: aws_dynamodb_1.AttributeType.STRING,
34
+ },
35
+ sortKey: {
36
+ name: kit_aws_1.SORT_KEY,
37
+ type: aws_dynamodb_1.AttributeType.STRING,
38
+ },
39
+ });
40
+ const invertedIndexName = (props.name ?? `webhook-management`) + "-inverted-table";
41
+ this.table.addGlobalSecondaryIndex({
42
+ indexName: invertedIndexName,
43
+ partitionKey: {
44
+ name: kit_aws_1.SORT_KEY,
45
+ type: aws_dynamodb_1.AttributeType.STRING,
46
+ },
47
+ sortKey: {
48
+ name: kit_aws_2.PARTITION_KEY,
49
+ type: aws_dynamodb_1.AttributeType.STRING,
50
+ },
51
+ });
37
52
  kit_log_1.XubeLog.getInstance().log(__dirname);
38
53
  kit_log_1.XubeLog.getInstance().log((0, path_1.join)(__dirname, "functions", exports.ADD_WEBHOOK_ENDPOINTS_FUNCTION_NAME + ".ts"));
39
54
  this.addWebhookEndpoints = new aws_lambda_nodejs_1.NodejsFunction(this, id + "-add-hooks-function", {
40
55
  entry: (0, path_1.join)(__dirname, "functions", exports.ADD_WEBHOOK_ENDPOINTS_FUNCTION_NAME + ".js"),
41
56
  functionName: (props.name ?? "webhook") + "-add-hooks",
42
57
  environment: {
43
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
58
+ [kit_aws_hooks_1.WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
44
59
  },
45
60
  });
46
61
  this.removeWebhookEndpoint = new aws_lambda_nodejs_1.NodejsFunction(this, id + "-remove-hooks-function", {
47
62
  entry: (0, path_1.join)(__dirname, "functions", exports.REMOVE_WEBHOOK_ENDPOINTS_FUNCTION_NAME + ".js"),
48
63
  functionName: (props.name ?? "webhook") + "-remove-hooks",
49
64
  environment: {
50
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
65
+ [kit_aws_hooks_1.WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
51
66
  },
52
67
  });
53
68
  this.getWebhookEndpoints = new aws_lambda_nodejs_1.NodejsFunction(this, id + "-get-hooks-function", {
54
69
  entry: (0, path_1.join)(__dirname, "functions", exports.GET_WEBHOOK_ENDPOINTS_FUNCTION_NAME + ".js"),
55
70
  functionName: (props.name ?? "webhook") + "-get-hook",
56
71
  environment: {
57
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
72
+ [kit_aws_hooks_1.WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
58
73
  },
59
74
  });
60
75
  this.webhookAPI = new aws_apigateway_1.RestApi(this, id + "-api", {
@@ -87,6 +102,23 @@ class WebhookManagement extends constructs_1.Construct {
87
102
  this.webhookAPI.root.addMethod(aws_lambda_1.HttpMethod.GET, getWebhookEndpointsTarget);
88
103
  this.webhookAPI.root.addMethod(aws_lambda_1.HttpMethod.POST, addWebhookEndpointsTarget);
89
104
  this.webhookAPI.root.addMethod(aws_lambda_1.HttpMethod.DELETE, removeWebhookEndpointsTarget);
105
+ this.handleStreams = new aws_lambda_nodejs_1.NodejsFunction(this, id + "-handle-streams", {
106
+ entry: (0, path_1.join)(__dirname, "functions", exports.HANDLE_STREAMS_FUNCTION_NAME + ".js"),
107
+ functionName: (props.name ?? "webhook") + "-handle-streams",
108
+ environment: {
109
+ [kit_aws_hooks_1.WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
110
+ [kit_aws_hooks_1.WEBHOOK_TABLE_INVERTED_INDEX_NAME_ENV_VAR]: invertedIndexName,
111
+ },
112
+ });
113
+ for (const stream of props.streams) {
114
+ this.handleStreams.addEventSource(new aws_lambda_event_sources_1.DynamoEventSource(stream, {
115
+ startingPosition: aws_lambda_1.StartingPosition.TRIM_HORIZON,
116
+ batchSize: 50,
117
+ bisectBatchOnError: true,
118
+ maxBatchingWindow: aws_cdk_lib_1.Duration.seconds(1),
119
+ retryAttempts: 10,
120
+ }));
121
+ }
90
122
  }
91
123
  }
92
124
  exports.WebhookManagement = WebhookManagement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xube/kit-aws-hooks-infrastructure",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -17,14 +17,14 @@
17
17
  },
18
18
  "homepage": "https://github.com/XubeLtd/dev-kit#readme",
19
19
  "devDependencies": {
20
- "@xube/kit-build": "^0.0.32"
20
+ "@xube/kit-build": "^0.0.34"
21
21
  },
22
22
  "dependencies": {
23
- "@xube/kit-aws": "^0.0.32",
24
- "@xube/kit-aws-hooks": "^0.0.32",
25
- "@xube/kit-aws-infrastructure": "^0.0.32",
26
- "@xube/kit-constants": "^0.0.32",
27
- "@xube/kit-log": "^0.0.32",
23
+ "@xube/kit-aws": "^0.0.34",
24
+ "@xube/kit-aws-hooks": "^0.0.34",
25
+ "@xube/kit-aws-infrastructure": "^0.0.34",
26
+ "@xube/kit-constants": "^0.0.34",
27
+ "@xube/kit-log": "^0.0.34",
28
28
  "aws-cdk-lib": "^2.100.0",
29
29
  "aws-lambda": "^1.0.7",
30
30
  "constructs": "^10.3.0"
@@ -0,0 +1,54 @@
1
+ import { isTableItem, unmarshallItem } from "@xube/kit-aws";
2
+ import { sendDataToEndpoints } from "@xube/kit-aws-hooks";
3
+ import { Callback, Context, DynamoDBStreamEvent } from "aws-lambda";
4
+ import { TableItem } from "@xube/kit-aws";
5
+ import { XubeResponse } from "@xube/kit-request";
6
+
7
+ export const tableName = process.env.WEBHOOK_TABLE_NAME || "";
8
+ export const invertedIndexName =
9
+ process.env.WEBHOOK_TABLE_INVERTED_INDEX_NAME || "";
10
+
11
+ export const handler = async (
12
+ event: DynamoDBStreamEvent,
13
+ context: Context,
14
+ callback: Callback
15
+ ) => {
16
+ if (!tableName) {
17
+ console.log("No table name found");
18
+ return;
19
+ }
20
+
21
+ if (!invertedIndexName) {
22
+ console.log("No inverted index name found");
23
+ return;
24
+ }
25
+
26
+ const tableItems: TableItem[] = [];
27
+
28
+ for (const record of event.Records) {
29
+ const newItem = record.dynamodb?.NewImage;
30
+
31
+ if (!newItem) {
32
+ console.log("No new item found");
33
+ continue;
34
+ }
35
+
36
+ const tableItem = unmarshallItem(newItem);
37
+
38
+ if (!isTableItem(tableItem)) {
39
+ console.log("Item is not a valid table item");
40
+ continue;
41
+ }
42
+ }
43
+ const sendDataResponse: XubeResponse<boolean> = await sendDataToEndpoints(
44
+ tableName,
45
+ invertedIndexName,
46
+ tableItems
47
+ );
48
+
49
+ if (sendDataResponse.hasFailed()) {
50
+ console.log(`Failed to send data to endpoints: ${sendDataResponse.error}`);
51
+ }
52
+
53
+ return callback(`Failed to send data to endpoints`, sendDataResponse.data);
54
+ };
@@ -1,7 +1,11 @@
1
1
  import { SORT_KEY } from "@xube/kit-aws";
2
2
  import { PARTITION_KEY } from "@xube/kit-aws";
3
- import { WEBHOOK_TABLE_NAME_ENV_VAR } from "@xube/kit-aws-hooks";
3
+ import {
4
+ WEBHOOK_TABLE_INVERTED_INDEX_NAME_ENV_VAR,
5
+ WEBHOOK_TABLE_NAME_ENV_VAR,
6
+ } from "@xube/kit-aws-hooks";
4
7
  import { XubeLog } from "@xube/kit-log";
8
+ import { Duration } from "aws-cdk-lib";
5
9
  import {
6
10
  CorsOptions,
7
11
  IAuthorizer,
@@ -11,7 +15,8 @@ import {
11
15
  } from "aws-cdk-lib/aws-apigateway";
12
16
  import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
13
17
  import { AttributeType, ITable, Table } from "aws-cdk-lib/aws-dynamodb";
14
- import { HttpMethod } from "aws-cdk-lib/aws-lambda";
18
+ import { HttpMethod, StartingPosition } from "aws-cdk-lib/aws-lambda";
19
+ import { DynamoEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
15
20
  import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
16
21
  import { Construct } from "constructs";
17
22
  import { join } from "path";
@@ -21,8 +26,9 @@ export const REMOVE_WEBHOOK_ENDPOINTS_FUNCTION_NAME =
21
26
  "remove-webhook-endpoints";
22
27
  export const GET_WEBHOOK_ENDPOINTS_FUNCTION_NAME = "get-webhook-endpoints";
23
28
 
29
+ export const HANDLE_STREAMS_FUNCTION_NAME = "handle-streams";
30
+
24
31
  export interface WebhookManagementProps {
25
- table?: ITable;
26
32
  name?: string;
27
33
  domainName?: IDomainName;
28
34
  certificate?: ICertificate;
@@ -30,32 +36,47 @@ export interface WebhookManagementProps {
30
36
  basePath?: string;
31
37
  stage?: string;
32
38
  corsOptions?: CorsOptions;
39
+ streams: ITable[];
33
40
  }
34
41
 
35
42
  export class WebhookManagement extends Construct {
36
- table: ITable;
43
+ table: Table;
37
44
  addWebhookEndpoints: NodejsFunction;
38
45
  removeWebhookEndpoint: NodejsFunction;
39
46
  getWebhookEndpoints: NodejsFunction;
47
+ handleStreams: NodejsFunction;
40
48
 
41
49
  webhookAPI: RestApi;
42
50
 
43
51
  constructor(scope: Construct, id: string, props: WebhookManagementProps) {
44
52
  super(scope, id);
45
53
 
46
- this.table =
47
- props.table ??
48
- new Table(scope, id + "-table", {
49
- tableName: (props.name ?? `webhook-management`) + "-table",
50
- partitionKey: {
51
- name: PARTITION_KEY,
52
- type: AttributeType.STRING,
53
- },
54
- sortKey: {
55
- name: SORT_KEY,
56
- type: AttributeType.STRING,
57
- },
58
- });
54
+ this.table = new Table(scope, id + "-table", {
55
+ tableName: (props.name ?? `webhook-management`) + "-table",
56
+ partitionKey: {
57
+ name: PARTITION_KEY,
58
+ type: AttributeType.STRING,
59
+ },
60
+ sortKey: {
61
+ name: SORT_KEY,
62
+ type: AttributeType.STRING,
63
+ },
64
+ });
65
+
66
+ const invertedIndexName: string =
67
+ (props.name ?? `webhook-management`) + "-inverted-table";
68
+
69
+ this.table.addGlobalSecondaryIndex({
70
+ indexName: invertedIndexName,
71
+ partitionKey: {
72
+ name: SORT_KEY,
73
+ type: AttributeType.STRING,
74
+ },
75
+ sortKey: {
76
+ name: PARTITION_KEY,
77
+ type: AttributeType.STRING,
78
+ },
79
+ });
59
80
 
60
81
  XubeLog.getInstance().log(__dirname);
61
82
  XubeLog.getInstance().log(
@@ -73,7 +94,7 @@ export class WebhookManagement extends Construct {
73
94
  ),
74
95
  functionName: (props.name ?? "webhook") + "-add-hooks",
75
96
  environment: {
76
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
97
+ [WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
77
98
  },
78
99
  }
79
100
  );
@@ -88,7 +109,7 @@ export class WebhookManagement extends Construct {
88
109
  ),
89
110
  functionName: (props.name ?? "webhook") + "-remove-hooks",
90
111
  environment: {
91
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
112
+ [WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
92
113
  },
93
114
  }
94
115
  );
@@ -103,7 +124,7 @@ export class WebhookManagement extends Construct {
103
124
  ),
104
125
  functionName: (props.name ?? "webhook") + "-get-hook",
105
126
  environment: {
106
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
127
+ [WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
107
128
  },
108
129
  }
109
130
  );
@@ -151,5 +172,26 @@ export class WebhookManagement extends Construct {
151
172
  HttpMethod.DELETE,
152
173
  removeWebhookEndpointsTarget
153
174
  );
175
+
176
+ this.handleStreams = new NodejsFunction(this, id + "-handle-streams", {
177
+ entry: join(__dirname, "functions", HANDLE_STREAMS_FUNCTION_NAME + ".js"),
178
+ functionName: (props.name ?? "webhook") + "-handle-streams",
179
+ environment: {
180
+ [WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
181
+ [WEBHOOK_TABLE_INVERTED_INDEX_NAME_ENV_VAR]: invertedIndexName,
182
+ },
183
+ });
184
+
185
+ for (const stream of props.streams) {
186
+ this.handleStreams.addEventSource(
187
+ new DynamoEventSource(stream, {
188
+ startingPosition: StartingPosition.TRIM_HORIZON,
189
+ batchSize: 50,
190
+ bisectBatchOnError: true,
191
+ maxBatchingWindow: Duration.seconds(1),
192
+ retryAttempts: 10,
193
+ })
194
+ );
195
+ }
154
196
  }
155
197
  }