@aspan-corporation/ac-shared 0.0.9 → 1.0.0

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/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from "./plumbingMiddleware.js";
2
2
  export * from "./sharedUtils.js";
3
- export * from "./s3.js";
4
- export * from "./dynamoDB.js";
3
+ export * from "./services/s3.js";
4
+ export * from "./services/dynamoDB.js";
5
5
  export * from "./assertUtils.js";
6
6
  export * from "./logger.js";
package/lib/index.js CHANGED
@@ -102,182 +102,157 @@ var getPersistenceStore = () => {
102
102
  });
103
103
  };
104
104
 
105
- // src/s3.ts
105
+ // src/services/s3.ts
106
106
  import {
107
107
  GetObjectCommand,
108
+ ListObjectsV2Command,
108
109
  S3Client
109
110
  } from "@aws-sdk/client-s3";
110
111
  import { Upload } from "@aws-sdk/lib-storage";
111
112
  import { PassThrough } from "node:stream";
112
- var client = new S3Client({});
113
- function createS3UploadStream({
114
- bucket,
115
- key,
116
- logger
117
- }) {
118
- const pass = new PassThrough();
119
- const parallelUploads3 = new Upload({
113
+
114
+ // src/logger.ts
115
+ import { Logger as Logger3 } from "@aws-lambda-powertools/logger";
116
+ var rootLogger = new Logger3();
117
+ var getLoggerWithScope = (scope, logger = rootLogger) => {
118
+ const childLogger = logger.createChild();
119
+ childLogger.appendKeys({ scope });
120
+ return childLogger;
121
+ };
122
+
123
+ // src/services/s3.ts
124
+ var S3Service = class {
125
+ logger;
126
+ client;
127
+ constructor({
128
+ logger,
120
129
  client,
121
- params: {
122
- Bucket: bucket,
123
- Key: key,
124
- Body: pass
130
+ assumeRoleCommandOutput
131
+ }) {
132
+ this.logger = getLoggerWithScope("ac-shared:s3", logger);
133
+ if (assumeRoleCommandOutput) {
134
+ this.client = new S3Client({
135
+ region: process.env.AWS_REGION,
136
+ logger: this.logger,
137
+ credentials: {
138
+ accessKeyId: assumeRoleCommandOutput?.Credentials?.AccessKeyId,
139
+ secretAccessKey: assumeRoleCommandOutput?.Credentials?.SecretAccessKey,
140
+ sessionToken: assumeRoleCommandOutput?.Credentials?.SessionToken
141
+ }
142
+ });
143
+ } else {
144
+ this.client = client ?? new S3Client({
145
+ region: process.env.AWS_REGION,
146
+ logger: this.logger
147
+ });
125
148
  }
126
- });
127
- parallelUploads3.done();
128
- logger.info("setup upload stream");
129
- return pass;
130
- }
131
- async function createS3DownloadStream({
132
- logger,
133
- getObjectCommandInput
134
- }) {
135
- try {
136
- const item = await client.send(new GetObjectCommand(getObjectCommandInput));
137
- logger.debug("sent GetObjectCommand", { getObjectCommandInput });
149
+ }
150
+ async createS3UploadStream(putObjectCommandInput) {
151
+ this.logger.debug("upload stream started");
152
+ const pass = new PassThrough();
153
+ const parallelUploads3 = new Upload({
154
+ client: this.client,
155
+ params: {
156
+ ...putObjectCommandInput,
157
+ Body: pass
158
+ }
159
+ });
160
+ parallelUploads3.done();
161
+ this.logger.debug("upload stream finished");
162
+ return pass;
163
+ }
164
+ async createS3DownloadStream(getObjectCommandInput) {
165
+ this.logger.debug("download stream started");
166
+ const item = await this.client.send(
167
+ new GetObjectCommand(getObjectCommandInput)
168
+ );
169
+ this.logger.debug("download stream finished");
138
170
  return item.Body;
139
- } catch (error) {
140
- throw Error(`downloadStreamFromS3: ${error.message}`);
141
171
  }
142
- }
172
+ async listObjectsV2(listObjectsV2CommandInput) {
173
+ return await this.client.send(
174
+ new ListObjectsV2Command(listObjectsV2CommandInput)
175
+ );
176
+ }
177
+ async getObject(getObjectCommandInput) {
178
+ const stream = await this.createS3DownloadStream(getObjectCommandInput);
179
+ return Buffer.from(await stream.transformToByteArray());
180
+ }
181
+ };
143
182
 
144
- // src/dynamoDB.ts
145
- import {
146
- CreateTableCommand,
147
- DeleteTableCommand,
148
- DescribeTableCommand,
149
- DynamoDBClient
150
- } from "@aws-sdk/client-dynamodb";
183
+ // src/services/dynamoDB.ts
184
+ import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
151
185
  import {
152
186
  DynamoDBDocumentClient,
153
187
  GetCommand,
188
+ PutCommand,
154
189
  QueryCommand,
155
- UpdateCommand,
156
- PutCommand
190
+ UpdateCommand
157
191
  } from "@aws-sdk/lib-dynamodb";
158
- import { setTimeout } from "timers/promises";
159
- var dynamoDBClient = new DynamoDBClient({});
160
- var dynamoDBDocumentClient = DynamoDBDocumentClient.from(dynamoDBClient);
161
- var deleteDynamoDBTable = async (deleteTableInput, logger) => {
162
- logger.debug("DeleteTableCommand input", {
163
- DeleteTableInput: deleteTableInput
164
- });
165
- const deleteTableCommandOutput = await dynamoDBClient.send(
166
- new DeleteTableCommand(deleteTableInput)
167
- );
168
- logger.debug("DeleteTableCommand output", {
169
- DeleteTableCommandOutput: deleteTableCommandOutput
170
- });
171
- let describeTableCommandOutput;
172
- try {
173
- do {
174
- describeTableCommandOutput = await dynamoDBClient.send(
175
- new DescribeTableCommand(deleteTableInput)
176
- );
177
- logger.debug("DescribeTableCommand", {
178
- DescribeTableCommandOutput: describeTableCommandOutput
192
+ var DynamoDBService = class {
193
+ logger;
194
+ client;
195
+ documentClient;
196
+ constructor({
197
+ logger,
198
+ client,
199
+ assumeRoleCommandOutput
200
+ }) {
201
+ this.logger = getLoggerWithScope("ac-shared:dynamodb", logger);
202
+ if (assumeRoleCommandOutput) {
203
+ this.client = new DynamoDBClient({
204
+ region: process.env.AWS_REGION,
205
+ logger: this.logger,
206
+ credentials: {
207
+ accessKeyId: assumeRoleCommandOutput?.Credentials?.AccessKeyId,
208
+ secretAccessKey: assumeRoleCommandOutput?.Credentials?.SecretAccessKey,
209
+ sessionToken: assumeRoleCommandOutput?.Credentials?.SessionToken
210
+ }
211
+ });
212
+ } else {
213
+ this.client = client ?? new DynamoDBClient({
214
+ region: process.env.AWS_REGION,
215
+ logger: this.logger
179
216
  });
180
- await setTimeout(2e3);
181
- } while (true);
182
- } catch (error) {
217
+ }
218
+ this.documentClient = DynamoDBDocumentClient.from(this.client);
183
219
  }
184
- return describeTableCommandOutput;
185
- };
186
- var createDynamoDBTable = async (createTableInput, logger) => {
187
- logger.debug("CreateTableCommand input", {
188
- CreateTableInput: createTableInput
189
- });
190
- const createTableCommandOutput = await dynamoDBClient.send(
191
- new CreateTableCommand(createTableInput)
192
- );
193
- logger.debug("CreateTableCommand output", {
194
- CreateTableCommandOutput: createTableCommandOutput
195
- });
196
- let describeTableCommandOutput;
197
- do {
198
- await setTimeout(2e3);
199
- describeTableCommandOutput = await dynamoDBClient.send(
200
- new DescribeTableCommand(createTableInput)
220
+ async getCommand(getCommandInput) {
221
+ return await this.documentClient.send(new GetCommand(getCommandInput));
222
+ }
223
+ async updateCommand(updateCommandInput) {
224
+ return await this.documentClient.send(
225
+ new UpdateCommand(updateCommandInput)
201
226
  );
202
- logger.debug("DescribeTableCommand", {
203
- DescribeTableCommandOutput: describeTableCommandOutput
204
- });
205
- } while (describeTableCommandOutput.Table?.TableStatus !== "ACTIVE");
206
- return describeTableCommandOutput;
207
- };
208
- var getCommand = async (getCommandInput, logger) => {
209
- logger.debug("GetCommand input", {
210
- GetCommandInput: getCommandInput
211
- });
212
- const getCommandOutput = await dynamoDBDocumentClient.send(
213
- new GetCommand(getCommandInput)
214
- );
215
- logger.debug("GetCommand output", {
216
- GetCommandOutput: getCommandOutput
217
- });
218
- return getCommandOutput;
219
- };
220
- var updateCommand = async (updateCommandInput, logger) => {
221
- logger.debug("UpdateCommand input", {
222
- UpdateCommandInput: updateCommandInput
223
- });
224
- const updateCommandOutput = await dynamoDBDocumentClient.send(
225
- new UpdateCommand(updateCommandInput)
226
- );
227
- logger.debug("UpdateCommand output", {
228
- UpdateCommandOutput: updateCommandOutput
229
- });
230
- return updateCommandOutput;
231
- };
232
- var queryCommand = async (queryCommandInput, logger) => {
233
- logger.debug("QueryCommand input", {
234
- QueryCommandInput: queryCommandInput
235
- });
236
- const queryCommandOutput = await dynamoDBDocumentClient.send(
237
- new QueryCommand(queryCommandInput)
238
- );
239
- logger.debug("QueryCommand output", {
240
- QueryCommandOutput: queryCommandOutput
241
- });
242
- return queryCommandOutput;
243
- };
244
- var putCommand = async (putCommandInput, logger) => {
245
- logger.debug("PutCommand input", {
246
- PutCommandInput: putCommandInput
247
- });
248
- const putCommandOutput = await dynamoDBDocumentClient.send(
249
- new PutCommand(putCommandInput)
250
- );
251
- logger.debug("PutCommand output", {
252
- PutCommandOutput: putCommandOutput
253
- });
254
- return putCommandOutput;
255
- };
256
-
257
- // src/logger.ts
258
- import { Logger as Logger3 } from "@aws-lambda-powertools/logger";
259
- var rootLogger = new Logger3();
260
- var getLoggerWithScope = (scope, logger = rootLogger) => {
261
- const childLogger = rootLogger.createChild();
262
- childLogger.appendKeys({ scope });
263
- return childLogger;
227
+ }
228
+ async queryCommand(queryCommandInput) {
229
+ return await this.documentClient.send(new QueryCommand(queryCommandInput));
230
+ }
231
+ async putCommand(putCommandInput) {
232
+ return await this.documentClient.send(new PutCommand(putCommandInput));
233
+ }
234
+ async checkIfItemExists({
235
+ tableName,
236
+ key
237
+ }) {
238
+ const params = {
239
+ TableName: tableName,
240
+ Key: key
241
+ };
242
+ const result = await this.getCommand(params);
243
+ return !!result.Item;
244
+ }
264
245
  };
265
246
  export {
247
+ DynamoDBService,
266
248
  Logger2 as Logger,
267
249
  Metrics2 as Metrics,
250
+ S3Service,
268
251
  Tracer2 as Tracer,
269
252
  assertEnvVar,
270
253
  assertString,
271
- createDynamoDBTable,
272
- createS3DownloadStream,
273
- createS3UploadStream,
274
- deleteDynamoDBTable,
275
- getCommand,
276
254
  getLoggerWithScope,
277
255
  getPersistenceStore,
278
256
  plumbingMiddleware,
279
- putCommand,
280
- queryCommand,
281
- rootLogger,
282
- updateCommand
257
+ rootLogger
283
258
  };
@@ -0,0 +1,22 @@
1
+ import { Logger } from "@aws-lambda-powertools/logger";
2
+ import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
3
+ import { DynamoDBDocumentClient, GetCommandInput, GetCommandOutput, PutCommandInput, PutCommandOutput, QueryCommandInput, QueryCommandOutput, UpdateCommandInput, UpdateCommandOutput } from "@aws-sdk/lib-dynamodb";
4
+ import { AssumeRoleCommandOutput } from "@aws-sdk/client-sts";
5
+ export declare class DynamoDBService {
6
+ logger: Logger;
7
+ client: DynamoDBClient;
8
+ documentClient: DynamoDBDocumentClient;
9
+ constructor({ logger, client, assumeRoleCommandOutput }: {
10
+ logger?: Logger;
11
+ client?: DynamoDBClient;
12
+ assumeRoleCommandOutput?: AssumeRoleCommandOutput;
13
+ });
14
+ getCommand(getCommandInput: GetCommandInput): Promise<GetCommandOutput>;
15
+ updateCommand(updateCommandInput: UpdateCommandInput): Promise<UpdateCommandOutput>;
16
+ queryCommand(queryCommandInput: QueryCommandInput): Promise<QueryCommandOutput>;
17
+ putCommand(putCommandInput: PutCommandInput): Promise<PutCommandOutput>;
18
+ checkIfItemExists({ tableName, key }: {
19
+ tableName: string;
20
+ key: any;
21
+ }): Promise<boolean>;
22
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./s3.js";
2
+ export * from "./dynamoDB.js";
3
+ export * from "./ssm.js";
4
+ export * from "./sqs.js";
5
+ export * from "./sts.js";
@@ -0,0 +1,19 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import { Logger } from "@aws-lambda-powertools/logger";
4
+ import { GetObjectCommandInput, ListObjectsV2CommandInput, PutObjectCommandInput, S3Client } from "@aws-sdk/client-s3";
5
+ import { AssumeRoleCommandOutput } from "@aws-sdk/client-sts";
6
+ import { PassThrough } from "node:stream";
7
+ export declare class S3Service {
8
+ logger: Logger;
9
+ client: S3Client;
10
+ constructor({ logger, client, assumeRoleCommandOutput }: {
11
+ logger?: Logger;
12
+ client?: S3Client;
13
+ assumeRoleCommandOutput?: AssumeRoleCommandOutput;
14
+ });
15
+ createS3UploadStream(putObjectCommandInput: PutObjectCommandInput): Promise<PassThrough>;
16
+ createS3DownloadStream(getObjectCommandInput: GetObjectCommandInput): Promise<import("@smithy/types").StreamingBlobPayloadOutputTypes>;
17
+ listObjectsV2(listObjectsV2CommandInput: ListObjectsV2CommandInput): Promise<import("@aws-sdk/client-s3").ListObjectsV2CommandOutput>;
18
+ getObject(getObjectCommandInput: GetObjectCommandInput): Promise<Buffer>;
19
+ }
@@ -0,0 +1,16 @@
1
+ import { Logger } from "@aws-lambda-powertools/logger";
2
+ import { SQSClient, SendMessageCommandInput, ReceiveMessageCommandInput, DeleteMessageCommandInput, SendMessageBatchCommandInput, ReceiveMessageCommandOutput, SendMessageCommandOutput, DeleteMessageCommandOutput, SendMessageBatchCommandOutput } from "@aws-sdk/client-sqs";
3
+ import { AssumeRoleCommandOutput } from "@aws-sdk/client-sts";
4
+ export declare class SQSService {
5
+ logger: Logger;
6
+ client: SQSClient;
7
+ constructor({ logger, client, assumeRoleCommandOutput }: {
8
+ logger?: Logger;
9
+ client?: SQSClient;
10
+ assumeRoleCommandOutput?: AssumeRoleCommandOutput;
11
+ });
12
+ sendMessage(params: SendMessageCommandInput): Promise<SendMessageCommandOutput>;
13
+ sendMessageBatch(params: SendMessageBatchCommandInput): Promise<SendMessageBatchCommandOutput>;
14
+ receiveMessage(params: ReceiveMessageCommandInput): Promise<ReceiveMessageCommandOutput>;
15
+ deleteMessage(params: DeleteMessageCommandInput): Promise<DeleteMessageCommandOutput>;
16
+ }
@@ -0,0 +1,14 @@
1
+ import { Logger } from "@aws-lambda-powertools/logger";
2
+ import { SSMClient, GetParameterCommandInput, GetParameterCommandOutput, PutParameterCommandInput, PutParameterCommandOutput } from "@aws-sdk/client-ssm";
3
+ import { AssumeRoleCommandOutput } from "@aws-sdk/client-sts";
4
+ export declare class SSMService {
5
+ logger: Logger;
6
+ client: SSMClient;
7
+ constructor({ logger, client, assumeRoleCommandOutput }: {
8
+ logger?: Logger;
9
+ client?: SSMClient;
10
+ assumeRoleCommandOutput?: AssumeRoleCommandOutput;
11
+ });
12
+ getParameter(params: GetParameterCommandInput): Promise<GetParameterCommandOutput>;
13
+ putParameter(params: PutParameterCommandInput): Promise<PutParameterCommandOutput>;
14
+ }
@@ -0,0 +1,11 @@
1
+ import { STSClient, AssumeRoleCommandInput } from "@aws-sdk/client-sts";
2
+ import { Logger } from "@aws-lambda-powertools/logger";
3
+ export declare class STSService {
4
+ logger: Logger;
5
+ client: STSClient;
6
+ constructor({ logger }: {
7
+ logger?: Logger;
8
+ });
9
+ getCallerIdentity(): Promise<import("@aws-sdk/client-sts").GetCallerIdentityCommandOutput>;
10
+ assumeRole(assumeRoleCommandInput: AssumeRoleCommandInput): Promise<import("@aws-sdk/client-sts").AssumeRoleCommandOutput>;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aspan-corporation/ac-shared",
3
- "version": "0.0.9",
3
+ "version": "1.0.0",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "main": "lib/index.js",
@@ -25,10 +25,13 @@
25
25
  "@aws-lambda-powertools/logger": "^2.19.0",
26
26
  "@aws-lambda-powertools/metrics": "^2.19.0",
27
27
  "@aws-lambda-powertools/tracer": "^2.19.0",
28
- "@aws-sdk/client-dynamodb": "^3.549.0",
29
- "@aws-sdk/client-s3": "^3.550.0",
30
- "@aws-sdk/lib-dynamodb": "^3.549.0",
31
- "@aws-sdk/lib-storage": "^3.550.0",
28
+ "@aws-sdk/client-dynamodb": "^3.812.0",
29
+ "@aws-sdk/client-s3": "^3.812.0",
30
+ "@aws-sdk/client-sqs": "^3.812.0",
31
+ "@aws-sdk/client-ssm": "^3.812.0",
32
+ "@aws-sdk/client-sts": "^3.812.0",
33
+ "@aws-sdk/lib-dynamodb": "^3.812.0",
34
+ "@aws-sdk/lib-storage": "^3.812.0",
32
35
  "@middy/core": "^4.7.0",
33
36
  "aws-lambda": "^1.0.7"
34
37
  },
package/lib/dynamoDB.d.ts DELETED
@@ -1,9 +0,0 @@
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/s3.d.ts DELETED
@@ -1,13 +0,0 @@
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>;