kangaroo-expression
Version:
Secure expression evaluator with AST-based execution - A fast, safe, and powerful JavaScript-like expression language
93 lines • 2.82 kB
TypeScript
import type { Node } from 'acorn';
import type { SafeFunction } from './functions';
export interface ExpressionContext {
item?: any;
inputs?: Record<string, any[]>;
outputs?: Record<string, any[]>;
node?: {
id: string;
name?: string;
type?: string;
};
execution?: {
id: string;
state?: string;
};
[key: string]: any;
}
export interface EvaluationResult<T = any> {
success: boolean;
value?: T;
error?: string;
errorType?: 'syntax' | 'security' | 'runtime' | 'type' | 'complexity';
position?: {
line: number;
column: number;
};
metadata?: {
executionTime?: number;
complexity?: number;
accessedVariables?: string[];
calledFunctions?: string[];
};
}
export interface SecurityValidation {
isValid: boolean;
violations: SecurityViolation[];
metadata?: {
riskLevel: 'low' | 'medium' | 'high';
rulesChecked: string[];
validationTime?: number;
};
}
export interface SecurityViolation {
type: 'blocked_identifier' | 'blocked_property' | 'invalid_node_type' | 'blocked_pattern' | 'complexity_limit' | 'depth_limit';
message: string;
node?: Node;
position?: {
line: number;
column: number;
};
severity?: 'error' | 'warning';
suggestion?: string;
}
export interface ParsedExpression {
ast: Node;
dependencies: Set<string>;
functions: Set<string>;
complexity: number;
isSimple: boolean;
hasTemplates: boolean;
depth: number;
estimatedMemoryUsage?: number;
}
export interface EvaluatorOptions {
maxComplexity?: number;
maxDepth?: number;
enableDebugging?: boolean;
customFunctions?: SafeFunction[];
strictMode?: boolean;
timeout?: number;
enableCaching?: boolean;
maxCacheSize?: number;
collectMetrics?: boolean;
customSecurityRules?: SecurityRule[];
}
export type SupportedNodeType = 'MemberExpression' | 'CallExpression' | 'Literal' | 'Identifier' | 'BinaryExpression' | 'ConditionalExpression' | 'ArrayExpression' | 'ObjectExpression' | 'Property' | 'UnaryExpression' | 'LogicalExpression' | 'ArrowFunctionExpression' | 'TemplateLiteral' | 'TemplateElement';
export interface ASTNodeHandler<T extends Node = Node> {
nodeType: SupportedNodeType;
validate: (node: T) => SecurityValidation;
execute: (node: T, context: ExpressionContext) => any;
calculateComplexity?: (node: T) => number;
extractDependencies?: (node: T) => string[];
}
export interface SecurityRule {
id: string;
name: string;
description: string;
severity: 'error' | 'warning';
check: (node: Node, context?: ExpressionContext) => boolean;
message: string;
suggestion?: string;
}
//# sourceMappingURL=core.d.ts.map