#!/usr/bin/env node // Captures one real Jellyfin webhook body and reports exactly what the bridge // would make of it. Verification tool, not part of the Worker. // // node scripts/catch-webhook.mjs [port] // // No secret is checked, nothing is sent anywhere, and nothing is written to // Popfeed. The Authorization header is redacted before printing. // // WHAT THIS ANSWERS // // Whether the Webhook plugin HTML-escapes `{{Name}}` (so "Rock & Roll" arrives // as "Rock & Roll"), and whether `UserId` and `NotificationUsername` are // the real field names. The second one matters: if they are wrong, turning on // JELLYFIN_USER would silently skip every event. // // A Jellyfin API key cannot answer either question. It shows the name stored in // the database, not the bytes Handlebars renders. Only a capture is ground truth. // // HOW TO RUN IT // // Add TWO throwaway Generic Destinations in Dashboard > Plugins > Webhook, both // pointing at this server, and leave your real destination alone. Both: Playback // Stop only, Item Type Movies and Episodes, no request header needed. // // 1. "capture-raw" Send All Properties CHECKED, template empty. // Bypasses Handlebars, so it shows the true field // names and the unescaped title. // 2. "capture-template" Send All Properties UNCHECKED, template = // src/sources/jellyfin/template.hbs. // Shows what Handlebars did to that same title. // // Then play anything for about five seconds and stop it. One playback delivers // both, and comparing them settles the escaping question with no guessing. // // The escaping half only proves something if the title contains `&` or an // apostrophe. If nothing in the library fits, temporarily rename one item's // metadata and undo it afterwards. // // Afterwards: delete both destinations, and close the firewall port you opened. import { createServer } from 'node:http'; import { writeFileSync } from 'node:fs'; import { parseBody, toWatchEvent } from '../src/sources/jellyfin/index.js'; const port = Number(process.argv[2] ?? 8123); const isJson = (text) => { try { JSON.parse(text); return true; } catch { return false; } }; /** * The forms that decide whether the rendered template is JSON-safe. * * "a control character inside a string" fires for any pretty-printed body, * because of the newlines between fields rather than inside one. When the two * disagree, `valid JSON as sent` is the honest answer. */ function escapingIn(text) { const hits = []; if (/&(#x?[\da-fA-F]+|[a-zA-Z]+);/.test(text)) hits.push('HTML entities'); if (/[^\\]\\(?!["\\/bfnrtu])/.test(text)) hits.push('a bare backslash'); if (/"[^"]*[\n\r\t][^"]*"/.test(text)) hits.push('a control character inside a string'); return hits; } const report = (label, value) => console.log(` ${label.padEnd(24)} ${value}`); const FIELDS = [ 'NotificationType', 'ItemType', 'Name', 'SeriesName', 'SeriesId', 'UserId', 'NotificationUsername', 'PlayedToCompletion', 'SeasonNumber', 'EpisodeNumber', 'Provider_tmdb', 'Provider_imdb', 'Provider_tvdb', ]; createServer((req, res) => { const chunks = []; req.on('data', (c) => chunks.push(c)); req.on('end', () => { res.writeHead(200, { 'content-type': 'text/plain' }); res.end('captured\n'); const raw = Buffer.concat(chunks); const text = raw.toString('utf8'); console.log(`\n${'='.repeat(72)}`); console.log(`${req.method} ${req.url} from ${req.socket.remoteAddress}`); console.log('='.repeat(72)); console.log('\n-- headers --'); for (const [k, v] of Object.entries(req.headers)) { // Never print the shared secret, even locally. report(k, /^(authorization|cookie)$/i.test(k) ? `<${v.split(' ')[0]} ...redacted>` : v); } console.log(`\n-- raw body (${raw.length} bytes, exactly as sent) --`); console.log(text); const found = escapingIn(text); console.log('-- what is in it --'); report('escaping found', found.length ? found.join(', ') : 'none of the risky forms'); report('valid JSON as sent', isJson(text) ? 'yes' : 'NO -- this used to be a silent 400'); const payload = parseBody(text); if (!payload) { console.log('\n parseBody could not salvage this one.'); } else { console.log('\n-- the fields the bridge relies on --'); for (const key of FIELDS) { report( key, Object.hasOwn(payload, key) ? JSON.stringify(payload[key]) : '** ABSENT from the template **' ); } console.log('\n-- what toWatchEvent makes of it --'); const parsed = toWatchEvent(payload, {}); if (parsed.status === 'skipped') { report('result', `skipped: ${parsed.reason}`); } else { report('title after decoding', JSON.stringify(parsed.event.title)); report('seriesName', JSON.stringify(parsed.event.seriesName)); report('seriesKey', JSON.stringify(parsed.event.seriesKey)); report('ids kept', JSON.stringify(parsed.event.ids)); } } const file = `/tmp/jellyfin-payload-${Date.now()}.json`; writeFileSync(file, raw); console.log(`\n raw body saved to ${file}\n`); }); }).listen(port, '0.0.0.0', () => { console.log(`listening on 0.0.0.0:${port} -- waiting for a webhook`); console.log('press ctrl-c when done\n'); });