@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,86 @@
1
+ contentType = "text/plain";
2
+
3
+ headers = {
4
+ "Content-Type": contentType
5
+ };
6
+
7
+ text = (text) => { return text; }
8
+
9
+ response200 = {
10
+ statusCode: 200,
11
+ headers: headers,
12
+ body: text("Success")
13
+ };
14
+
15
+ response400 = {
16
+ statusCode: 400,
17
+ headers: headers,
18
+ body: text("Bad Request")
19
+ };
20
+
21
+ response401 = {
22
+ statusCode: 401,
23
+ headers: headers,
24
+ body: text("Unauthorized")
25
+ };
26
+
27
+ response403 = {
28
+ statusCode: 403,
29
+ headers: headers,
30
+ body: text("Forbidden")
31
+ };
32
+
33
+ response404 = {
34
+ statusCode: 404,
35
+ headers: headers,
36
+ body: text("Not Found")
37
+ };
38
+
39
+ response405 = {
40
+ statusCode: 405,
41
+ headers: headers,
42
+ body: text("Method Not Allowed")
43
+ };
44
+
45
+ response500 = {
46
+ statusCode: 500,
47
+ headers: headers,
48
+ body: text("Internal Server Error")
49
+ };
50
+
51
+ /**
52
+ *
53
+ * @param {number|string} statusCode
54
+ * @returns {{statusCode: number, headers: object, body: Array|Object|string}}
55
+ */
56
+ response = function (statusCode) {
57
+ // convert to int
58
+ statusCode = parseInt(statusCode, 10);
59
+
60
+ switch (statusCode) {
61
+ case 200:
62
+ return this.response200;
63
+ case 400:
64
+ return this.response400;
65
+ case 401:
66
+ return this.response401;
67
+ case 403:
68
+ return this.response403;
69
+ case 404:
70
+ return this.response404;
71
+ case 500:
72
+ return this.response500;
73
+ default:
74
+ return this.response500;
75
+ }
76
+ };
77
+
78
+ module.exports = {
79
+ contentType,
80
+ headers,
81
+ text,
82
+ response200,
83
+ response404,
84
+ response500,
85
+ response
86
+ }
@@ -0,0 +1,82 @@
1
+ contentType = "application/xml";
2
+
3
+ headers = {
4
+ "Content-Type": contentType
5
+ };
6
+
7
+ xml = (body) => {
8
+ return `<?xml version="1.0" encoding="UTF-8" ?>${body}`;
9
+ }
10
+
11
+ response200 = {
12
+ statusCode: 200,
13
+ headers: headers,
14
+ body: xml("<hello>Success</hello>")
15
+ };
16
+
17
+ response400 = {
18
+ statusCode: 400,
19
+ headers: headers,
20
+ body: xml("<error>Bad Request</error>")
21
+ };
22
+
23
+ response401 = {
24
+ statusCode: 401,
25
+ headers: headers,
26
+ body: xml("<error>Unauthorized</error>")
27
+ };
28
+
29
+ response403 = {
30
+ statusCode: 403,
31
+ headers: headers,
32
+ body: xml("<error>Forbidden</error>")
33
+ };
34
+
35
+ response404 = {
36
+ statusCode: 404,
37
+ headers: headers,
38
+ body: xml("<error>Not Found</error>")
39
+ };
40
+
41
+ response405 = {
42
+ statusCode: 405,
43
+ headers: headers,
44
+ body: xml("<error>Method Not Allowed</error>")
45
+ };
46
+
47
+ response500 = {
48
+ statusCode: 500,
49
+ headers: headers,
50
+ body: xml("<error>Internal Server Error</error>")
51
+ };
52
+
53
+ /**
54
+ *
55
+ * @param {number|string} statusCode
56
+ * @returns {{statusCode: number, headers: object, body: Array|Object|string}}
57
+ */
58
+ response = function (statusCode) {
59
+ // convert to int
60
+ statusCode = parseInt(statusCode, 10);
61
+
62
+ switch (statusCode) {
63
+ case 200:
64
+ return this.response200;
65
+ case 404:
66
+ return this.response404;
67
+ case 500:
68
+ return this.response500;
69
+ default:
70
+ return this.response500;
71
+ }
72
+ };
73
+
74
+ module.exports = {
75
+ contentType,
76
+ headers,
77
+ xml,
78
+ response200,
79
+ response404,
80
+ response500,
81
+ response
82
+ }
@@ -0,0 +1,318 @@
1
+ /*
2
+ * =============================================================================
3
+ * Tools
4
+ * -----------------------------------------------------------------------------
5
+ *
6
+ * Tools used for endpoint data access objects (DAOs) and cache-data. These
7
+ * tools are also available for other app functionality
8
+ *
9
+ * Some classes are internal and not exposed via export. Check list of exports
10
+ * for available classes and functions.
11
+ *
12
+ * -----------------------------------------------------------------------------
13
+ * Environment Variables used
14
+ * -----------------------------------------------------------------------------
15
+ *
16
+ * This script uses the Node.js environment variable process.env.AWS_REGION if
17
+ * present.
18
+ *
19
+ */
20
+
21
+ const { nodeVer, nodeVerMajor, nodeVerMinor, nodeVerMajorMinor } = require('./vars');
22
+ const { AWS, AWSXRay } = require('./AWS.classes');
23
+ const APIRequest = require("./APIRequest.class");
24
+ const RequestInfo = require("./RequestInfo.class");
25
+ const ClientRequest = require("./ClientRequest.class");
26
+ const ResponseDataModel = require("./ResponseDataModel.class");
27
+ const Response = require("./Response.class");
28
+ const Timer = require("./Timer.class");
29
+ const DebugAndLog = require("./DebugAndLog.class");
30
+ const ImmutableObject = require('./ImmutableObject.class');
31
+ const jsonGenericResponse = require('./generic.response.json');
32
+ const htmlGenericResponse = require('./generic.response.html');
33
+ const xmlGenericResponse = require('./generic.response.xml');
34
+ const rssGenericResponse = require('./generic.response.rss');
35
+ const textGenericResponse = require('./generic.response.text');
36
+ const { printMsg, sanitize, obfuscate, hashThisData} = require('./utils');
37
+ const { CachedParameterSecrets, CachedParameterSecret, CachedSSMParameter, CachedSecret } = require('./CachedParametersSecrets.classes')
38
+ const { Connections, Connection, ConnectionRequest, ConnectionAuthentication } = require('./Connections.classes')
39
+
40
+ /*
41
+ * -----------------------------------------------------------------------------
42
+ * Object definitions
43
+ * -----------------------------------------------------------------------------
44
+ */
45
+
46
+ /**
47
+ * @typedef ConnectionObject
48
+ * @property {object} connection A connection object
49
+ * @property {string} connection.method GET or POST
50
+ * @property {string} connection.uri the full uri (overrides protocol, host, path, and parameters) ex https://example.com/api/v1/1004/?key=asdf&y=4
51
+ * @property {string} connection.protocol https
52
+ * @property {string} connection.host host/domain: example.com
53
+ * @property {string} connection.path path of the request: /api/v1/1004
54
+ * @property {object} connection.parameters parameters for the query string as an object in key/value pairs
55
+ * @property {object} connection.headers headers for the request as an object in key/value pairs
56
+ * @property {string} connection.body for POST requests, the body
57
+ * @property {string} connection.note a note for logging
58
+ * @property {object} connection.options https_get options
59
+ * @property {number} connection.options.timeout timeout in milliseconds
60
+ */
61
+
62
+ /* ****************************************************************************
63
+ * Configure classes
64
+ * ----------------------------------------------------------------------------
65
+ *
66
+ * Provides base functionality to be extended by a custom Config class in the
67
+ * application.
68
+ *
69
+ *************************************************************************** */
70
+
71
+ /**
72
+ * _ConfigSuperClass needs to be extended by your own Config class definition.
73
+ *
74
+ * This super class holds common variables and methods that can be used by any
75
+ * application. However, each application requires it's own methods and logic
76
+ * to init.
77
+ *
78
+ * Usage: The child class Config should be placed near the top of the script
79
+ * file outside of the event handler. It should be global and must be
80
+ * initialized.
81
+ *
82
+ * @example
83
+ * class Config extends tools._ConfigSuperClass {
84
+ * // your custom class definition including your implementation of .init()
85
+ * }
86
+ *
87
+ * Config.init();
88
+ */
89
+ class _ConfigSuperClass {
90
+
91
+ static _promise = null;
92
+ static _connections = null;
93
+ static _settings = null;
94
+
95
+ static settings() {
96
+ return _ConfigSuperClass._settings;
97
+ };
98
+
99
+ /**
100
+ *
101
+ * @returns {Connections}
102
+ */
103
+ static connections() {
104
+ return _ConfigSuperClass._connections;
105
+ };
106
+
107
+ /**
108
+ *
109
+ * @param {string} name
110
+ * @returns {Connection}
111
+ */
112
+ static getConnection(name) {
113
+ return _ConfigSuperClass._connections.get(name);
114
+ }
115
+
116
+ /**
117
+ *
118
+ * @returns {Promise} A promise that resolves when the Config class has finished initializing
119
+ */
120
+ static promise() {
121
+ return _ConfigSuperClass._promise;
122
+ };
123
+
124
+
125
+ /**
126
+ * Retrieve all the parameters (listed in const params) from the
127
+ * parameter store and parse out the name. Then return the name
128
+ * along with their value.
129
+ *
130
+ * This will automatically decrypt any encrypted values (it will
131
+ * leave any String and StringList parameters as their normal,
132
+ * unencrypted self (WithDecryption is ignored for them))
133
+ *
134
+ * @returns {Promise<array>} parameters and their values
135
+ */
136
+ static async _getParametersFromStore (parameters) {
137
+
138
+ let paramstore = {};
139
+
140
+ /* go through PARAMS and compile all parameters with
141
+ their paths pre-pended into a list of names */
142
+ const paramNames = function () {
143
+ let names = [];
144
+ let paths = [];
145
+
146
+ /* we have two levels to work through, the base path has param names
147
+ grouped under it. So get all the names within each base path grouping. */
148
+ parameters.forEach(function(item) {
149
+ if ("names" in item) {
150
+ item.names.forEach(function(p) {
151
+ names.push(item.path+p);
152
+ });
153
+ } else {
154
+ paths.push(item.path);
155
+ }
156
+
157
+ });
158
+
159
+ return { names: names, paths: paths};
160
+ };
161
+
162
+ let request = [];
163
+
164
+ let pNames = paramNames();
165
+
166
+ if (pNames.names.length > 0 || pNames.paths.length > 0 ) {
167
+
168
+ let paramResultsArr = [];
169
+
170
+ // process all params by name and place promise in results array
171
+ if (pNames.names.length > 0) {
172
+
173
+ // put the list of full path names into query.Names
174
+ const query = {
175
+ 'Names': pNames.names,
176
+ 'WithDecryption': true
177
+ };
178
+
179
+ DebugAndLog.debug("Param by name query:",query);
180
+
181
+ // get parameters from query - wait for the promise to resolve
182
+ paramResultsArr.push(AWS.ssm.getByName(query));
183
+
184
+ }
185
+
186
+ // process all params by path and place each promise into results array
187
+ if (pNames.paths.length > 0) {
188
+
189
+ pNames.paths.forEach( function (path) {
190
+ const query = {
191
+ 'Path': path,
192
+ 'WithDecryption': true
193
+ };
194
+
195
+ DebugAndLog.debug("Param by path query", query);
196
+
197
+ paramResultsArr.push(AWS.ssm.getByPath(query));
198
+
199
+ });
200
+
201
+ }
202
+
203
+ // wait for all parameter request promises to resolve then combine
204
+ let promiseArray = await Promise.all(paramResultsArr); // wait
205
+ let results = [];
206
+ promiseArray.forEach( function (result) { // add parameter list in each result promise to an array
207
+ results.push.apply(results, result.Parameters);
208
+ //DebugAndLog.debug("added results", result.Parameters);
209
+ });
210
+
211
+ //DebugAndLog.debug("Parameters", results );
212
+
213
+ /* now that the promise has resolved and we've combined them,
214
+ crop off the path and store key and value within the group */
215
+ results.forEach(param => {
216
+ let nameSections = param.Name.split('/'); // get the last part of the name
217
+ const name = nameSections.pop(); // return last section and return as variable name
218
+ const groupPath = nameSections.join('/')+"/"; // since we removed the last section, join rest together for path
219
+
220
+ // put the parameter into its group
221
+ const obj = parameters.find(o => o.path === groupPath);
222
+ const group = obj.group;
223
+ if ( !(group in paramstore)) {
224
+ paramstore[group] = {};
225
+ }
226
+
227
+ // store key and value
228
+ paramstore[group][name] = param.Value;
229
+ });
230
+
231
+ }
232
+
233
+ // return an array of keys and values
234
+ return paramstore;
235
+ };
236
+
237
+ /**
238
+ * This is an intermediary wait
239
+ * @param {*} parameters
240
+ * @returns {Promise<array>} parameters and their values
241
+ */
242
+ static async _getParameters(parameters) {
243
+ return await this._getParametersFromStore(parameters);
244
+ };
245
+
246
+ /**
247
+ * @example
248
+ *
249
+ * let params = await this._initParameters(
250
+ * [
251
+ * {
252
+ * "group": "appone", // so we can do params.app.authOSTActionsUsername later
253
+ * "path": process.env.paramStorePath, // Lambda environment variable
254
+ * "names": [
255
+ * "authOSTActionsUsername",
256
+ * "authOSTActionsPassword",
257
+ * "authExLibrisAPIkey",
258
+ * "crypt_secureDataKey"
259
+ * ]
260
+ * }, // OR get all under a single path
261
+ * {
262
+ * "group": "app", // so we can do params.app.authOSTActionsUsername later
263
+ * "path": process.env.paramStorePath // Lambda environment variable
264
+ * }
265
+ * ]
266
+ * );
267
+ * @param {array} parameters An array of parameter locations
268
+ * @returns {object} Parameters from the parameter store
269
+ */
270
+ static async _initParameters(parameters) {
271
+ // make the call to get the parameters and wait before proceeding to the return
272
+ return await this._getParameters(parameters);
273
+ };
274
+
275
+ static async _initS3File(paths) {
276
+ return {};
277
+ };
278
+
279
+ static async _initDynamoDbRecord(query) {
280
+ return {};
281
+ };
282
+
283
+ };
284
+
285
+ module.exports = {
286
+ nodeVer,
287
+ nodeVerMajor,
288
+ nodeVerMinor,
289
+ nodeVerMajorMinor,
290
+ AWS,
291
+ AWSXRay,
292
+ APIRequest,
293
+ ImmutableObject,
294
+ Timer,
295
+ DebugAndLog,
296
+ Connection,
297
+ Connections,
298
+ ConnectionRequest,
299
+ ConnectionAuthentication,
300
+ RequestInfo,
301
+ ClientRequest,
302
+ ResponseDataModel,
303
+ Response,
304
+ _ConfigSuperClass,
305
+ CachedSSMParameter,
306
+ CachedSecret,
307
+ CachedParameterSecret,
308
+ CachedParameterSecrets,
309
+ jsonGenericResponse,
310
+ htmlGenericResponse,
311
+ rssGenericResponse,
312
+ xmlGenericResponse,
313
+ textGenericResponse,
314
+ printMsg,
315
+ sanitize,
316
+ obfuscate,
317
+ hashThisData
318
+ };