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:
SchluffeandClaude Opus 5.5 committed 2026-10-24 16:04:14 +02:00
1 parent bacc15baaf
commit 2abd0b504d
7 files changed
+1442 -752

No files matched your search

+70 -15
View File
@@ -12,7 +12,7 @@ import {
type Events,
type Incoming,
type ReceiveEvents,
} from './main';
} from './core';
const SETTINGS_KEY = 'hellcon:settings';
@@ -88,10 +88,16 @@ const receiveCode = ref('');
const check = ref<Check>();
const confirmed = ref(false);
const log = ref<string[]>([]);
// Of the session, shown in the card of its side.
const error = ref('');
// Shared by both sides, each adds its own progress.
const events: Omit<Events, 'progress'> = {
log: (message) => log.value.push(`${new Date().toISOString().slice(11, 23)} ${message}`),
error: (message) => {
events.log(message);
error.value = message;
},
check: (result) => {
check.value = result;
confirmed.value = false;
@@ -138,7 +144,40 @@ async function onCopy(text: string) {
copiedTimer = window.setTimeout(() => (copied.value = ''), 2000);
}
let acceptAnswer:((answer: string) => Promise<void>) | undefined;
let acceptAnswer: ((answer: string) => Promise<void>) | undefined;
// One session at a time, started by Share, Create offer, Receive or Create answer. Starting one ends
// the one before: its connections close and what it showed goes, so nothing else can write here.
let session: AbortController | undefined;
const side = ref<'send' | 'receive'>();
function begin(on: 'send' | 'receive') {
session?.abort();
session = new AbortController();
side.value = on;
error.value = '';
check.value = undefined;
confirmed.value = false;
sent.value = undefined;
incoming.value = undefined;
received.value = undefined;
sendCode.value = '';
offer.value = '';
answer.value = '';
acceptAnswer = undefined;
return session.signal;
}
// Resolves with the result of a session step, or undefined if it failed, which is shown, or if a
// newer session replaced it meanwhile.
async function attempt<T>(signal: AbortSignal, step: Promise<T>) {
try {
const result = await step;
return signal.aborted ? undefined : result;
} catch (failure) {
if (!signal.aborted) events.error(failure instanceof Error ? failure.message : String(failure));
}
}
function onPick(event: Event) {
file.value = (event.target as HTMLInputElement).files?.[0];
@@ -162,27 +201,34 @@ function formatSize(bytes: number) {
}
async function onCreateOffer() {
sent.value = undefined;
const result = await createOffer(file.value!, sendEvents);
const signal = begin('send');
const result = await attempt(signal, createOffer(file.value!, sendEvents, signal));
if (!result) return;
offer.value = result.offer;
acceptAnswer = result.accept;
}
// Part of the current session, a wrong answer can be corrected.
function onAcceptAnswer() {
acceptAnswer?.(answerInput.value);
error.value = '';
if (acceptAnswer) attempt(session!.signal, acceptAnswer(answerInput.value));
}
async function onCreateAnswer() {
answer.value = await answerOffer(offerInput.value, receiveEvents);
const signal = begin('receive');
const result = await attempt(signal, answerOffer(offerInput.value, receiveEvents, signal));
if (result) answer.value = result;
}
async function onShare() {
sent.value = undefined;
sendCode.value = await sendViaTracker(file.value!, sendEvents);
const signal = begin('send');
const code = await attempt(signal, sendViaTracker(file.value!, sendEvents, signal));
if (code) sendCode.value = code;
}
function onReceive() {
receiveViaTracker(receiveCode.value, receiveEvents);
const signal = begin('receive');
attempt(signal, receiveViaTracker(receiveCode.value, receiveEvents, signal));
}
</script>
@@ -230,9 +276,10 @@ function onReceive() {
<template v-else>Drop a file or click to choose</template>
</label>
<p v-if="sent">
<progress id="send-progress" max="100" :value="percent(sent)"></progress>
<progress id="send-progress" max="100" :value="percent(sent)" aria-label="Sent"></progress>
{{ percent(sent) }} %
</p>
<p v-if="error && side === 'send'" id="send-error" role="alert">{{ error }}</p>
<section>
<h3>Tracker</h3>
@@ -241,7 +288,7 @@ function onReceive() {
<label for="send-code">Code, give this to the receiver:</label>
<div role="group">
<output id="send-code">{{ sendCode }}</output>
<button id="copy-send-code" @click="onCopy(sendCode)">{{ copied === sendCode ? 'Copied' : 'Copy' }}</button>
<button id="copy-send-code" data-copy @click="onCopy(sendCode)">{{ copied === sendCode ? 'Copied' : 'Copy' }}</button>
</div>
</template>
</section>
@@ -256,7 +303,7 @@ function onReceive() {
<button id="copy-offer" data-copy @click="onCopy(offer)">{{ copied === offer ? 'Copied' : 'Copy' }}</button>
<label for="answer-input">Answer from the receiver:</label>
<textarea id="answer-input" v-model="answerInput"></textarea>
<button id="accept-answer" @click="onAcceptAnswer">Connect</button>
<button id="accept-answer" :disabled="!answerInput.trim()" @click="onAcceptAnswer">Connect</button>
</template>
</section>
</details>
@@ -273,9 +320,10 @@ function onReceive() {
<template v-else>The sender's file shows up here after the security check</template>
</div>
<p v-if="received">
<progress id="receive-progress" max="100" :value="percent(received)"></progress>
<progress id="receive-progress" max="100" :value="percent(received)" aria-label="Received"></progress>
{{ percent(received) }} %
</p>
<p v-if="error && side === 'receive'" id="receive-error" role="alert">{{ error }}</p>
<section>
<h3>Tracker</h3>
@@ -289,7 +337,7 @@ function onReceive() {
autocorrect="off"
spellcheck="false"
/>
<button id="receive" :disabled="!settings.trackers.length" @click="onReceive">Receive</button>
<button id="receive" :disabled="!settings.trackers.length || !receiveCode.trim()" @click="onReceive">Receive</button>
</div>
</section>
@@ -298,7 +346,7 @@ function onReceive() {
<section>
<label for="offer-input">Offer from the sender:</label>
<textarea id="offer-input" v-model="offerInput"></textarea>
<button id="create-answer" @click="onCreateAnswer">Create answer</button>
<button id="create-answer" :disabled="!offerInput.trim()" @click="onCreateAnswer">Create answer</button>
<template v-if="answer">
<label for="answer">Answer, send this back to the sender:</label>
<textarea id="answer" readonly :value="answer"></textarea>
@@ -332,4 +380,11 @@ function onReceive() {
<pre id="log">{{ log.join('\n') }}</pre>
</details>
</main>
<footer>
<a href="https://gitea.pagequit.net/Schluffe/HellCon" target="_blank" rel="noopener noreferrer">
<img src="/gitea.svg" alt="" width="20" height="20" />
Source code
</a>
</footer>
</template>