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:
2026-08-14 15:37:30 +07:00
parent 389f94f950
commit 8519c92ed1
17 changed files with 810 additions and 176 deletions
+20 -3
View File
@@ -291,6 +291,10 @@ function createScriptSandbox({ log, script, workflowName }) {
return sandbox;
}
function compileScriptSource(source, filename) {
return new vm.Script(wrapScriptSource(source, filename), { filename });
}
function loadCompiledScript(script) {
const filePath = path.join(SCRIPTS_DIR, script);
const { mtimeMs } = fs.statSync(filePath);
@@ -300,9 +304,7 @@ function loadCompiledScript(script) {
}
const source = fs.readFileSync(filePath, "utf8");
const compiled = new vm.Script(wrapScriptSource(source, script), {
filename: filePath,
});
const compiled = compileScriptSource(source, filePath);
scriptCache.set(script, { compiled, mtimeMs });
return compiled;
}
@@ -320,3 +322,18 @@ export async function runScript(script, ctx, { log, workflowName }) {
const fn = compiled.runInContext(sandbox);
return await fn(ctx);
}
/**
* Evaluate script source in memory (dry-run). Does not read from disk or use the disk cache.
*
* @param {string} script
* @param {string} source
* @param {unknown} ctx
* @param {{ log: import("pino").Logger, workflowName: string }} opts
*/
export async function runScriptSource(script, source, ctx, { log, workflowName }) {
const compiled = compileScriptSource(source, script);
const sandbox = createScriptSandbox({ log, script, workflowName });
const fn = compiled.runInContext(sandbox);
return await fn(ctx);
}
+70
View File
@@ -0,0 +1,70 @@
function ensureDataObject(ctx) {
if (ctx.data == null || typeof ctx.data !== "object" || Array.isArray(ctx.data)) {
ctx.data = {};
}
}
function filenameFromUrl(url) {
try {
const name = new URL(url).pathname.split("/").filter(Boolean).pop();
return name || "attachment";
} catch {
return "attachment";
}
}
function resolveUrl(ctx) {
const fromConfig = ctx.config?.url;
if (typeof fromConfig === "string" && fromConfig.length > 0) {
return fromConfig;
}
const urlVar = ctx.config?.urlVar;
if (typeof urlVar === "string" && urlVar.length > 0) {
const value = ctx.data?.[urlVar];
if (typeof value === "string" && value.length > 0) {
return value;
}
}
for (const key of ["attach", "url"]) {
const value = ctx.data?.[key];
if (typeof value === "string" && value.length > 0) {
return value;
}
}
return null;
}
export default async function fetchBinary(ctx) {
ensureDataObject(ctx);
const url = resolveUrl(ctx);
if (!url) {
throw new Error(
"fetch-binary: url is required (ctx.config.url, ctx.config.urlVar, ctx.data.attach, or ctx.data.url)",
);
}
const outputVar = typeof ctx.config?.outputVar === "string" && ctx.config.outputVar.length > 0
? ctx.config.outputVar
: "file";
log.info({ url, outputVar }, "fetch-binary: fetching");
const response = await $axios.get(url, { responseType: "arraybuffer" });
const file = Buffer.from(response.data ?? []);
const contentType = String(response.headers?.["content-type"] ?? "application/octet-stream");
const filename = ctx.config?.filename || ctx.data.filename || filenameFromUrl(url);
ctx.data[outputVar] = file;
ctx.data.filename = filename;
ctx.data.contentType = contentType;
log.info(
{ outputVar, filename, contentType, length: file.length },
"fetch-binary: saved",
);
return ctx;
}
+60 -14
View File
@@ -1,20 +1,66 @@
function ntfyHeaders(ctx) {
const headers = {};
if (ctx.data?.title) {
log.info("ntfy: setting title %s", ctx.data.title);
headers.Title = ctx.data.title;
}
return headers;
}
export default async function ntfy(ctx) {
log.info({ ctx }, "ntfy incoming context");
const headers = {}
if(ctx.data?.title) {
log.info("ntfy: setting title %s", ctx.data.title);
headers.Title = ctx.data.title
const file = ctx.data?.file;
const hasFile = Buffer.isBuffer(file) || file instanceof Uint8Array;
log.info(
{
title: ctx.data?.title,
message: ctx.data?.message,
filename: ctx.data?.filename,
hasFile,
fileLength: hasFile ? file.length : 0,
attach: ctx.data?.attach,
},
"ntfy incoming context",
);
const headers = ntfyHeaders(ctx);
const ntfyUrl = ctx.config?.url || "https://ntfy.sh/scrunner";
if (hasFile) {
const filename = ctx.data?.filename || "attachment";
headers.Filename = filename;
if (ctx.data?.message) {
headers.Message = ctx.data.message;
}
if (ctx.data?.contentType) {
headers["Content-Type"] = ctx.data.contentType;
}
const ntfyUrl = ctx.config?.url || "https://ntfy.sh/scrunner";
log.info(
{ ntfyUrl, filename, length: file.length },
"ntfy uploading file",
);
await $axios.put(ntfyUrl, file, {
headers,
maxBodyLength: Infinity,
maxContentLength: Infinity,
});
return { sent: "true" };
}
log.info("ntfy sending message to %s", ntfyUrl);
const truncatedMessage = ctx.data?.message?.substring(0, 100);
log.info("ntfy messsage: %s", truncatedMessage);
if (ctx.data?.attach) {
log.info("ntfy: setting attach %s", ctx.data.attach);
headers.Attach = ctx.data.attach;
}
await $axios.post(ntfyUrl, ctx.data?.message || "Hello from scrunner", {
headers: headers
})
return {sent: "true"}
const truncatedMessage = ctx.data?.message?.substring(0, 100);
log.info("ntfy sending message to %s", ntfyUrl);
log.info("ntfy messsage: %s", truncatedMessage);
await $axios.post(ntfyUrl, ctx.data?.message || "Hello from scrunner", {
headers,
});
return { sent: "true" };
}
+78
View File
@@ -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),
};
}
}
+48 -1
View File
@@ -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,
};
}
});
};
}
@@ -0,0 +1,22 @@
name: Comic - monkeyuser to ntfy
scripts:
- script: fetch-html.js
config:
url: "https://www.monkeyuser.com/"
outputVar: "httpResponse"
selector: ".comic img"
jsonata: |
{"url": "https://www.monkeyuser.com" & [attributes.src][0], "title": [attributes.title][0]}
- script: jsonata.js
config:
expression: |
{"data": {"title": httpResponse.title, "message": httpResponse.title, "attach": httpResponse.url}}
- fetch-binary.js
- script: ntfy.js
config:
url: https://ntfy.sh/scrunner
triggers:
- type: HTTP
method: POST
path: /new
@@ -4,3 +4,4 @@ scripts:
- manual-trigger.yaml
- cron-example.yaml
- fetch-devto.yaml
- comic-monkeyuser-to-ntfy.yaml