Own each transfer in a session and harden the protocol
Every operation (share, receive, manual offer or answer) now runs as one session tied to an AbortSignal. Starting another ends it: its connections, tracker sockets and timers close, and it can no longer touch the shared security check, progress or incoming file. Finished, failed and cancelled transfers close their connections, and the sender's tracker sockets close once the download starts. - A malformed or never-connecting tracker offer no longer blocks a share - The receiver validates the file offer and rejects excess bytes and data beyond the flow control window; sw.js checks name and size too - Failures show up in the card instead of disappearing into rejected promises: bad manual codes, unreachable trackers, lost connections - Primary buttons and pink text meet 4.5:1 contrast, the STUN switch shows keyboard focus again, the tracker Copy button matches the others - Split the core into src/core.ts, src/main.ts only mounts the app, which removes the main.ts <-> App.vue import cycle - Bring back the Gitea source link, write the README - Tests reuse one browser per worker with fresh contexts, fail on page errors, check cleanup after transfers, use a local fake STUN server and cover empty files, window boundaries, odd names, malformed messages, session replacement and the production build Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bacc15baaf
commit
2abd0b504d
7 files changed
+1442
-752
No files matched your search
+6
-4
@@ -1,6 +1,6 @@
|
||||
// Streams received files into the browser's download manager. It is the only script besides the app
|
||||
// because service workers can only be registered from a same-origin script URL. Registered with the
|
||||
// scope download/ by src/main.ts, it sees no other requests.
|
||||
// scope download/ by src/core.ts, it sees no other requests.
|
||||
|
||||
// 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.
|
||||
@@ -9,6 +9,8 @@ const downloads = new Map();
|
||||
const responses = new Map();
|
||||
|
||||
function open(id, { name, size }, port) {
|
||||
// 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 };
|
||||
const body = new ReadableStream(
|
||||
{
|
||||
@@ -20,7 +22,7 @@ function open(id, { name, size }, port) {
|
||||
const chunk = download.chunks.shift();
|
||||
if (chunk) {
|
||||
download.read += chunk.byteLength;
|
||||
// Every MiB, src/main.ts forwards it to the sender for its flow control window.
|
||||
// Every MiB, src/core.ts forwards it to the sender for its flow control window.
|
||||
if (download.read - download.reported >= 2 ** 20) port.postMessage((download.reported = download.read));
|
||||
return controller.enqueue(chunk);
|
||||
}
|
||||
@@ -52,7 +54,7 @@ function open(id, { name, size }, port) {
|
||||
// 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.
|
||||
self.addEventListener('message', ({ data, ports: [port] }) => {
|
||||
// Only keeps this worker alive while a transfer stalls, see KEEPALIVE in src/main.ts.
|
||||
// Only keeps this worker alive while a transfer stalls, see KEEPALIVE in src/core.ts.
|
||||
if (data.ping) return;
|
||||
if (port) return open(data.id, data, port);
|
||||
const download = downloads.get(data.id);
|
||||
@@ -69,7 +71,7 @@ self.addEventListener('message', ({ data, ports: [port] }) => {
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const id = new URL(event.request.url).pathname.split('/').pop();
|
||||
// download/ itself is the empty page src/main.ts keeps open to hold this worker during a transfer.
|
||||
// download/ itself is the empty page src/core.ts keeps open to hold this worker during a transfer.
|
||||
if (!id) return event.respondWith(new Response('<!doctype html>', { headers: { 'Content-Type': 'text/html; charset=utf-8' } }));
|
||||
const response = responses.get(id);
|
||||
responses.delete(id);
|
||||
|
||||
Reference in new issue
Block a user