@fatehan/tsrp 1.3.43 → 1.3.44

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,EA2FT,CAAC;AAqFF,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;AA2IF,eAAe,YAAY,CAAC"}
package/dist/system.io.js CHANGED
@@ -12,7 +12,7 @@ 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
- const value = evalExpression(io.formula, data);
15
+ const value = evalExpressionSafe(io.formula, data);
16
16
  if (value !== null) {
17
17
  let style = io.activeStyle;
18
18
  if (io.type === devices_1.SystemIo_SystemIoType.BOOLEAN) {
@@ -90,46 +90,6 @@ function Separator(value, seprator) {
90
90
  const formattedInt = intPart.replace(regex, ",");
91
91
  return decimalPart ? `${formattedInt}.${decimalPart}` : formattedInt;
92
92
  }
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;
105
- }
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)) {
119
- return null;
120
- }
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
93
  function safeEvalFormula(formula, xValue) {
134
94
  try {
135
95
  const replaced = formula.replace(/x/g, String(xValue));
@@ -164,4 +124,95 @@ function safeEvalCondition(formula, xValue) {
164
124
  return null;
165
125
  }
166
126
  }
127
+ function isPlainObject(v) {
128
+ return v !== null && typeof v === "object" && !Array.isArray(v) && !(v instanceof Uint8Array);
129
+ }
130
+ function getValueByPathCaseInsensitive(obj, path) {
131
+ if (!path)
132
+ return undefined;
133
+ const parts = path.replace(/\[(\d+)\]/g, '.$1').split('.').filter(Boolean);
134
+ let cur = obj;
135
+ for (const rawPart of parts) {
136
+ if (cur == null)
137
+ return undefined;
138
+ if (/^\d+$/.test(rawPart)) {
139
+ const idx = Number(rawPart);
140
+ if (Array.isArray(cur) || cur instanceof Uint8Array) {
141
+ cur = cur[idx];
142
+ continue;
143
+ }
144
+ else {
145
+ return undefined;
146
+ }
147
+ }
148
+ if (isPlainObject(cur)) {
149
+ const lower = rawPart.toLowerCase();
150
+ const foundKey = Object.keys(cur).find(k => k.toLowerCase() === lower);
151
+ if (foundKey !== undefined) {
152
+ cur = cur[foundKey];
153
+ continue;
154
+ }
155
+ else {
156
+ return undefined;
157
+ }
158
+ }
159
+ return undefined;
160
+ }
161
+ return cur;
162
+ }
163
+ function evalExpressionSafe(template, context) {
164
+ try {
165
+ if (typeof template !== 'string')
166
+ return null;
167
+ const replaced = template.replace(/\$([A-Za-z0-9_.\[\]]+)/gi, (_m, expr) => {
168
+ const value = getValueByPathCaseInsensitive(context, expr);
169
+ if (value === undefined || value === null)
170
+ return "null";
171
+ if (typeof value === "number")
172
+ return String(value);
173
+ if (typeof value === "boolean")
174
+ return value ? "true" : "false";
175
+ if (typeof value === "string")
176
+ return JSON.stringify(value);
177
+ if (value instanceof Uint8Array)
178
+ return `[${Array.from(value).join(',')}]`;
179
+ if (Array.isArray(value)) {
180
+ return `[${value.map(v => (typeof v === 'string' ? JSON.stringify(v) : String(v))).join(',')}]`;
181
+ }
182
+ return "null";
183
+ });
184
+ if (replaced.includes("null")) {
185
+ return null;
186
+ }
187
+ const cleaned = replaced
188
+ .replace(/;/g, ' ')
189
+ .replace(/`/g, ' ')
190
+ .replace(/\\/g, ' ')
191
+ .trim();
192
+ const allowedPattern = /^[0-9+\-*/ %().,<>=!&|"\[\]truefalsenull,':\s]*$/i;
193
+ if (!allowedPattern.test(cleaned))
194
+ return null;
195
+ const forbidden = [/constructor\b/i, /process\b/i, /require\b/i, /global\b/i, /__proto__\b/i];
196
+ if (forbidden.some(rx => rx.test(cleaned)))
197
+ return null;
198
+ if (cleaned.length === 0)
199
+ return null;
200
+ let result;
201
+ try {
202
+ const fn = new Function(`return (${cleaned});`);
203
+ result = fn();
204
+ }
205
+ catch {
206
+ return null;
207
+ }
208
+ if (result === undefined || result === null)
209
+ return null;
210
+ if (typeof result === "number" || typeof result === "boolean" || typeof result === "string")
211
+ return result;
212
+ return null;
213
+ }
214
+ catch {
215
+ return null;
216
+ }
217
+ }
167
218
  exports.default = SystemIoCast;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fatehan/tsrp",
3
- "version": "1.3.43",
3
+ "version": "1.3.44",
4
4
  "description": "fatehan main models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",