@dmptool/utils 1.0.4 → 1.0.6
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/README.md +101 -82
- package/dist/cloudFormation.d.ts +4 -1
- package/dist/cloudFormation.js +17 -6
- package/dist/dynamo.d.ts +34 -12
- package/dist/dynamo.js +178 -86
- package/dist/eventBridge.d.ts +3 -1
- package/dist/eventBridge.js +41 -27
- package/dist/general.d.ts +8 -0
- package/dist/general.js +10 -1
- package/dist/logger.d.ts +9 -9
- package/dist/logger.js +13 -13
- package/dist/maDMP.d.ts +12 -3
- package/dist/maDMP.js +112 -72
- package/dist/rds.d.ts +11 -1
- package/dist/rds.js +33 -21
- package/dist/s3.d.ts +13 -4
- package/dist/s3.js +79 -39
- package/dist/ssm.d.ts +5 -1
- package/dist/ssm.js +22 -17
- package/package.json +2 -1
package/dist/rds.js
CHANGED
|
@@ -38,19 +38,20 @@ const mysql = __importStar(require("mysql2/promise"));
|
|
|
38
38
|
const date_fns_1 = require("date-fns");
|
|
39
39
|
/**
|
|
40
40
|
* Connect to the MySQL database and run a query.
|
|
41
|
+
*
|
|
42
|
+
* @param params Connection parameters to use for the connection.
|
|
41
43
|
* @returns A new connection to the MySQL database.
|
|
42
44
|
*/
|
|
43
|
-
const createConnection = async () => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
host
|
|
47
|
-
port
|
|
48
|
-
user
|
|
49
|
-
password
|
|
50
|
-
database
|
|
51
|
-
multipleStatements: false
|
|
52
|
-
};
|
|
53
|
-
return mysql.createConnection(args);
|
|
45
|
+
const createConnection = async (params) => {
|
|
46
|
+
const { host, port, user, password, database } = params;
|
|
47
|
+
return mysql.createConnection({
|
|
48
|
+
host,
|
|
49
|
+
port,
|
|
50
|
+
user,
|
|
51
|
+
password,
|
|
52
|
+
database,
|
|
53
|
+
multipleStatements: false
|
|
54
|
+
});
|
|
54
55
|
};
|
|
55
56
|
/**
|
|
56
57
|
* Function to convert the incoming value into an appropriate type for insertion
|
|
@@ -90,19 +91,30 @@ const prepareValue = (val, type) => {
|
|
|
90
91
|
* Function to run a SQL query against the MySQL database.
|
|
91
92
|
*
|
|
92
93
|
* @param query the SQL query to run
|
|
94
|
+
* @param connectionParams the connection parameters to use for the connection
|
|
93
95
|
* @param params the parameters to use in the query
|
|
94
96
|
* @returns the results of the query
|
|
95
97
|
*/
|
|
96
98
|
// Runs the provided SQL query and returns the results
|
|
97
|
-
const queryTable = async (query, params = []) => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
99
|
+
const queryTable = async (connectionParams, query, params = []) => {
|
|
100
|
+
try {
|
|
101
|
+
if (!connectionParams || !connectionParams || !query || query.trim() === '') {
|
|
102
|
+
throw new Error('Missing connectionParameters or query');
|
|
103
|
+
}
|
|
104
|
+
const connection = await createConnection(connectionParams);
|
|
105
|
+
// Remove all tabs and new lines
|
|
106
|
+
const sql = query.split(/[\s\t\n]+/).join(' ');
|
|
107
|
+
// Prepare the values for the query
|
|
108
|
+
const vals = params.map((val) => prepareValue(val, typeof val));
|
|
109
|
+
// Run the query and then close the connection
|
|
110
|
+
connectionParams.logger.debug({ query, params }, 'Running MySQL query');
|
|
111
|
+
const [results, fields] = await connection.query(sql, vals);
|
|
112
|
+
await connection.end();
|
|
113
|
+
return { results: results, fields: fields };
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
connectionParams.logger.fatal({ query, params, error }, 'Error running MySQL query');
|
|
117
|
+
return { results: [], fields: [] };
|
|
118
|
+
}
|
|
107
119
|
};
|
|
108
120
|
exports.queryTable = queryTable;
|
package/dist/s3.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Logger } from 'pino';
|
|
1
2
|
import { GetObjectCommandOutput, PutObjectCommandOutput } from "@aws-sdk/client-s3";
|
|
2
3
|
export interface DMPToolListObjectsOutput {
|
|
3
4
|
key: string;
|
|
@@ -7,38 +8,46 @@ export interface DMPToolListObjectsOutput {
|
|
|
7
8
|
/**
|
|
8
9
|
* List the contents of a bucket that match a key prefix.
|
|
9
10
|
*
|
|
11
|
+
* @param logger The logger to use for logging.
|
|
10
12
|
* @param bucket The name of the bucket to list.
|
|
11
13
|
* @param keyPrefix The prefix to filter the results by.
|
|
14
|
+
* @param region The region to generate the URL in. Defaults to 'us-west-2'.
|
|
12
15
|
* @returns A list of objects that match the key prefix, or undefined if the
|
|
13
16
|
* bucket or key prefix are invalid.
|
|
14
17
|
*/
|
|
15
|
-
export declare const listObjects: (bucket: string, keyPrefix: string) => Promise<DMPToolListObjectsOutput[] | []>;
|
|
18
|
+
export declare const listObjects: (logger: Logger, bucket: string, keyPrefix: string, region?: string) => Promise<DMPToolListObjectsOutput[] | []>;
|
|
16
19
|
/**
|
|
17
20
|
* Get an object from the specified bucket.
|
|
18
21
|
*
|
|
22
|
+
* @param logger The logger to use for logging.
|
|
19
23
|
* @param bucket The name of the bucket to get the object from.
|
|
20
24
|
* @param key The key of the object to get.
|
|
25
|
+
* @param region The region to generate the URL in. Defaults to 'us-west-2'.
|
|
21
26
|
* @returns The object, or undefined if the bucket or key are invalid.
|
|
22
27
|
*/
|
|
23
|
-
export declare const getObject: (bucket: string, key: string) => Promise<GetObjectCommandOutput | undefined>;
|
|
28
|
+
export declare const getObject: (logger: Logger, bucket: string, key: string, region?: string) => Promise<GetObjectCommandOutput | undefined>;
|
|
24
29
|
/**
|
|
25
30
|
* Put an object into the specified bucket.
|
|
26
31
|
*
|
|
32
|
+
* @param logger The logger to use for logging.
|
|
27
33
|
* @param bucket The name of the bucket to put the object into.
|
|
28
34
|
* @param key The key of the object to put.
|
|
29
35
|
* @param body The object to put.
|
|
30
36
|
* @param contentType The content type of the object.
|
|
31
37
|
* @param contentEncoding The content encoding of the object.
|
|
38
|
+
* @param region The region to generate the URL in. Defaults to 'us-west-2'.
|
|
32
39
|
* @returns The response from the S3 putObject operation, or undefined if the
|
|
33
40
|
* bucket or key are invalid.
|
|
34
41
|
*/
|
|
35
|
-
export declare const putObject: (bucket: string, key: string, body: any, contentType?: string, contentEncoding?: string) => Promise<PutObjectCommandOutput | undefined>;
|
|
42
|
+
export declare const putObject: (logger: Logger, bucket: string, key: string, body: any, contentType?: string, contentEncoding?: string, region?: string) => Promise<PutObjectCommandOutput | undefined>;
|
|
36
43
|
/**
|
|
37
44
|
* Generate a Pre-signed URL for an S3 object.
|
|
38
45
|
*
|
|
46
|
+
* @param logger The logger to use for logging.
|
|
39
47
|
* @param bucket The name of the bucket to generate the URL for.
|
|
40
48
|
* @param key The key of the object to generate the URL for.
|
|
41
49
|
* @param usePutMethod Whether to use the PUT method for the URL. Defaults to false.
|
|
50
|
+
* @param region The region to generate the URL in. Defaults to 'us-west-2'.
|
|
42
51
|
* @returns The Pre-signed URL, or undefined if the bucket or key are invalid.
|
|
43
52
|
*/
|
|
44
|
-
export declare const getPresignedURL: (bucket: string, key: string, usePutMethod?: boolean) => Promise<string | undefined>;
|
|
53
|
+
export declare const getPresignedURL: (logger: Logger, bucket: string, key: string, usePutMethod?: boolean, region?: string) => Promise<string | undefined>;
|
package/dist/s3.js
CHANGED
|
@@ -3,34 +3,44 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getPresignedURL = exports.putObject = exports.getObject = exports.listObjects = void 0;
|
|
4
4
|
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
5
5
|
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
6
|
-
const s3Client = new client_s3_1.S3Client({});
|
|
7
6
|
/**
|
|
8
7
|
* List the contents of a bucket that match a key prefix.
|
|
9
8
|
*
|
|
9
|
+
* @param logger The logger to use for logging.
|
|
10
10
|
* @param bucket The name of the bucket to list.
|
|
11
11
|
* @param keyPrefix The prefix to filter the results by.
|
|
12
|
+
* @param region The region to generate the URL in. Defaults to 'us-west-2'.
|
|
12
13
|
* @returns A list of objects that match the key prefix, or undefined if the
|
|
13
14
|
* bucket or key prefix are invalid.
|
|
14
15
|
*/
|
|
15
|
-
const listObjects = async (bucket, keyPrefix) => {
|
|
16
|
-
if (bucket && bucket.trim() !== '') {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
const listObjects = async (logger, bucket, keyPrefix, region = 'us-west-2') => {
|
|
17
|
+
if (logger && bucket && bucket.trim() !== '') {
|
|
18
|
+
const s3Client = new client_s3_1.S3Client({ region });
|
|
19
|
+
try {
|
|
20
|
+
const listObjectsCommand = new client_s3_1.ListObjectsV2Command({
|
|
21
|
+
Bucket: bucket,
|
|
22
|
+
Prefix: keyPrefix
|
|
23
|
+
});
|
|
24
|
+
logger.debug({ bucket, keyPrefix }, 'Listing objects in bucket');
|
|
25
|
+
const response = await s3Client.send(listObjectsCommand);
|
|
26
|
+
if (Array.isArray(response.Contents) && response.Contents.length > 0) {
|
|
27
|
+
const objects = [];
|
|
28
|
+
for (const obj of response.Contents) {
|
|
29
|
+
if (obj && obj.Key && obj.LastModified && obj.Size) {
|
|
30
|
+
objects.push({
|
|
31
|
+
key: obj.Key,
|
|
32
|
+
lastModified: obj.LastModified,
|
|
33
|
+
size: obj.Size
|
|
34
|
+
});
|
|
35
|
+
}
|
|
31
36
|
}
|
|
37
|
+
logger.debug({ objects }, 'Found objects in bucket');
|
|
38
|
+
return objects;
|
|
32
39
|
}
|
|
33
|
-
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
logger.fatal({ bucket, error }, 'Error listing objects in bucket');
|
|
43
|
+
throw error;
|
|
34
44
|
}
|
|
35
45
|
}
|
|
36
46
|
return [];
|
|
@@ -39,14 +49,24 @@ exports.listObjects = listObjects;
|
|
|
39
49
|
/**
|
|
40
50
|
* Get an object from the specified bucket.
|
|
41
51
|
*
|
|
52
|
+
* @param logger The logger to use for logging.
|
|
42
53
|
* @param bucket The name of the bucket to get the object from.
|
|
43
54
|
* @param key The key of the object to get.
|
|
55
|
+
* @param region The region to generate the URL in. Defaults to 'us-west-2'.
|
|
44
56
|
* @returns The object, or undefined if the bucket or key are invalid.
|
|
45
57
|
*/
|
|
46
|
-
const getObject = async (bucket, key) => {
|
|
47
|
-
if (bucket && key && bucket.trim() !== '' && key.trim() !== '') {
|
|
48
|
-
|
|
49
|
-
|
|
58
|
+
const getObject = async (logger, bucket, key, region = 'us-west-2') => {
|
|
59
|
+
if (logger && bucket && key && bucket.trim() !== '' && key.trim() !== '') {
|
|
60
|
+
try {
|
|
61
|
+
const s3Client = new client_s3_1.S3Client({ region });
|
|
62
|
+
const command = new client_s3_1.GetObjectCommand({ Bucket: bucket, Key: key });
|
|
63
|
+
logger.debug({ bucket, key }, 'Getting object from bucket');
|
|
64
|
+
return await s3Client.send(command);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
logger.fatal({ bucket, key, error }, 'Error getting object from bucket');
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
50
70
|
}
|
|
51
71
|
return undefined;
|
|
52
72
|
};
|
|
@@ -54,24 +74,34 @@ exports.getObject = getObject;
|
|
|
54
74
|
/**
|
|
55
75
|
* Put an object into the specified bucket.
|
|
56
76
|
*
|
|
77
|
+
* @param logger The logger to use for logging.
|
|
57
78
|
* @param bucket The name of the bucket to put the object into.
|
|
58
79
|
* @param key The key of the object to put.
|
|
59
80
|
* @param body The object to put.
|
|
60
81
|
* @param contentType The content type of the object.
|
|
61
82
|
* @param contentEncoding The content encoding of the object.
|
|
83
|
+
* @param region The region to generate the URL in. Defaults to 'us-west-2'.
|
|
62
84
|
* @returns The response from the S3 putObject operation, or undefined if the
|
|
63
85
|
* bucket or key are invalid.
|
|
64
86
|
*/
|
|
65
|
-
const putObject = async (bucket, key, body, contentType = 'application/json', contentEncoding = 'utf-8') => {
|
|
66
|
-
if (bucket && key && bucket.trim() !== '' && key.trim() !== '') {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
87
|
+
const putObject = async (logger, bucket, key, body, contentType = 'application/json', contentEncoding = 'utf-8', region = 'us-west-2') => {
|
|
88
|
+
if (logger && bucket && key && bucket.trim() !== '' && key.trim() !== '') {
|
|
89
|
+
try {
|
|
90
|
+
const s3Client = new client_s3_1.S3Client({ region });
|
|
91
|
+
const command = new client_s3_1.PutObjectCommand({
|
|
92
|
+
Bucket: bucket,
|
|
93
|
+
Key: key,
|
|
94
|
+
Body: body,
|
|
95
|
+
ContentType: contentType,
|
|
96
|
+
ContentEncoding: contentEncoding,
|
|
97
|
+
});
|
|
98
|
+
logger.debug({ bucket, key }, 'Putting object into bucket');
|
|
99
|
+
return await s3Client.send(command);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
logger.fatal({ bucket, key, error }, 'Error putting object into bucket');
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
75
105
|
}
|
|
76
106
|
return undefined;
|
|
77
107
|
};
|
|
@@ -79,19 +109,29 @@ exports.putObject = putObject;
|
|
|
79
109
|
/**
|
|
80
110
|
* Generate a Pre-signed URL for an S3 object.
|
|
81
111
|
*
|
|
112
|
+
* @param logger The logger to use for logging.
|
|
82
113
|
* @param bucket The name of the bucket to generate the URL for.
|
|
83
114
|
* @param key The key of the object to generate the URL for.
|
|
84
115
|
* @param usePutMethod Whether to use the PUT method for the URL. Defaults to false.
|
|
116
|
+
* @param region The region to generate the URL in. Defaults to 'us-west-2'.
|
|
85
117
|
* @returns The Pre-signed URL, or undefined if the bucket or key are invalid.
|
|
86
118
|
*/
|
|
87
|
-
const getPresignedURL = async (bucket, key, usePutMethod = false) => {
|
|
88
|
-
if (bucket && key && bucket.trim() !== '' && key.trim() !== '') {
|
|
119
|
+
const getPresignedURL = async (logger, bucket, key, usePutMethod = false, region = 'us-west-2') => {
|
|
120
|
+
if (logger && bucket && key && bucket.trim() !== '' && key.trim() !== '') {
|
|
121
|
+
const s3Client = new client_s3_1.S3Client({ region });
|
|
89
122
|
const params = { Bucket: bucket, Key: key };
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
123
|
+
try {
|
|
124
|
+
const command = usePutMethod
|
|
125
|
+
? new client_s3_1.PutObjectCommand(params)
|
|
126
|
+
: new client_s3_1.GetObjectCommand(params);
|
|
127
|
+
logger.debug(Object.assign(Object.assign({}, params), { usePutMethod }), 'Generating presigned URL');
|
|
128
|
+
return await (0, s3_request_presigner_1.getSignedUrl)(s3Client, command, { expiresIn: 900 });
|
|
129
|
+
;
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
logger.fatal(Object.assign(Object.assign({}, params), { usePutMethod, error }), 'Error generating a presigned URL');
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
95
135
|
}
|
|
96
136
|
return undefined;
|
|
97
137
|
};
|
package/dist/ssm.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
import { EnvironmentEnum } from "./general";
|
|
2
|
+
import { Logger } from 'pino';
|
|
1
3
|
/**
|
|
2
4
|
* Retrieve a variable from the SSM Parameter store.
|
|
3
5
|
*
|
|
6
|
+
* @param logger The logger to use for logging.
|
|
4
7
|
* @param key The name of the variable to retrieve.
|
|
8
|
+
* @param env The environment to retrieve the variable from. Defaults to `EnvironmentEnum.DEV`.
|
|
5
9
|
* @returns The value of the variable, or undefined if the variable could not be found.
|
|
6
10
|
* @throws
|
|
7
11
|
*/
|
|
8
|
-
export declare const getSSMParameter: (key: string) => Promise<string | undefined>;
|
|
12
|
+
export declare const getSSMParameter: (logger: Logger, key: string, env?: EnvironmentEnum) => Promise<string | undefined>;
|
package/dist/ssm.js
CHANGED
|
@@ -1,32 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getSSMParameter = void 0;
|
|
4
|
-
const client_ssm_1 = require("@aws-sdk/client-ssm");
|
|
5
4
|
const general_1 = require("./general");
|
|
6
|
-
|
|
7
|
-
const client = new client_ssm_1.SSMClient();
|
|
8
|
-
const ENV = process.env.NODE_ENV === 'production'
|
|
9
|
-
? 'prd' : (process.env.NODE_ENV === 'staging'
|
|
10
|
-
? 'stg' : 'dev');
|
|
11
|
-
const KEY_PREFIX = `/uc3/dmp/tool/${ENV}/`;
|
|
5
|
+
const client_ssm_1 = require("@aws-sdk/client-ssm");
|
|
12
6
|
/**
|
|
13
7
|
* Retrieve a variable from the SSM Parameter store.
|
|
14
8
|
*
|
|
9
|
+
* @param logger The logger to use for logging.
|
|
15
10
|
* @param key The name of the variable to retrieve.
|
|
11
|
+
* @param env The environment to retrieve the variable from. Defaults to `EnvironmentEnum.DEV`.
|
|
16
12
|
* @returns The value of the variable, or undefined if the variable could not be found.
|
|
17
13
|
* @throws
|
|
18
14
|
*/
|
|
19
|
-
const getSSMParameter = async (key) => {
|
|
15
|
+
const getSSMParameter = async (logger, key, env = general_1.EnvironmentEnum.DEV) => {
|
|
20
16
|
var _a;
|
|
21
|
-
if (key && key.trim() !== '') {
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
if (logger && key && key.trim() !== '') {
|
|
18
|
+
// Create an SSM client
|
|
19
|
+
const client = new client_ssm_1.SSMClient();
|
|
20
|
+
const keyPrefix = `/uc3/dmp/tool/${env.toLowerCase()}/`;
|
|
21
|
+
logger.debug(`Fetching parameter ${keyPrefix}${key}`);
|
|
22
|
+
try {
|
|
23
|
+
const command = new client_ssm_1.GetParameterCommand({
|
|
24
|
+
Name: `${keyPrefix}${key}`,
|
|
25
|
+
WithDecryption: true
|
|
26
|
+
});
|
|
27
|
+
const response = await client.send(command);
|
|
28
|
+
if (!(0, general_1.isNullOrUndefined)(response) && !(0, general_1.isNullOrUndefined)(response.Parameter)) {
|
|
29
|
+
return (_a = response.Parameter) === null || _a === void 0 ? void 0 : _a.Value;
|
|
30
|
+
}
|
|
31
|
+
logger.warn(`Parameter ${keyPrefix}${key} not found.`);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
logger.fatal(`Error fetching parameter ${keyPrefix}${key}: ${error}`);
|
|
30
35
|
}
|
|
31
36
|
}
|
|
32
37
|
return undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dmptool/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Helper/Utility functions for use in the DMP Tool services. Particularly AWS tooling and maDMP serialization",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
"@aws-sdk/util-dynamodb": "^3.966.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
+
"@aws-sdk/util-stream-node": "^3.370.0",
|
|
54
55
|
"@eslint/js": "^9.26.0",
|
|
55
56
|
"@types/jest": "^29.5.14",
|
|
56
57
|
"@types/node": "^20.4.3",
|