const http = require('http'); const fs = require('fs').promises; const routes = { '/': (req, res) => { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(`
Expected: downloads hello.txt
Expected: progress bar fills over ~3s, downloads large.bin
Expected: downloads auto.txt with no hx-swap needed on the element
Expected: downloads redirected.txt AND swaps HTML into the box below
Expected: downloads café report.txt
Expected: downloads unknown-size.txt, progress shows bytes but no percent
Expected: spinner visible during download, hidden after — button re-enabled
`); }, '/htmx.js': async (req, res) => { res.writeHead(200, {'Content-Type': 'application/javascript'}); res.end(await fs.readFile('src/htmx.js')); }, '/ext/hx-download.js': async (req, res) => { res.writeHead(200, {'Content-Type': 'application/javascript'}); res.end(await fs.readFile('src/ext/hx-download.js')); }, // 1. Small file — downloads instantly via hx-swap="download" '/small-file': (req, res) => { let body = 'Hello from hx-download!\n'; res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(body), 'Content-Disposition': 'attachment; filename="hello.txt"' }); res.end(body); }, // 2. Large file — streams slowly so progress bar is visible '/large-file': (req, res) => { let chunkSize = 16 * 1024; let totalChunks = 32; let total = chunkSize * totalChunks; res.writeHead(200, { 'Content-Type': 'application/octet-stream', 'Content-Length': total, 'Content-Disposition': 'attachment; filename="large.bin"' }); let sent = 0; let interval = setInterval(() => { res.write(Buffer.alloc(chunkSize, 0x42)); sent += chunkSize; if (sent >= total) { clearInterval(interval); res.end(); } }, 100); req.on('close', () => clearInterval(interval)); }, // 3. Auto-detect via Content-Disposition: attachment, no hx-swap needed '/auto-detect': (req, res) => { let body = 'auto-detected download\n'; res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(body), 'Content-Disposition': 'attachment; filename="auto.txt"' }); res.end(body); }, // 4a. HX-Download header — also returns HTML for the swap '/hx-download-header': (req, res) => { let body = 'swap also happened!'; res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': Buffer.byteLength(body), 'HX-Download': '/hx-download-target' }); res.end(body); }, // 4b. The actual file fetched by the extension via HX-Download '/hx-download-target': (req, res) => { let body = 'redirected download content\n'; res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(body), 'Content-Disposition': 'attachment; filename="redirected.txt"' }); res.end(body); }, // 5. UTF-8 encoded filename via filename*=UTF-8'' '/utf8-filename': (req, res) => { let body = 'unicode filename test\n'; res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(body), 'Content-Disposition': "attachment; filename*=UTF-8''caf%C3%A9%20report.txt" }); res.end(body); }, // 6. No Content-Length — percent should be null in progress events '/no-content-length': (req, res) => { let chunks = ['no ', 'content-', 'length\n'].map(s => Buffer.from(s)); res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Disposition': 'attachment; filename="unknown-size.txt"' }); let i = 0; let interval = setInterval(() => { res.write(chunks[i++]); if (i >= chunks.length) { clearInterval(interval); res.end(); } }, 200); req.on('close', () => clearInterval(interval)); } }; http.createServer(async (req, res) => { let pathname = new URL(req.url, 'http://localhost').pathname; let handler = routes[pathname]; if (handler) { try { await handler(req, res); } catch (e) { if (!res.headersSent) { res.writeHead(500); res.end('Error'); } } } else { res.writeHead(404); res.end('Not Found'); } }).listen(3001, () => console.log('http://localhost:3001'));