Version the protocol for 1.0, fail downloads the page abandoned

PROTOCOL (hellcon/1) goes in front of the PBKDF2 salt and names the data
channel, so different versions never meet via a tracker and the manual
receiver tells a sender on another protocol apart. The footer shows it, the
receiver's hints ask to compare it. Forks are asked to name their own.

public/sw.js and the page exchange a VERSION, a mismatch fails the download
with outdated. The worker fails a download after 20 s without a chunk or
ping from its page, else Firefox saves the truncated file of a closed tab as
complete.

The build uses relative links and works under any path. On https only wss:
trackers and ws: to this device are accepted, one the browser refuses to
open counts as lost. Also: a file read short stops the transfer, an offer
waiting on a lost tracker isn't answered, a share ended while its key was
derived opens no tracker, and tracker and receiver errors lose hidden
characters. The README lists what isn't planned and the phone limitation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Opus 5.5 committed 2026-10-30 15:10:09 +01:00
1 parent 44a8a72261
commit ac4d496a48
11 files changed
+399 -56

No files matched your search

+33 -8
View File
@@ -2,13 +2,36 @@
// because service workers can only be registered from a same-origin script URL. Registered with the
// scope download/ by src/download.ts, its counterpart in the page, it sees no other requests.
// Of the messages with the page, see VERSION in src/download.ts.
const VERSION = 1;
// Downloads by id while the page sends chunks: the queue, the stream controller, its wake up, the
// reply port and how many bytes the download manager has read.
const downloads = new Map();
// Responses by download id, until the download navigation fetches them. Each is served only once.
const responses = new Map();
// The page pings every KEEPALIVE while a transfer stalls, see src/download.ts. Silent for this long,
// it is gone, e.g. its tab was closed: Firefox would stop this worker 30 s after the last event and
// save the truncated file as complete. A failed stream leaves the download failed instead. A page
// that is still there, only late, e.g. throttled in the background, is told with silent.
const SILENCE = 20_000;
function open(id, { name, size }, port) {
function watch(id, download) {
clearTimeout(download.watchdog);
download.watchdog = setTimeout(() => {
download.controller.error(new Error('the page is gone'));
forget(id);
download.port.postMessage('silent');
}, SILENCE);
}
function forget(id) {
clearTimeout(downloads.get(id)?.watchdog);
downloads.delete(id);
}
function open(id, { version, name, size }, port) {
if (version !== VERSION) return port.postMessage('outdated');
// The page checks the sender's offer already. Anything else would throw below or lie to the browser.
if (typeof name !== 'string' || !Number.isSafeInteger(size) || size < 0) return port.postMessage('failed');
const download = { chunks: [], wake: () => {}, port, read: 0, reported: 0 };
@@ -30,18 +53,19 @@ function open(id, { name, size }, port) {
return controller.enqueue(chunk);
}
controller.close();
downloads.delete(id);
forget(id);
port.postMessage('finished');
},
// The user cancelled the download.
cancel: () => {
downloads.delete(id);
forget(id);
port.postMessage('cancelled');
},
},
{ highWaterMark: 0 },
);
downloads.set(id, download);
watch(id, download);
// RFC 8187 encoding, which leaves nothing in the name that could end the header value.
const encoded = encodeURIComponent(name.toWellFormed()).replace(
@@ -57,17 +81,18 @@ function open(id, { name, size }, port) {
port.postMessage('ready');
}
// The page posts the metadata with a reply port first, then every chunk as its own message and
// finally done (queued as the end marker null) or an error. Pings come in between.
// The page posts its version and the metadata with a reply port first, then every chunk as its own
// message and finally done (queued as the end marker null) or an error. Pings come in between.
self.addEventListener('message', ({ data, ports: [port] }) => {
// Only keeps this worker alive while a transfer stalls, see KEEPALIVE in src/download.ts.
if (data.ping) return;
if (port) return open(data.id, data, port);
const download = downloads.get(data.id);
if (!download) return;
watch(data.id, download);
// Only keeps this worker alive while a transfer stalls, see KEEPALIVE in src/download.ts.
if (data.ping) return;
if (data.error) {
download.controller.error(new Error(data.error));
downloads.delete(data.id);
forget(data.id);
download.port.postMessage('failed');
} else {
download.chunks.push(data.chunk ? new Uint8Array(data.chunk) : null);