@xuda.io/xuda-worker-bundle-min 1.3.1512 → 1.3.1514

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/index.js +314 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -7753,7 +7753,7 @@ func.events.invoke = async function (event_id) {
7753
7753
  };
7754
7754
  func.expression = {};
7755
7755
 
7756
- func.expression.get = async function (SESSION_ID, valP, dsSessionP, sourceP, rowIdP, sourceActionP, secondPassP, calling_fieldIdP, fieldsP, debug_infoP, iterate_info, js_script_callback, jobNo, api_output_type) {
7756
+ func.expression.get_org = async function (SESSION_ID, valP, dsSessionP, sourceP, rowIdP, sourceActionP, secondPassP, calling_fieldIdP, fieldsP, debug_infoP, iterate_info, js_script_callback, jobNo, api_output_type) {
7757
7757
  class xu_class {
7758
7758
  async get() {
7759
7759
  var ret;
@@ -7873,7 +7873,7 @@ func.expression.get = async function (SESSION_ID, valP, dsSessionP, sourceP, row
7873
7873
  // var split = [];
7874
7874
  var var_Arr = [];
7875
7875
  const split = func.expression.parse(ret) || [];
7876
- console.log(valP, split);
7876
+ // console.log(valP, split);
7877
7877
  for await (const [arr_key, val] of Object.entries(split)) {
7878
7878
  // run each field
7879
7879
  const key = Number(arr_key);
@@ -8107,6 +8107,177 @@ func.expression.get = async function (SESSION_ID, valP, dsSessionP, sourceP, row
8107
8107
  return new_class.get();
8108
8108
  };
8109
8109
 
8110
+ func.expression.get = async function (SESSION_ID, valP, dsSessionP, sourceP, rowIdP, sourceActionP, secondPassP, calling_fieldIdP, fieldsP = {}, debug_infoP, iterate_info, js_script_callback, jobNo, api_output_type) {
8111
+ const evalJson = (text) => eval(`(${text})`);
8112
+ const replaceQuotes = (str) => {
8113
+ for (const [key, val] of Object.entries(fields)) {
8114
+ if (typeof val === 'string') str = str.replace(`"${val}"`, val.replace(/"/g, ''));
8115
+ }
8116
+ return str;
8117
+ };
8118
+
8119
+ let ret, error, warning, var_error_found;
8120
+ const fields = { ...fieldsP };
8121
+
8122
+ // Initial value processing
8123
+ if (valP === null || typeof valP === 'undefined') ret = '';
8124
+ else if (typeof valP === 'boolean') ret = valP ? 'Y' : 'N';
8125
+ else ret = valP.toString();
8126
+
8127
+ ret = ret.replace(/\&/g, '&');
8128
+ ret = func.utils.replace_studio_drive_url(SESSION_ID, ret);
8129
+
8130
+ // End results helper
8131
+ const endResults = () => {
8132
+ if (['update', 'javascript'].includes(sourceP) && typeof ret === 'string') {
8133
+ ret = replaceQuotes(ret);
8134
+ }
8135
+ if ((error || warning) && SESSION_OBJ[SESSION_ID]?.DS_GLB[dsSessionP]) {
8136
+ func.utils.debug.log(SESSION_ID, SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP].nodeId, {
8137
+ module: 'expression',
8138
+ action: sourceP,
8139
+ source: calling_fieldIdP,
8140
+ prop: ret,
8141
+ details: ret,
8142
+ result: ret,
8143
+ error,
8144
+ warning,
8145
+ fields: null,
8146
+ type: 'exp',
8147
+ prog_id: SESSION_OBJ[SESSION_ID].DS_GLB[dsSessionP].prog_id,
8148
+ debug_info: debug_infoP,
8149
+ });
8150
+ }
8151
+ return { result: ret, fields, error, warning, req: valP, var_error_found };
8152
+ };
8153
+
8154
+ // Handle non-variable cases
8155
+ const handleNonVariable = async () => {
8156
+ try {
8157
+ if (sourceP !== 'arguments') {
8158
+ if (ret.startsWith('_DATE_')) ret = ret.slice(6);
8159
+ else if (/^\d{4}-\d{2}-\d{2}$/.test(ret) || ret === 'self') return endResults();
8160
+ else ret = await func.expression.secure_eval(SESSION_ID, sourceP, ret, jobNo, dsSessionP, js_script_callback);
8161
+ } else {
8162
+ ret = ret.replace(/_NULL/gi, '');
8163
+ }
8164
+ return endResults();
8165
+ } catch (err) {
8166
+ error = err.message;
8167
+ return endResults();
8168
+ }
8169
+ };
8170
+
8171
+ // Early return for simple cases
8172
+ if (!func.expression.validate_variables(valP)) return await handleNonVariable();
8173
+ if (glb.emailRegex.test(await func.expression.secure_eval(SESSION_ID, sourceP, valP, jobNo, dsSessionP, js_script_callback))) {
8174
+ return await handleNonVariable();
8175
+ }
8176
+
8177
+ // Parse and process variables
8178
+ const split = func.expression.parse(ret) || [];
8179
+ const var_Arr = await Promise.all(
8180
+ split.map(async (val, key) => {
8181
+ const result = { value: val.value, fieldId: val.fieldId };
8182
+
8183
+ if (!val.fieldId) return result;
8184
+
8185
+ // Handle _THIS substitution
8186
+ if (val.fieldId.startsWith('_THIS') && calling_fieldIdP) {
8187
+ result.fieldId = val.fieldId.length === 5 ? calling_fieldIdP : calling_fieldIdP + val.fieldId.slice(5);
8188
+ }
8189
+
8190
+ // Fetch value from datasource
8191
+ const { ret: fetchedValue, fieldIdP } = await func.datasource.get_value(SESSION_ID, result.fieldId, dsSessionP, rowIdP);
8192
+ result.value = fetchedValue?.value ?? (sourceP === 'exp' ? fetchedValue?.value : '""');
8193
+ result.type = fetchedValue?.type;
8194
+
8195
+ // Handle iteration
8196
+ if (iterate_info) {
8197
+ if (iterate_info.iterator_key === fieldIdP) result.value = iterate_info._key;
8198
+ if (iterate_info.iterator_val === fieldIdP) result.value = iterate_info._val;
8199
+ }
8200
+
8201
+ // Process nested properties
8202
+ if (val.value.includes('[') || val.value.includes('.')) {
8203
+ const { property1, property2 } = await func.expression.get_property(val.value);
8204
+ const data = fetchedValue?.type === 'object' ? fetchedValue.value : fetchedValue?.prop;
8205
+
8206
+ if (key > 0 && val.value.includes(']') && !val.value.includes('[') && split[key - 1].value) {
8207
+ const prevData = split[key - 1].value;
8208
+ result.value = prevData[fieldIdP];
8209
+ if (val.value.includes('.') && prevData[fieldIdP]) {
8210
+ result.value = prevData[fieldIdP][property2] ?? '';
8211
+ }
8212
+ } else if (data) {
8213
+ if (property1) result.value = data[property1] ?? '';
8214
+ if (property2) result.value = (property1 ? data[property1]?.[property2] : data[property2]) ?? '';
8215
+ }
8216
+ }
8217
+
8218
+ fields[fieldIdP] = result.value;
8219
+ return result;
8220
+ }),
8221
+ );
8222
+
8223
+ // Final evaluation
8224
+ try {
8225
+ const res = var_Arr.map((val, key) => {
8226
+ if (sourceP === 'UI Property EXP' || sourceP === 'UI Attr EXP') {
8227
+ const { changed, value } = func.utils.get_drive_url(SESSION_ID, val.value, sourceP === 'UI Attr EXP' && var_Arr.length > 1);
8228
+ if (changed) return value;
8229
+ }
8230
+
8231
+ let value = val.value;
8232
+ if (var_Arr.length > 1) {
8233
+ if (!['DbQuery', 'alert', 'exp', 'api_rendered_output'].includes(sourceP) && ['string', 'date'].includes(val.type)) {
8234
+ value = `\`${value}\``;
8235
+ } else if (sourceP === 'api_rendered_output' && api_output_type === 'json' && ['string', 'date'].includes(val.type)) {
8236
+ value = `"${value}"`;
8237
+ }
8238
+ }
8239
+
8240
+ if (val.fieldId && typeof value === 'string') {
8241
+ if (['query', 'condition', 'range', 'sort', 'locate'].includes(sourceP)) value = value.replace(/↵|\r\n|\n|\r/g, '');
8242
+ if (['init', 'update', 'virtual'].includes(sourceP)) value = value.replace(/↵|\r\n|\n|\r/g, '\\n');
8243
+ if (typeof IS_PROCESS_SERVER !== 'undefined') value = value.replace(/↵|\r\n|\n|\r/g, '<br>');
8244
+ fields[val.fieldId] = value;
8245
+ }
8246
+
8247
+ if (typeof value === 'object' && var_Arr.length > 1) {
8248
+ value = Array.isArray(value) || var_Arr[key + 1]?.value?.includes('.') ? JSON.stringify(value) : `(${JSON.stringify(value)})`;
8249
+ }
8250
+
8251
+ if (!val.type === 'exp' && sourceP !== 'exp' && typeof value === 'string' && value.startsWith('@')) {
8252
+ warning = `Error encoding ${value}`;
8253
+ var_error_found = true;
8254
+ return '0';
8255
+ }
8256
+ return value;
8257
+ });
8258
+
8259
+ ret = res.length === 1 ? res[0] : res.join('');
8260
+ if (var_Arr.some((v) => v.type === 'exp') && sourceP !== 'exp' && !secondPassP) {
8261
+ const exp = await func.expression.get(SESSION_ID, ret, dsSessionP, sourceP, rowIdP, sourceActionP, true, calling_fieldIdP, fields, debug_infoP);
8262
+ ret = exp.res?.[0] ?? exp.result;
8263
+ Object.assign(fields, exp.fields);
8264
+ } else if (!secondPassP && !['arguments', 'api_rendered_output', 'DbQuery'].includes(sourceP)) {
8265
+ ret = await func.expression.secure_eval(SESSION_ID, sourceP, ret, jobNo, dsSessionP, js_script_callback);
8266
+ } else if (sourceP === 'DbQuery') {
8267
+ ret = JSON.stringify(evalJson(ret));
8268
+ }
8269
+
8270
+ if (typeof ret === 'string' && ret.startsWith('@')) {
8271
+ error = 'Error encoding @ var';
8272
+ var_error_found = true;
8273
+ }
8274
+ } catch (err) {
8275
+ error = err.message;
8276
+ }
8277
+
8278
+ return endResults();
8279
+ };
8280
+
8110
8281
  func.expression.parse_org = function (strP) {
8111
8282
  var extract_str = function (strP, posP) {
8112
8283
  if (!posP) posP = 0;
@@ -8288,7 +8459,7 @@ func.expression.parse = function (input) {
8288
8459
  return segments;
8289
8460
  };
8290
8461
 
8291
- func.expression.get_property = async function (valP) {
8462
+ func.expression.get_property_org = async function (valP) {
8292
8463
  async function secure_eval(val) {
8293
8464
  if (typeof IS_PROCESS_SERVER === 'undefined') {
8294
8465
  try {
@@ -8326,21 +8497,73 @@ func.expression.get_property = async function (valP) {
8326
8497
  property2: property2,
8327
8498
  };
8328
8499
  };
8329
- func.expression.validate_constant = function (valP) {
8500
+
8501
+ func.expression.get_property = async function (valP) {
8502
+ if (typeof valP !== 'string') return { property1: undefined, property2: undefined };
8503
+
8504
+ const secureEval = async (expr) => {
8505
+ if (typeof IS_PROCESS_SERVER === 'undefined') {
8506
+ try {
8507
+ return eval(expr);
8508
+ } catch (err) {
8509
+ console.error(err);
8510
+ return undefined;
8511
+ }
8512
+ }
8513
+ try {
8514
+ const vm = new VM.VM({
8515
+ sandbox: {
8516
+ func,
8517
+ SESSION_ID,
8518
+ SESSION_OBJ: { [SESSION_ID]: SESSION_OBJ[SESSION_ID] },
8519
+ },
8520
+ timeout: 1000,
8521
+ allowAsync: false,
8522
+ });
8523
+ return await vm.run(expr);
8524
+ } catch {
8525
+ return undefined; // Simplified error handling
8526
+ }
8527
+ };
8528
+
8529
+ let property1, property2;
8530
+ const bracketStart = valP.indexOf('[');
8531
+ const bracketEnd = valP.indexOf(']');
8532
+
8533
+ if (bracketStart > -1 && bracketEnd > bracketStart) {
8534
+ const expr = valP.slice(bracketStart + 1, bracketEnd);
8535
+ property1 = await secureEval(expr);
8536
+ }
8537
+
8538
+ const dotIndex = valP.indexOf('.');
8539
+ if (dotIndex > -1) {
8540
+ property2 = valP.slice(dotIndex + 1);
8541
+ }
8542
+
8543
+ return { property1, property2 };
8544
+ };
8545
+
8546
+ func.expression.validate_constant_org = function (valP) {
8330
8547
  var patt = /["']/;
8331
8548
  if (typeof valP === 'string' && patt.test(valP.substr(0, 1)) && patt.test(valP.substr(0, valP.length - 1))) return true;
8332
8549
  else return false;
8333
8550
  };
8334
- func.expression.validate_variables = function (valP) {
8551
+ func.expression.validate_variables_org = function (valP) {
8335
8552
  if (typeof valP === 'string' && valP.indexOf('@') > -1) return true;
8336
8553
  else return false;
8337
8554
  };
8338
- func.expression.remove_quotes = function (valP) {
8555
+ func.expression.remove_quotes_org = function (valP) {
8339
8556
  if (func.expression.validate_constant(valP)) return valP.substr(1, valP.length - 2);
8340
8557
  else return valP;
8341
8558
  };
8342
8559
 
8343
- func.expression.secure_eval = async function (SESSION_ID, sourceP, val, job_id, dsSessionP, js_script_callback, evt) {
8560
+ func.expression.validate_constant = (valP) => typeof valP === 'string' && /^["'].*["']$/.test(valP);
8561
+
8562
+ func.expression.validate_variables = (valP) => typeof valP === 'string' && valP.includes('@');
8563
+
8564
+ func.expression.remove_quotes = (valP) => (func.expression.validate_constant(valP) && typeof valP === 'string' ? valP.slice(1, -1) : valP);
8565
+
8566
+ func.expression.secure_eval_org = async function (SESSION_ID, sourceP, val, job_id, dsSessionP, js_script_callback, evt) {
8344
8567
  const api_utils = await func.common.get_module(SESSION_ID, 'xuda-api-library.mjs', {
8345
8568
  func,
8346
8569
  glb,
@@ -8441,3 +8664,87 @@ func.expression.secure_eval = async function (SESSION_ID, sourceP, val, job_id,
8441
8664
  }
8442
8665
  }
8443
8666
  };
8667
+
8668
+ func.expression.secure_eval = async function (SESSION_ID, sourceP, val, job_id, dsSessionP, js_script_callback, evt) {
8669
+ if (typeof val !== 'string') return val;
8670
+
8671
+ const xu = await func.common.get_module(SESSION_ID, 'xuda-api-library.mjs', {
8672
+ func,
8673
+ glb,
8674
+ SESSION_OBJ,
8675
+ SESSION_ID,
8676
+ APP_OBJ,
8677
+ dsSession: dsSessionP,
8678
+ job_id,
8679
+ });
8680
+
8681
+ const isServer = typeof IS_PROCESS_SERVER !== 'undefined' || typeof IS_DOCKER !== 'undefined';
8682
+
8683
+ // Client-side execution
8684
+ if (!isServer) {
8685
+ try {
8686
+ return eval(val);
8687
+ } catch {
8688
+ try {
8689
+ return JSON5.parse(val);
8690
+ } catch {
8691
+ return val;
8692
+ }
8693
+ }
8694
+ }
8695
+
8696
+ // Server-side execution
8697
+ const sandbox = {
8698
+ func,
8699
+ xu,
8700
+ SESSION_ID,
8701
+ SESSION_OBJ: { [SESSION_ID]: SESSION_OBJ[SESSION_ID] },
8702
+ callback: js_script_callback,
8703
+ job_id,
8704
+ ...(sourceP === 'javascript' ? { axios, got, FormData } : {}),
8705
+ };
8706
+
8707
+ const handleError = (err) => {
8708
+ console.error('Execution error:', err);
8709
+ func.events.delete_job(SESSION_ID, job_id);
8710
+ if (isServer && !SESSION_OBJ[SESSION_ID].crawler) {
8711
+ if (sourceP === 'javascript') {
8712
+ __.rpi.write_log(SESSION_OBJ[SESSION_ID].app_id, 'error', 'worker', 'vm error', err, null, val, 'func.expression.get.secure_eval');
8713
+ } else {
8714
+ __.db.add_error_log(SESSION_OBJ[SESSION_ID].app_id, 'api', err);
8715
+ }
8716
+ }
8717
+ return val; // Fallback to original value
8718
+ };
8719
+
8720
+ if (sourceP === 'javascript') {
8721
+ process.on('uncaughtException', handleError);
8722
+ try {
8723
+ const dir = path.join(_conf.studio_drive_path, SESSION_OBJ[SESSION_ID].app_id, 'node_modules');
8724
+ const script = new VM.VMScript(`try { ${val} } catch (e) { func.api.error(SESSION_ID, "nodejs error", e); console.error(e); func.events.delete_job(SESSION_ID, "${job_id}"); }`, { filename: dir, dirname: dir });
8725
+ const vm = new VM.NodeVM({
8726
+ require: { external: true },
8727
+ sandbox,
8728
+ timeout: 60000,
8729
+ });
8730
+ return await vm.run(script, { filename: dir, dirname: dir });
8731
+ } catch (err) {
8732
+ return handleError(err);
8733
+ }
8734
+ }
8735
+
8736
+ try {
8737
+ const vm = new VM.VM({
8738
+ sandbox,
8739
+ timeout: 1000,
8740
+ allowAsync: false,
8741
+ });
8742
+ return await vm.run(val);
8743
+ } catch {
8744
+ try {
8745
+ return JSON5.parse(val);
8746
+ } catch {
8747
+ return val;
8748
+ }
8749
+ }
8750
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/xuda-worker-bundle-min",
3
- "version": "1.3.1512",
3
+ "version": "1.3.1514",
4
4
  "description": "xuda framework min",
5
5
  "main": "index.js",
6
6
  "scripts": {