feat: add script dry-run functionality and enhance script management
- Introduced a new dry-run endpoint for executing scripts without saving changes. - Implemented a dedicated ScriptDryRunPage for user interaction with dry-run functionality. - Enhanced ScriptsPage to support navigation to the dry-run feature. - Added LogViewer component for displaying logs during script execution. - Created ScriptEditPage for editing scripts with integrated dry-run capabilities. - Updated workflows to include new scripts for fetching and notifying data. - Improved overall user experience with better navigation and error handling.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import pino from "pino";
|
||||
|
||||
const LEVEL_TO_NUM = {
|
||||
trace: 10,
|
||||
debug: 20,
|
||||
info: 30,
|
||||
warn: 40,
|
||||
error: 50,
|
||||
fatal: 60,
|
||||
};
|
||||
|
||||
/**
|
||||
* Pino logger that collects log lines in memory (no SQLite / process logger).
|
||||
*
|
||||
* @returns {{ log: import("pino").Logger, logs: Array<{ ts: string, level: number, msg?: string | null, payload?: unknown }> }}
|
||||
*/
|
||||
export function createDryRunLogger() {
|
||||
/** @type {Array<{ ts: string, level: number, msg?: string | null, payload?: unknown }>} */
|
||||
const logs = [];
|
||||
|
||||
const dest = {
|
||||
write(line) {
|
||||
let record;
|
||||
try {
|
||||
record = JSON.parse(typeof line === "string" ? line : String(line));
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
const ts =
|
||||
typeof record.time === "number"
|
||||
? new Date(record.time).toISOString()
|
||||
: typeof record.time === "string"
|
||||
? record.time
|
||||
: new Date().toISOString();
|
||||
|
||||
const level =
|
||||
typeof record.level === "number"
|
||||
? record.level
|
||||
: LEVEL_TO_NUM[record.level] ?? LEVEL_TO_NUM.info;
|
||||
|
||||
const { time: _time, level: _level, msg = null, ...rest } = record;
|
||||
logs.push({
|
||||
ts,
|
||||
level,
|
||||
msg,
|
||||
payload: Object.keys(rest).length ? rest : null,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
const log = pino({ level: "trace" }, dest);
|
||||
return { log, logs };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {unknown} value
|
||||
*/
|
||||
export function safeSerialize(value) {
|
||||
const seen = new WeakSet();
|
||||
try {
|
||||
return JSON.parse(
|
||||
JSON.stringify(value, (_key, v) => {
|
||||
if (typeof v === "bigint") return v.toString();
|
||||
if (typeof v === "object" && v !== null) {
|
||||
if (seen.has(v)) return "[Circular]";
|
||||
seen.add(v);
|
||||
}
|
||||
return v;
|
||||
}),
|
||||
);
|
||||
} catch (err) {
|
||||
return {
|
||||
error: "output could not be serialized",
|
||||
detail: err instanceof Error ? err.message : String(err),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { clearScriptCache } from "../../script-sandbox.js";
|
||||
import { clearScriptCache, runScriptSource } from "../../script-sandbox.js";
|
||||
import * as fsStore from "../../fs-store.js";
|
||||
import { createDryRunLogger, safeSerialize } from "./dry-run-logger.js";
|
||||
|
||||
/**
|
||||
* @param {{ referencedScripts: () => Set<string> }} registry
|
||||
@@ -60,5 +61,51 @@ export default function scriptsPluginFactory(registry) {
|
||||
clearScriptCache();
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
fastify.post("/scripts/:name/dry-run", async (req, reply) => {
|
||||
const { name } = /** @type {{ name: string }} */ (req.params);
|
||||
try {
|
||||
fsStore.assertScriptName(name);
|
||||
} catch (err) {
|
||||
return reply.code(err.statusCode ?? 400).send({ error: err.message });
|
||||
}
|
||||
|
||||
const body = /** @type {{ content?: string, data?: unknown, config?: unknown }} */ (
|
||||
req.body ?? {}
|
||||
);
|
||||
if (typeof body.content !== "string") {
|
||||
return reply.code(400).send({ error: "content is required" });
|
||||
}
|
||||
|
||||
const ctx = {
|
||||
data: body.data ?? null,
|
||||
config: body.config ?? null,
|
||||
};
|
||||
|
||||
const { log, logs } = createDryRunLogger();
|
||||
const started = Date.now();
|
||||
|
||||
try {
|
||||
const output = await runScriptSource(name, body.content, ctx, {
|
||||
log,
|
||||
workflowName: "dry-run",
|
||||
});
|
||||
return {
|
||||
status: "success",
|
||||
output: safeSerialize(output),
|
||||
error: null,
|
||||
logs,
|
||||
durationMs: Date.now() - started,
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
status: "failed",
|
||||
output: null,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
logs,
|
||||
durationMs: Date.now() - started,
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user