@znemz/cfn-include 2.1.18 → 2.1.19
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/replaceEnv.js +38 -2
- package/package.json +1 -1
package/lib/replaceEnv.js
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
const passThrough = (template) => template
|
|
2
2
|
const replaceProcessEnv = (template) => replaceEnv(template, process.env, false)
|
|
3
3
|
|
|
4
|
+
// Cache for compiled regex patterns to avoid re-creating them
|
|
5
|
+
const regexCache = new Map();
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Escape special regex characters in a string
|
|
9
|
+
* @param {string} str - String to escape
|
|
10
|
+
* @returns {string} Escaped string safe for regex
|
|
11
|
+
*/
|
|
12
|
+
function escapeRegExp(str) {
|
|
13
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get a cached regex for variable substitution
|
|
18
|
+
* @param {string} key - Variable name
|
|
19
|
+
* @param {boolean} withBraces - Whether to match ${key} format (true) or $key format (false)
|
|
20
|
+
* @returns {RegExp} Cached regex pattern
|
|
21
|
+
*/
|
|
22
|
+
function getVarRegex(key, withBraces) {
|
|
23
|
+
const cacheKey = `${key}:${withBraces}`;
|
|
24
|
+
if (!regexCache.has(cacheKey)) {
|
|
25
|
+
const escaped = escapeRegExp(key);
|
|
26
|
+
const pattern = withBraces ? `\\$\\{${escaped}\\}` : `\\$${escaped}`;
|
|
27
|
+
regexCache.set(cacheKey, new RegExp(pattern, 'g'));
|
|
28
|
+
}
|
|
29
|
+
return regexCache.get(cacheKey);
|
|
30
|
+
}
|
|
31
|
+
|
|
4
32
|
const replaceEnv = (template, inject = {}, doEnv) => {
|
|
5
33
|
processTemplate = doEnv ? replaceProcessEnv : passThrough;
|
|
6
34
|
|
|
@@ -15,9 +43,17 @@ const replaceEnv = (template, inject = {}, doEnv) => {
|
|
|
15
43
|
const key = keys[i];
|
|
16
44
|
let val = inject[key];
|
|
17
45
|
|
|
46
|
+
// Use cached regex patterns instead of creating new ones each time
|
|
47
|
+
const bareRegex = getVarRegex(key, false);
|
|
48
|
+
const bracedRegex = getVarRegex(key, true);
|
|
49
|
+
|
|
50
|
+
// Reset lastIndex for global regex reuse
|
|
51
|
+
bareRegex.lastIndex = 0;
|
|
52
|
+
bracedRegex.lastIndex = 0;
|
|
53
|
+
|
|
18
54
|
template = template
|
|
19
|
-
.replace(
|
|
20
|
-
.replace(
|
|
55
|
+
.replace(bareRegex, val)
|
|
56
|
+
.replace(bracedRegex, val);
|
|
21
57
|
}
|
|
22
58
|
|
|
23
59
|
// return template;
|