fix gateway probe bad port transport
This commit is contained in:
@@ -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 }) {
|
||||
|
||||
Reference in New Issue
Block a user