| 1 | const http = require('http'); |
| 2 | const fs = require('fs').promises; |
| 3 | |
| 4 | const serve = (file, type = 'text/html') => async (req, res) => { |
| 5 | try { |
| 6 | const content = await fs.readFile(file); |
| 7 | res.writeHead(200, { 'Content-Type': type }); |
| 8 | res.end(content); |
| 9 | } catch (err) { |
| 10 | res.writeHead(404); |
| 11 | res.end('Not Found'); |
| 12 | } |
| 13 | }; |
| 14 | |
| 15 | const sse = (handler) => (req, res) => { |
| 16 | res.writeHead(200, { |
| 17 | 'Content-Type': 'text/event-stream', |
| 18 | 'Cache-Control': 'no-cache', |
| 19 | 'Connection': 'keep-alive' |
| 20 | }); |
| 21 | handler(req, res); |
| 22 | }; |
| 23 | |
| 24 | const routes = { |
| 25 | '/': (req, res) => { res.writeHead(302, {'Location': '/sse'}); res.end(); }, |
| 26 | '/sse': serve('test/manual/sse.html'), |
| 27 | '/ios-sse': serve('test/manual/ios-sse.html'), |
| 28 | '/htmx.js': serve('src/htmx.js', 'application/javascript'), |
| 29 | '/ext/hx-sse.js': serve('src/ext/hx-sse.js', 'application/javascript'), |
| 30 | |
| 31 | '/heartbeat': sse((req, res) => { |
| 32 | let count = 0; |
| 33 | const send = () => { |
| 34 | count++; |
| 35 | const time = new Date().toLocaleTimeString(); |
| 36 | res.write(`data: <div>#${count} - ${time}</div>\n\n`); |
| 37 | }; |
| 38 | send(); |
| 39 | const interval = setInterval(send, 2000); |
| 40 | req.on('close', () => clearInterval(interval)); |
| 41 | }), |
| 42 | |
| 43 | '/matrix-stream': sse((req, res) => { |
| 44 | const fullText = "Wake up, Neo... The Matrix has you."; |
| 45 | let index = 0; |
| 46 | |
| 47 | const typeChar = () => { |
| 48 | if (index < fullText.length) { |
| 49 | index++; |
| 50 | res.write(`data: <div class="matrix-text">${fullText.slice(0, index)}</div>\n\n`); |
| 51 | setTimeout(typeChar, index === 16 ? 250 : 20 + Math.random() * 20); |
| 52 | } else { |
| 53 | setTimeout(() => { |
| 54 | const deleteChar = () => { |
| 55 | if (index > 0) { |
| 56 | index--; |
| 57 | res.write(`data: <div class="matrix-text">${fullText.slice(0, index)}</div>\n\n`); |
| 58 | setTimeout(deleteChar, 20); |
| 59 | } else { |
| 60 | res.write(`data: <div>\u200E</div>\n\n`); |
| 61 | res.end(); |
| 62 | } |
| 63 | }; |
| 64 | deleteChar(); |
| 65 | }, 750); |
| 66 | } |
| 67 | }; |
| 68 | typeChar(); |
| 69 | }), |
| 70 | |
| 71 | '/events': sse((req, res) => { |
| 72 | const activities = ['User joined', 'File uploaded', 'Comment added', 'Task completed', 'Message sent']; |
| 73 | const statuses = ['Paused', 'Active', 'Overdrive']; |
| 74 | let status = 'Active'; |
| 75 | let statusChangeTimer = Date.now() + 2000; |
| 76 | |
| 77 | res.write(`data: <hx-partial hx-target="#events-output" hx-swap="beforeend"><div class="events-active"></div></hx-partial>\n\n`); |
| 78 | res.write(`data: <hx-partial hx-target="#system-status" hx-swap="innerHTML">${status}</hx-partial>\n\n`); |
| 79 | |
| 80 | const send = () => { |
| 81 | const now = Date.now(); |
| 82 | if (now >= statusChangeTimer) { |
| 83 | const available = statuses.filter(s => s !== status); |
| 84 | status = available[Math.floor(Math.random() * available.length)]; |
| 85 | res.write(`data: <hx-partial hx-target="#system-status" hx-swap="innerHTML">${status}</hx-partial>\n\n`); |
| 86 | statusChangeTimer = now + 2000; |
| 87 | } |
| 88 | |
| 89 | if (status === 'Paused') return setTimeout(send, 100); |
| 90 | |
| 91 | const delay = status === 'Overdrive' ? 100 : 500; |
| 92 | res.write(`data: <hx-partial hx-target="#activity" hx-swap="beforeend"><div>${activities[Math.floor(Math.random() * activities.length)]}</div></hx-partial>\n\n`); |
| 93 | setTimeout(send, delay); |
| 94 | }; |
| 95 | send(); |
| 96 | }), |
| 97 | |
| 98 | '/progress-stream': sse((req, res) => { |
| 99 | let progress = 0; |
| 100 | const update = () => { |
| 101 | if (progress < 100) { |
| 102 | progress += Math.floor(Math.random() * 5) + 5; |
| 103 | if (progress > 100) progress = 100; |
| 104 | res.write(`event: progress\ndata: ${progress}\n\n`); |
| 105 | setTimeout(update, 50); |
| 106 | } else { |
| 107 | res.write(`event: done\ndata: complete\n\n`); |
| 108 | res.end(); |
| 109 | } |
| 110 | }; |
| 111 | update(); |
| 112 | }) |
| 113 | }; |
| 114 | |
| 115 | http.createServer(async (req, res) => { |
| 116 | const pathname = new URL(req.url, 'http://localhost').pathname; |
| 117 | const handler = routes[pathname]; |
| 118 | |
| 119 | if (handler) { |
| 120 | try { |
| 121 | await handler(req, res); |
| 122 | } catch (err) { |
| 123 | if (!res.headersSent) { |
| 124 | res.writeHead(500); |
| 125 | res.end('Server Error'); |
| 126 | } |
| 127 | } |
| 128 | } else { |
| 129 | res.writeHead(404); |
| 130 | res.end('404 Not Found'); |
| 131 | } |
| 132 | }).listen(3000, '0.0.0.0', () => { |
| 133 | const nets = require('os').networkInterfaces(); |
| 134 | const ip = Object.values(nets).flat().find(n => n.family === 'IPv4' && !n.internal)?.address; |
| 135 | console.log('Local: http://localhost:3000'); |
| 136 | if (ip) console.log(`Network: http://${ip}:3000`); |
| 137 | }); |