@aspan-corporation/ac-shared 0.0.1

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/LICENSE.md ADDED
File without changes
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Aspan Corporation code sharing
@@ -0,0 +1,9 @@
1
+ import { Logger } from "@aws-lambda-powertools/logger";
2
+ import { CreateTableInput, DeleteTableInput, DescribeTableCommandOutput } from "@aws-sdk/client-dynamodb";
3
+ import { GetCommandInput, GetCommandOutput, QueryCommandInput, QueryCommandOutput, UpdateCommandInput, UpdateCommandOutput, PutCommandOutput, PutCommandInput } from "@aws-sdk/lib-dynamodb";
4
+ export declare const deleteDynamoDBTable: (deleteTableInput: DeleteTableInput, logger: Logger) => Promise<DescribeTableCommandOutput>;
5
+ export declare const createDynamoDBTable: (createTableInput: CreateTableInput, logger: Logger) => Promise<DescribeTableCommandOutput>;
6
+ export declare const getCommand: (getCommandInput: GetCommandInput, logger: Logger) => Promise<GetCommandOutput>;
7
+ export declare const updateCommand: (updateCommandInput: UpdateCommandInput, logger: Logger) => Promise<UpdateCommandOutput>;
8
+ export declare const queryCommand: (queryCommandInput: QueryCommandInput, logger: Logger) => Promise<QueryCommandOutput>;
9
+ export declare const putCommand: (putCommandInput: PutCommandInput, logger: Logger) => Promise<PutCommandOutput>;
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './plumbingMiddleware.js';
2
+ export * from './sharedUtils.js';
3
+ export * from './s3.js';
4
+ export * from './dynamoDB.js';
package/lib/index.js ADDED
@@ -0,0 +1,256 @@
1
+ // src/plumbingMiddleware.ts
2
+ import { Logger } from "@aws-lambda-powertools/logger";
3
+ import { Metrics } from "@aws-lambda-powertools/metrics";
4
+ import { Tracer } from "@aws-lambda-powertools/tracer";
5
+ import assert from "node:assert";
6
+ var plumbingMiddleware = () => {
7
+ assert(
8
+ process.env.POWERTOOLS_METRICS_NAMESPACE,
9
+ "POWERTOOLS_METRICS_NAMESPACE is not configured"
10
+ );
11
+ assert(
12
+ process.env.POWERTOOLS_SERVICE_NAME,
13
+ "POWERTOOLS_SERVICE_NAME is not configured"
14
+ );
15
+ const metrics = new Metrics();
16
+ const logger = new Logger();
17
+ const tracer = new Tracer();
18
+ let segment;
19
+ let subsegment;
20
+ let handlerSegment;
21
+ const before = async ({ context, event }) => {
22
+ logger.appendKeys({
23
+ awsRequestId: context.awsRequestId
24
+ });
25
+ const localLogger = logger.createChild();
26
+ localLogger.appendKeys({ function: "plumbingMiddleware.before" });
27
+ localLogger.info("incoming", { event });
28
+ segment = tracer.getSegment();
29
+ if (!segment) {
30
+ localLogger.error("Failed to get segment");
31
+ return;
32
+ }
33
+ metrics.captureColdStartMetric();
34
+ handlerSegment = segment.addNewSubsegment(`## ${process.env._HANDLER}`);
35
+ tracer.setSegment(handlerSegment);
36
+ tracer.annotateColdStart();
37
+ tracer.addServiceNameAnnotation();
38
+ tracer.putAnnotation("awsRequestId", context.awsRequestId);
39
+ subsegment = handlerSegment.addNewSubsegment("### MySubSegment");
40
+ tracer.setSegment(subsegment);
41
+ context.clientContext = {
42
+ client: context.clientContext?.client || {
43
+ appPackageName: "",
44
+ appTitle: "",
45
+ appVersionCode: "",
46
+ appVersionName: "",
47
+ installationId: ""
48
+ },
49
+ env: context.clientContext?.env || {
50
+ platform: "",
51
+ platformVersion: "",
52
+ make: "",
53
+ model: "",
54
+ locale: ""
55
+ },
56
+ Custom: { tracer, logger, metrics }
57
+ };
58
+ };
59
+ const after = async () => {
60
+ const localLogger = logger.createChild();
61
+ localLogger.appendKeys({ function: "plumbingMiddleware.after" });
62
+ try {
63
+ localLogger.debug("started closing in after");
64
+ subsegment.close();
65
+ handlerSegment.close();
66
+ tracer.setSegment(segment);
67
+ metrics.publishStoredMetrics();
68
+ localLogger.debug("finished closing in after");
69
+ } catch (error) {
70
+ localLogger.error("error in after", { error });
71
+ throw error;
72
+ }
73
+ };
74
+ return {
75
+ before,
76
+ after
77
+ };
78
+ };
79
+
80
+ // src/sharedUtils.ts
81
+ import { DynamoDBPersistenceLayer } from "@aws-lambda-powertools/idempotency/dynamodb";
82
+ import assert2 from "node:assert";
83
+ var getPersistenceStore = () => {
84
+ assert2(
85
+ process.env.AC_IDEMPOTENCY_TABLE_NAME,
86
+ "AC_IDEMPOTENCY_TABLE_NAME is not configured"
87
+ );
88
+ const idempotencyTableName = process.env.AC_IDEMPOTENCY_TABLE_NAME;
89
+ return new DynamoDBPersistenceLayer({
90
+ tableName: idempotencyTableName
91
+ });
92
+ };
93
+
94
+ // src/s3.ts
95
+ import {
96
+ GetObjectCommand,
97
+ S3Client
98
+ } from "@aws-sdk/client-s3";
99
+ import { Upload } from "@aws-sdk/lib-storage";
100
+ import { PassThrough } from "node:stream";
101
+ var client = new S3Client({});
102
+ function createS3UploadStream({
103
+ bucket,
104
+ key,
105
+ logger
106
+ }) {
107
+ const pass = new PassThrough();
108
+ const parallelUploads3 = new Upload({
109
+ client,
110
+ params: {
111
+ Bucket: bucket,
112
+ Key: key,
113
+ Body: pass
114
+ }
115
+ });
116
+ parallelUploads3.done();
117
+ logger.info("setup upload stream");
118
+ return pass;
119
+ }
120
+ async function createS3DownloadStream({
121
+ logger,
122
+ getObjectCommandInput
123
+ }) {
124
+ try {
125
+ const item = await client.send(new GetObjectCommand(getObjectCommandInput));
126
+ logger.debug("sent GetObjectCommand", { getObjectCommandInput });
127
+ return item.Body;
128
+ } catch (error) {
129
+ throw Error(`downloadStreamFromS3: ${error.message}`);
130
+ }
131
+ }
132
+
133
+ // src/dynamoDB.ts
134
+ import {
135
+ CreateTableCommand,
136
+ DeleteTableCommand,
137
+ DescribeTableCommand,
138
+ DynamoDBClient
139
+ } from "@aws-sdk/client-dynamodb";
140
+ import {
141
+ DynamoDBDocumentClient,
142
+ GetCommand,
143
+ QueryCommand,
144
+ UpdateCommand,
145
+ PutCommand
146
+ } from "@aws-sdk/lib-dynamodb";
147
+ import { setTimeout } from "timers/promises";
148
+ var dynamoDBClient = new DynamoDBClient({});
149
+ var dynamoDBDocumentClient = DynamoDBDocumentClient.from(dynamoDBClient);
150
+ var deleteDynamoDBTable = async (deleteTableInput, logger) => {
151
+ logger.debug("DeleteTableCommand input", {
152
+ DeleteTableInput: deleteTableInput
153
+ });
154
+ const deleteTableCommandOutput = await dynamoDBClient.send(
155
+ new DeleteTableCommand(deleteTableInput)
156
+ );
157
+ logger.debug("DeleteTableCommand output", {
158
+ DeleteTableCommandOutput: deleteTableCommandOutput
159
+ });
160
+ let describeTableCommandOutput;
161
+ try {
162
+ do {
163
+ describeTableCommandOutput = await dynamoDBClient.send(
164
+ new DescribeTableCommand(deleteTableInput)
165
+ );
166
+ logger.debug("DescribeTableCommand", {
167
+ DescribeTableCommandOutput: describeTableCommandOutput
168
+ });
169
+ await setTimeout(2e3);
170
+ } while (true);
171
+ } catch (error) {
172
+ }
173
+ return describeTableCommandOutput;
174
+ };
175
+ var createDynamoDBTable = async (createTableInput, logger) => {
176
+ logger.debug("CreateTableCommand input", {
177
+ CreateTableInput: createTableInput
178
+ });
179
+ const createTableCommandOutput = await dynamoDBClient.send(
180
+ new CreateTableCommand(createTableInput)
181
+ );
182
+ logger.debug("CreateTableCommand output", {
183
+ CreateTableCommandOutput: createTableCommandOutput
184
+ });
185
+ let describeTableCommandOutput;
186
+ do {
187
+ await setTimeout(2e3);
188
+ describeTableCommandOutput = await dynamoDBClient.send(
189
+ new DescribeTableCommand(createTableInput)
190
+ );
191
+ logger.debug("DescribeTableCommand", {
192
+ DescribeTableCommandOutput: describeTableCommandOutput
193
+ });
194
+ } while (describeTableCommandOutput.Table?.TableStatus !== "ACTIVE");
195
+ return describeTableCommandOutput;
196
+ };
197
+ var getCommand = async (getCommandInput, logger) => {
198
+ logger.debug("GetCommand input", {
199
+ GetCommandInput: getCommandInput
200
+ });
201
+ const getCommandOutput = await dynamoDBDocumentClient.send(
202
+ new GetCommand(getCommandInput)
203
+ );
204
+ logger.debug("GetCommand output", {
205
+ GetCommandOutput: getCommandOutput
206
+ });
207
+ return getCommandOutput;
208
+ };
209
+ var updateCommand = async (updateCommandInput, logger) => {
210
+ logger.debug("UpdateCommand input", {
211
+ UpdateCommandInput: updateCommandInput
212
+ });
213
+ const updateCommandOutput = await dynamoDBDocumentClient.send(
214
+ new UpdateCommand(updateCommandInput)
215
+ );
216
+ logger.debug("UpdateCommand output", {
217
+ UpdateCommandOutput: updateCommandOutput
218
+ });
219
+ return updateCommandOutput;
220
+ };
221
+ var queryCommand = async (queryCommandInput, logger) => {
222
+ logger.debug("QueryCommand input", {
223
+ QueryCommandInput: queryCommandInput
224
+ });
225
+ const queryCommandOutput = await dynamoDBDocumentClient.send(
226
+ new QueryCommand(queryCommandInput)
227
+ );
228
+ logger.debug("QueryCommand output", {
229
+ QueryCommandOutput: queryCommandOutput
230
+ });
231
+ return queryCommandOutput;
232
+ };
233
+ var putCommand = async (putCommandInput, logger) => {
234
+ logger.debug("PutCommand input", {
235
+ PutCommandInput: putCommandInput
236
+ });
237
+ const putCommandOutput = await dynamoDBDocumentClient.send(
238
+ new PutCommand(putCommandInput)
239
+ );
240
+ logger.debug("PutCommand output", {
241
+ PutCommandOutput: putCommandOutput
242
+ });
243
+ return putCommandOutput;
244
+ };
245
+ export {
246
+ createDynamoDBTable,
247
+ createS3DownloadStream,
248
+ createS3UploadStream,
249
+ deleteDynamoDBTable,
250
+ getCommand,
251
+ getPersistenceStore,
252
+ plumbingMiddleware,
253
+ putCommand,
254
+ queryCommand,
255
+ updateCommand
256
+ };
@@ -0,0 +1,2 @@
1
+ import { MiddlewareObj } from "@middy/core";
2
+ export declare const plumbingMiddleware: () => MiddlewareObj;
package/lib/s3.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { Logger } from "@aws-lambda-powertools/logger";
3
+ import { GetObjectCommandInput } from "@aws-sdk/client-s3";
4
+ import { PassThrough } from "node:stream";
5
+ export declare function createS3UploadStream({ bucket, key, logger, }: {
6
+ bucket: string;
7
+ key: string;
8
+ logger: Logger;
9
+ }): PassThrough;
10
+ export declare function createS3DownloadStream({ logger, getObjectCommandInput, }: {
11
+ getObjectCommandInput: GetObjectCommandInput;
12
+ logger: Logger;
13
+ }): Promise<import("@smithy/types").StreamingBlobPayloadOutputTypes>;
@@ -0,0 +1,2 @@
1
+ import { DynamoDBPersistenceLayer } from "@aws-lambda-powertools/idempotency/dynamodb";
2
+ export declare const getPersistenceStore: () => DynamoDBPersistenceLayer;
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@aspan-corporation/ac-shared",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "keywords": [],
6
+ "main": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
+ "author": "",
9
+ "license": "MIT",
10
+ "type": "module",
11
+ "scripts": {
12
+ "clean": "rm -rf lib",
13
+ "build": "npm run clean && npm run build:code && npm run build:types",
14
+ "build:code": "esbuild src/index.ts --outdir=lib --bundle --format=esm --platform=node --external:@aws-lambda-powertools/logger --external:@aws-lambda-powertools/metrics --external:@aws-lambda-powertools/tracer --external:@aws-sdk/client-dynamodb --external:@aws-sdk/lib-dynamodb --external:@aws-sdk/client-s3 --external:@aws-sdk/lib-storage --external:@aws-lambda-powertools/idempotency",
15
+ "build:types": "tsc --emitDeclarationOnly --declaration"
16
+ },
17
+ "devDependencies": {
18
+ "@aws-lambda-powertools/idempotency": "^2.0.3",
19
+ "@aws-lambda-powertools/logger": "^2.0.3",
20
+ "@aws-lambda-powertools/metrics": "^2.0.3",
21
+ "@aws-lambda-powertools/tracer": "^2.0.3",
22
+ "@aws-sdk/client-dynamodb": "^3.549.0",
23
+ "@aws-sdk/client-s3": "^3.550.0",
24
+ "@aws-sdk/lib-dynamodb": "^3.549.0",
25
+ "@aws-sdk/lib-storage": "^3.550.0",
26
+ "@types/aws-lambda": "^8.10.137",
27
+ "@types/node": "^20.12.5",
28
+ "esbuild": "^0.20.2",
29
+ "typescript": "^5.4.4"
30
+ },
31
+ "dependencies": {
32
+ "@middy/core": "^4.7.0"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "files": [
38
+ "lib",
39
+ "package.json",
40
+ "README.md",
41
+ "LICENSE"
42
+ ]
43
+ }