Debian Codegraph @

www/public/test/manual/download-example.js

raw symbols SCIP
1 const http = require('http');
2 const fs = require('fs').promises;
3
4 const routes = {
5 '/': (req, res) => {
6 res.writeHead(200, {'Content-Type': 'text/html'});
7 res.end(`<!DOCTYPE html>
8 <html>
9 <head>
10 <title>hx-download demo</title>
11 <script src="/htmx.js"></script>
12 <script src="/ext/hx-download.js"></script>
13 <style>
14 body { font-family: system-ui; max-width: 600px; margin: 40px auto; padding: 0 20px; }
15 button { padding: 8px 16px; cursor: pointer; font-size: 14px; }
16 progress { width: 100%; height: 24px; margin: 12px 0; }
17 #status { color: #666; font-size: 14px; min-height: 20px; }
18 </style>
19 </head>
20 <body>
21 <h2>hx-swap="download" demo</h2>
22
23 <button hx-get="/small-file" hx-swap="download" hx-ext="download">
24 Small file (instant)
25 </button>
26
27 <button hx-get="/large-file" hx-swap="download" hx-ext="download">
28 Large file (slow, with progress)
29 </button>
30
31 <progress id="prog" value="0" max="100"></progress>
32 <div id="status"></div>
33
34 <script>
35 let status = document.getElementById("status");
36 let prog = document.getElementById("prog");
37
38 document.body.addEventListener("htmx:download:start", e => {
39 prog.value = 0;
40 let total = e.detail.total;
41 status.textContent = total
42 ? "Downloading " + (total / 1024).toFixed(0) + " KB..."
43 : "Downloading...";
44 });
45
46 document.body.addEventListener("htmx:download:progress", e => {
47 if (e.detail.percent != null) prog.value = e.detail.percent;
48 });
49
50 document.body.addEventListener("htmx:download:complete", e => {
51 prog.value = 100;
52 status.textContent = "Saved " + e.detail.filename + " (" + (e.detail.size / 1024).toFixed(0) + " KB)";
53 });
54 </script>
55 </body>
56 </html>`);
57 },
58
59 '/htmx.js': async (req, res) => {
60 res.writeHead(200, {'Content-Type': 'application/javascript'});
61 res.end(await fs.readFile('src/htmx.js'));
62 },
63
64 '/ext/hx-download.js': async (req, res) => {
65 res.writeHead(200, {'Content-Type': 'application/javascript'});
66 res.end(await fs.readFile('src/ext/hx-download.js'));
67 },
68
69 // Small file — downloads instantly
70 '/small-file': (req, res) => {
71 let body = 'Hello from hx-download!\n';
72 res.writeHead(200, {
73 'Content-Type': 'text/plain',
74 'Content-Length': Buffer.byteLength(body),
75 'Content-Disposition': 'attachment; filename="hello.txt"'
76 });
77 res.end(body);
78 },
79
80 // Large file — streams slowly so you can see the progress bar
81 '/large-file': (req, res) => {
82 let chunkSize = 16 * 1024; // 16 KB chunks
83 let totalChunks = 32; // 512 KB total
84 let total = chunkSize * totalChunks;
85
86 res.writeHead(200, {
87 'Content-Type': 'application/octet-stream',
88 'Content-Length': total,
89 'Content-Disposition': 'attachment; filename="test-data.bin"'
90 });
91
92 let sent = 0;
93 let interval = setInterval(() => {
94 let chunk = Buffer.alloc(chunkSize, 0x42); // fill with 'B'
95 res.write(chunk);
96 sent += chunkSize;
97 if (sent >= total) {
98 clearInterval(interval);
99 res.end();
100 }
101 }, 100);
102
103 req.on('close', () => clearInterval(interval));
104 }
105 };
106
107 http.createServer(async (req, res) => {
108 let pathname = new URL(req.url, 'http://localhost').pathname;
109 let handler = routes[pathname];
110 if (handler) {
111 try { await handler(req, res); }
112 catch (e) { if (!res.headersSent) { res.writeHead(500); res.end('Error'); } }
113 } else {
114 res.writeHead(404); res.end('Not Found');
115 }
116 }).listen(3001, () => console.log('http://localhost:3001'));