* Probe a single port for a HyperFrames config response. * Returns the full config or null if not a HyperFrames server.
(port: number)
| 223 | * Returns the full config or null if not a HyperFrames server. |
| 224 | */ |
| 225 | function probePort(port: number): Promise<HyperframesConfigResponse | null> { |
| 226 | return new Promise<HyperframesConfigResponse | null>((resolveResult) => { |
| 227 | const req = http.get( |
| 228 | { hostname: "127.0.0.1", port, path: "/__hyperframes_config", timeout: PROBE_TIMEOUT_MS }, |
| 229 | (res) => { |
| 230 | if (res.statusCode !== 200) { |
| 231 | res.resume(); |
| 232 | return resolveResult(null); |
| 233 | } |
| 234 | let data = ""; |
| 235 | let bytes = 0; |
| 236 | res.on("data", (chunk: Buffer | string) => { |
| 237 | bytes += typeof chunk === "string" ? chunk.length : chunk.byteLength; |
| 238 | if (bytes > PROBE_MAX_BYTES) { |
| 239 | req.destroy(); |
| 240 | return resolveResult(null); |
| 241 | } |
| 242 | data += chunk; |
| 243 | }); |
| 244 | res.on("error", () => resolveResult(null)); |
| 245 | res.on("end", () => { |
| 246 | try { |
| 247 | const json = JSON.parse(data) as HyperframesConfigResponse; |
| 248 | resolveResult(json.isHyperframes === true ? json : null); |
| 249 | } catch { |
| 250 | resolveResult(null); |
| 251 | } |
| 252 | }); |
| 253 | }, |
| 254 | ); |
| 255 | req.on("error", () => resolveResult(null)); |
| 256 | req.on("timeout", () => { |
| 257 | req.destroy(); |
| 258 | resolveResult(null); |
| 259 | }); |
| 260 | }); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Scan the default port range for active HyperFrames preview servers. |
no test coverage detected