@fatehan/tsrp 1.3.44 → 1.3.46

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,5 +1,6 @@
1
1
  import { ShownIo, SystemIo } from "./fatehan/devices/devices";
2
2
  import { Data } from "./fatehan/packets/dataModel";
3
3
  declare const SystemIoCast: (systemIo: SystemIo[], deviceIo: SystemIo[], data: Data) => ShownIo[];
4
+ export declare function evalExpressionSafe(template: string, context: any): number | boolean | string | null;
4
5
  export default SystemIoCast;
5
6
  //# sourceMappingURL=system.io.d.ts.map
@@ -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;AA2IF,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,EA4FT,CAAC;AAkGF,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,IAAI,CA4BnG;AACD,eAAe,YAAY,CAAC"}
package/dist/system.io.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.evalExpressionSafe = evalExpressionSafe;
3
4
  const devices_1 = require("./fatehan/devices/devices");
4
5
  const SystemIoCast = (systemIo, deviceIo, data) => {
5
6
  let list = [];
@@ -93,11 +94,15 @@ function Separator(value, seprator) {
93
94
  function safeEvalFormula(formula, xValue) {
94
95
  try {
95
96
  const replaced = formula.replace(/x/g, String(xValue));
96
- if (!/^[0-9+\-*/ ().]+$/.test(replaced))
97
+ if (!/^[0-9+\-*/ ().><=!&|]+$/.test(replaced))
97
98
  return null;
98
99
  const fn = new Function(`return (${replaced});`);
99
100
  const result = fn();
100
- return typeof result === "number" && !isNaN(result) ? result : null;
101
+ if (typeof result === "number" && !isNaN(result))
102
+ return result;
103
+ if (typeof result === "boolean")
104
+ return result;
105
+ return null;
101
106
  }
102
107
  catch {
103
108
  return null;
@@ -135,16 +140,20 @@ function getValueByPathCaseInsensitive(obj, path) {
135
140
  for (const rawPart of parts) {
136
141
  if (cur == null)
137
142
  return undefined;
138
- if (/^\d+$/.test(rawPart)) {
139
- const idx = Number(rawPart);
143
+ const isIndex = /^\d+$/.test(rawPart);
144
+ if (isIndex) {
145
+ const idx = rawPart;
140
146
  if (Array.isArray(cur) || cur instanceof Uint8Array) {
141
- cur = cur[idx];
147
+ cur = cur[Number(idx)];
142
148
  continue;
143
149
  }
144
- else {
145
- return undefined;
150
+ if (typeof cur === "object" && cur !== null && idx in cur) {
151
+ cur = cur[idx];
152
+ continue;
146
153
  }
154
+ return undefined;
147
155
  }
156
+ // حالت 3: object معمولی (case-insensitive)
148
157
  if (isPlainObject(cur)) {
149
158
  const lower = rawPart.toLowerCase();
150
159
  const foundKey = Object.keys(cur).find(k => k.toLowerCase() === lower);
@@ -164,7 +173,7 @@ function evalExpressionSafe(template, context) {
164
173
  try {
165
174
  if (typeof template !== 'string')
166
175
  return null;
167
- const replaced = template.replace(/\$([A-Za-z0-9_.\[\]]+)/gi, (_m, expr) => {
176
+ const replaced = template.replace(/\$([A-Za-z0-9_.\[\]]+)/gi, (_, expr) => {
168
177
  const value = getValueByPathCaseInsensitive(context, expr);
169
178
  if (value === undefined || value === null)
170
179
  return "null";
@@ -176,38 +185,20 @@ function evalExpressionSafe(template, context) {
176
185
  return JSON.stringify(value);
177
186
  if (value instanceof Uint8Array)
178
187
  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
- }
188
+ if (Array.isArray(value))
189
+ return `[${value.map(v => (typeof v === "string" ? JSON.stringify(v) : v)).join(',')}]`;
182
190
  return "null";
183
191
  });
184
- if (replaced.includes("null")) {
192
+ const cleaned = replaced.replace(/;/g, ' ').replace(/`/g, ' ').replace(/\\/g, ' ').trim();
193
+ if (!/^[0-9+\-*/ ().><=!&|,"'\[\]truefalsenull]*$/i.test(cleaned))
185
194
  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")
195
+ const fn = new Function(`return (${cleaned});`);
196
+ const result = fn();
197
+ if (typeof result === "number" && !isNaN(result))
198
+ return result;
199
+ if (typeof result === "boolean")
200
+ return result;
201
+ if (typeof result === "string")
211
202
  return result;
212
203
  return null;
213
204
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fatehan/tsrp",
3
- "version": "1.3.44",
3
+ "version": "1.3.46",
4
4
  "description": "fatehan main models",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",