@xube/kit-aws-hooks-infrastructure 0.0.33 → 0.0.35

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;
@@ -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,11 +1,12 @@
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 { Table } 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
11
  name?: string;
11
12
  domainName?: IDomainName;
@@ -14,12 +15,14 @@ export interface WebhookManagementProps {
14
15
  basePath?: string;
15
16
  stage?: string;
16
17
  corsOptions?: CorsOptions;
18
+ streams: ITable[];
17
19
  }
18
20
  export declare class WebhookManagement extends Construct {
19
21
  table: Table;
20
22
  addWebhookEndpoints: NodejsFunction;
21
23
  removeWebhookEndpoint: NodejsFunction;
22
24
  getWebhookEndpoints: NodejsFunction;
25
+ handleStreams: NodejsFunction;
23
26
  webhookAPI: RestApi;
24
27
  constructor(scope: Construct, id: string, props: WebhookManagementProps);
25
28
  }
@@ -1,23 +1,28 @@
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);
@@ -32,8 +37,9 @@ class WebhookManagement extends constructs_1.Construct {
32
37
  type: aws_dynamodb_1.AttributeType.STRING,
33
38
  },
34
39
  });
40
+ const invertedIndexName = (props.name ?? `webhook-management`) + "-inverted-table";
35
41
  this.table.addGlobalSecondaryIndex({
36
- indexName: (props.name ?? `webhook-management`) + "-inverted-table",
42
+ indexName: invertedIndexName,
37
43
  partitionKey: {
38
44
  name: kit_aws_1.SORT_KEY,
39
45
  type: aws_dynamodb_1.AttributeType.STRING,
@@ -49,21 +55,21 @@ class WebhookManagement extends constructs_1.Construct {
49
55
  entry: (0, path_1.join)(__dirname, "functions", exports.ADD_WEBHOOK_ENDPOINTS_FUNCTION_NAME + ".js"),
50
56
  functionName: (props.name ?? "webhook") + "-add-hooks",
51
57
  environment: {
52
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
58
+ [kit_aws_hooks_1.WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
53
59
  },
54
60
  });
55
61
  this.removeWebhookEndpoint = new aws_lambda_nodejs_1.NodejsFunction(this, id + "-remove-hooks-function", {
56
62
  entry: (0, path_1.join)(__dirname, "functions", exports.REMOVE_WEBHOOK_ENDPOINTS_FUNCTION_NAME + ".js"),
57
63
  functionName: (props.name ?? "webhook") + "-remove-hooks",
58
64
  environment: {
59
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
65
+ [kit_aws_hooks_1.WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
60
66
  },
61
67
  });
62
68
  this.getWebhookEndpoints = new aws_lambda_nodejs_1.NodejsFunction(this, id + "-get-hooks-function", {
63
69
  entry: (0, path_1.join)(__dirname, "functions", exports.GET_WEBHOOK_ENDPOINTS_FUNCTION_NAME + ".js"),
64
70
  functionName: (props.name ?? "webhook") + "-get-hook",
65
71
  environment: {
66
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
72
+ [kit_aws_hooks_1.WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
67
73
  },
68
74
  });
69
75
  this.webhookAPI = new aws_apigateway_1.RestApi(this, id + "-api", {
@@ -96,6 +102,23 @@ class WebhookManagement extends constructs_1.Construct {
96
102
  this.webhookAPI.root.addMethod(aws_lambda_1.HttpMethod.GET, getWebhookEndpointsTarget);
97
103
  this.webhookAPI.root.addMethod(aws_lambda_1.HttpMethod.POST, addWebhookEndpointsTarget);
98
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
+ }
99
122
  }
100
123
  }
101
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.33",
3
+ "version": "0.0.35",
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.33"
20
+ "@xube/kit-build": "^0.0.35"
21
21
  },
22
22
  "dependencies": {
23
- "@xube/kit-aws": "^0.0.33",
24
- "@xube/kit-aws-hooks": "^0.0.33",
25
- "@xube/kit-aws-infrastructure": "^0.0.33",
26
- "@xube/kit-constants": "^0.0.33",
27
- "@xube/kit-log": "^0.0.33",
23
+ "@xube/kit-aws": "^0.0.35",
24
+ "@xube/kit-aws-hooks": "^0.0.35",
25
+ "@xube/kit-aws-infrastructure": "^0.0.35",
26
+ "@xube/kit-constants": "^0.0.35",
27
+ "@xube/kit-log": "^0.0.35",
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,6 +26,8 @@ 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
32
  name?: string;
26
33
  domainName?: IDomainName;
@@ -29,6 +36,7 @@ export interface WebhookManagementProps {
29
36
  basePath?: string;
30
37
  stage?: string;
31
38
  corsOptions?: CorsOptions;
39
+ streams: ITable[];
32
40
  }
33
41
 
34
42
  export class WebhookManagement extends Construct {
@@ -36,6 +44,7 @@ export class WebhookManagement extends Construct {
36
44
  addWebhookEndpoints: NodejsFunction;
37
45
  removeWebhookEndpoint: NodejsFunction;
38
46
  getWebhookEndpoints: NodejsFunction;
47
+ handleStreams: NodejsFunction;
39
48
 
40
49
  webhookAPI: RestApi;
41
50
 
@@ -54,8 +63,11 @@ export class WebhookManagement extends Construct {
54
63
  },
55
64
  });
56
65
 
66
+ const invertedIndexName: string =
67
+ (props.name ?? `webhook-management`) + "-inverted-table";
68
+
57
69
  this.table.addGlobalSecondaryIndex({
58
- indexName: (props.name ?? `webhook-management`) + "-inverted-table",
70
+ indexName: invertedIndexName,
59
71
  partitionKey: {
60
72
  name: SORT_KEY,
61
73
  type: AttributeType.STRING,
@@ -82,7 +94,7 @@ export class WebhookManagement extends Construct {
82
94
  ),
83
95
  functionName: (props.name ?? "webhook") + "-add-hooks",
84
96
  environment: {
85
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
97
+ [WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
86
98
  },
87
99
  }
88
100
  );
@@ -97,7 +109,7 @@ export class WebhookManagement extends Construct {
97
109
  ),
98
110
  functionName: (props.name ?? "webhook") + "-remove-hooks",
99
111
  environment: {
100
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
112
+ [WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
101
113
  },
102
114
  }
103
115
  );
@@ -112,7 +124,7 @@ export class WebhookManagement extends Construct {
112
124
  ),
113
125
  functionName: (props.name ?? "webhook") + "-get-hook",
114
126
  environment: {
115
- WEBHOOK_TABLE_NAME_ENV_VAR: this.table.tableName,
127
+ [WEBHOOK_TABLE_NAME_ENV_VAR]: this.table.tableName,
116
128
  },
117
129
  }
118
130
  );
@@ -160,5 +172,26 @@ export class WebhookManagement extends Construct {
160
172
  HttpMethod.DELETE,
161
173
  removeWebhookEndpointsTarget
162
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
+ }
163
196
  }
164
197
  }