import { ExecutionOptions, DynaJsConfig, ModuleImports } from './types'; /** * DynaJs - A safe dynamic code execution library * * Provides secure execution of dynamic JavaScript code using new Function() * with configurable security policies and sandboxing capabilities. * * @example * ```typescript * const loader = new DynaJs({ * defaultImports: { MyClass }, * validateCode: true, * allowBrowserAPIs: false * }); * * const result = loader.executeSync(`return new MyClass()`); * ``` */ export default class DynaJs { private config; /** * Creates a new DynaJs instance * * @param config - Configuration options for the DynaJs instance * @param config.defaultTimeout - Default timeout for async execution in milliseconds (default: 5000) * @param config.defaultStrict - Whether to use strict mode by default (default: true) * @param config.allowedGlobals - Whitelist of allowed global variables (empty = allow all defaultImports) * @param config.blockedGlobals - Blacklist of blocked global variables * @param config.defaultImports - Pre-imported classes/functions available to dynamic code * @param config.allowTimers - Allow setTimeout/setInterval in dynamic code (default: false) * @param config.allowDynamicImports - Allow import()/require() in dynamic code (default: false) * @param config.validateCode - Enable code validation before execution (default: true) * @param config.allowBrowserAPIs - Allow access to browser APIs like window/document (default: false) */ constructor(config?: DynaJsConfig); /** * Asynchronously execute dynamic code with timeout support * * @template T - The expected return type of the executed code * @param code - The JavaScript code to execute * @param options - Execution options * @param options.context - Additional context variables for the code * @param options.timeout - Execution timeout in milliseconds (overrides defaultTimeout) * @param options.strict - Whether to use strict mode (overrides defaultStrict) * @param options.imports - Additional imports for this execution only * @returns Promise resolving to the execution result */ execute(code: string, options?: ExecutionOptions): Promise; /** * Synchronously execute dynamic code and return result directly * * @template T - The expected return type of the executed code * @param code - The JavaScript code to execute * @param options - Execution options * @param options.context - Additional context variables for the code * @param options.strict - Whether to use strict mode (overrides defaultStrict) * @param options.imports - Additional imports for this execution only * @returns The direct result of code execution */ executeSync(code: string, options?: ExecutionOptions): T; /** * Execute code with a timeout constraint * * @private * @template T - The expected return type * @param code - The JavaScript code to execute * @param context - Execution context with available variables * @param timeout - Maximum execution time in milliseconds * @param strict - Whether to use strict mode * @returns Promise resolving to the execution result * @throws {Error} If execution exceeds timeout or runtime error occurs */ private executeWithTimeout; /** * Core code execution engine using new Function() * * @private * @param code - The JavaScript code to execute * @param context - Execution context with available variables * @param strict - Whether to use strict mode * @returns The execution result * @throws {Error} If code execution fails */ private executeCode; /** * Create a reusable function from dynamic code * * @template T - The function signature type * @param code - The JavaScript code for the function body * @param paramNames - Array of parameter names for the function * @param options - Execution options * @param options.context - Additional context variables * @param options.strict - Whether to use strict mode * @param options.imports - Additional imports * @returns A function that can be called multiple times * @throws {Error} If function creation fails * * @example * ```typescript * const validator = loader.createFunction<(data: any) => boolean>(` * return function(data) { * return data.name && data.email; * }; * `); * const isValid = validator({ name: 'John', email: 'john@example.com' }); * ``` */ createFunction any>(code: string, paramNames?: string[], options?: ExecutionOptions): T; /** * Synchronously execute code with additional temporary imports * * @template T - The expected return type * @param code - The JavaScript code to execute * @param imports - Additional imports to merge with defaultImports * @param options - Additional execution options * @returns The direct result of code execution * @throws {Error} If code validation fails or runtime error occurs * * @example * ```typescript * const result = loader.executeWithImports(` * return new CustomComponent(); * `, { * CustomComponent: MyCustomComponent * }); * ``` */ executeWithImports(code: string, imports: ModuleImports, options?: ExecutionOptions): T; /** * Asynchronously execute code with additional temporary imports * * @template T - The expected return type * @param code - The JavaScript code to execute * @param imports - Additional imports to merge with defaultImports * @param options - Additional execution options (including timeout) * @returns Promise resolving to the execution result * @throws {Error} If code validation fails, execution times out, or runtime error occurs * * @example * ```typescript * const result = await loader.executeWithImportsAsync(` * return await fetchData(); * `, { * fetchData: myFetchFunction * }, { timeout: 5000 }); * ``` */ executeWithImportsAsync(code: string, imports: ModuleImports, options?: ExecutionOptions): Promise; /** * Singleton instance of DynaJs * @private */ static instance: DynaJs | null; /** * Initialize the singleton instance of DynaJs * * @param config - Configuration options for the singleton instance * @returns The initialized singleton instance * * @example * ```typescript * DynaJs.initialize({ * defaultImports: { MyClass, MyFunction }, * validateCode: true * }); * ``` */ static initialize(config: DynaJsConfig): DynaJs; /** * Get the singleton instance of DynaJs * * @returns The singleton instance * @throws {Error} If DynaJs hasn't been initialized * * @example * ```typescript * const loader = DynaJs.getInstance(); * const result = loader.executeSync(`return 1 + 1`); * ``` */ static getInstance(): DynaJs; /** * Reset the singleton instance (useful for testing) * * @example * ```typescript * DynaJs.reset(); * DynaJs.initialize(newConfig); * ``` */ static reset(): void; /** * Prepare the execution context by merging user context, imports, and applying security filters * * Execution flow: * 1. Create safe context (filtering browser APIs based on config) * 2. Merge with defaultImports and additional imports * 3. Apply allowedGlobals whitelist filter if configured * * @private * @param userContext - User-provided context variables * @param imports - Additional imports for this execution * @returns The final execution context * * @remarks * If allowedGlobals is set and not empty, only variables in the whitelist will be available. * This happens AFTER merging defaultImports, so make sure to include all needed imports * in the allowedGlobals array if you use this feature. */ private prepareContext; }