-
Notifications
You must be signed in to change notification settings - Fork 246
/
utils.ts
125 lines (107 loc) · 3.67 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import * as deepmerge from 'deepmerge';
import { flagOverriddenDefaults } from './override-warning-service';
import * as log from 'npmlog';
import * as crypto from 'crypto';
function isObject(val: object) {
return val != null && typeof val === 'object'
&& Object.prototype.toString.call(val) === '[object Object]';
}
function isPlainObject(o: object) {
if (Array.isArray(o) === true) {
return true;
}
if (isObject(o) === false) {
return false;
}
// If has modified constructor
const ctor = o.constructor;
if (typeof ctor !== 'function') {
return false;
}
// If has modified prototype
const prot = ctor.prototype;
if (isObject(prot) === false) {
return false;
}
// If constructor does not have an Object-specific method
if (prot.hasOwnProperty('isPrototypeOf') === false) {
return false;
}
// Most likely a plain Object
return true;
}
function combineMerge(target: any[], source: any[]) {
return target.concat(source);
}
function overwriteMerge(target: any[], source: any[]) {
target = source;
return target;
}
export function overrideProps(DefaultProps: object, userProps: object, concatArray: boolean = false): any {
// Notify the user via console output if defaults are overridden
const overrideWarningsEnabled = (process.env.overrideWarningsEnabled !== 'false');
if (overrideWarningsEnabled) {
flagOverriddenDefaults(DefaultProps, userProps);
}
// Override the sensible defaults with user provided props
if (concatArray) {
return deepmerge(DefaultProps, userProps, {
arrayMerge: combineMerge,
isMergeableObject: isPlainObject
});
} else {
return deepmerge(DefaultProps, userProps, {
arrayMerge: overwriteMerge,
isMergeableObject: isPlainObject
});
}
}
export function printWarning(message: string) {
// Style the log output
log.prefixStyle.bold = true;
log.prefixStyle.fg = 'red';
log.enableColor();
log.warn('AWS_SOLUTIONS_CONSTRUCTS_WARNING: ', message);
}
/**
* @summary Creates a resource name in the style of the CDK (string+hash)
* @param {string[]} parts - the various string components of the name (eg - stackName, solutions construct ID, L2 construct ID)
* @param {number} maxLength - the longest string that can be returned
* @returns {string} - a string with concatenated parts (truncated if neccessary) + a hash of the full concatenated parts
*
* This is based upon this discussion - https://github.com/aws/aws-cdk/issues/1424
*/
export function generateResourceName(
parts: string[],
maxLength: number
): string {
const hashLength = 12;
const maxPartLength = Math.floor( (maxLength - hashLength) / parts.length);
const sha256 = crypto.createHash("sha256");
let finalName: string = '';
parts.forEach((part) => {
sha256.update(part);
finalName += removeNonAlphanumeric(part.slice(0, maxPartLength));
});
const hash = sha256.digest("hex").slice(0, hashLength);
finalName += hash;
return finalName.toLowerCase();
}
/**
* Removes all non-alphanumeric characters in a string.
*/
function removeNonAlphanumeric(s: string) {
return s.replace(/[^A-Za-z0-9]/g, '');
}