@63klabs/cache-data 1.2.2

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,250 @@
1
+ const AWSXRay = (process.env?.CacheData_AWSXRayOn === "true") ? require("aws-xray-sdk-core") : null;
2
+
3
+ if (AWSXRay !== null) {
4
+ // Configure capture options
5
+ const captureOptions = {
6
+ captureRequestInit: true, // Capture request init
7
+ captureResponse: true, // Capture response
8
+ generateUniqueId: true // Generate unique IDs for each request
9
+ };
10
+
11
+ AWSXRay.captureHTTPsGlobal(require('http'), captureOptions);
12
+ AWSXRay.captureHTTPsGlobal(require("https"), captureOptions);
13
+ }
14
+
15
+ /**
16
+ * AWS Helper Functions - Functions to perform common get and put operations for DynamoDB, S3, and SSM parameter store.
17
+ * Uses AWS SDK v2 or v3 depending on the Node.js version. It will perform this check for you and utilize the proper SDK.
18
+ *
19
+ * @example
20
+ * console.log(AWS.REGION); // outputs the region set in Node environment: process.env.AWS_REGION
21
+ *
22
+ * @example
23
+ * const result = await AWS.dynamo.get(params);
24
+ * const response = await AWS.dynamo.put(params);
25
+ * AWS.dynamo.client; // access the DynamoDb Document client directly
26
+ * AWS.dynamo.sdk; // access the DynamoDB SDK (V2: { DynamoDB }, V3: { DynamoDB, DynamoDBClient, GetItemCommand, PutItemCommand }
27
+ * const dbDocClient = new AWS.dynamo.sdk.DynamoDB.DocumentClient( {region: AWS.REGION} );
28
+ *
29
+ * @example
30
+ * result = await AWS.s3.get(params);
31
+ * response = await AWS.s3.put(params);
32
+ * AWS.s3.client; // access the S3 client directly
33
+ *
34
+ * @example
35
+ * ssmParams1 = await AWS.ssm.getByName(query);
36
+ * ssmParams2 = await AWS.ssm.getByPath(query);
37
+ * AWS.ssm.client; // access the SSM Client
38
+ * AWS.ssm.sdk; // access the SSM SDK (V3 contains { SSM, SSMClient, GetParameterCommand, PutParameterCommand })
39
+ *
40
+ * @class AWS
41
+ * @property {string} NODE_VER
42
+ * @property {number} NODE_VER_MAJOR
43
+ * @property {number} NODE_VER_MINOR
44
+ * @property {number} NODE_VER_PATCH
45
+ * @property {string} NODE_VER_MAJOR_MINOR
46
+ * @property {string} SDK_VER 'V2' or 'V3'
47
+ * @property {boolean} SDK_V2 true if using AWS SDK v2
48
+ * @property {boolean} SDK_V3 true if using AWS SDK v3
49
+ * @property {string} REGION AWS region grabbed from Node process.env.AWS_REGION. If not set uses 'us-east-1'
50
+ * @property {object} dynamo
51
+ * @property {object} dynamo.client DynamoDb Document client (either V2 or V3)
52
+ * @property {object} dynamo.sdk V2: { DynamoDb }, V3: { DynamoDB, DynamoDBClient, DynamoDBDocumentClient, GetCommand, PutCommand }
53
+ * @property {object} dynamo.put function(params) Given a DynamoDb param object, uses the correct SDK version to perform a DynamoDb put command
54
+ * @property {object} dynamo.get function(params) Given a DynamoDb param object, uses the correct SDK version to perform a DynamoDb get command
55
+ * @property {object} dynamo.scan function(params) Given a DynamoDb param object, uses the correct SDK version to perform a DynamoDb scan command
56
+ * @property {object} dynamo.delete function(params) Given a DynamoDb param object, uses the correct SDK version to perform a DynamoDb delete command
57
+ * @property {object} dynamo.update function(params) Given a DynamoDb param object, uses the correct SDK version to perform a DynamoDb update command
58
+ * @property {object} s3
59
+ * @property {object} s3.client S3 client (either V2 or V3)
60
+ * @property {object} s3.sdk V2: { S3 }, V3: { S3Client, GetObjectCommand, PutObjectCommand }
61
+ * @property {object} s3.put function(params) Given an S3 param object, uses the correct SDK version to perform a S3 put command
62
+ * @property {object} s3.get function(params) Given an S3 param object, uses the correct SDK version to perform a S3 get command
63
+ * @property {object} ssm
64
+ * @property {object} ssm.client SSM client (either V2 or V3)
65
+ * @property {object} ssm.sdk V2: { SSM }, V3: { SSMClient, GetParameterCommand, GetParametersByPathCommand }
66
+ * @property {object} ssm.getByName function(query) Given SSM Parameter Store query, uses the correct SDK version to perform the getParameters command
67
+ * @property {object} ssm.getByPath function(query) Given SSM Parameter Store query, uses the correct SDK version to perform the getParametersByPath command
68
+ * @property {object} AWSXRay
69
+ */
70
+ class AWS {
71
+
72
+ static #nodeVer = [];
73
+ static #aws_region = null;
74
+
75
+ static #XRayOn = (AWSXRay !== null);
76
+
77
+ constructor() {}
78
+
79
+ static get nodeVersionArray() {
80
+ if (this.#nodeVer.length === 0) {
81
+ // split this.NODE_VER into an array of integers
82
+ this.#nodeVer = this.NODE_VER.split(".").map( (x) => parseInt(x, 10) );
83
+ }
84
+ return this.#nodeVer;
85
+ };
86
+
87
+ static get region() {
88
+ if (this.#aws_region === null) {
89
+
90
+ const hasRegion = (
91
+ "AWS_REGION" in process.env
92
+ && typeof process.env.AWS_REGION !== 'undefined'
93
+ && process.env.AWS_REGION !== null
94
+ && process.env.AWS_REGION !== ""
95
+ );
96
+
97
+ if (!hasRegion) {
98
+ console.warn("AWS_REGION is NOT set in Lambda Node environment variables. Trying 'us-east-1'. To prevent unexpected results, please create and set the 'AWS_REGION' in your Lambda environment variables.");
99
+ }
100
+
101
+ this.#aws_region = ( hasRegion ? process.env.AWS_REGION : "us-east-1" );
102
+ }
103
+
104
+ return this.#aws_region;
105
+ }
106
+
107
+ static get NODE_VER() { return ( ("versions" in process && "node" in process.versions) ? process.versions.node : "0.0.0"); }
108
+ static get NODE_VER_MAJOR() { return ( this.nodeVersionArray[0] ); }
109
+ static get NODE_VER_MINOR() { return ( this.nodeVersionArray[1] ); }
110
+ static get NODE_VER_PATCH() { return ( this.nodeVersionArray[2] ); }
111
+ static get NODE_VER_MAJOR_MINOR() { return (this.nodeVersionArray[0] + "." + this.nodeVersionArray[1]); }
112
+ static get NODE_VER_ARRAY() { return (this.nodeVersionArray); }
113
+ static get SDK_VER() { return ((this.NODE_VER_MAJOR < 18) ? "V2" : "V3"); }
114
+ static get REGION() { return ( this.region ); }
115
+ static get SDK_V2() { return (this.SDK_VER === "V2"); }
116
+ static get SDK_V3() { return (this.SDK_VER === "V3"); }
117
+
118
+ static get INFO() {
119
+ return ( {
120
+ NODE_VER: this.NODE_VER,
121
+ NODE_VER_MAJOR: this.NODE_VER_MAJOR,
122
+ NODE_VER_MINOR: this.NODE_VER_MINOR,
123
+ NODE_VER_PATCH: this.NODE_VER_PATCH,
124
+ NODE_VER_MAJOR_MINOR: this.NODE_VER_MAJOR_MINOR,
125
+ NODE_VER_ARRAY: this.NODE_VER_ARRAY,
126
+ SDK_VER: this.SDK_VER,
127
+ REGION: this.REGION,
128
+ SDK_V2: this.SDK_V2,
129
+ SDK_V3: this.SDK_V3,
130
+ AWSXRayOn: this.#XRayOn
131
+ });
132
+ }
133
+
134
+ static #SDK = (
135
+ function(){
136
+ if (AWS.SDK_V2) {
137
+ const { DynamoDB, S3, SSM } = (this.#XRayOn) ? AWSXRay.captureAWS(require("aws-sdk")) : require("aws-sdk");
138
+ return {
139
+ dynamo: {
140
+ client: (new DynamoDB.DocumentClient( {region: AWS.REGION} )),
141
+ put: (client, params) => client.put(params).promise(),
142
+ get: (client, params) => client.get(params).promise(),
143
+ scan: (client, params) => client.scan(params).promise(),
144
+ delete: (client, params) => client.delete(params).promise(),
145
+ update: (client, params) => client.update(params).promise(),
146
+ sdk: { DynamoDB }
147
+ },
148
+ s3: {
149
+ client: (new S3()),
150
+ put: (client, params) => client.putObject(params).promise(),
151
+ get: (client, params) => client.getObject(params).promise(),
152
+ sdk: { S3 }
153
+ },
154
+ ssm: {
155
+ client: (new SSM( {region: AWS.REGION} )),
156
+ getByName: (client, params) => client.getParameters(params).promise(),
157
+ getByPath: (client, params) => client.getParametersByPath(params).promise(),
158
+ sdk: { SSM }
159
+ }
160
+ }
161
+ } else {
162
+ const { DynamoDBClient} = require("@aws-sdk/client-dynamodb");
163
+ const { DynamoDBDocumentClient, GetCommand, PutCommand, ScanCommand, DeleteCommand, UpdateCommand } = require("@aws-sdk/lib-dynamodb");
164
+ const { S3, GetObjectCommand, PutObjectCommand } = require("@aws-sdk/client-s3");
165
+ const { SSMClient, GetParametersByPathCommand, GetParametersCommand } = require("@aws-sdk/client-ssm");
166
+
167
+ return {
168
+ dynamo: {
169
+ client: (DynamoDBDocumentClient.from(
170
+ (AWS.#XRayOn) ? AWSXRay.captureAWSv3Client(new DynamoDBClient({ region: AWS.REGION }))
171
+ : new DynamoDBClient({ region: AWS.REGION })) ),
172
+ put: (client, params) => client.send(new PutCommand(params)),
173
+ get: (client, params) => client.send(new GetCommand(params)),
174
+ scan: (client, params) => client.send(new ScanCommand(params)),
175
+ delete: (client, params) => client.send(new DeleteCommand(params)),
176
+ update: (client, params) => client.send(new UpdateCommand(params)),
177
+ sdk: {
178
+ DynamoDBClient,
179
+ DynamoDBDocumentClient,
180
+ GetCommand,
181
+ PutCommand
182
+ }
183
+ },
184
+ s3: {
185
+ client: (
186
+ (AWS.#XRayOn) ? AWSXRay.captureAWSv3Client(new S3())
187
+ : new S3()),
188
+ put: (client, params) => client.send(new PutObjectCommand(params)),
189
+ get: (client, params) => client.send(new GetObjectCommand(params)),
190
+ sdk: {
191
+ S3,
192
+ GetObjectCommand,
193
+ PutObjectCommand
194
+ }
195
+
196
+ },
197
+ ssm: {
198
+ client: (
199
+ (AWS.#XRayOn) ? AWSXRay.captureAWSv3Client(new SSMClient({ region: AWS.REGION }))
200
+ : new SSMClient({ region: AWS.REGION })),
201
+ getByName: (client, query) => client.send(new GetParametersCommand(query)),
202
+ getByPath: (client, query) => client.send(new GetParametersByPathCommand(query)),
203
+ sdk: {
204
+ SSMClient,
205
+ GetParametersByPathCommand,
206
+ GetParametersCommand
207
+ }
208
+ }
209
+ }
210
+ }
211
+ }
212
+ )();
213
+
214
+ static get dynamo() {
215
+ return {
216
+ client: this.#SDK.dynamo.client,
217
+ put: ( params ) => this.#SDK.dynamo.put(this.#SDK.dynamo.client, params),
218
+ get: ( params ) => this.#SDK.dynamo.get(this.#SDK.dynamo.client, params),
219
+ scan: ( params ) => this.#SDK.dynamo.scan(this.#SDK.dynamo.client, params),
220
+ delete: ( params ) => this.#SDK.dynamo.delete(this.#SDK.dynamo.client, params),
221
+ update: ( params ) => this.#SDK.dynamo.update(this.#SDK.dynamo.client, params),
222
+ sdk: this.#SDK.dynamo.sdk
223
+ };
224
+ }
225
+
226
+ static get s3() {
227
+ return {
228
+ client: this.#SDK.s3.client,
229
+ put: ( params ) => this.#SDK.s3.put(this.#SDK.s3.client, params),
230
+ get: ( params ) => this.#SDK.s3.get(this.#SDK.s3.client, params),
231
+ sdk: this.#SDK.s3.sdk
232
+ };
233
+ }
234
+
235
+ static get ssm() {
236
+ return {
237
+ client: this.#SDK.ssm.client,
238
+ getByName: ( query ) => this.#SDK.ssm.getByName(this.#SDK.ssm.client, query),
239
+ getByPath: ( query ) => this.#SDK.ssm.getByPath(this.#SDK.ssm.client, query),
240
+ sdk: this.#SDK.ssm.sdk
241
+ };
242
+ }
243
+
244
+ static get XRay() {
245
+ return AWSXRay;
246
+ }
247
+
248
+ };
249
+
250
+ module.exports = {AWS, AWSXRay};