Files
openclaw/scripts/e2e/telegram-user-credential-io.ts
2026-05-28 20:29:16 +02:00

217 lines
5.9 KiB
TypeScript

import { spawn } from "node:child_process";
export type JsonObject = Record<string, unknown>;
type FetchJsonParams = {
fetchImpl?: (url: string, init: RequestInit) => Promise<Response>;
init: RequestInit;
label: string;
maxBodyBytes?: number;
timeoutMs: number;
url: string;
};
type RunCommandOptions = {
outputLimit?: number;
timeoutKillGraceMs?: number;
timeoutMs: number;
};
const DEFAULT_OUTPUT_LIMIT = 128 * 1024;
const DEFAULT_FETCH_BODY_LIMIT = 1024 * 1024;
const KILL_GRACE_MS = 5_000;
function timeoutError(message: string) {
return Object.assign(new Error(message), { code: "ETIMEDOUT" });
}
function bodyTooLargeError(message: string) {
return Object.assign(new Error(message), { code: "ETOOBIG" });
}
function resolveFetchBodyLimit(limit: number | undefined) {
if (limit !== undefined) {
if (!Number.isSafeInteger(limit) || limit < 1) {
throw new Error(`fetch JSON body limit must be a positive integer; got: ${limit}`);
}
return limit;
}
const raw = process.env.OPENCLAW_QA_CREDENTIAL_HTTP_MAX_BODY_BYTES?.trim();
if (!raw) {
return DEFAULT_FETCH_BODY_LIMIT;
}
if (!/^\d+$/u.test(raw)) {
throw new Error(
`OPENCLAW_QA_CREDENTIAL_HTTP_MAX_BODY_BYTES must be a positive integer; got: ${raw}`,
);
}
const parsed = Number(raw);
if (!Number.isSafeInteger(parsed) || parsed < 1) {
throw new Error(
`OPENCLAW_QA_CREDENTIAL_HTTP_MAX_BODY_BYTES must be a positive integer; got: ${raw}`,
);
}
return parsed;
}
function appendBounded(previous: string, chunk: Buffer, limit: number) {
const next = previous + chunk.toString();
if (next.length <= limit) {
return next;
}
return next.slice(next.length - limit);
}
export function runCommand(
command: string,
args: string[],
cwd: string | undefined,
options: RunCommandOptions,
) {
return new Promise<void>((resolve, reject) => {
const child = spawn(command, args, {
cwd,
stdio: ["ignore", "pipe", "pipe"],
});
const outputLimit = options.outputLimit ?? DEFAULT_OUTPUT_LIMIT;
let stdout = "";
let stderr = "";
let settled = false;
let timeout: NodeJS.Timeout;
let killTimer: NodeJS.Timeout | undefined;
let timedOutError: Error | undefined;
const timeoutMs = Math.max(1, options.timeoutMs);
const timeoutKillGraceMs = Math.max(0, options.timeoutKillGraceMs ?? KILL_GRACE_MS);
const clearTimers = () => {
clearTimeout(timeout);
if (killTimer) {
clearTimeout(killTimer);
}
};
const fail = (error: Error) => {
if (settled) {
return;
}
settled = true;
clearTimers();
reject(error);
};
timeout = setTimeout(() => {
if (settled) {
return;
}
timedOutError = timeoutError(
`${command} ${args.join(" ")} timed out after ${timeoutMs}ms\n${stdout}${stderr}`,
);
child.kill("SIGTERM");
killTimer = setTimeout(() => {
child.kill("SIGKILL");
}, timeoutKillGraceMs);
killTimer.unref?.();
}, timeoutMs);
timeout.unref?.();
child.stdout.on("data", (chunk: Buffer) => {
stdout = appendBounded(stdout, chunk, outputLimit);
});
child.stderr.on("data", (chunk: Buffer) => {
stderr = appendBounded(stderr, chunk, outputLimit);
});
child.on("error", fail);
child.on("close", (code, signal) => {
if (settled) {
return;
}
settled = true;
clearTimers();
if (timedOutError) {
reject(timedOutError);
return;
}
if (code === 0) {
resolve();
return;
}
const detail = signal ? `signal ${signal}` : `exit code ${code ?? "unknown"}`;
reject(new Error(`${command} ${args.join(" ")} failed with ${detail}\n${stdout}${stderr}`));
});
});
}
async function readBoundedResponseText(
response: Response,
label: string,
byteLimit: number,
timeoutPromise: Promise<never>,
) {
const contentLength = response.headers.get("content-length");
if (contentLength) {
const parsedLength = Number(contentLength);
if (Number.isSafeInteger(parsedLength) && parsedLength > byteLimit) {
await response.body?.cancel().catch(() => {});
throw bodyTooLargeError(`${label} response body exceeded ${byteLimit} bytes`);
}
}
if (!response.body) {
return "";
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let byteCount = 0;
let text = "";
try {
while (true) {
const { done, value } = await Promise.race([reader.read(), timeoutPromise]);
if (done) {
return text + decoder.decode();
}
byteCount += value.byteLength;
if (byteCount > byteLimit) {
await reader.cancel().catch(() => {});
throw bodyTooLargeError(`${label} response body exceeded ${byteLimit} bytes`);
}
text += decoder.decode(value, { stream: true });
}
} finally {
reader.releaseLock();
}
}
export async function fetchJsonWithTimeout(params: FetchJsonParams) {
const timeoutMs = Math.max(1, params.timeoutMs);
const maxBodyBytes = resolveFetchBodyLimit(params.maxBodyBytes);
const controller = new AbortController();
const error = timeoutError(`${params.label} timed out after ${timeoutMs}ms`);
let timeout: NodeJS.Timeout | undefined;
const timeoutPromise = new Promise<never>((_, reject) => {
timeout = setTimeout(() => {
controller.abort(error);
reject(error);
}, timeoutMs);
timeout.unref?.();
});
try {
const response = await Promise.race([
(params.fetchImpl ?? fetch)(params.url, {
...params.init,
signal: controller.signal,
}),
timeoutPromise,
]);
const rawPayload = await readBoundedResponseText(
response,
params.label,
maxBodyBytes,
timeoutPromise,
);
const payload = JSON.parse(rawPayload) as JsonObject;
return { payload, response };
} finally {
if (timeout) {
clearTimeout(timeout);
}
}
}