@fatehan/tsrp 1.3.42 → 1.3.43

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.
@@ -1 +1 @@
1
- {"version":3,"file":"system.io.d.ts","sourceRoot":"","sources":["../src/system.io.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,QAAQ,EAET,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AAEnD,QAAA,MAAM,YAAY,GAChB,UAAU,QAAQ,EAAE,EACpB,UAAU,QAAQ,EAAE,EACpB,MAAM,IAAI,KACT,OAAO,EAwGT,CAAC;AA6BF,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"system.io.d.ts","sourceRoot":"","sources":["../src/system.io.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,QAAQ,EAET,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AAEnD,QAAA,MAAM,YAAY,GAChB,UAAU,QAAQ,EAAE,EACpB,UAAU,QAAQ,EAAE,EACpB,MAAM,IAAI,KACT,OAAO,EA2FT,CAAC;AAqFF,eAAe,YAAY,CAAC"}
package/dist/system.io.js CHANGED
@@ -12,19 +12,8 @@ const SystemIoCast = (systemIo, deviceIo, data) => {
12
12
  const tempSystemIo = [...systemIo, ...deviceIo].sort((a, b) => a.sord - b.sord);
13
13
  tempSystemIo.forEach((io) => {
14
14
  var _a;
15
- let items = extractVariables(io.formula);
16
- let formula = io.formula;
17
- let hasIos = true;
18
- for (let i = 0; i < items.length; i++) {
19
- const value = getNestedPropertyValue(data, items[i]);
20
- if (value == null) {
21
- hasIos = false;
22
- break;
23
- }
24
- formula = formula.replace(new RegExp("\\$" + items[i], "g"), value);
25
- }
26
- if (hasIos) {
27
- const value = eval(formula);
15
+ const value = evalExpression(io.formula, data);
16
+ if (value !== null) {
28
17
  let style = io.activeStyle;
29
18
  if (io.type === devices_1.SystemIo_SystemIoType.BOOLEAN) {
30
19
  style =
@@ -33,13 +22,13 @@ const SystemIoCast = (systemIo, deviceIo, data) => {
33
22
  : io.activeStyle;
34
23
  }
35
24
  let newValue = value;
36
- if (io.unknown != null && io.unknown != undefined) {
25
+ if (io.unknown != null) {
37
26
  for (const key in io.unknown) {
38
27
  let UnkValue = io.unknown[key];
39
28
  if (key.toString().toLowerCase().includes("x")) {
40
- const formula = key.toLowerCase().replace(/x/g, newValue);
41
- const tempVal = eval(formula);
42
- if (tempVal) {
29
+ const formula = key.toLowerCase();
30
+ const tempVal = safeEvalFormula(formula, Number(newValue));
31
+ if (tempVal !== null && tempVal) {
43
32
  newValue = UnkValue;
44
33
  io.unit = "";
45
34
  }
@@ -54,14 +43,16 @@ const SystemIoCast = (systemIo, deviceIo, data) => {
54
43
  }
55
44
  let hidden = false;
56
45
  io.hidden.forEach((item) => {
57
- if (item.toLowerCase().includes("x")) {
58
- const formula = item.toLowerCase().replace(/x/g, newValue);
59
- if (eval(formula)) {
46
+ const lower = item.toLowerCase();
47
+ if (lower.includes("x")) {
48
+ const formula = lower;
49
+ const ok = safeEvalCondition(formula, Number(newValue));
50
+ if (ok === true) {
60
51
  hidden = true;
61
52
  }
62
53
  }
63
54
  else {
64
- if (newValue.toString().includes(item)) {
55
+ if (String(newValue).includes(item)) {
65
56
  hidden = true;
66
57
  }
67
58
  }
@@ -71,7 +62,7 @@ const SystemIoCast = (systemIo, deviceIo, data) => {
71
62
  newValue = Number(newValue).toFixed(io.DecimalCount);
72
63
  }
73
64
  else if (io.DecimalCount === 0) {
74
- newValue = Math.round(newValue).toString();
65
+ newValue = Math.round(Number(newValue)).toString();
75
66
  }
76
67
  if (io.SeparatorsCount && io.SeparatorsCount > 0) {
77
68
  newValue = Separator(String(newValue), (_a = io.SeparatorsCount) !== null && _a !== void 0 ? _a : 0);
@@ -99,24 +90,78 @@ function Separator(value, seprator) {
99
90
  const formattedInt = intPart.replace(regex, ",");
100
91
  return decimalPart ? `${formattedInt}.${decimalPart}` : formattedInt;
101
92
  }
102
- function extractVariables(str) {
103
- const matches = str.match(/\$(\w+(?:\.\w+)*)/g);
104
- if (!matches)
105
- return [];
106
- return matches.map((match) => match.substring(1));
93
+ function getValueByPath(obj, path) {
94
+ const parts = path
95
+ .replace(/\[(\w+)\]/g, '.$1')
96
+ .split('.')
97
+ .filter(Boolean);
98
+ let current = obj;
99
+ for (const key of parts) {
100
+ if (current == null)
101
+ return undefined;
102
+ current = current[key];
103
+ }
104
+ return current;
107
105
  }
108
- function getNestedPropertyValue(obj, propertyPath) {
109
- const properties = propertyPath.split(".");
110
- let currentObj = obj;
111
- for (const property of properties) {
112
- if (!Object.prototype.hasOwnProperty.call(currentObj, property)) {
106
+ function evalExpression(template, context) {
107
+ try {
108
+ const replaced = template.replace(/\$([A-Za-z0-9_.\[\]]+)/g, (_, expr) => {
109
+ const value = getValueByPath(context, expr);
110
+ if (value === undefined || value === null)
111
+ return "null";
112
+ if (typeof value === "string")
113
+ return JSON.stringify(value);
114
+ if (typeof value === "boolean")
115
+ return value ? "true" : "false";
116
+ return String(value);
117
+ });
118
+ if (!/^[0-9+\-*/ ().truefalsenull"']+$/.test(replaced)) {
113
119
  return null;
114
120
  }
115
- currentObj = currentObj[property];
116
- if (currentObj === null || currentObj === undefined) {
121
+ const fn = new Function(`return (${replaced});`);
122
+ const result = fn();
123
+ if (result === undefined || result === null)
124
+ return null;
125
+ if (typeof result === "number" || typeof result === "boolean" || typeof result === "string")
126
+ return result;
127
+ return null;
128
+ }
129
+ catch {
130
+ return null;
131
+ }
132
+ }
133
+ function safeEvalFormula(formula, xValue) {
134
+ try {
135
+ const replaced = formula.replace(/x/g, String(xValue));
136
+ if (!/^[0-9+\-*/ ().]+$/.test(replaced))
137
+ return null;
138
+ const fn = new Function(`return (${replaced});`);
139
+ const result = fn();
140
+ return typeof result === "number" && !isNaN(result) ? result : null;
141
+ }
142
+ catch {
143
+ return null;
144
+ }
145
+ }
146
+ function safeEvalCondition(formula, xValue) {
147
+ try {
148
+ const replaced = formula.replace(/x/g, String(xValue));
149
+ if (!/^[0-9+\-*/ %().<>=!&|]+$/.test(replaced)) {
117
150
  return null;
118
151
  }
152
+ const forbiddenPatterns = [/;;/, /process\b/, /constructor\b/, /require\b/, /global\b/];
153
+ if (forbiddenPatterns.some((rx) => rx.test(replaced)))
154
+ return null;
155
+ const fn = new Function(`return (${replaced});`);
156
+ const result = fn();
157
+ if (typeof result === "boolean")
158
+ return result;
159
+ if (typeof result === "number")
160
+ return Boolean(result);
161
+ return null;
162
+ }
163
+ catch {
164
+ return null;
119
165
  }
120
- return String(currentObj);
121
166
  }
122
167
  exports.default = SystemIoCast;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fatehan/tsrp",
3
- "version": "1.3.42",
3
+ "version": "1.3.43",
4
4
  "description": "fatehan main models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",