Fallback invalid CommandBoard API ports

This commit is contained in:
Codex Microtask Operator 2026-06-13 01:29:56 +02:00
parent 8f4691584c
commit 237f37517e
2 changed files with 14 additions and 2 deletions

View file

@ -1,6 +1,6 @@
import type { Server } from "node:http"; import type { Server } from "node:http";
import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { createCommandBoardServer } from "./index.js"; import { createCommandBoardServer, readPort } from "./index.js";
let server: Server; let server: Server;
let baseUrl: string; let baseUrl: string;
@ -22,6 +22,13 @@ afterAll(async () => {
}); });
describe("CommandBoard API contracts", () => { describe("CommandBoard API contracts", () => {
it("falls back when PORT is invalid", () => {
expect(readPort("not-a-port", 4010)).toBe(4010);
expect(readPort("0", 4010)).toBe(4010);
expect(readPort("65536", 4010)).toBe(4010);
expect(readPort("4020", 4010)).toBe(4020);
});
it("exposes root API index", async () => { it("exposes root API index", async () => {
const response = await fetch(`${baseUrl}/`); const response = await fetch(`${baseUrl}/`);
const body = await response.json() as { ok: boolean; service: string; endpoints: string[] }; const body = await response.json() as { ok: boolean; service: string; endpoints: string[] };

View file

@ -377,7 +377,7 @@ function formattedDiscoverMatch(format: "rss" | "opml" | "atom" | "json-feed", e
} }
} }
export function startCommandBoardServer(port = Number(process.env.PORT ?? 4010)) { export function startCommandBoardServer(port = readPort(process.env.PORT, 4010)) {
const server = createCommandBoardServer(); const server = createCommandBoardServer();
server.listen(port, () => { server.listen(port, () => {
console.log(`CommandBoard.run API listening on http://localhost:${port}`); console.log(`CommandBoard.run API listening on http://localhost:${port}`);
@ -385,6 +385,11 @@ export function startCommandBoardServer(port = Number(process.env.PORT ?? 4010))
return server; return server;
} }
export function readPort(value: string | undefined, fallback: number) {
const parsed = Number(value ?? fallback);
return Number.isInteger(parsed) && parsed > 0 && parsed <= 65535 ? parsed : fallback;
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
startCommandBoardServer(); startCommandBoardServer();
} }