2021-12-23 14:17:20 +00:00
|
|
|
/* eslint-disable no-console */
|
2021-08-27 09:25:28 +00:00
|
|
|
import { DataTree } from "entities/DataTree/dataTreeFactory";
|
2021-03-13 14:12:21 +00:00
|
|
|
import {
|
2021-06-21 11:09:51 +00:00
|
|
|
EvaluationError,
|
|
|
|
|
PropertyEvaluationErrorType,
|
2021-03-13 14:12:21 +00:00
|
|
|
} from "utils/DynamicBindingUtils";
|
|
|
|
|
import unescapeJS from "unescape-js";
|
2022-09-30 12:59:02 +00:00
|
|
|
import { LogObject, Severity } from "entities/AppsmithConsole";
|
2022-12-22 06:34:28 +00:00
|
|
|
import { ActionDescription } from "@appsmith/entities/DataTree/actionTriggers";
|
2022-09-30 12:59:02 +00:00
|
|
|
import userLogs from "./UserLog";
|
2022-10-10 06:39:14 +00:00
|
|
|
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
|
2022-12-22 06:34:28 +00:00
|
|
|
import { TriggerMeta } from "@appsmith/sagas/ActionExecution/ActionExecutionSagas";
|
2022-11-30 21:58:58 +00:00
|
|
|
import indirectEval from "./indirectEval";
|
2023-01-16 11:56:18 +00:00
|
|
|
import { JSFunctionProxy, JSProxy } from "workers/Evaluation/JSObject/JSProxy";
|
2022-12-21 17:14:47 +00:00
|
|
|
import { DOM_APIS } from "./SetupDOM";
|
|
|
|
|
import { JSLibraries, libraryReservedIdentifiers } from "../common/JSLibrary";
|
2022-12-23 10:04:39 +00:00
|
|
|
import { errorModifier, FoundPromiseInSyncEvalError } from "./errorModifier";
|
2023-01-10 00:25:39 +00:00
|
|
|
import { addDataTreeToContext } from "@appsmith/workers/Evaluation/Actions";
|
2021-03-13 14:12:21 +00:00
|
|
|
|
|
|
|
|
export type EvalResult = {
|
|
|
|
|
result: any;
|
2021-06-21 11:09:51 +00:00
|
|
|
errors: EvaluationError[];
|
2021-12-23 14:17:20 +00:00
|
|
|
triggers?: ActionDescription[];
|
2022-09-04 11:58:05 +00:00
|
|
|
logs?: LogObject[];
|
2021-03-13 14:12:21 +00:00
|
|
|
};
|
|
|
|
|
|
2021-06-21 11:09:51 +00:00
|
|
|
export enum EvaluationScriptType {
|
|
|
|
|
EXPRESSION = "EXPRESSION",
|
|
|
|
|
ANONYMOUS_FUNCTION = "ANONYMOUS_FUNCTION",
|
2022-02-28 09:35:43 +00:00
|
|
|
ASYNC_ANONYMOUS_FUNCTION = "ASYNC_ANONYMOUS_FUNCTION",
|
2021-07-07 09:59:44 +00:00
|
|
|
TRIGGERS = "TRIGGERS",
|
2021-06-21 11:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-07 12:33:15 +00:00
|
|
|
export const ScriptTemplate = "<<string>>";
|
|
|
|
|
|
2021-10-05 13:52:27 +00:00
|
|
|
export const EvaluationScripts: Record<EvaluationScriptType, string> = {
|
2021-09-17 10:31:45 +00:00
|
|
|
[EvaluationScriptType.EXPRESSION]: `
|
2021-07-15 04:59:04 +00:00
|
|
|
function closedFunction () {
|
2021-10-07 12:33:15 +00:00
|
|
|
const result = ${ScriptTemplate}
|
2021-07-15 04:59:04 +00:00
|
|
|
return result;
|
|
|
|
|
}
|
2022-02-04 12:28:46 +00:00
|
|
|
closedFunction.call(THIS_CONTEXT)
|
2021-07-15 04:59:04 +00:00
|
|
|
`,
|
2021-09-17 10:31:45 +00:00
|
|
|
[EvaluationScriptType.ANONYMOUS_FUNCTION]: `
|
2021-07-15 04:59:04 +00:00
|
|
|
function callback (script) {
|
|
|
|
|
const userFunction = script;
|
2022-02-04 12:28:46 +00:00
|
|
|
const result = userFunction?.apply(THIS_CONTEXT, ARGUMENTS);
|
2021-07-15 04:59:04 +00:00
|
|
|
return result;
|
|
|
|
|
}
|
2021-10-07 12:33:15 +00:00
|
|
|
callback(${ScriptTemplate})
|
2021-07-15 04:59:04 +00:00
|
|
|
`,
|
2022-02-28 09:35:43 +00:00
|
|
|
[EvaluationScriptType.ASYNC_ANONYMOUS_FUNCTION]: `
|
|
|
|
|
async function callback (script) {
|
|
|
|
|
const userFunction = script;
|
|
|
|
|
const result = await userFunction?.apply(THIS_CONTEXT, ARGUMENTS);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
callback(${ScriptTemplate})
|
|
|
|
|
`,
|
2021-09-17 10:31:45 +00:00
|
|
|
[EvaluationScriptType.TRIGGERS]: `
|
2021-12-23 14:17:20 +00:00
|
|
|
async function closedFunction () {
|
|
|
|
|
const result = await ${ScriptTemplate};
|
|
|
|
|
return result;
|
2021-07-15 04:59:04 +00:00
|
|
|
}
|
2022-02-04 12:28:46 +00:00
|
|
|
closedFunction.call(THIS_CONTEXT);
|
2021-07-15 04:59:04 +00:00
|
|
|
`,
|
2021-06-21 11:09:51 +00:00
|
|
|
};
|
|
|
|
|
|
2022-09-30 01:31:05 +00:00
|
|
|
const topLevelWorkerAPIs = Object.keys(self).reduce((acc, key: string) => {
|
|
|
|
|
acc[key] = true;
|
|
|
|
|
return acc;
|
|
|
|
|
}, {} as any);
|
|
|
|
|
|
|
|
|
|
function resetWorkerGlobalScope() {
|
|
|
|
|
for (const key of Object.keys(self)) {
|
2022-12-21 17:14:47 +00:00
|
|
|
if (topLevelWorkerAPIs[key] || DOM_APIS[key]) continue;
|
|
|
|
|
//TODO: Remove this once we have a better way to handle this
|
|
|
|
|
if (["evaluationVersion", "window", "document", "location"].includes(key))
|
|
|
|
|
continue;
|
|
|
|
|
if (JSLibraries.find((lib) => lib.accessor.includes(key))) continue;
|
|
|
|
|
if (libraryReservedIdentifiers[key]) continue;
|
|
|
|
|
try {
|
|
|
|
|
// @ts-expect-error: Types are not available
|
|
|
|
|
delete self[key];
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// @ts-expect-error: Types are not available
|
|
|
|
|
self[key] = undefined;
|
|
|
|
|
}
|
2022-09-30 01:31:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 20:31:08 +00:00
|
|
|
export const getScriptType = (
|
2021-12-23 14:17:20 +00:00
|
|
|
evalArgumentsExist = false,
|
2021-07-07 09:59:44 +00:00
|
|
|
isTriggerBased = false,
|
2021-08-26 04:45:17 +00:00
|
|
|
): EvaluationScriptType => {
|
2021-07-07 09:59:44 +00:00
|
|
|
let scriptType = EvaluationScriptType.EXPRESSION;
|
2022-02-28 09:35:43 +00:00
|
|
|
if (evalArgumentsExist && isTriggerBased) {
|
|
|
|
|
scriptType = EvaluationScriptType.ASYNC_ANONYMOUS_FUNCTION;
|
|
|
|
|
} else if (evalArgumentsExist && !isTriggerBased) {
|
2021-07-07 09:59:44 +00:00
|
|
|
scriptType = EvaluationScriptType.ANONYMOUS_FUNCTION;
|
2022-02-28 09:35:43 +00:00
|
|
|
} else if (isTriggerBased && !evalArgumentsExist) {
|
2021-07-07 09:59:44 +00:00
|
|
|
scriptType = EvaluationScriptType.TRIGGERS;
|
|
|
|
|
}
|
2021-08-26 04:45:17 +00:00
|
|
|
return scriptType;
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-21 17:14:47 +00:00
|
|
|
export const additionalLibrariesNames: string[] = [];
|
|
|
|
|
|
2021-10-05 13:52:27 +00:00
|
|
|
export const getScriptToEval = (
|
2021-08-26 04:45:17 +00:00
|
|
|
userScript: string,
|
2021-09-17 10:31:45 +00:00
|
|
|
type: EvaluationScriptType,
|
2021-08-26 04:45:17 +00:00
|
|
|
): string => {
|
2021-10-07 12:33:15 +00:00
|
|
|
// Using replace here would break scripts with replacement patterns (ex: $&, $$)
|
|
|
|
|
const buffer = EvaluationScripts[type].split(ScriptTemplate);
|
|
|
|
|
return `${buffer[0]}${userScript}${buffer[1]}`;
|
2021-06-21 11:09:51 +00:00
|
|
|
};
|
|
|
|
|
|
2021-10-05 13:52:27 +00:00
|
|
|
const beginsWithLineBreakRegex = /^\s+|\s+$/;
|
2022-12-23 10:04:39 +00:00
|
|
|
|
|
|
|
|
export type EvalContext = Record<string, any>;
|
|
|
|
|
type ResolvedFunctions = Record<string, any>;
|
|
|
|
|
export interface createEvaluationContextArgs {
|
2022-07-22 20:31:08 +00:00
|
|
|
dataTree: DataTree;
|
2022-12-23 10:04:39 +00:00
|
|
|
resolvedFunctions: ResolvedFunctions;
|
2022-07-22 20:31:08 +00:00
|
|
|
context?: EvaluateContext;
|
|
|
|
|
isTriggerBased: boolean;
|
2022-12-23 10:04:39 +00:00
|
|
|
evalArguments?: Array<unknown>;
|
2022-07-22 20:31:08 +00:00
|
|
|
// Whether not to add functions like "run", "clear" to entity in global data
|
|
|
|
|
skipEntityFunctions?: boolean;
|
2022-12-30 12:31:08 +00:00
|
|
|
JSFunctionProxy?: JSFunctionProxy;
|
2022-07-22 20:31:08 +00:00
|
|
|
}
|
2022-12-23 10:04:39 +00:00
|
|
|
/**
|
|
|
|
|
* This method created an object with dataTree and appsmith's framework actions that needs to be added to worker global scope for the JS code evaluation to then consume it.
|
|
|
|
|
*
|
|
|
|
|
* Example:
|
|
|
|
|
* - For `eval("Table1.tableData")` code to work as expected, we define Table1.tableData in worker global scope and for that we use `createEvaluationContext` to get the object to set in global scope.
|
|
|
|
|
*/
|
|
|
|
|
export const createEvaluationContext = (args: createEvaluationContextArgs) => {
|
2022-07-22 20:31:08 +00:00
|
|
|
const {
|
|
|
|
|
context,
|
|
|
|
|
dataTree,
|
|
|
|
|
evalArguments,
|
|
|
|
|
isTriggerBased,
|
2022-12-30 12:31:08 +00:00
|
|
|
JSFunctionProxy,
|
2022-07-22 20:31:08 +00:00
|
|
|
resolvedFunctions,
|
|
|
|
|
skipEntityFunctions,
|
|
|
|
|
} = args;
|
|
|
|
|
|
2022-12-23 10:04:39 +00:00
|
|
|
const EVAL_CONTEXT: EvalContext = {};
|
2021-10-05 13:52:27 +00:00
|
|
|
///// Adding callback data
|
2022-12-23 10:04:39 +00:00
|
|
|
EVAL_CONTEXT.ARGUMENTS = evalArguments;
|
2022-02-04 12:28:46 +00:00
|
|
|
//// Adding contextual data not part of data tree
|
2022-12-23 10:04:39 +00:00
|
|
|
EVAL_CONTEXT.THIS_CONTEXT = context?.thisContext || {};
|
|
|
|
|
|
|
|
|
|
if (context?.globalContext) {
|
|
|
|
|
Object.assign(EVAL_CONTEXT, context.globalContext);
|
2022-02-11 10:52:27 +00:00
|
|
|
}
|
2022-12-23 10:04:39 +00:00
|
|
|
|
|
|
|
|
addDataTreeToContext({
|
|
|
|
|
EVAL_CONTEXT,
|
|
|
|
|
dataTree,
|
|
|
|
|
skipEntityFunctions: !!skipEntityFunctions,
|
|
|
|
|
eventType: context?.eventType,
|
|
|
|
|
isTriggerBased,
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-30 12:31:08 +00:00
|
|
|
assignJSFunctionsToContext(EVAL_CONTEXT, resolvedFunctions, JSFunctionProxy);
|
2022-12-23 10:04:39 +00:00
|
|
|
|
|
|
|
|
return EVAL_CONTEXT;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const assignJSFunctionsToContext = (
|
|
|
|
|
EVAL_CONTEXT: EvalContext,
|
|
|
|
|
resolvedFunctions: ResolvedFunctions,
|
2022-12-30 12:31:08 +00:00
|
|
|
JSFunctionProxy?: JSFunctionProxy,
|
2022-12-23 10:04:39 +00:00
|
|
|
) => {
|
|
|
|
|
const jsObjectNames = Object.keys(resolvedFunctions || {});
|
|
|
|
|
for (const jsObjectName of jsObjectNames) {
|
|
|
|
|
const resolvedObject = resolvedFunctions[jsObjectName];
|
|
|
|
|
const jsObject = EVAL_CONTEXT[jsObjectName];
|
|
|
|
|
const jsObjectFunction: Record<string, Record<"data", unknown>> = {};
|
|
|
|
|
if (!jsObject) continue;
|
|
|
|
|
for (const fnName of Object.keys(resolvedObject)) {
|
|
|
|
|
const fn = resolvedObject[fnName];
|
|
|
|
|
if (typeof fn !== "function") continue;
|
|
|
|
|
// Investigate promisify of JSObject function confirmation
|
|
|
|
|
// Task: https://github.com/appsmithorg/appsmith/issues/13289
|
|
|
|
|
// Previous implementation commented code: https://github.com/appsmithorg/appsmith/pull/18471
|
|
|
|
|
const data = jsObject[fnName]?.data;
|
2022-12-30 12:31:08 +00:00
|
|
|
jsObjectFunction[fnName] = JSFunctionProxy
|
|
|
|
|
? JSFunctionProxy(fn, jsObjectName + "." + fnName)
|
|
|
|
|
: fn;
|
2022-12-23 10:04:39 +00:00
|
|
|
if (!!data) {
|
|
|
|
|
jsObjectFunction[fnName]["data"] = data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EVAL_CONTEXT[jsObjectName] = Object.assign({}, jsObject, jsObjectFunction);
|
2021-10-05 13:52:27 +00:00
|
|
|
}
|
2021-06-21 11:09:51 +00:00
|
|
|
};
|
|
|
|
|
|
2021-12-14 08:30:43 +00:00
|
|
|
export function sanitizeScript(js: string) {
|
2021-12-02 10:03:43 +00:00
|
|
|
// We remove any line breaks from the beginning of the script because that
|
|
|
|
|
// makes the final function invalid. We also unescape any escaped characters
|
|
|
|
|
// so that eval can happen
|
|
|
|
|
const trimmedJS = js.replace(beginsWithLineBreakRegex, "");
|
2021-12-14 08:30:43 +00:00
|
|
|
return self.evaluationVersion > 1 ? trimmedJS : unescapeJS(trimmedJS);
|
2021-12-02 10:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:30:43 +00:00
|
|
|
/** Define a context just for this script
|
2022-02-04 12:28:46 +00:00
|
|
|
* thisContext will define it on the `this`
|
|
|
|
|
* globalContext will define it globally
|
2022-01-14 06:20:01 +00:00
|
|
|
* requestId is used for completing promises
|
2021-12-14 08:30:43 +00:00
|
|
|
*/
|
|
|
|
|
export type EvaluateContext = {
|
2022-02-04 12:28:46 +00:00
|
|
|
thisContext?: Record<string, any>;
|
|
|
|
|
globalContext?: Record<string, any>;
|
2021-12-23 14:17:20 +00:00
|
|
|
requestId?: string;
|
2022-10-10 06:39:14 +00:00
|
|
|
eventType?: EventType;
|
2022-10-17 17:10:17 +00:00
|
|
|
triggerMeta?: TriggerMeta;
|
2021-12-14 08:30:43 +00:00
|
|
|
};
|
|
|
|
|
|
2021-12-23 14:17:20 +00:00
|
|
|
export const getUserScriptToEvaluate = (
|
|
|
|
|
userScript: string,
|
|
|
|
|
isTriggerBased: boolean,
|
2021-06-21 11:09:51 +00:00
|
|
|
evalArguments?: Array<any>,
|
2021-12-23 14:17:20 +00:00
|
|
|
) => {
|
|
|
|
|
const unescapedJS = sanitizeScript(userScript);
|
2022-07-22 20:31:08 +00:00
|
|
|
// If nothing is present to evaluate, return
|
2021-12-23 14:17:20 +00:00
|
|
|
if (!unescapedJS.length) {
|
2021-12-21 14:30:19 +00:00
|
|
|
return {
|
2021-12-23 14:17:20 +00:00
|
|
|
script: "",
|
2021-12-21 14:30:19 +00:00
|
|
|
};
|
|
|
|
|
}
|
2021-12-23 14:17:20 +00:00
|
|
|
const scriptType = getScriptType(!!evalArguments, isTriggerBased);
|
|
|
|
|
const script = getScriptToEval(unescapedJS, scriptType);
|
2022-07-22 20:31:08 +00:00
|
|
|
return { script };
|
2021-12-23 14:17:20 +00:00
|
|
|
};
|
2021-12-21 14:30:19 +00:00
|
|
|
|
2021-12-23 14:17:20 +00:00
|
|
|
export default function evaluateSync(
|
|
|
|
|
userScript: string,
|
|
|
|
|
dataTree: DataTree,
|
|
|
|
|
resolvedFunctions: Record<string, any>,
|
2022-02-11 10:52:27 +00:00
|
|
|
isJSCollection: boolean,
|
2022-02-04 12:28:46 +00:00
|
|
|
context?: EvaluateContext,
|
2021-12-23 14:17:20 +00:00
|
|
|
evalArguments?: Array<any>,
|
2022-09-07 06:23:47 +00:00
|
|
|
skipLogsOperations = false,
|
2021-12-23 14:17:20 +00:00
|
|
|
): EvalResult {
|
2021-06-21 11:09:51 +00:00
|
|
|
return (function() {
|
2022-09-30 01:31:05 +00:00
|
|
|
resetWorkerGlobalScope();
|
2022-07-22 20:31:08 +00:00
|
|
|
const errors: EvaluationError[] = [];
|
2022-09-04 11:58:05 +00:00
|
|
|
let logs: LogObject[] = [];
|
2021-06-21 11:09:51 +00:00
|
|
|
let result;
|
2023-01-09 08:04:53 +00:00
|
|
|
|
2022-09-07 06:23:47 +00:00
|
|
|
// skipping log reset if the js collection is being evaluated without run
|
|
|
|
|
// Doing this because the promise execution is losing logs in the process due to resets
|
2023-01-09 08:04:53 +00:00
|
|
|
if (!skipLogsOperations) userLogs.resetLogs();
|
|
|
|
|
|
2021-03-13 14:12:21 +00:00
|
|
|
/**** Setting the eval context ****/
|
2022-12-23 10:04:39 +00:00
|
|
|
const evalContext: EvalContext = createEvaluationContext({
|
2021-12-23 14:17:20 +00:00
|
|
|
dataTree,
|
2021-10-05 13:52:27 +00:00
|
|
|
resolvedFunctions,
|
2022-02-04 12:28:46 +00:00
|
|
|
context,
|
2021-10-05 13:52:27 +00:00
|
|
|
evalArguments,
|
2022-12-23 10:04:39 +00:00
|
|
|
isTriggerBased: isJSCollection,
|
2022-07-22 20:31:08 +00:00
|
|
|
});
|
2022-12-23 10:04:39 +00:00
|
|
|
|
2023-01-30 05:27:12 +00:00
|
|
|
evalContext.ALLOW_SYNC = true;
|
2022-12-23 10:04:39 +00:00
|
|
|
|
2022-07-22 20:31:08 +00:00
|
|
|
const { script } = getUserScriptToEvaluate(
|
2021-12-23 14:17:20 +00:00
|
|
|
userScript,
|
|
|
|
|
false,
|
|
|
|
|
evalArguments,
|
|
|
|
|
);
|
2022-12-23 10:04:39 +00:00
|
|
|
|
2021-12-23 14:17:20 +00:00
|
|
|
// If nothing is present to evaluate, return instead of evaluating
|
|
|
|
|
if (!script.length) {
|
|
|
|
|
return {
|
|
|
|
|
errors: [],
|
|
|
|
|
result: undefined,
|
|
|
|
|
triggers: [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 14:12:21 +00:00
|
|
|
// Set it to self so that the eval function can have access to it
|
|
|
|
|
// as global data. This is what enables access all appsmith
|
|
|
|
|
// entity properties from the global context
|
2022-12-23 10:04:39 +00:00
|
|
|
Object.assign(self, evalContext);
|
2021-03-13 14:12:21 +00:00
|
|
|
|
2021-06-21 11:09:51 +00:00
|
|
|
try {
|
2022-11-30 21:58:58 +00:00
|
|
|
result = indirectEval(script);
|
2022-12-23 10:04:39 +00:00
|
|
|
if (result instanceof Promise) {
|
|
|
|
|
/**
|
|
|
|
|
* If a promise is returned in sync field then show the error to help understand sync field doesn't await to resolve promise.
|
|
|
|
|
* NOTE: Awaiting for promise will make sync field evaluation slower.
|
|
|
|
|
*/
|
|
|
|
|
throw new FoundPromiseInSyncEvalError();
|
|
|
|
|
}
|
2022-06-21 13:57:34 +00:00
|
|
|
} catch (error) {
|
2021-06-21 11:09:51 +00:00
|
|
|
errors.push({
|
2022-12-23 10:04:39 +00:00
|
|
|
errorMessage: errorModifier.run(error as Error),
|
2021-06-21 11:09:51 +00:00
|
|
|
severity: Severity.ERROR,
|
|
|
|
|
raw: script,
|
|
|
|
|
errorType: PropertyEvaluationErrorType.PARSE,
|
2021-12-23 14:17:20 +00:00
|
|
|
originalBinding: userScript,
|
2021-06-21 11:09:51 +00:00
|
|
|
});
|
2021-12-23 14:17:20 +00:00
|
|
|
} finally {
|
2022-09-19 06:29:04 +00:00
|
|
|
if (!skipLogsOperations) logs = userLogs.flushLogs();
|
2023-01-30 05:27:12 +00:00
|
|
|
evalContext.ALLOW_SYNC = false;
|
2022-12-23 10:04:39 +00:00
|
|
|
for (const entityName in evalContext) {
|
|
|
|
|
if (evalContext.hasOwnProperty(entityName)) {
|
|
|
|
|
// @ts-expect-error: Types are not available
|
|
|
|
|
delete self[entityName];
|
|
|
|
|
}
|
2021-12-23 14:17:20 +00:00
|
|
|
}
|
2021-06-21 11:09:51 +00:00
|
|
|
}
|
2022-09-04 11:58:05 +00:00
|
|
|
return { result, errors, logs };
|
2021-12-23 14:17:20 +00:00
|
|
|
})();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function evaluateAsync(
|
|
|
|
|
userScript: string,
|
|
|
|
|
dataTree: DataTree,
|
|
|
|
|
resolvedFunctions: Record<string, any>,
|
|
|
|
|
context?: EvaluateContext,
|
|
|
|
|
evalArguments?: Array<any>,
|
|
|
|
|
) {
|
|
|
|
|
return (async function() {
|
2022-09-30 01:31:05 +00:00
|
|
|
resetWorkerGlobalScope();
|
2021-12-23 14:17:20 +00:00
|
|
|
const errors: EvaluationError[] = [];
|
|
|
|
|
let result;
|
2022-09-04 11:58:05 +00:00
|
|
|
let logs;
|
2023-01-09 08:04:53 +00:00
|
|
|
|
|
|
|
|
/**** JSObject function proxy method ****/
|
2022-12-30 12:31:08 +00:00
|
|
|
const { JSFunctionProxy, setEvaluationEnd } = new JSProxy();
|
2023-01-09 08:04:53 +00:00
|
|
|
|
|
|
|
|
/**** console logs setup ****/
|
2022-09-04 11:58:05 +00:00
|
|
|
userLogs.resetLogs();
|
2022-10-17 17:10:17 +00:00
|
|
|
userLogs.setCurrentRequestInfo({
|
|
|
|
|
eventType: context?.eventType,
|
|
|
|
|
triggerMeta: context?.triggerMeta,
|
|
|
|
|
});
|
2023-01-09 08:04:53 +00:00
|
|
|
|
|
|
|
|
/**** Setting the eval context ****/
|
|
|
|
|
|
2022-12-23 10:04:39 +00:00
|
|
|
const evalContext: EvalContext = createEvaluationContext({
|
2021-12-23 14:17:20 +00:00
|
|
|
dataTree,
|
|
|
|
|
resolvedFunctions,
|
2022-12-21 17:14:47 +00:00
|
|
|
context,
|
2021-12-23 14:17:20 +00:00
|
|
|
evalArguments,
|
2022-12-30 12:31:08 +00:00
|
|
|
JSFunctionProxy,
|
2022-12-23 10:04:39 +00:00
|
|
|
isTriggerBased: true,
|
2022-07-22 20:31:08 +00:00
|
|
|
});
|
2023-01-09 08:04:53 +00:00
|
|
|
|
2022-07-22 20:31:08 +00:00
|
|
|
const { script } = getUserScriptToEvaluate(userScript, true, evalArguments);
|
2023-01-30 05:27:12 +00:00
|
|
|
evalContext.ALLOW_SYNC = false;
|
2022-12-23 10:04:39 +00:00
|
|
|
|
2021-12-23 14:17:20 +00:00
|
|
|
// Set it to self so that the eval function can have access to it
|
|
|
|
|
// as global data. This is what enables access all appsmith
|
|
|
|
|
// entity properties from the global context
|
2022-12-23 10:04:39 +00:00
|
|
|
Object.assign(self, evalContext);
|
2021-12-23 14:17:20 +00:00
|
|
|
|
|
|
|
|
try {
|
2022-11-30 21:58:58 +00:00
|
|
|
result = await indirectEval(script);
|
2022-09-04 11:58:05 +00:00
|
|
|
logs = userLogs.flushLogs();
|
2023-01-02 07:40:24 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
const error = e as Error;
|
|
|
|
|
const errorMessage = error.name
|
|
|
|
|
? `${error.name}: ${error.message}`
|
|
|
|
|
: `UncaughtPromiseRejection: ${error.message}`;
|
2021-12-23 14:17:20 +00:00
|
|
|
errors.push({
|
|
|
|
|
errorMessage: errorMessage,
|
|
|
|
|
severity: Severity.ERROR,
|
|
|
|
|
raw: script,
|
|
|
|
|
errorType: PropertyEvaluationErrorType.PARSE,
|
|
|
|
|
originalBinding: userScript,
|
|
|
|
|
});
|
2022-09-04 11:58:05 +00:00
|
|
|
logs = userLogs.flushLogs();
|
2021-12-23 14:17:20 +00:00
|
|
|
} finally {
|
2022-12-30 12:31:08 +00:00
|
|
|
setEvaluationEnd(true);
|
2023-01-30 05:27:12 +00:00
|
|
|
|
2022-09-04 11:58:05 +00:00
|
|
|
// Adding this extra try catch because there are cases when logs have child objects
|
|
|
|
|
// like functions or promises that cause issue in complete promise action, thus
|
|
|
|
|
// leading the app into a bad state.
|
2022-12-21 17:14:47 +00:00
|
|
|
return {
|
|
|
|
|
result,
|
|
|
|
|
errors,
|
|
|
|
|
logs,
|
|
|
|
|
triggers: Array.from(self.TRIGGER_COLLECTOR),
|
|
|
|
|
};
|
2021-09-21 06:02:45 +00:00
|
|
|
}
|
2021-12-23 14:17:20 +00:00
|
|
|
})();
|
|
|
|
|
}
|