Where they come from, in order: the chapters file an item names
(<podcast:chapters url>, the Podcasting 2.0 namespace's JSON), stored on
the item as entries.chapters and read when someone opens Now Playing; else
the downloaded file's own chapters (ID3 CHAP frames, MP4 chapters), which
ffprobe reads as it does for DeadAir. A chapter with toc false is only
there to change the artwork and is left out.
GET /api/enclosures/{id}/chapters answers for a file, since a file is what
plays. With ?deadair=true the starts are moved into DeadAir's shortened
clock with to_copy, as positions are, or a chapter would start late by
every silence cut before it. A chapters file that cannot be read is an
empty list: the player plays on without them, and the step's span says why.
Items already stored get their chapters address when a scan next finds
them told differently; feeds that answer 304 add it with their next items.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqkrVBuJRne2JAhFpaDV2U
168 lines
10 KiB
TypeScript
168 lines
10 KiB
TypeScript
// Serves the fixture feeds so the daemon under test has something real to scan.
|
|
import http from 'node:http';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const dir = import.meta.dirname;
|
|
const port = Number(process.env.FIXTURE_PORT || 8792);
|
|
|
|
let flip = 'ok';
|
|
|
|
http.createServer((req, res) => {
|
|
// Jev, for Signals (#254): sure of an item whose text has a question's keyword in it. Refuses
|
|
// a request in the shape TypeSafe refuses, as it does (#257).
|
|
if (req.method === 'POST' && req.url === '/jev') {
|
|
let body = '';
|
|
req.on('data', c => { body += c; });
|
|
req.on('end', () => {
|
|
// As TypeSafe answers a key it does not know (#271).
|
|
if (req.headers.authorization === 'Bearer bad-key') {
|
|
res.writeHead(401, { 'content-type': 'application/json' }).end('{"detail":"invalid api key"}');
|
|
return;
|
|
}
|
|
// As ipx asks TypeSafe's Jev: the item, and a question a Signal.
|
|
const { state, questions } = JSON.parse(body) as { state: unknown, questions: Record<string, { criteria: unknown, instructions: string }> };
|
|
if (Object.values(questions).some(q => typeof q.criteria !== 'object')) {
|
|
res.writeHead(422, { 'content-type': 'application/json' }).end('{"detail":"criteria should be an object"}');
|
|
return;
|
|
}
|
|
const title = String(state).toLowerCase();
|
|
const answers: Record<string, { noul: number }> = {};
|
|
for (const [id, q] of Object.entries(questions)) {
|
|
// A key's check asks about no Signal's words.
|
|
const words = /described by these words: ([^.]*)\./.exec(q.instructions)?.[1].split(', ') ?? [];
|
|
answers[id] = { noul: words.some(w => title.includes(w.toLowerCase())) ? 0.9 : 0.1 };
|
|
}
|
|
res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ answers }));
|
|
});
|
|
return;
|
|
}
|
|
// Dated today, since a Signal looks at the last 30 days only.
|
|
if (req.url === '/signal.xml') {
|
|
const now = new Date().toUTCString();
|
|
const item = (guid: string, title: string) => `<item><guid>${guid}</guid><title>${title}</title><pubDate>${now}</pubDate></item>`;
|
|
res.writeHead(200, { 'content-type': 'application/xml' }).end(`<?xml version="1.0"?><rss version="2.0"><channel>
|
|
<title>Signal Show</title>${item('sig-1', 'Monaco qualifying')}${item('sig-2', 'Baking bread')}</channel></rss>`);
|
|
return;
|
|
}
|
|
// A show to download from by hand (#404): one episode sent slowly enough for its progress to be
|
|
// seen, and one that always fails. Their files are theirs alone, as enclosure URLs must be.
|
|
if (req.url === '/live.xml') {
|
|
const item = (guid: string, title: string, file: string) =>
|
|
`<item><guid>${guid}</guid><title>${title}</title><enclosure url="http://127.0.0.1:${port}/${file}" type="audio/mpeg" length="40000"/></item>`;
|
|
res.writeHead(200, { 'content-type': 'application/xml' }).end(`<?xml version="1.0"?><rss version="2.0"><channel>
|
|
<title>Live Show</title>${item('live-1', 'Slow episode', 'live-slow.mp3')}${item('live-2', 'Broken episode', 'live-broken.mp3')}</channel></rss>`);
|
|
return;
|
|
}
|
|
// Four episodes, newest first, for Download latest and a schedule's window (#406).
|
|
if (req.url === '/latest.xml') {
|
|
const item = (n: number) => `<item><guid>latest-${n}</guid><title>Latest ${n}</title><link>http://127.0.0.1:${port}/latest-${n}.html</link><pubDate>${new Date(Date.UTC(2026, 0, 10 - n)).toUTCString()}</pubDate>`
|
|
+ `<enclosure url="http://127.0.0.1:${port}/latest-${n}.mp3" type="audio/mpeg" length="40000"/></item>`;
|
|
res.writeHead(200, { 'content-type': 'application/xml' }).end(`<?xml version="1.0"?><rss version="2.0"><channel>
|
|
<title>Latest Show</title>${[1, 2, 3, 4].map(item).join('')}</channel></rss>`);
|
|
return;
|
|
}
|
|
// Kept on the device and played offline (#412): real audio, which ep1.mp3 is not, at a URL of
|
|
// its own, as enclosure URLs must be.
|
|
if (req.url === '/offline-ep.mp3') {
|
|
const body = fs.readFileSync(path.join(dir, 'quiet.mp3'));
|
|
res.writeHead(200, { 'content-type': 'audio/mpeg', 'content-length': body.length }).end(body);
|
|
return;
|
|
}
|
|
if (/^\/latest-\d\.mp3$/.test(req.url ?? '')) {
|
|
const body = fs.readFileSync(path.join(dir, 'ep1.mp3'));
|
|
res.writeHead(200, { 'content-type': 'audio/mpeg', 'content-length': body.length }).end(body);
|
|
return;
|
|
}
|
|
if (req.url === '/live-broken.mp3') { res.writeHead(500).end('broken'); return; }
|
|
if (req.url === '/live-slow.mp3') {
|
|
const body = fs.readFileSync(path.join(dir, 'ep1.mp3'));
|
|
res.writeHead(200, { 'content-type': 'audio/mpeg', 'content-length': body.length });
|
|
// Ten parts, a fifth of a second apart: two seconds of progress.
|
|
let part = 0;
|
|
const step = Math.ceil(body.length / 10);
|
|
const next = () => {
|
|
if (part * step >= body.length) { res.end(); return; }
|
|
res.write(body.subarray(part * step, ++part * step));
|
|
setTimeout(next, 200);
|
|
};
|
|
next();
|
|
return;
|
|
}
|
|
// Feeds in the shapes the others are not (#463): Atom and RSS each with a bare "&", the RSS with the iTunes and
|
|
// Media RSS fields; WordPress.com's posts as its API sends them; and replies that are no feed.
|
|
const at = `http://127.0.0.1:${port}`;
|
|
const shapes: Record<string, [string, string]> = {
|
|
'/atom.xml': ['application/atom+xml', `<?xml version="1.0" encoding="utf-8"?>
|
|
<feed xmlns="http://www.w3.org/2005/Atom"><title type="html">Atom&#8217;s Show</title>
|
|
<subtitle>An Atom feed</subtitle><link rel="alternate" href="${at}/atom.html"/><logo>${at}/art.jpg</logo>
|
|
<id>urn:atom-show</id><updated>2026-01-01T00:00:00Z</updated>
|
|
<entry><id>urn:atom-1</id><title>Q&A in Atom</title><updated>2026-01-02T00:00:00Z</updated>
|
|
<published>2026-01-02T00:00:00Z</published><link rel="alternate" href="${at}/atom-1.html"/>
|
|
<link rel="enclosure" href="${at}/atom-1.mp3" type="audio/mpeg" length="40000"/>
|
|
<link rel="enclosure" href="${at}/atom-1.mp3?_=1" type="audio/mpeg" length="40000"/>
|
|
<category term="tech"/><summary>Short summary</summary>
|
|
<content type="html"><p>Full <b>notes</b></p></content></entry>
|
|
<entry><id>urn:atom-2</id><title>Markdown entry</title><updated>2026-01-03T00:00:00Z</updated>
|
|
<summary>A [link](http://example.com) and **bold**</summary></entry></feed>`],
|
|
'/amp.xml': ['application/rss+xml', `<?xml version="1.0"?>
|
|
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
|
|
<channel><title>Ampersand Radio</title><link>${at}/amp.html?a=1&b=2</link><description></description><ttl>90</ttl>
|
|
<itunes:summary>Summary from iTunes</itunes:summary><itunes:explicit>yes</itunes:explicit>
|
|
<itunes:category text="Leisure"><itunes:category text="Games"/></itunes:category>
|
|
<item><guid>amp-1</guid><title>Tom & Jerry</title><pubDate>2026-01-05T10:00:00Z</pubDate>
|
|
<itunes:duration>1:02:03</itunes:duration><media:thumbnail url="${at}/art2.jpg"/>
|
|
<description>A subtitle not in the body</description><content:encoded><![CDATA[<p>The body of the notes.</p>]]></content:encoded></item>
|
|
<item><guid>amp-2</guid><title>Cut body</title><description>The whole description</description>
|
|
<content:encoded><![CDATA[x">tail of a tag</p>]]></content:encoded><media:content url="${at}/pic.png" medium="image"/></item>
|
|
<item><title>Picture only</title><enclosure url="${at}/amp-pic.png" type="image/png" length="10"/></item>
|
|
</channel></rss>`],
|
|
'/wp.json': ['application/json', JSON.stringify([{
|
|
guid: { rendered: `${at}/?p=1` }, link: `${at}/wp-1`, title: { rendered: 'WordPress “post”' },
|
|
date_gmt: '2026-01-04T12:00:00', content: { rendered: '<p>Post body</p>' }, jetpack_featured_media_url: `${at}/art.jpg`,
|
|
}])],
|
|
// Where /flip.xml moves to for good.
|
|
'/moved.xml': ['application/rss+xml', `<?xml version="1.0"?><rss version="2.0"><channel><title>Flip Show</title>
|
|
<item><guid>flip-1</guid><title>Flip one</title></item></channel></rss>`],
|
|
// Second Episode's chapters (#477): one only for its artwork, which is not listed.
|
|
'/chapters.json': ['application/json+chapters', JSON.stringify({ version: '1.2.0', chapters: [
|
|
{ startTime: 1.5, title: 'The middle' }, { startTime: 0, title: 'Opening' }, { startTime: 1, img: `${at}/art.jpg`, toc: false },
|
|
] })],
|
|
'/said.txt': ['text/plain', 'Unable to establish a DB connection'],
|
|
'/long.txt': ['text/plain', 'This site is down for maintenance and will be back shortly, thank you for your patience while we work'],
|
|
'/empty.xml': ['application/xml', ''],
|
|
'/garbled.xml': ['application/xml', '<rss><channel><title>Broken'],
|
|
};
|
|
const shape = shapes[req.url ?? ''];
|
|
if (shape) { res.writeHead(200, { 'content-type': shape[0] }).end(shape[1]); return; }
|
|
// A feed that starts failing, in whichever way a test asks for with /flip?to= (#463).
|
|
if (req.url?.startsWith('/flip?to=')) { flip = req.url.slice('/flip?to='.length); res.writeHead(204).end(); return; }
|
|
if (req.url === '/flip.xml') {
|
|
if (/^\d{3}$/.test(flip)) { res.writeHead(Number(flip)).end('no'); return; }
|
|
if (flip === 'moved') { res.writeHead(301, { location: '/moved.xml' }).end(); return; }
|
|
if (flip === 'loop') { res.writeHead(302, { location: '/flip.xml' }).end(); return; }
|
|
if (flip === 'drop') { req.socket.destroy(); return; }
|
|
const body = flip === 'text' ? 'Unable to establish a DB connection'
|
|
: flip === 'nothing' ? ''
|
|
: flip === 'page' ? `<!doctype html><html><head><link rel="alternate" type="application/rss+xml" href="${at}/moved.xml"></head></html>`
|
|
: flip === 'garbled' ? '<rss><channel><title>Broken'
|
|
: `<?xml version="1.0"?><rss version="2.0"><channel><title>Flip Show</title><item><guid>flip-1</guid><title>Flip one</title></item></channel></rss>`;
|
|
res.writeHead(200, { 'content-type': 'application/xml' }).end(body);
|
|
return;
|
|
}
|
|
const name = decodeURIComponent((req.url ?? '/').split('?')[0]!.replace(/^\//, '')) || 'index';
|
|
const file = path.join(dir, path.basename(name));
|
|
fs.readFile(file, (err, body) => {
|
|
if (err) { res.writeHead(404).end('no'); return; }
|
|
const type = file.endsWith('.mp3') ? 'audio/mpeg'
|
|
: file.endsWith('.opml') ? 'text/x-opml'
|
|
// Artwork as an image, or /api/art refuses it as not one.
|
|
: file.endsWith('.jpg') ? 'image/jpeg'
|
|
// A picture a browser can draw: the JPEGs are a few bytes that only say they are one.
|
|
: file.endsWith('.png') ? 'image/png'
|
|
: file.endsWith('.html') ? 'text/html' : 'application/xml';
|
|
res.writeHead(200, { 'content-type': type, 'content-length': body.length });
|
|
res.end(body);
|
|
});
|
|
}).listen(port, '127.0.0.1', () => console.log(`fixtures on ${port}`));
|