Skip to content

Commit 3afda8c

Browse files
Autowebassat-blipCodex Microtask Operator
andauthored
Fallback invalid CommandBoard API ports (#39)
Co-authored-by: Codex Microtask Operator <codex-microtask@example.com>
1 parent e89c10c commit 3afda8c

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

apps/commandboard-api/src/contract.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Server } from "node:http";
22
import { afterAll, beforeAll, describe, expect, it } from "vitest";
3-
import { createCommandBoardServer } from "./index.js";
3+
import { createCommandBoardServer, readPort } from "./index.js";
44

55
let server: Server;
66
let baseUrl: string;
@@ -22,6 +22,13 @@ afterAll(async () => {
2222
});
2323

2424
describe("CommandBoard API contracts", () => {
25+
it("falls back when PORT is invalid", () => {
26+
expect(readPort("not-a-port", 4010)).toBe(4010);
27+
expect(readPort("0", 4010)).toBe(4010);
28+
expect(readPort("65536", 4010)).toBe(4010);
29+
expect(readPort("4020", 4010)).toBe(4020);
30+
});
31+
2532
it("exposes root API index", async () => {
2633
const response = await fetch(`${baseUrl}/`);
2734
const body = await response.json() as { ok: boolean; service: string; endpoints: string[] };

apps/commandboard-api/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,19 @@ function formattedDiscoverMatch(format: "rss" | "opml" | "atom" | "json-feed", e
396396
}
397397
}
398398

399-
export function startCommandBoardServer(port = Number(process.env.PORT ?? 4010)) {
399+
export function startCommandBoardServer(port = readPort(process.env.PORT, 4010)) {
400400
const server = createCommandBoardServer();
401401
server.listen(port, () => {
402402
console.log(`CommandBoard.run API listening on http://localhost:${port}`);
403403
});
404404
return server;
405405
}
406406

407+
export function readPort(value: string | undefined, fallback: number) {
408+
const parsed = Number(value ?? fallback);
409+
return Number.isInteger(parsed) && parsed > 0 && parsed <= 65535 ? parsed : fallback;
410+
}
411+
407412
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
408413
startCommandBoardServer();
409414
}

0 commit comments

Comments
 (0)