This commit is contained in:
Max Kong 2026-01-30 02:44:44 +00:00 committed by GitHub
commit b45e568cfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 2 deletions

View File

@ -44,7 +44,7 @@ export async function ensureMediaHosted(
if (!mediaServer) {
mediaServer = await startMediaServer(port, TTL_MS, runtime);
logInfo(
`🦞 Started temporary media host on http://localhost:${port}/media/:id (TTL ${TTL_MS / 1000}s)`,
`🦞 Started temporary media host on http://127.0.0.1:${port}/media/:id (TTL ${TTL_MS / 1000}s)`,
runtime,
);
mediaServer.unref?.();

View File

@ -54,6 +54,13 @@ describe("media server", () => {
await new Promise((r) => server.close(r));
});
it("binds to loopback by default", async () => {
const server = await startMediaServer(0, 5_000);
const addr = server.address() as AddressInfo;
expect(["127.0.0.1", "::1"]).toContain(addr.address);
await new Promise((r) => server.close(r));
});
it("expires old media", async () => {
const file = path.join(MEDIA_DIR, "old");
await fs.writeFile(file, "stale");

View File

@ -84,11 +84,12 @@ export async function startMediaServer(
port: number,
ttlMs = DEFAULT_TTL_MS,
runtime: RuntimeEnv = defaultRuntime,
host = "127.0.0.1",
): Promise<Server> {
const app = express();
attachMediaRoutes(app, ttlMs, runtime);
return await new Promise((resolve, reject) => {
const server = app.listen(port);
const server = app.listen(port, host);
server.once("listening", () => resolve(server));
server.once("error", (err) => {
runtime.error(danger(`Media server failed: ${String(err)}`));