import { BadRequestException, Injectable, InternalServerErrorException, } from "@nestjs/common"; import { expect as playwrightExpect } from "@playwright/test"; import { parse } from "acorn"; import type { BrowserContext, Page } from "playwright"; import { URL } from "url"; import { downloadFile as downloadFileImpl } from "../common/file-downloader"; import { TraceLogger } from "../common/trace-logger"; import type { EnvironmentData } from "../environment/environment.entity"; import type { FileStorageService } from "../file/file-storage.service"; import type { DomNode } from "./dom-helpers"; import { dumpDom } from "./dom-helpers"; export interface ExecResult { result: unknown; } export type ScriptLogger = ( level: "log" | "warn" | "error", message: string, ) => void; /** The `context` object available inside user scripts as the sole argument. */ export interface ScriptContext { /** Playwright Page for the current session. */ page: Page; /** Playwright BrowserContext for the current session. */ browser: BrowserContext; /** All environment values for the current run (empty object if none set). */ env: Record; /** Returns the environment value for `key`, or throws if not defined. */ getEnv: (key: string) => string; /** Returns the credential value for `alias`, or throws if not found. */ getCredential: (alias: string) => unknown; /** Returns the serialised output of a previous step by order index (negative = relative). */ getStepOutput: (order: number) => Promise; /** Dumps the DOM of the current page, optionally scoped to a CSS selector. */ dumpDom: (selector?: string) => Promise; log: (...args: unknown[]) => void; warn: (...args: unknown[]) => void; error: (...args: unknown[]) => void; /** * Runs `fn` and requires it to return `true`. Logs the outcome under * `description`, then throws if `fn` returned anything other than `true` * or threw an exception. */ assert: ( fn: () => boolean | Promise, description: string, ) => Promise; /** Runs a named snippet with the same context, plus any extra positional args. */ runSnippet: (alias: string, ...args: unknown[]) => Promise; /** Returns metadata + absolute disk path for files attached to the current scenario. */ getScenarioFiles: (opts?: { limit?: number; offset?: number }) => Promise<{ items: Array<{ id: string; name: string; mimeType: string; size: number; sha256: string; path: string }>; total: number; }>; /** Downloads a file and creates a run artifact. */ downloadFile: ( url: string, opts?: { method?: string; headers?: Record; body?: string; filename?: string }, ) => Promise<{ id: string; name: string; mimeType: string; size: number; sha256: string; path: string }>; } export interface ExecContext { page: Page; browser: BrowserContext; code: string; log?: ScriptLogger; getStepOutput?: (order: number) => Promise; credentials?: Record; environment?: EnvironmentData | null; snippets?: Record | null; result?: unknown; scenarioId?: string; runId?: string; fileService?: FileStorageService; } @Injectable() export class CodeExecutorService { private readonly logger = new TraceLogger(CodeExecutorService.name); /** * Validates `code` by wrapping it in an async function body and attempting * to parse it with acorn. Throws BadRequestException if it is not valid JS. */ validate(code: string): void { const wrapped = `async function __validate__(context) { ${code} }`; try { parse(wrapped, { ecmaVersion: 2022 }); } catch (err) { throw new BadRequestException( `Code parse error: ${(err as Error).message}`, { cause: err }, ); } } /** * Executes `code` as an async function body with a single `context` argument * that exposes page, browser, env helpers, credentials, snippets, and logging. * Always call `validate()` before this method. */ async execute(ctx: ExecContext): Promise { const { page, browser, code, log, getStepOutput, credentials, environment, snippets, result, scenarioId, runId, fileService, } = ctx; const scriptLog: ScriptLogger = log ?? ((level, msg) => this.logger[level](msg)); const toStr = (args: unknown[]) => args .map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a))) .join(" "); const credMap: Record = credentials ?? {}; const envData: EnvironmentData = environment ?? {}; const snippetMap: Record = snippets ?? {}; // scriptContext is referenced by runSnippet, so we declare it first. let scriptContext: ScriptContext; const fakeConsole = { log: (...args: unknown[]) => scriptLog("log", toStr(args)), warn: (...args: unknown[]) => scriptLog("warn", toStr(args)), error: (...args: unknown[]) => scriptLog("error", toStr(args)), info: (...args: unknown[]) => scriptLog("log", toStr(args)), debug: (...args: unknown[]) => scriptLog("log", toStr(args)), }; try { scriptContext = { page, browser, dumpDom: (selector?: string) => dumpDom(page, selector), log: (...args: unknown[]) => scriptLog("log", toStr(args)), warn: (...args: unknown[]) => scriptLog("warn", toStr(args)), error: (...args: unknown[]) => scriptLog("error", toStr(args)), assert: async ( fn: () => boolean | Promise, description: string, ): Promise => { let passed: boolean; let failureReason: string | undefined; try { passed = (await fn()) === true; } catch (err) { passed = false; failureReason = (err as Error).message; } if (passed) { scriptLog("log", `Assertion passed: ${description}`); return; } const message = failureReason ? `Assertion failed: ${description} (${failureReason})` : `Assertion failed: ${description}`; scriptLog("error", message); throw new Error(message); }, getStepOutput: getStepOutput ?? (() => Promise.resolve(null)), getCredential: (alias: string): unknown => { if (!(alias in credMap)) { throw new Error( `Credential alias "${alias}" not found in this scenario`, ); } return credMap[alias]; }, env: { ...envData }, getEnv: (key: string): string => { const value = envData[key]; if (value == null) { throw new Error( `Environment value "${key}" is not defined for this environment`, ); } return value; }, runSnippet: async ( alias: string, ...args: unknown[] ): Promise => { const snippetCode = snippetMap[alias]; if (snippetCode == null) { throw new Error(`Snippet alias "${alias}" not found`); } scriptLog("log", `Snippet "${alias}" started`); const snippetFn = new Function( "context", "console", "snippetArgs", "expect", `return (async (context, ...args) => { ${snippetCode} })(context, ...snippetArgs)`, ); try { const snippetResult = await snippetFn( scriptContext, fakeConsole, args, playwrightExpect, ); scriptLog("log", `Snippet "${alias}" finished`); return snippetResult; } catch (err) { scriptLog( "error", `Snippet "${alias}" failed: ${(err as Error).message}`, ); throw err; } }, getScenarioFiles: async (opts?: { limit?: number; offset?: number }) => { if (!fileService || !scenarioId) { throw new Error("File service not available in this context"); } const result = await fileService.listScenarioFiles( scenarioId, opts?.limit, opts?.offset, ); return { items: result.items.map((item) => ({ id: item.id, name: item.name, mimeType: item.mimeType, size: item.size, sha256: item.sha256, path: item.path, })), total: result.total, }; }, downloadFile: async ( url: string, opts?: { method?: string; headers?: Record; body?: string; filename?: string }, ) => { if (!fileService || !scenarioId || !runId) { throw new Error("File service or run context not available"); } // Download file const buffer = await downloadFileImpl(url, opts); // Determine filename and mime type const filename = opts?.filename ?? new URL(url).pathname.split("/").pop() ?? "download"; const mimeType = this.getMimeType(filename); // Create run artifact with mapping const savedFile = await fileService.createAndSaveRunArtifact( runId, buffer, filename, mimeType, ); return { id: savedFile.id, name: savedFile.originalName, mimeType: savedFile.mimeType, size: savedFile.size, sha256: savedFile.sha256, path: fileService.getAbsolutePath(savedFile.filePath), }; }, }; // `console`, `result`, and `expect` are injected as named parameters so // existing scripts that reference them directly still work. const fn = new Function( "context", "console", "result", "expect", `return (async (context) => { ${code} })(context)`, ); this.logger.debug("Executing user code"); const execResult = await fn( scriptContext, fakeConsole, result, playwrightExpect, ); return { result: execResult }; } catch (err) { throw new InternalServerErrorException( `Code execution failed: ${(err as Error).message}`, { cause: err }, ); } } /** * Infers MIME type from filename. */ private getMimeType(filename: string): string { const ext = filename.split(".").pop()?.toLowerCase() ?? ""; const mimeTypes: Record = { pdf: "application/pdf", doc: "application/msword", docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", xls: "application/vnd.ms-excel", xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", jpg: "image/jpeg", jpeg: "image/jpeg", png: "image/png", gif: "image/gif", csv: "text/csv", txt: "text/plain", zip: "application/zip", }; return mimeTypes[ext] ?? "application/octet-stream"; } }