refactor(platform): organize workflow capabilities into structured submodules
This commit is contained in:
1 parent
c917df13cc
commit
5aef332026
36 files changed
+2539
-2472
No files matched your search
@@ -0,0 +1,100 @@
|
||||
import vm from "node:vm";
|
||||
import type { ScriptEvalResult, ScriptEvalLog } from "./types.js";
|
||||
|
||||
/**
|
||||
* 在隔离沙箱中执行 JavaScript 代码节点,仿真合思云端运行时并捕获输出与异常
|
||||
*/
|
||||
export async function executeJavaScriptSandbox(
|
||||
code: string,
|
||||
inputs: Record<string, unknown> = {},
|
||||
options: { timeoutMs?: number } = {}
|
||||
): Promise<ScriptEvalResult> {
|
||||
const timeoutMs = options.timeoutMs ?? 3000;
|
||||
const logs: ScriptEvalLog[] = [];
|
||||
const start = performance.now();
|
||||
|
||||
const recordLog = (level: "log" | "warn" | "error", ...args: unknown[]) => {
|
||||
const message = args
|
||||
.map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a)))
|
||||
.join(" ");
|
||||
logs.push({ level, message, timestamp: Date.now() });
|
||||
};
|
||||
|
||||
const sandboxConsole = {
|
||||
log: (...args: unknown[]) => recordLog("log", ...args),
|
||||
info: (...args: unknown[]) => recordLog("log", ...args),
|
||||
warn: (...args: unknown[]) => recordLog("warn", ...args),
|
||||
error: (...args: unknown[]) => recordLog("error", ...args),
|
||||
};
|
||||
|
||||
// 创建安全的执行沙箱上下文
|
||||
const sandboxContext = vm.createContext({
|
||||
...inputs,
|
||||
console: sandboxConsole,
|
||||
Date,
|
||||
Math,
|
||||
JSON,
|
||||
RegExp,
|
||||
Array,
|
||||
Object,
|
||||
Number,
|
||||
String,
|
||||
Boolean,
|
||||
parseInt,
|
||||
parseFloat,
|
||||
isNaN,
|
||||
isFinite,
|
||||
encodeURI,
|
||||
decodeURI,
|
||||
encodeURIComponent,
|
||||
decodeURIComponent,
|
||||
});
|
||||
|
||||
// 包裹立即执行函数体 (IIFE),既允许顶层 return 语句,又能直接受 vm timeout 保护
|
||||
const wrappedCode = `(function() {\n${code}\n})()`;
|
||||
|
||||
try {
|
||||
const returnValue = vm.runInContext(wrappedCode, sandboxContext, {
|
||||
timeout: timeoutMs,
|
||||
displayErrors: true,
|
||||
});
|
||||
const durationMs = Number((performance.now() - start).toFixed(2));
|
||||
|
||||
return {
|
||||
success: true,
|
||||
durationMs,
|
||||
returnValue,
|
||||
logs,
|
||||
inputs,
|
||||
};
|
||||
} catch (err: any) {
|
||||
const durationMs = Number((performance.now() - start).toFixed(2));
|
||||
const msg = err?.message || String(err);
|
||||
let line: number | undefined;
|
||||
let column: number | undefined;
|
||||
|
||||
const stack = err?.stack || "";
|
||||
// 匹配包裹函数内的行号(第 1 行是包裹头,因此需要 -1)
|
||||
const match = stack.match(/evalmachine\.<anonymous>:(\d+):(\d+)/);
|
||||
if (match) {
|
||||
const rawLine = Number(match[1]);
|
||||
line = Math.max(1, rawLine - 1);
|
||||
column = Number(match[2]);
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
durationMs,
|
||||
returnValue: undefined,
|
||||
logs,
|
||||
error: {
|
||||
name: err?.name || "ExecutionError",
|
||||
message: msg,
|
||||
stack,
|
||||
line,
|
||||
column,
|
||||
},
|
||||
inputs,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user