Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nixamp",
"version": "0.45.1",
"version": "0.45.2",
"description": "It really whips the terminal's ass. A Winamp-shaped audio player for your terminal.",
"license": "MIT",
"type": "module",
Expand Down
8 changes: 7 additions & 1 deletion src/guide-rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ export async function handleRooms(request: IncomingMessage, response: ServerResp
return true;
}
if (path === "/api/v1/guide/rooms" && method === "POST") {
const body = (await ctx.readBody(request).catch(() => null)) as { play?: unknown; basis?: unknown; note?: unknown; title?: unknown; agree?: unknown } | null;
// The server hands the body over as text; parsed here, and anything unreadable is no body.
const raw = await ctx.readBody(request).catch(() => null);
let parsed: unknown = raw;
if (typeof raw === "string") {
try { parsed = JSON.parse(raw); } catch { parsed = null; }
}
const body = (parsed && typeof parsed === "object" ? parsed : null) as { play?: unknown; basis?: unknown; note?: unknown; title?: unknown; agree?: unknown } | null;
const address = roomAddress(typeof body?.play === "string" ? body.play : "");
if (!address) {
ctx.json(response, 400, { error: "play must be a nixamp room's stream address: https://host/api/channels/<id>?k=KEY", code: "bad_address" });
Expand Down
1 change: 1 addition & 0 deletions src/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function readConfig(): Config {
...(Array.isArray(record["guideHosts"]) ? { guideHosts: (record["guideHosts"] as unknown[]).filter((one): one is string => typeof one === "string") } : {}),
...(typeof record["guideAds"] === "object" && record["guideAds"] !== null ? { guideAds: record["guideAds"] as Config["guideAds"] } : {}),
...(Array.isArray(record["guidePeers"]) ? { guidePeers: (record["guidePeers"] as unknown[]).filter((one): one is string => typeof one === "string") } : {}),
...(typeof record["watchBase"] === "string" ? { watchBase: record["watchBase"] } : {}),
};
} catch {
return {};
Expand Down
20 changes: 18 additions & 2 deletions test/guide-rooms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { AddressInfo } from "node:net";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { handleRooms, RoomProbe, roomAddress, roomEntries, RoomStore, ROOMS_PER_MEMBER, watchPage } from "../src/guide-rooms.ts";
import { handleRooms, RoomProbe, roomAddress, roomEntries, RoomStore, ROOMS_PER_MEMBER, watchBase, watchPage } from "../src/guide-rooms.ts";
import { readConfig, writeConfig } from "../src/library.ts";
import { renderGuide } from "../src/station-guide.ts";

const PLAY = "https://server1.chovy.nixamp.com:4321/api/channels/folder-b19b85542264?k=JV5m_XbnQFD0K9_JEnGEXw";
Expand All @@ -25,6 +26,20 @@ test("a room address is a channel or the live stream, with a key and nothing els
]) assert.equal(roomAddress(bad), null, bad);
});

test("watchBase survives a config read, so the guide links rooms where it was told", () => {
const dir = mkdtempSync(join(tmpdir(), "rooms-config-"));
const saved = process.env["XDG_STATE_HOME"];
process.env["XDG_STATE_HOME"] = dir;
try {
writeConfig({ watchBase: "https://live.streaming.pit.world" });
assert.equal(readConfig().watchBase, "https://live.streaming.pit.world");
assert.equal(watchBase("https://fallback.example", {}), "https://live.streaming.pit.world");
} finally {
if (saved === undefined) delete process.env["XDG_STATE_HOME"]; else process.env["XDG_STATE_HOME"] = saved;
rmSync(dir, { recursive: true, force: true });
}
});

test("the room page carries the stream address", () => {
assert.equal(watchPage("https://live.streaming.pit.world", PLAY), `https://live.streaming.pit.world/watch?play=${encodeURIComponent(PLAY)}`);
});
Expand Down Expand Up @@ -54,7 +69,8 @@ async function serve(ctx: Omit<Parameters<typeof handleRooms>[3], "json" | "read
void handleRooms(request, response, new URL(request.url ?? "/", "http://x").pathname, {
...ctx,
json: (res, status, body) => { res.writeHead(status, { "content-type": "application/json" }); res.end(JSON.stringify(body)); },
readBody: async (req) => { let text = ""; for await (const chunk of req) text += chunk; return JSON.parse(text); },
// Text, as the server's own readBody hands it over (a parsed object here hid a broken publish).
readBody: async (req) => { let text = ""; for await (const chunk of req) text += chunk; return text; },
}).then((handled) => { if (!handled) { response.writeHead(404); response.end(); } });
});
await new Promise<void>((done) => server.listen(0, "127.0.0.1", done));
Expand Down
Loading