@znemz/cfn-include 2.1.17 → 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.
Files changed (2) hide show
  1. package/lib/replaceEnv.js +38 -2
  2. package/package.json +3 -3
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(new RegExp(`\\$${key}`, "g"), val)
20
- .replace(new RegExp(`\\$\{${key}}`, "g"), val);
55
+ .replace(bareRegex, val)
56
+ .replace(bracedRegex, val);
21
57
  }
22
58
 
23
59
  // return template;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@znemz/cfn-include",
3
- "version": "2.1.17",
3
+ "version": "2.1.19",
4
4
  "description": "Preprocessor for CloudFormation templates with support for loops and flexible include statements",
5
5
  "keywords": [
6
6
  "aws",
@@ -45,7 +45,7 @@
45
45
  "dependencies": {
46
46
  "@aws-sdk/client-cloudformation": "^3.637.0",
47
47
  "@aws-sdk/client-s3": "^3.637.0",
48
- "@znemz/cft-utils": "0.1.31",
48
+ "@znemz/cft-utils": "0.1.33",
49
49
  "@znemz/sort-object": "^3.0.4",
50
50
  "aws-sdk-v3-proxy": "2.2.0",
51
51
  "bluebird": "^3.7.2",
@@ -73,7 +73,7 @@
73
73
  "npm-run-all": "4.1.5",
74
74
  "prettier": "3",
75
75
  "serve": "14.2.5",
76
- "sort-package-json": "3.6.0"
76
+ "sort-package-json": "3.6.1"
77
77
  },
78
78
  "engines": {
79
79
  "node": ">=20.19"