mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-17 04:50:52 +00:00
fix(perf): unify SFTP lifecycles and bound runtime resources
This commit is contained in:
1 parent
49918fd54a
commit
fe737ec555
240 files changed
+23968
-4865
No files matched your search
@@ -1,8 +1,72 @@
|
||||
"use strict";
|
||||
|
||||
const vm = require("node:vm");
|
||||
const path = require("node:path");
|
||||
const { AsyncLocalStorage } = require("node:async_hooks");
|
||||
const { Worker } = require("node:worker_threads");
|
||||
const { shellPromptPatterns } = require("./shellPromptPatterns.cjs");
|
||||
|
||||
const SCRIPT_SYNC_EXECUTION_TIMEOUT_MS = 1_000;
|
||||
const SCRIPT_WORKER_START_TIMEOUT_MS = 10_000;
|
||||
const SCRIPT_WORKER_MAX_OLD_GENERATION_MB = 64;
|
||||
const SCRIPT_WORKER_MAX_YOUNG_GENERATION_MB = 16;
|
||||
const SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS = 128;
|
||||
const SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS = 512;
|
||||
const SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS = 20_000;
|
||||
const SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS = 64;
|
||||
const SCRIPT_WORKER_PROGRESS_THROTTLE_MS = 50;
|
||||
const activeScriptWorkers = new Set();
|
||||
const activeScriptHostRequests = new Set();
|
||||
let prewarmedScriptWorker = null;
|
||||
|
||||
function createScriptWorkerHandle() {
|
||||
const worker = new Worker(path.join(__dirname, "scriptExecutionWorker.cjs"), {
|
||||
resourceLimits: {
|
||||
maxOldGenerationSizeMb: SCRIPT_WORKER_MAX_OLD_GENERATION_MB,
|
||||
maxYoungGenerationSizeMb: SCRIPT_WORKER_MAX_YOUNG_GENERATION_MB,
|
||||
stackSizeMb: 4,
|
||||
},
|
||||
});
|
||||
worker.unref();
|
||||
let resolveReady;
|
||||
let rejectReady;
|
||||
const readyPromise = new Promise((resolve, reject) => {
|
||||
resolveReady = resolve;
|
||||
rejectReady = reject;
|
||||
});
|
||||
const onMessage = (message) => {
|
||||
if (message?.type !== "ready") return;
|
||||
worker.removeListener("message", onMessage);
|
||||
worker.removeListener("error", onStartupError);
|
||||
resolveReady();
|
||||
};
|
||||
const onStartupError = (error) => {
|
||||
worker.removeListener("message", onMessage);
|
||||
rejectReady(error);
|
||||
};
|
||||
worker.on("message", onMessage);
|
||||
worker.once("error", onStartupError);
|
||||
void readyPromise.catch(() => {});
|
||||
return { worker, readyPromise };
|
||||
}
|
||||
|
||||
function ensurePrewarmedScriptWorker() {
|
||||
if (prewarmedScriptWorker) return prewarmedScriptWorker;
|
||||
const handle = createScriptWorkerHandle();
|
||||
prewarmedScriptWorker = handle;
|
||||
void handle.readyPromise.catch(() => {
|
||||
if (prewarmedScriptWorker === handle) prewarmedScriptWorker = null;
|
||||
});
|
||||
return handle;
|
||||
}
|
||||
|
||||
function acquireScriptWorker() {
|
||||
const handle = prewarmedScriptWorker || createScriptWorkerHandle();
|
||||
if (prewarmedScriptWorker === handle) prewarmedScriptWorker = null;
|
||||
handle.worker.ref();
|
||||
ensurePrewarmedScriptWorker();
|
||||
return handle;
|
||||
}
|
||||
|
||||
function wrapScriptSource(source) {
|
||||
const trimmed = String(source || "").trim();
|
||||
if (!trimmed) {
|
||||
@@ -25,6 +89,373 @@ function wrapScriptSource(source) {
|
||||
return `(async () => {\n${trimmed}\n})();`;
|
||||
}
|
||||
|
||||
function serializeWorkerError(error) {
|
||||
return {
|
||||
name: error?.name || "Error",
|
||||
message: error?.message || String(error),
|
||||
stack: typeof error?.stack === "string" ? error.stack : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function reviveWorkerError(value) {
|
||||
const error = new Error(value?.message || "Script worker failed");
|
||||
error.name = value?.name || "Error";
|
||||
if (typeof value?.stack === "string") error.stack = value.stack;
|
||||
return error;
|
||||
}
|
||||
|
||||
function readRuntimeSnapshot(nct) {
|
||||
return {
|
||||
session: {
|
||||
connected: Boolean(nct.session.connected),
|
||||
hostname: String(nct.session.hostname || ""),
|
||||
username: String(nct.session.username || ""),
|
||||
},
|
||||
screen: {
|
||||
currentRow: Number(nct.screen.currentRow) || 0,
|
||||
rows: Number(nct.screen.rows) || 24,
|
||||
cols: Number(nct.screen.cols) || 80,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const WORKER_RPC_METHODS = new Set([
|
||||
"session.sleep",
|
||||
"session.startLog",
|
||||
"session.stopLog",
|
||||
"session.disconnect",
|
||||
"screen.send",
|
||||
"screen.sendLine",
|
||||
"screen.waitFor",
|
||||
"screen.waitForText",
|
||||
"screen.waitForRegex",
|
||||
"screen.waitForPrompt",
|
||||
"screen.waitForAny",
|
||||
"screen.getText",
|
||||
"screen.clear",
|
||||
"dialog.alert",
|
||||
"dialog.confirm",
|
||||
"dialog.prompt",
|
||||
"dialog.form",
|
||||
"dialog.select",
|
||||
"dialog.radio",
|
||||
"dialog.checkbox",
|
||||
]);
|
||||
|
||||
function invokeWorkerRpc(nct, method, args) {
|
||||
if (!WORKER_RPC_METHODS.has(method)) {
|
||||
throw new Error(`Unsupported script API method: ${method}`);
|
||||
}
|
||||
const [namespace, methodName] = method.split(".");
|
||||
const target = nct[namespace];
|
||||
const fn = target?.[methodName];
|
||||
if (typeof fn !== "function") {
|
||||
throw new Error(`Script API method unavailable: ${method}`);
|
||||
}
|
||||
return fn.apply(target, Array.isArray(args) ? args : []);
|
||||
}
|
||||
|
||||
function handleWorkerNotification({ nct, appendLog, runId }, method, args) {
|
||||
if (method === "log") {
|
||||
nct.log(args?.[0]);
|
||||
return;
|
||||
}
|
||||
if (method === "console.log") {
|
||||
appendLog(runId, String(args?.[0] ?? ""));
|
||||
return;
|
||||
}
|
||||
if (method.startsWith("progress.")) {
|
||||
const methodName = method.slice("progress.".length);
|
||||
const fn = nct.progress?.[methodName];
|
||||
if (typeof fn !== "function") throw new Error(`Unsupported script notification: ${method}`);
|
||||
fn.apply(nct.progress, Array.isArray(args) ? args : []);
|
||||
return;
|
||||
}
|
||||
throw new Error(`Unsupported script notification: ${method}`);
|
||||
}
|
||||
|
||||
function executeInScriptWorker({
|
||||
source,
|
||||
runId,
|
||||
nct,
|
||||
appendLog,
|
||||
isAborted,
|
||||
syncExecutionTimeoutMs,
|
||||
registerStop,
|
||||
executionContext,
|
||||
executionToken,
|
||||
}) {
|
||||
const heartbeatIntervalMs = Math.max(
|
||||
2,
|
||||
Math.min(50, Math.floor(syncExecutionTimeoutMs / 4) || 2),
|
||||
);
|
||||
const heartbeatBuffer = new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT);
|
||||
const heartbeat = new BigInt64Array(heartbeatBuffer);
|
||||
Atomics.store(heartbeat, 0, BigInt(Date.now()));
|
||||
const workerHandle = acquireScriptWorker();
|
||||
const { worker } = workerHandle;
|
||||
activeScriptWorkers.add(worker);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let settled = false;
|
||||
let ready = false;
|
||||
let watchdog = null;
|
||||
const pendingHostRequests = new Set();
|
||||
let notificationCount = 0;
|
||||
let logNotificationCount = 0;
|
||||
let progressNotificationCount = 0;
|
||||
let pendingProgressNotification = null;
|
||||
let progressTimer = null;
|
||||
let progressCurrent = 0;
|
||||
let progressTotal = 1;
|
||||
let rejectHostRequests;
|
||||
const hostRequestsClosed = new Promise((_, rejectClosed) => {
|
||||
rejectHostRequests = rejectClosed;
|
||||
});
|
||||
void hostRequestsClosed.catch(() => {});
|
||||
|
||||
const cleanup = () => {
|
||||
clearTimeout(startTimer);
|
||||
if (watchdog) clearInterval(watchdog);
|
||||
if (progressTimer) clearTimeout(progressTimer);
|
||||
watchdog = null;
|
||||
progressTimer = null;
|
||||
pendingProgressNotification = null;
|
||||
worker.removeAllListeners();
|
||||
registerStop?.(() => {});
|
||||
};
|
||||
const finish = (error) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
executionToken.closed = true;
|
||||
rejectHostRequests?.(error || new Error("Script execution finished"));
|
||||
const pendingDrain = Promise.allSettled([...pendingHostRequests]);
|
||||
cleanup();
|
||||
void Promise.all([
|
||||
worker.terminate().catch(() => {}),
|
||||
pendingDrain,
|
||||
]).then(() => {
|
||||
pendingHostRequests.clear();
|
||||
activeScriptWorkers.delete(worker);
|
||||
if (error) reject(error);
|
||||
else resolve(undefined);
|
||||
});
|
||||
};
|
||||
registerStop?.((reason) => {
|
||||
finish(reason instanceof Error ? reason : new Error("Script stopped"));
|
||||
});
|
||||
const postSnapshot = () => {
|
||||
if (settled) return;
|
||||
try {
|
||||
worker.postMessage({ type: "snapshot", snapshot: readRuntimeSnapshot(nct) });
|
||||
} catch {
|
||||
// Worker may have crossed its terminal boundary between the checks.
|
||||
}
|
||||
};
|
||||
const deliverNotification = (message) => {
|
||||
executionContext.run(executionToken, () => {
|
||||
handleWorkerNotification({ nct, appendLog, runId }, message.method, message.args);
|
||||
});
|
||||
};
|
||||
const flushPendingProgress = () => {
|
||||
if (progressTimer) clearTimeout(progressTimer);
|
||||
progressTimer = null;
|
||||
const pending = pendingProgressNotification;
|
||||
pendingProgressNotification = null;
|
||||
if (!pending || settled) return;
|
||||
deliverNotification(pending);
|
||||
};
|
||||
const scheduleProgressFlush = () => {
|
||||
if (progressTimer || settled) return;
|
||||
progressTimer = setTimeout(() => {
|
||||
progressTimer = null;
|
||||
try {
|
||||
flushPendingProgress();
|
||||
} catch (error) {
|
||||
finish(error);
|
||||
}
|
||||
}, SCRIPT_WORKER_PROGRESS_THROTTLE_MS);
|
||||
progressTimer.unref?.();
|
||||
};
|
||||
const handleProgressNotification = (message) => {
|
||||
const methodName = message.method.slice("progress.".length);
|
||||
if (methodName === "start") {
|
||||
flushPendingProgress();
|
||||
progressCurrent = 0;
|
||||
progressTotal = Math.max(1, Number(message.args?.[1]) || 1);
|
||||
} else if (methodName === "set") {
|
||||
progressCurrent = Math.max(0, Math.min(progressTotal, Number(message.args?.[0]) || 0));
|
||||
} else if (methodName === "step") {
|
||||
progressCurrent = Math.min(progressTotal, progressCurrent + 1);
|
||||
} else if (methodName === "done") {
|
||||
flushPendingProgress();
|
||||
}
|
||||
|
||||
progressNotificationCount += 1;
|
||||
if (
|
||||
progressNotificationCount <= SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS
|
||||
|| methodName === "start"
|
||||
|| methodName === "done"
|
||||
) {
|
||||
deliverNotification(message);
|
||||
return;
|
||||
}
|
||||
pendingProgressNotification = {
|
||||
type: "notify",
|
||||
method: "progress.set",
|
||||
args: [progressCurrent, message.args?.[1] ?? message.args?.[0]],
|
||||
};
|
||||
scheduleProgressFlush();
|
||||
};
|
||||
const startTimer = setTimeout(() => {
|
||||
finish(new Error(`Script worker failed to start after ${SCRIPT_WORKER_START_TIMEOUT_MS}ms`));
|
||||
}, SCRIPT_WORKER_START_TIMEOUT_MS);
|
||||
startTimer.unref?.();
|
||||
|
||||
const startWatchdog = () => {
|
||||
if (watchdog || settled) return;
|
||||
const checkIntervalMs = Math.max(2, Math.min(25, heartbeatIntervalMs));
|
||||
watchdog = setInterval(() => {
|
||||
if (isAborted?.()) {
|
||||
finish(new Error("Script stopped"));
|
||||
return;
|
||||
}
|
||||
const lastHeartbeat = Number(Atomics.load(heartbeat, 0));
|
||||
if (Date.now() - lastHeartbeat > syncExecutionTimeoutMs + heartbeatIntervalMs) {
|
||||
finish(new Error(`Script execution timed out after ${syncExecutionTimeoutMs}ms`));
|
||||
return;
|
||||
}
|
||||
postSnapshot();
|
||||
}, checkIntervalMs);
|
||||
watchdog.unref?.();
|
||||
};
|
||||
|
||||
worker.on("message", (message) => {
|
||||
if (settled) return;
|
||||
if (message?.type === "notify") {
|
||||
try {
|
||||
notificationCount += 1;
|
||||
if (notificationCount > SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS) {
|
||||
finish(new Error(
|
||||
`Script exceeded the ${SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS} notification limit`,
|
||||
));
|
||||
return;
|
||||
}
|
||||
if (message.method === "log" || message.method === "console.log") {
|
||||
flushPendingProgress();
|
||||
logNotificationCount += 1;
|
||||
if (logNotificationCount > SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS) {
|
||||
finish(new Error(
|
||||
`Script exceeded the ${SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS} log notification limit`,
|
||||
));
|
||||
return;
|
||||
}
|
||||
deliverNotification(message);
|
||||
return;
|
||||
}
|
||||
if (message.method?.startsWith("progress.")) {
|
||||
handleProgressNotification(message);
|
||||
return;
|
||||
}
|
||||
flushPendingProgress();
|
||||
deliverNotification(message);
|
||||
} catch (error) {
|
||||
finish(error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (message?.type === "rpc") {
|
||||
if (pendingHostRequests.size >= SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS) {
|
||||
finish(new Error(
|
||||
`Script exceeded the ${SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS} pending host request limit`,
|
||||
));
|
||||
return;
|
||||
}
|
||||
const hostRequest = executionContext.run(executionToken, () => (
|
||||
Promise.resolve().then(() => invokeWorkerRpc(nct, message.method, message.args))
|
||||
));
|
||||
// The underlying API observes executionToken and normally stops itself.
|
||||
// Race it as well so one misbehaving host API cannot keep bookkeeping,
|
||||
// timers, or the script completion promise alive after worker teardown.
|
||||
void hostRequest.catch(() => {});
|
||||
const request = Promise.race([hostRequest, hostRequestsClosed]);
|
||||
pendingHostRequests.add(request);
|
||||
activeScriptHostRequests.add(request);
|
||||
void request.then(
|
||||
(value) => {
|
||||
if (settled) return;
|
||||
worker.postMessage({
|
||||
type: "rpc-result",
|
||||
requestId: message.requestId,
|
||||
ok: true,
|
||||
value,
|
||||
snapshot: readRuntimeSnapshot(nct),
|
||||
});
|
||||
},
|
||||
(error) => {
|
||||
if (settled) return;
|
||||
worker.postMessage({
|
||||
type: "rpc-result",
|
||||
requestId: message.requestId,
|
||||
ok: false,
|
||||
error: serializeWorkerError(error),
|
||||
snapshot: readRuntimeSnapshot(nct),
|
||||
});
|
||||
},
|
||||
).finally(() => {
|
||||
pendingHostRequests.delete(request);
|
||||
activeScriptHostRequests.delete(request);
|
||||
}).catch((error) => {
|
||||
if (!settled) finish(error);
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (message?.type === "completed") {
|
||||
try { flushPendingProgress(); } catch (error) {
|
||||
finish(error);
|
||||
return;
|
||||
}
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
if (message?.type === "failed") {
|
||||
finish(reviveWorkerError(message.error));
|
||||
}
|
||||
});
|
||||
worker.once("error", (error) => finish(error));
|
||||
worker.once("exit", (code) => {
|
||||
if (!settled) {
|
||||
finish(new Error(
|
||||
ready
|
||||
? `Script worker exited before completion (code ${code})`
|
||||
: `Script worker failed to start (code ${code})`,
|
||||
));
|
||||
}
|
||||
});
|
||||
void workerHandle.readyPromise.then(() => {
|
||||
if (settled) return;
|
||||
ready = true;
|
||||
clearTimeout(startTimer);
|
||||
Atomics.store(heartbeat, 0, BigInt(Date.now()));
|
||||
worker.postMessage({
|
||||
type: "start",
|
||||
config: {
|
||||
source,
|
||||
filename: `script-${runId}.js`,
|
||||
version: nct.version,
|
||||
snapshot: readRuntimeSnapshot(nct),
|
||||
heartbeatBuffer,
|
||||
heartbeatIntervalMs,
|
||||
maxPendingHostRequests: SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS,
|
||||
maxLogNotifications: SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS,
|
||||
maxTotalNotifications: SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS,
|
||||
},
|
||||
});
|
||||
startWatchdog();
|
||||
}, (error) => finish(error));
|
||||
});
|
||||
}
|
||||
|
||||
function truncateActivityLabel(value, max = 80) {
|
||||
const text = String(value ?? "");
|
||||
return text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
||||
@@ -254,6 +685,7 @@ function normalizeDialogFormSpec(spec) {
|
||||
}
|
||||
|
||||
function createScriptRuntime(deps) {
|
||||
ensurePrewarmedScriptWorker();
|
||||
const {
|
||||
sessionId,
|
||||
runId,
|
||||
@@ -278,6 +710,8 @@ function createScriptRuntime(deps) {
|
||||
let progressLabel;
|
||||
let progressCurrent = 0;
|
||||
let progressTotal = 0;
|
||||
let stopActiveExecution = () => {};
|
||||
const executionContext = new AsyncLocalStorage();
|
||||
|
||||
let screenSnapshot = {
|
||||
rows: 24,
|
||||
@@ -286,16 +720,17 @@ function createScriptRuntime(deps) {
|
||||
lines: [],
|
||||
};
|
||||
|
||||
function isExecutionAborted() {
|
||||
return Boolean(executionContext.getStore()?.closed || deps.isAborted?.());
|
||||
}
|
||||
|
||||
function assertNotAborted() {
|
||||
if (deps.isAborted?.()) {
|
||||
if (isExecutionAborted()) {
|
||||
throw new Error("Script stopped");
|
||||
}
|
||||
}
|
||||
|
||||
function abortable(promise) {
|
||||
if (typeof deps.isAborted !== "function") {
|
||||
return promise;
|
||||
}
|
||||
assertNotAborted();
|
||||
return new Promise((resolve, reject) => {
|
||||
let settled = false;
|
||||
@@ -306,19 +741,19 @@ function createScriptRuntime(deps) {
|
||||
callback(value);
|
||||
};
|
||||
const timer = setInterval(() => {
|
||||
if (!deps.isAborted?.()) return;
|
||||
if (!isExecutionAborted()) return;
|
||||
finish(reject, new Error("Script stopped"));
|
||||
}, 50);
|
||||
Promise.resolve(promise).then(
|
||||
(value) => {
|
||||
if (deps.isAborted?.()) {
|
||||
if (isExecutionAborted()) {
|
||||
finish(reject, new Error("Script stopped"));
|
||||
return;
|
||||
}
|
||||
finish(resolve, value);
|
||||
},
|
||||
(err) => {
|
||||
if (deps.isAborted?.()) {
|
||||
if (isExecutionAborted()) {
|
||||
finish(reject, new Error("Script stopped"));
|
||||
return;
|
||||
}
|
||||
@@ -329,7 +764,8 @@ function createScriptRuntime(deps) {
|
||||
}
|
||||
|
||||
function markHandled(promise) {
|
||||
Promise.resolve(promise).catch(() => {});
|
||||
const observed = Promise.resolve(promise);
|
||||
observed.catch(() => {});
|
||||
return promise;
|
||||
}
|
||||
|
||||
@@ -337,7 +773,7 @@ function createScriptRuntime(deps) {
|
||||
try {
|
||||
return await task();
|
||||
} catch (err) {
|
||||
if (deps.isAborted?.() && err?.message === "Script stopped") {
|
||||
if (isExecutionAborted() && err?.message === "Script stopped") {
|
||||
return undefined;
|
||||
}
|
||||
throw err;
|
||||
@@ -388,7 +824,7 @@ function createScriptRuntime(deps) {
|
||||
async function waitForPromptWithRecovery(timeoutMs = 60000) {
|
||||
let stepTracked = false;
|
||||
while (true) {
|
||||
if (deps.isAborted?.()) {
|
||||
if (isExecutionAborted()) {
|
||||
throw new Error("Script stopped");
|
||||
}
|
||||
if (!stepTracked) {
|
||||
@@ -400,7 +836,7 @@ function createScriptRuntime(deps) {
|
||||
return await getOutputBuffer(sessionId).waitForAny(
|
||||
shellPromptPatterns(),
|
||||
timeoutMs,
|
||||
() => Boolean(deps.isAborted?.()),
|
||||
isExecutionAborted,
|
||||
{ allowPreservedTailMatch: true },
|
||||
);
|
||||
} catch (err) {
|
||||
@@ -430,7 +866,7 @@ function createScriptRuntime(deps) {
|
||||
: String(patterns);
|
||||
let stepTracked = false;
|
||||
while (true) {
|
||||
if (deps.isAborted?.()) {
|
||||
if (isExecutionAborted()) {
|
||||
throw new Error("Script stopped");
|
||||
}
|
||||
if (!stepTracked) {
|
||||
@@ -442,7 +878,7 @@ function createScriptRuntime(deps) {
|
||||
return await getOutputBuffer(sessionId).waitForAny(
|
||||
patterns,
|
||||
timeoutMs,
|
||||
() => Boolean(deps.isAborted?.()),
|
||||
isExecutionAborted,
|
||||
);
|
||||
} catch (err) {
|
||||
if (!String(err?.message || err).includes("timed out")) {
|
||||
@@ -471,7 +907,7 @@ function createScriptRuntime(deps) {
|
||||
const patternLabel = pattern instanceof RegExp ? pattern.source : String(pattern);
|
||||
let stepTracked = false;
|
||||
while (true) {
|
||||
if (deps.isAborted?.()) {
|
||||
if (isExecutionAborted()) {
|
||||
throw new Error("Script stopped");
|
||||
}
|
||||
if (!stepTracked) {
|
||||
@@ -483,7 +919,7 @@ function createScriptRuntime(deps) {
|
||||
return await getOutputBuffer(sessionId)[waitMethod](
|
||||
pattern,
|
||||
timeoutMs,
|
||||
() => Boolean(deps.isAborted?.()),
|
||||
isExecutionAborted,
|
||||
);
|
||||
} catch (err) {
|
||||
if (!String(err?.message || err).includes("timed out")) {
|
||||
@@ -580,14 +1016,14 @@ function createScriptRuntime(deps) {
|
||||
},
|
||||
sleep(ms) {
|
||||
const delay = Math.max(0, Number(ms) || 0);
|
||||
return markHandled(trackStep(`sleep ${delay}ms`).then(() => interruptibleSleep(delay, deps.isAborted)));
|
||||
return markHandled(trackStep(`sleep ${delay}ms`).then(() => interruptibleSleep(delay, isExecutionAborted)));
|
||||
},
|
||||
startLog(path) {
|
||||
return markHandled(ignoreIfStopped(async () => {
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
assertWriteAllowed("session.startLog");
|
||||
await trackStep("startLog");
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
await startSessionLog?.(sessionId, path);
|
||||
}));
|
||||
},
|
||||
@@ -598,10 +1034,10 @@ function createScriptRuntime(deps) {
|
||||
},
|
||||
disconnect() {
|
||||
return markHandled(ignoreIfStopped(async () => {
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
assertWriteAllowed("session.disconnect");
|
||||
await trackStep("disconnect");
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
await disconnectSession?.(sessionId);
|
||||
}));
|
||||
},
|
||||
@@ -610,28 +1046,28 @@ function createScriptRuntime(deps) {
|
||||
const screenApi = {
|
||||
send(text, options = {}) {
|
||||
return markHandled(ignoreIfStopped(async () => {
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
assertWriteAllowed("screen.send");
|
||||
await waitIfPaused();
|
||||
const payload = String(text ?? "");
|
||||
const sensitive = options?.sensitive === true;
|
||||
const visiblePayload = sensitive ? "[sensitive]" : formatScriptInputForLog(payload);
|
||||
await trackStep(`send: ${truncateActivityLabel(visiblePayload, 60)}`);
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
appendLog(runId, `→ ${visiblePayload}`);
|
||||
writeToSession(sessionId, payload, { automated: true, sensitive });
|
||||
}));
|
||||
},
|
||||
sendLine(text, options = {}) {
|
||||
return markHandled(ignoreIfStopped(async () => {
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
assertWriteAllowed("screen.sendLine");
|
||||
await waitIfPaused();
|
||||
const line = String(text ?? "");
|
||||
const sensitive = options?.sensitive === true;
|
||||
const visibleLine = sensitive ? "[sensitive]" : line;
|
||||
await trackStep(`sendLine: ${truncateActivityLabel(visibleLine, 60)}`);
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
appendLog(runId, `→ ${visibleLine}`);
|
||||
// Bastion menus can ignore a single "line\r" packet even when
|
||||
// stream.write succeeds. Match xterm: body, then Enter (#1960).
|
||||
@@ -645,8 +1081,8 @@ function createScriptRuntime(deps) {
|
||||
sensitive,
|
||||
invalidateStartupSeed: false,
|
||||
});
|
||||
await interruptibleSleep(30, deps.isAborted);
|
||||
if (deps.isAborted?.()) return;
|
||||
await interruptibleSleep(30, isExecutionAborted);
|
||||
if (isExecutionAborted()) return;
|
||||
}
|
||||
writeToSession(sessionId, "\r", {
|
||||
automated: true,
|
||||
@@ -701,10 +1137,10 @@ function createScriptRuntime(deps) {
|
||||
},
|
||||
clear() {
|
||||
return markHandled(ignoreIfStopped(async () => {
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
assertWriteAllowed("screen.clear");
|
||||
await trackStep("clear");
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
writeToSession(sessionId, "\x1b[2J\x1b[H", { automated: true });
|
||||
}));
|
||||
},
|
||||
@@ -713,25 +1149,25 @@ function createScriptRuntime(deps) {
|
||||
const dialogApi = {
|
||||
alert(message) {
|
||||
assertNotAborted();
|
||||
return markHandled(showDialog("alert", String(message ?? "")));
|
||||
return markHandled(abortable(showDialog("alert", String(message ?? ""))));
|
||||
},
|
||||
confirm(message) {
|
||||
assertNotAborted();
|
||||
return markHandled(showDialog("confirm", String(message ?? "")));
|
||||
return markHandled(abortable(showDialog("confirm", String(message ?? ""))));
|
||||
},
|
||||
prompt(message, defaultValue = "", options = {}) {
|
||||
assertNotAborted();
|
||||
return markHandled(showDialog(
|
||||
return markHandled(abortable(showDialog(
|
||||
"prompt",
|
||||
String(message ?? ""),
|
||||
String(defaultValue ?? ""),
|
||||
{ sensitive: options?.sensitive === true },
|
||||
));
|
||||
)));
|
||||
},
|
||||
form(spec) {
|
||||
assertNotAborted();
|
||||
const form = normalizeDialogFormSpec(spec);
|
||||
return markHandled(showDialog("form", form.message, undefined, { form }));
|
||||
return markHandled(abortable(showDialog("form", form.message, undefined, { form })));
|
||||
},
|
||||
select(message, options, defaultValue) {
|
||||
return markHandled((async () => {
|
||||
@@ -787,7 +1223,7 @@ function createScriptRuntime(deps) {
|
||||
version: deps.appVersion || "0.0.0",
|
||||
sleep: sessionApi.sleep.bind(sessionApi),
|
||||
log(message) {
|
||||
if (deps.isAborted?.()) return;
|
||||
if (isExecutionAborted()) return;
|
||||
assertNotAborted();
|
||||
stepIndex += 1;
|
||||
emitStatus({
|
||||
@@ -803,7 +1239,7 @@ function createScriptRuntime(deps) {
|
||||
while (isPaused?.()) {
|
||||
assertNotAborted();
|
||||
onStatusChange?.(runId, { status: "paused", elapsedMs: Math.max(0, Date.now() - startedAt) });
|
||||
await interruptibleSleep(100, deps.isAborted);
|
||||
await interruptibleSleep(100, isExecutionAborted);
|
||||
}
|
||||
assertNotAborted();
|
||||
onStatusChange?.(runId, { status: "running", elapsedMs: Math.max(0, Date.now() - startedAt) });
|
||||
@@ -811,25 +1247,32 @@ function createScriptRuntime(deps) {
|
||||
|
||||
async function execute(source) {
|
||||
assertNotAborted();
|
||||
const wrapped = wrapScriptSource(source);
|
||||
const sandbox = {
|
||||
const executionToken = { closed: false };
|
||||
const configuredSyncTimeoutMs = Number(deps.syncExecutionTimeoutMs);
|
||||
const syncExecutionTimeoutMs = Number.isFinite(configuredSyncTimeoutMs) && configuredSyncTimeoutMs > 0
|
||||
? Math.floor(configuredSyncTimeoutMs)
|
||||
: SCRIPT_SYNC_EXECUTION_TIMEOUT_MS;
|
||||
await executeInScriptWorker({
|
||||
source: wrapScriptSource(source),
|
||||
runId,
|
||||
nct,
|
||||
console: {
|
||||
log: (...args) => {
|
||||
appendLog(runId, args.map((arg) => String(arg)).join(" "));
|
||||
},
|
||||
},
|
||||
};
|
||||
vm.createContext(sandbox);
|
||||
const script = new vm.Script(wrapped, { filename: `script-${runId}.js` });
|
||||
const result = script.runInContext(sandbox, { displayErrors: true });
|
||||
if (result && typeof result.then === "function") {
|
||||
await abortable(result);
|
||||
}
|
||||
appendLog,
|
||||
isAborted: () => executionToken.closed || Boolean(deps.isAborted?.()),
|
||||
syncExecutionTimeoutMs,
|
||||
registerStop: (stop) => { stopActiveExecution = stop; },
|
||||
executionContext,
|
||||
executionToken,
|
||||
});
|
||||
assertNotAborted();
|
||||
}
|
||||
|
||||
return { execute, nct };
|
||||
return {
|
||||
execute,
|
||||
nct,
|
||||
stop(reason = new Error("Script stopped")) {
|
||||
stopActiveExecution(reason);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function interruptibleSleep(ms, isAborted) {
|
||||
@@ -862,9 +1305,16 @@ function formatScriptInputForLog(data) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS,
|
||||
SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS,
|
||||
SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS,
|
||||
SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS,
|
||||
SCRIPT_SYNC_EXECUTION_TIMEOUT_MS,
|
||||
createScriptRuntime,
|
||||
wrapScriptSource,
|
||||
interruptibleSleep,
|
||||
formatScriptInputForLog,
|
||||
normalizeDialogFormSpec,
|
||||
_getActiveScriptWorkerCountForTests: () => activeScriptWorkers.size,
|
||||
_getActiveScriptHostRequestCountForTests: () => activeScriptHostRequests.size,
|
||||
};
|
||||
Reference in new issue
Block a user