| 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 |
<meta name="htmx-config" content='{"extensions":"download"}'> |
| 12 |
<script src="/htmx.js"></script> |
| 13 |
<script src="/ext/hx-download.js"></script> |
| 14 |
<style> |
| 15 |
body { font-family: system-ui; max-width: 700px; margin: 40px auto; padding: 0 20px; } |
| 16 |
h2 { margin-top: 2em; border-bottom: 1px solid #ccc; padding-bottom: 4px; } |
| 17 |
button { padding: 8px 16px; cursor: pointer; font-size: 14px; margin-right: 8px; } |
| 18 |
progress { width: 100%; height: 20px; margin: 8px 0; } |
| 19 |
.status { color: #555; font-size: 13px; min-height: 18px; margin-bottom: 8px; } |
| 20 |
#swap-result { border: 1px solid #ccc; padding: 8px; min-height: 2em; margin-top: 8px; color: #333; } |
| 21 |
.htmx-indicator { display: none; } |
| 22 |
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline; } |
| 23 |
</style> |
| 24 |
</head> |
| 25 |
<body hx-ext="download"> |
| 26 |
<h1>hx-download demo</h1> |
| 27 |
|
| 28 |
<progress id="prog" value="0" max="100"></progress> |
| 29 |
<div class="status" id="status">—</div> |
| 30 |
|
| 31 |
<script> |
| 32 |
let prog = document.getElementById('prog'); |
| 33 |
let status = document.getElementById('status'); |
| 34 |
document.body.addEventListener('htmx:download:start', e => { |
| 35 |
prog.value = 0; |
| 36 |
status.textContent = 'start — total: ' + (e.detail.total ?? 'unknown'); |
| 37 |
}); |
| 38 |
document.body.addEventListener('htmx:download:progress', e => { |
| 39 |
if (e.detail.percent != null) prog.value = e.detail.percent; |
| 40 |
status.textContent = 'progress — ' + e.detail.loaded + ' bytes' |
| 41 |
+ (e.detail.percent != null ? ' (' + e.detail.percent + '%)' : ' (no Content-Length)'); |
| 42 |
}); |
| 43 |
document.body.addEventListener('htmx:download:complete', e => { |
| 44 |
prog.value = 100; |
| 45 |
status.textContent = 'complete — ' + e.detail.filename + ' (' + e.detail.size + ' bytes)'; |
| 46 |
}); |
| 47 |
</script> |
| 48 |
|
| 49 |
<h2>1. hx-swap="download" (explicit)</h2> |
| 50 |
<p>Expected: downloads <code>hello.txt</code></p> |
| 51 |
<button hx-get="/small-file" hx-swap="download">Small file (instant)</button> |
| 52 |
|
| 53 |
<h2>2. Large file with progress</h2> |
| 54 |
<p>Expected: progress bar fills over ~3s, downloads <code>large.bin</code></p> |
| 55 |
<button hx-get="/large-file" hx-swap="download">Large file (slow, with progress)</button> |
| 56 |
|
| 57 |
<h2>3. Content-Disposition: attachment auto-detect</h2> |
| 58 |
<p>Expected: downloads <code>auto.txt</code> with no <code>hx-swap</code> needed on the element</p> |
| 59 |
<button hx-get="/auto-detect" hx-target="#swap-result">Download (auto-detect)</button> |
| 60 |
|
| 61 |
<h2>4. HX-Download header — download + swap</h2> |
| 62 |
<p>Expected: downloads <code>redirected.txt</code> AND swaps HTML into the box below</p> |
| 63 |
<button hx-get="/hx-download-header" hx-target="#swap-result">Download + swap (HX-Download)</button> |
| 64 |
<div id="swap-result">swap target (should update on test 4)</div> |
| 65 |
|
| 66 |
<h2>5. UTF-8 filename (filename*=UTF-8''...)</h2> |
| 67 |
<p>Expected: downloads <code>café report.txt</code></p> |
| 68 |
<button hx-get="/utf8-filename" hx-swap="download">UTF-8 filename</button> |
| 69 |
|
| 70 |
<h2>6. No Content-Length header</h2> |
| 71 |
<p>Expected: downloads <code>unknown-size.txt</code>, progress shows bytes but no percent</p> |
| 72 |
<button hx-get="/no-content-length" hx-swap="download">No Content-Length</button> |
| 73 |
|
| 74 |
<h2>7. Indicators cleared after download</h2> |
| 75 |
<p>Expected: spinner visible during download, hidden after — button re-enabled</p> |
| 76 |
<button id="ind-btn" |
| 77 |
hx-get="/large-file" |
| 78 |
hx-swap="download" |
| 79 |
hx-indicator="#spinner" |
| 80 |
hx-disabled-elt="#ind-btn"> |
| 81 |
Download with indicator <span id="spinner" class="htmx-indicator">⏳ loading...</span> |
| 82 |
</button> |
| 83 |
</body> |
| 84 |
</html>`); |
| 85 |
}, |
| 86 |
|
| 87 |
'/htmx.js': async (req, res) => { |
| 88 |
res.writeHead(200, {'Content-Type': 'application/javascript'}); |
| 89 |
res.end(await fs.readFile('src/htmx.js')); |
| 90 |
}, |
| 91 |
|
| 92 |
'/ext/hx-download.js': async (req, res) => { |
| 93 |
res.writeHead(200, {'Content-Type': 'application/javascript'}); |
| 94 |
res.end(await fs.readFile('src/ext/hx-download.js')); |
| 95 |
}, |
| 96 |
|
| 97 |
d" |
| 98 |
'/small-file': (req, res) => { |
| 99 |
let body = 'Hello from hx-download!\n'; |
| 100 |
res.writeHead(200, { |
| 101 |
'Content-Type': 'text/plain', |
| 102 |
'Content-Length': Buffer.byteLength(body), |
| 103 |
'Content-Disposition': 'attachment; filename="hello.txt"' |
| 104 |
}); |
| 105 |
res.end(body); |
| 106 |
}, |
| 107 |
|
| 108 |
le |
| 109 |
'/large-file': (req, res) => { |
| 110 |
let chunkSize = 16 * 1024; |
| 111 |
let totalChunks = 32; |
| 112 |
let total = chunkSize * totalChunks; |
| 113 |
res.writeHead(200, { |
| 114 |
'Content-Type': 'application/octet-stream', |
| 115 |
'Content-Length': total, |
| 116 |
'Content-Disposition': 'attachment; filename="large.bin"' |
| 117 |
}); |
| 118 |
let sent = 0; |
| 119 |
let interval = setInterval(() => { |
| 120 |
res.write(Buffer.alloc(chunkSize, 0x42)); |
| 121 |
sent += chunkSize; |
| 122 |
if (sent >= total) { clearInterval(interval); res.end(); } |
| 123 |
}, 100); |
| 124 |
req.on('close', () => clearInterval(interval)); |
| 125 |
}, |
| 126 |
|
| 127 |
|
| 128 |
'/auto-detect': (req, res) => { |
| 129 |
let body = 'auto-detected download\n'; |
| 130 |
res.writeHead(200, { |
| 131 |
'Content-Type': 'text/plain', |
| 132 |
'Content-Length': Buffer.byteLength(body), |
| 133 |
'Content-Disposition': 'attachment; filename="auto.txt"' |
| 134 |
}); |
| 135 |
res.end(body); |
| 136 |
}, |
| 137 |
|
| 138 |
ap |
| 139 |
'/hx-download-header': (req, res) => { |
| 140 |
let body = '<em>swap also happened!</em>'; |
| 141 |
res.writeHead(200, { |
| 142 |
'Content-Type': 'text/html', |
| 143 |
'Content-Length': Buffer.byteLength(body), |
| 144 |
'HX-Download': '/hx-download-target' |
| 145 |
}); |
| 146 |
res.end(body); |
| 147 |
}, |
| 148 |
|
| 149 |
|
| 150 |
'/hx-download-target': (req, res) => { |
| 151 |
let body = 'redirected download content\n'; |
| 152 |
res.writeHead(200, { |
| 153 |
'Content-Type': 'text/plain', |
| 154 |
'Content-Length': Buffer.byteLength(body), |
| 155 |
'Content-Disposition': 'attachment; filename="redirected.txt"' |
| 156 |
}); |
| 157 |
res.end(body); |
| 158 |
}, |
| 159 |
|
| 160 |
|
| 161 |
'/utf8-filename': (req, res) => { |
| 162 |
let body = 'unicode filename test\n'; |
| 163 |
res.writeHead(200, { |
| 164 |
'Content-Type': 'text/plain', |
| 165 |
'Content-Length': Buffer.byteLength(body), |
| 166 |
'Content-Disposition': "attachment; filename*=UTF-8''caf%C3%A9%20report.txt" |
| 167 |
}); |
| 168 |
res.end(body); |
| 169 |
}, |
| 170 |
|
| 171 |
ts |
| 172 |
'/no-content-length': (req, res) => { |
| 173 |
let chunks = ['no ', 'content-', 'length\n'].map(s => Buffer.from(s)); |
| 174 |
res.writeHead(200, { |
| 175 |
'Content-Type': 'text/plain', |
| 176 |
'Content-Disposition': 'attachment; filename="unknown-size.txt"' |
| 177 |
}); |
| 178 |
let i = 0; |
| 179 |
let interval = setInterval(() => { |
| 180 |
res.write(chunks[i++]); |
| 181 |
if (i >= chunks.length) { clearInterval(interval); res.end(); } |
| 182 |
}, 200); |
| 183 |
req.on('close', () => clearInterval(interval)); |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
http.createServer(async (req, res) => { |
| 188 |
let pathname = new URL(req.url, 'http://localhost').pathname; |
| 189 |
let handler = routes[pathname]; |
| 190 |
if (handler) { |
| 191 |
try { await handler(req, res); } |
| 192 |
catch (e) { if (!res.headersSent) { res.writeHead(500); res.end('Error'); } } |
| 193 |
} else { |
| 194 |
res.writeHead(404); res.end('Not Found'); |
| 195 |
} |
| 196 |
}).listen(3001, () => console.log('http://localhost:3001')); |