fix gateway probe bad port transport

This commit is contained in:
Codex
2026-05-25 03:29:52 +00:00
parent 57eb380247
commit 77e73f8bda
5 changed files with 93 additions and 32 deletions
+41 -9
View File
@@ -1,6 +1,8 @@
#!/usr/bin/env node
import { spawn } from "node:child_process";
import { createServer } from "node:net";
import http from "node:http";
import https from "node:https";
import { setTimeout as delay } from "node:timers/promises";
import assert from "node:assert/strict";
@@ -183,26 +185,56 @@ async function waitForJson(url) {
}
async function getJson(url) {
const response = await fetch(url, {
return requestJson(url, {
method: "GET",
headers: { accept: "application/json" }
});
const text = await response.text();
if (!response.ok) throw new Error(`HTTP ${response.status}: ${text}`);
return text ? JSON.parse(text) : null;
}
async function postJson(url, body) {
const response = await fetch(url, {
return requestJson(url, {
method: "POST",
headers: {
accept: "application/json",
"content-type": "application/json"
},
body: JSON.stringify(body)
body
});
}
function requestJson(url, { method, headers = {}, body }) {
return new Promise((resolve, reject) => {
const target = new URL(url);
const client = target.protocol === "https:" ? https : http;
const payload = body === undefined ? "" : JSON.stringify(body);
const request = client.request(target, {
method,
headers: {
...headers,
...(payload ? { "content-length": Buffer.byteLength(payload) } : {})
}
}, (response) => {
let text = "";
response.setEncoding("utf8");
response.on("data", (chunk) => {
text += chunk;
});
response.on("end", () => {
if ((response.statusCode ?? 0) < 200 || (response.statusCode ?? 0) >= 300) {
reject(new Error(`HTTP ${response.statusCode}: ${text}`));
return;
}
try {
resolve(text ? JSON.parse(text) : null);
} catch (error) {
reject(new Error(`non-JSON response: ${error.message}`));
}
});
});
request.on("error", reject);
if (payload) request.write(payload);
request.end();
});
const text = await response.text();
if (!response.ok) throw new Error(`HTTP ${response.status}: ${text}`);
return text ? JSON.parse(text) : null;
}
function shellRpc({ id, traceId, command, cwd, timeoutMs }) {