

















@@ -23,6 +23,13 @@ export type GatewayInstance = OpenClawTestInstance;
2323const GATEWAY_CONNECT_STATUS_TIMEOUT_MS = 10_000;
2424const GATEWAY_NODE_STATUS_TIMEOUT_MS = 15_000;
2525const GATEWAY_NODE_STATUS_POLL_MS = 20;
26+const POST_JSON_TIMEOUT_MS = 15_000;
27+const POST_JSON_MAX_RESPONSE_BYTES = 1024 * 1024;
28+29+export type PostJsonOptions = {
30+maxResponseBytes?: number;
31+timeoutMs?: number;
32+};
26332734export async function spawnGatewayInstance(name: string): Promise<GatewayInstance> {
2835const inst = await createOpenClawTestInstance({ name });
@@ -43,10 +50,32 @@ export async function postJson(
4350url: string,
4451body: unknown,
4552headers?: Record<string, string>,
53+options: PostJsonOptions = {},
4654): Promise<{ status: number; json: unknown }> {
4755const payload = JSON.stringify(body);
4856const parsed = new URL(url);
57+const timeoutMs = options.timeoutMs ?? POST_JSON_TIMEOUT_MS;
58+const maxResponseBytes = options.maxResponseBytes ?? POST_JSON_MAX_RESPONSE_BYTES;
4959return await new Promise<{ status: number; json: unknown }>((resolve, reject) => {
60+let settled = false;
61+let responseBytes = 0;
62+let timeout: NodeJS.Timeout | undefined;
63+64+const finish = (result: { status: number; json: unknown } | { error: Error }) => {
65+if (settled) {
66+return;
67+}
68+settled = true;
69+if (timeout) {
70+clearTimeout(timeout);
71+}
72+if ("error" in result) {
73+reject(result.error);
74+return;
75+}
76+resolve(result);
77+};
78+5079const req = httpRequest(
5180{
5281method: "POST",
@@ -63,6 +92,14 @@ export async function postJson(
6392let data = "";
6493res.setEncoding("utf8");
6594res.on("data", (chunk) => {
95+responseBytes += Buffer.byteLength(chunk, "utf8");
96+if (responseBytes > maxResponseBytes) {
97+const error = new Error(`POST ${url} response exceeded ${maxResponseBytes} bytes`);
98+req.destroy(error);
99+res.destroy(error);
100+finish({ error });
101+return;
102+}
66103data += chunk;
67104});
68105res.on("end", () => {
@@ -74,11 +111,16 @@ export async function postJson(
74111json = data;
75112}
76113}
77-resolve({ status: res.statusCode ?? 0, json });
114+finish({ status: res.statusCode ?? 0, json });
78115});
116+res.on("error", (error) => finish({ error }));
79117},
80118);
81-req.on("error", reject);
119+timeout = setTimeout(() => {
120+req.destroy(new Error(`POST ${url} timed out after ${timeoutMs}ms`));
121+}, timeoutMs);
122+timeout.unref?.();
123+req.on("error", (error) => finish({ error }));
82124req.write(payload);
83125req.end();
84126});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。