mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-21 11:49:59 +00:00
feat(core): Add Tournament as the new default expression evaluator (#6964)
Github issue / Community forum post (link here to close automatically): --------- Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as tmpl from '@n8n_io/riot-tmpl';
|
||||
import { DateTime, Duration, Interval } from 'luxon';
|
||||
import * as tmpl from '@n8n_io/riot-tmpl';
|
||||
|
||||
import type {
|
||||
IDataObject,
|
||||
@@ -22,6 +22,7 @@ import type { Workflow } from './Workflow';
|
||||
import { extend, extendOptional } from './Extensions';
|
||||
import { extendedFunctions } from './Extensions/ExtendedFunctions';
|
||||
import { extendSyntax } from './Extensions/ExpressionExtension';
|
||||
import { evaluateExpression, setErrorHandler } from './ExpressionEvaluatorProxy';
|
||||
|
||||
const IS_FRONTEND_IN_DEV_MODE =
|
||||
typeof process === 'object' &&
|
||||
@@ -40,13 +41,10 @@ export const isExpressionError = (error: unknown): error is ExpressionError =>
|
||||
export const isTypeError = (error: unknown): error is TypeError =>
|
||||
error instanceof TypeError || (error instanceof Error && error.name === 'TypeError');
|
||||
|
||||
// Set it to use double curly brackets instead of single ones
|
||||
tmpl.brackets.set('{{ }}');
|
||||
|
||||
// Make sure that error get forwarded
|
||||
tmpl.tmpl.errorHandler = (error: Error) => {
|
||||
setErrorHandler((error: Error) => {
|
||||
if (isExpressionError(error)) throw error;
|
||||
};
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
const AsyncFunction = (async () => {}).constructor as FunctionConstructor;
|
||||
@@ -339,7 +337,7 @@ export class Expression {
|
||||
[Function, AsyncFunction].forEach(({ prototype }) =>
|
||||
Object.defineProperty(prototype, 'constructor', { value: fnConstructors.mock }),
|
||||
);
|
||||
return tmpl.tmpl(expression, data);
|
||||
return evaluateExpression(expression, data);
|
||||
} catch (error) {
|
||||
if (isExpressionError(error)) throw error;
|
||||
|
||||
|
||||
149
packages/workflow/src/ExpressionEvaluatorProxy.ts
Normal file
149
packages/workflow/src/ExpressionEvaluatorProxy.ts
Normal file
@@ -0,0 +1,149 @@
|
||||
import * as tmpl from '@n8n_io/riot-tmpl';
|
||||
import type { ReturnValue, TmplDifference } from '@n8n/tournament';
|
||||
import { Tournament } from '@n8n/tournament';
|
||||
import type { ExpressionEvaluatorType } from './Interfaces';
|
||||
import * as LoggerProxy from './LoggerProxy';
|
||||
|
||||
type Evaluator = (expr: string, data: unknown) => tmpl.ReturnValue;
|
||||
type ErrorHandler = (error: Error) => void;
|
||||
type DifferenceHandler = (expr: string) => void;
|
||||
|
||||
// Set it to use double curly brackets instead of single ones
|
||||
tmpl.brackets.set('{{ }}');
|
||||
|
||||
let errorHandler: ErrorHandler = () => {};
|
||||
let differenceHandler: DifferenceHandler = () => {};
|
||||
const differenceChecker = (diff: TmplDifference) => {
|
||||
try {
|
||||
if (diff.same) {
|
||||
return;
|
||||
}
|
||||
if (diff.has?.function || diff.has?.templateString) {
|
||||
return;
|
||||
}
|
||||
if (diff.expression === 'UNPARSEABLE') {
|
||||
differenceHandler(diff.expression);
|
||||
} else {
|
||||
differenceHandler(diff.expression.value);
|
||||
}
|
||||
} catch {
|
||||
LoggerProxy.error('Expression evaluator difference checker failed');
|
||||
}
|
||||
};
|
||||
const tournamentEvaluator = new Tournament(errorHandler, undefined);
|
||||
let evaluator: Evaluator = tmpl.tmpl;
|
||||
let currentEvaluatorType: ExpressionEvaluatorType = 'tmpl';
|
||||
let diffExpressions = false;
|
||||
|
||||
export const setErrorHandler = (handler: ErrorHandler) => {
|
||||
errorHandler = handler;
|
||||
tmpl.tmpl.errorHandler = handler;
|
||||
tournamentEvaluator.errorHandler = handler;
|
||||
};
|
||||
|
||||
export const setEvaluator = (evalType: ExpressionEvaluatorType) => {
|
||||
currentEvaluatorType = evalType;
|
||||
if (evalType === 'tmpl') {
|
||||
evaluator = tmpl.tmpl;
|
||||
} else if (evalType === 'tournament') {
|
||||
evaluator = tournamentEvaluator.execute.bind(tournamentEvaluator);
|
||||
}
|
||||
};
|
||||
|
||||
export const setDiffReporter = (reporter: (expr: string) => void) => {
|
||||
differenceHandler = reporter;
|
||||
};
|
||||
|
||||
export const setDifferEnabled = (enabled: boolean) => {
|
||||
diffExpressions = enabled;
|
||||
};
|
||||
|
||||
const diffCache: Record<string, TmplDifference | null> = {};
|
||||
|
||||
export const checkEvaluatorDifferences = (expr: string): TmplDifference | null => {
|
||||
if (expr in diffCache) {
|
||||
return diffCache[expr];
|
||||
}
|
||||
let diff: TmplDifference | null;
|
||||
try {
|
||||
diff = tournamentEvaluator.tmplDiff(expr);
|
||||
} catch {
|
||||
// We don't include the expression for privacy reasons
|
||||
try {
|
||||
differenceHandler('ERROR');
|
||||
} catch {}
|
||||
diff = null;
|
||||
}
|
||||
|
||||
if (diff?.same === false) {
|
||||
differenceChecker(diff);
|
||||
}
|
||||
|
||||
diffCache[expr] = diff;
|
||||
return diff;
|
||||
};
|
||||
|
||||
export const getEvaluator = () => {
|
||||
return evaluator;
|
||||
};
|
||||
|
||||
export const evaluateExpression: Evaluator = (expr, data) => {
|
||||
if (!diffExpressions) {
|
||||
return evaluator(expr, data);
|
||||
}
|
||||
const diff = checkEvaluatorDifferences(expr);
|
||||
|
||||
// We already know that they're different so don't bother
|
||||
// evaluating with both evaluators
|
||||
if (!diff?.same) {
|
||||
return evaluator(expr, data);
|
||||
}
|
||||
|
||||
let tmplValue: tmpl.ReturnValue;
|
||||
let tournValue: ReturnValue;
|
||||
let wasTmplError = false;
|
||||
let tmplError: unknown;
|
||||
let wasTournError = false;
|
||||
let tournError: unknown;
|
||||
|
||||
try {
|
||||
tmplValue = tmpl.tmpl(expr, data);
|
||||
} catch (error) {
|
||||
tmplError = error;
|
||||
wasTmplError = true;
|
||||
}
|
||||
|
||||
try {
|
||||
tournValue = tournamentEvaluator.execute(expr, data);
|
||||
} catch (error) {
|
||||
tournError = error;
|
||||
wasTournError = true;
|
||||
}
|
||||
|
||||
if (
|
||||
wasTmplError !== wasTournError ||
|
||||
JSON.stringify(tmplValue!) !== JSON.stringify(tournValue!)
|
||||
) {
|
||||
try {
|
||||
if (diff.expression) {
|
||||
differenceHandler(diff.expression.value);
|
||||
} else {
|
||||
differenceHandler('VALUEDIFF');
|
||||
}
|
||||
} catch {
|
||||
LoggerProxy.error('Failed to report error difference');
|
||||
}
|
||||
}
|
||||
|
||||
if (currentEvaluatorType === 'tmpl') {
|
||||
if (wasTmplError) {
|
||||
throw tmplError;
|
||||
}
|
||||
return tmplValue!;
|
||||
}
|
||||
|
||||
if (wasTournError) {
|
||||
throw tournError;
|
||||
}
|
||||
return tournValue!;
|
||||
};
|
||||
@@ -2117,6 +2117,8 @@ export interface IPublicApiSettings {
|
||||
|
||||
export type ILogLevel = 'info' | 'debug' | 'warn' | 'error' | 'verbose' | 'silent';
|
||||
|
||||
export type ExpressionEvaluatorType = 'tmpl' | 'tournament';
|
||||
|
||||
export interface IN8nUISettings {
|
||||
endpointWebhook: string;
|
||||
endpointWebhookTest: string;
|
||||
@@ -2203,6 +2205,9 @@ export interface IN8nUISettings {
|
||||
variables: {
|
||||
limit: number;
|
||||
};
|
||||
expressions: {
|
||||
evaluator: ExpressionEvaluatorType;
|
||||
};
|
||||
mfa: {
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as LoggerProxy from './LoggerProxy';
|
||||
export * as ErrorReporterProxy from './ErrorReporterProxy';
|
||||
export * as ExpressionEvaluatorProxy from './ExpressionEvaluatorProxy';
|
||||
import * as NodeHelpers from './NodeHelpers';
|
||||
import * as ObservableObject from './ObservableObject';
|
||||
import * as TelemetryHelpers from './TelemetryHelpers';
|
||||
|
||||
Reference in New Issue
Block a user