refactor(nodes): share run parsing helpers
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
canvasSnapshotTempPath,
|
||||
parseCanvasSnapshotPayload,
|
||||
} from "./nodes-canvas.js";
|
||||
import { parseEnvPairs, parseTimeoutMs } from "./nodes-run.js";
|
||||
import {
|
||||
parseScreenRecordPayload,
|
||||
screenRecordTempPath,
|
||||
@@ -194,20 +195,6 @@ function normalizeNodeKey(value: string) {
|
||||
.replace(/-+$/, "");
|
||||
}
|
||||
|
||||
function parseEnvPairs(pairs: string[] | undefined) {
|
||||
if (!Array.isArray(pairs) || pairs.length === 0) return undefined;
|
||||
const env: Record<string, string> = {};
|
||||
for (const pair of pairs) {
|
||||
const idx = pair.indexOf("=");
|
||||
if (idx <= 0) continue;
|
||||
const key = pair.slice(0, idx).trim();
|
||||
const value = pair.slice(idx + 1);
|
||||
if (!key) continue;
|
||||
env[key] = value;
|
||||
}
|
||||
return Object.keys(env).length > 0 ? env : undefined;
|
||||
}
|
||||
|
||||
async function resolveNodeId(opts: NodesRpcOpts, query: string) {
|
||||
const q = String(query ?? "").trim();
|
||||
if (!q) throw new Error("node required");
|
||||
@@ -598,12 +585,8 @@ export function registerNodesCli(program: Command) {
|
||||
throw new Error("command required");
|
||||
}
|
||||
const env = parseEnvPairs(opts.env);
|
||||
const timeoutMs = opts.commandTimeout
|
||||
? Number.parseInt(String(opts.commandTimeout), 10)
|
||||
: undefined;
|
||||
const invokeTimeout = opts.invokeTimeout
|
||||
? Number.parseInt(String(opts.invokeTimeout), 10)
|
||||
: undefined;
|
||||
const timeoutMs = parseTimeoutMs(opts.commandTimeout);
|
||||
const invokeTimeout = parseTimeoutMs(opts.invokeTimeout);
|
||||
|
||||
const invokeParams: Record<string, unknown> = {
|
||||
nodeId,
|
||||
@@ -612,17 +595,14 @@ export function registerNodesCli(program: Command) {
|
||||
command,
|
||||
cwd: opts.cwd,
|
||||
env,
|
||||
timeoutMs: Number.isFinite(timeoutMs) ? timeoutMs : undefined,
|
||||
timeoutMs,
|
||||
needsScreenRecording: opts.needsScreenRecording === true,
|
||||
},
|
||||
idempotencyKey: String(
|
||||
opts.idempotencyKey ?? randomIdempotencyKey(),
|
||||
),
|
||||
};
|
||||
if (
|
||||
typeof invokeTimeout === "number" &&
|
||||
Number.isFinite(invokeTimeout)
|
||||
) {
|
||||
if (invokeTimeout !== undefined) {
|
||||
invokeParams.timeoutMs = invokeTimeout;
|
||||
}
|
||||
|
||||
|
||||
30
src/cli/nodes-run.ts
Normal file
30
src/cli/nodes-run.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export function parseEnvPairs(
|
||||
pairs: unknown,
|
||||
): Record<string, string> | undefined {
|
||||
if (!Array.isArray(pairs) || pairs.length === 0) return undefined;
|
||||
const env: Record<string, string> = {};
|
||||
for (const pair of pairs) {
|
||||
if (typeof pair !== "string") continue;
|
||||
const idx = pair.indexOf("=");
|
||||
if (idx <= 0) continue;
|
||||
const key = pair.slice(0, idx).trim();
|
||||
if (!key) continue;
|
||||
env[key] = pair.slice(idx + 1);
|
||||
}
|
||||
return Object.keys(env).length > 0 ? env : undefined;
|
||||
}
|
||||
|
||||
export function parseTimeoutMs(raw: unknown): number | undefined {
|
||||
if (raw === undefined || raw === null) return undefined;
|
||||
let value = Number.NaN;
|
||||
if (typeof raw === "number") {
|
||||
value = raw;
|
||||
} else if (typeof raw === "bigint") {
|
||||
value = Number(raw);
|
||||
} else if (typeof raw === "string") {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return undefined;
|
||||
value = Number.parseInt(trimmed, 10);
|
||||
}
|
||||
return Number.isFinite(value) ? value : undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user