From bacc15baaf48d33f52d2479830ed146d4d2b83eb Mon Sep 17 00:00:00 2001 From: Schluffe Date: Sat, 24 Oct 2026 14:15:48 +0200 Subject: [PATCH] Ask before downloading and show transfer progress Tracker codes now use 8 of [0-9a-zA-Z] instead of 8 digits. After both security checks the receiver sees the offered file and starts the download itself, the sender waits for it. Both sides show a progress bar of what the download manager has read. Copy buttons for the tracker code, offer and answer; the manual exchange is folded away below the tracker on both sides. Co-Authored-By: Claude Opus 5.5 --- e2e.spec.ts | 43 ++++++++++++++-- src/App.vue | 135 +++++++++++++++++++++++++++++++++++++++----------- src/main.ts | 97 ++++++++++++++++++++++++++++-------- src/style.css | 118 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 321 insertions(+), 72 deletions(-) diff --git a/e2e.spec.ts b/e2e.spec.ts index ceb99ee..49ad5f8 100644 --- a/e2e.spec.ts +++ b/e2e.spec.ts @@ -6,6 +6,8 @@ import { createServer, type ViteDevServer } from 'vite'; // Runs against the real public trackers and STUN servers, the defaults in src/main.ts. const BROWSERS: Record = { chromium, firefox }; +// Chromium needs a permission for the copy buttons, Firefox lets the click do it and rejects the name. +const PERMISSIONS: Record = { chromium: ['clipboard-write'], firefox: [] }; const SIZE = 5_000_000; // Opt-in with LARGE=1: beyond what fits comfortably in RAM and beyond the 2000 MiB the old OPFS // staging could store in incognito-like contexts. @@ -18,8 +20,12 @@ const WINDOW = 8 * 2 ** 20; // Each mode connects a sender page that already picked a file to a receiver page. const MODES: Record Promise> = { async manual(sender, receiver) { + await sender.click('#send-manual summary'); await sender.click('#create-offer'); await expect(sender.locator('#offer')).toHaveValue(/.+/, { timeout: 15_000 }); + await sender.click('#copy-offer'); + await expect(sender.locator('#copy-offer')).toHaveText('Copied'); + await receiver.click('#receive-manual summary'); await receiver.fill('#offer-input', await sender.inputValue('#offer')); await receiver.click('#create-answer'); await expect(receiver.locator('#answer')).toHaveValue(/.+/, { timeout: 15_000 }); @@ -48,7 +54,9 @@ const MODES: Record Promise> = { }, async tracker(sender, receiver) { await sender.click('#share'); - await expect(sender.locator('#send-code')).toHaveText(/^\d{8}$/); + await expect(sender.locator('#send-code')).toHaveText(/^[0-9a-zA-Z]{8}$/); + await sender.click('#copy-send-code'); + await expect(sender.locator('#copy-send-code')).toHaveText('Copied'); await expect(sender.locator('#log')).toContainText('waiting on'); await receiver.fill('#receive-code', (await sender.locator('#send-code').textContent())!); await receiver.click('#receive'); @@ -93,7 +101,7 @@ test.afterAll(async () => { async function open(name: string, sockets: string[], init?: () => void) { const browser = await BROWSERS[name]!.launch(); browsers.push(browser); - const page = await (await browser.newContext()).newPage(); + const page = await (await browser.newContext({ permissions: PERMISSIONS[name] })).newPage(); page.on('websocket', (ws) => sockets.push(ws.url())); if (init) await page.addInitScript(init); await page.goto(url); @@ -202,14 +210,14 @@ async function connect(mode: string, sender: Page, receiver: Page, source: strin } } -// Resolves with the download, which starts on its own as soon as the file is announced, and the -// receiver log at that moment. +// Resolves with the download, which starts once the receiver clicked Download, and the receiver log +// at that moment. const downloadStarts = (receiver: Page) => receiver .waitForEvent('download', { timeout: test.info().timeout }) .then(async (download) => ({ download, logAtStart: await log(receiver) })); -// Connects and confirms the security check on both sides. +// Connects, confirms the security check on both sides and starts the download. async function start(mode: string, sender: Page, receiver: Page, source: string) { await connect(mode, sender, receiver, source); const started = downloadStarts(receiver); @@ -217,6 +225,11 @@ async function start(mode: string, sender: Page, receiver: Page, source: string) await expect(sender.locator('#log')).toContainText("waiting for the receiver's security check"); await expect(receiver.locator('#log')).not.toContainText('receiving'); await receiver.click('#confirm'); + // Offered, but nothing is sent before the user asks for it. + await expect(receiver.locator('#incoming')).toContainText(source.split('/').pop()!); + await expect(sender.locator('#log')).toContainText('waiting for the receiver to download'); + await expect(receiver.locator('#log')).not.toContainText('receiving'); + await receiver.click('#download'); return started; } @@ -239,6 +252,8 @@ async function transfer( expect(await hash(await download.path())).toBe(await hash(source)); await expect(receiver.locator('#log')).toContainText('download complete'); await expect(sender.locator('#log')).toContainText('delivered'); + await expect(sender.locator('#send-progress')).toHaveJSProperty('value', 100); + await expect(receiver.locator('#receive-progress')).toHaveJSProperty('value', 100); // Removed hidden pages release the service worker for updates. await expect(receiver.locator('iframe')).toHaveCount(0); }); @@ -352,6 +367,20 @@ for (const name of Object.keys(BROWSERS)) { }); }); + test(`progress: ${name} follows the shared file, not a later pick`, async () => { + const [sender, receiver] = await Promise.all([open(name, []), open(name, [])]); + const source = test.info().outputPath('payload.bin'); + await writeFile(source, randomBytes(SIZE)); + + await withLogs(sender!, receiver!, async () => { + await start('offline', sender!, receiver!, source); + await expect(sender!.locator('#log')).toContainText('delivered'); + const other = { name: 'other.bin', mimeType: 'application/octet-stream', buffer: Buffer.alloc(2 * SIZE) }; + await sender!.setInputFiles('input[type=file]', other); + await expect(sender!.locator('#send-progress')).toHaveJSProperty('value', 100); + }); + }); + test(`check: ${name} accepts nothing before the receiver confirmed`, async () => { const [sender, receiver] = await Promise.all([open(name, [], recordChannels), open(name, [])]); const source = test.info().outputPath('payload.bin'); @@ -377,6 +406,7 @@ for (const name of Object.keys(BROWSERS)) { const started = downloadStarts(receiver!); await receiver!.click('#confirm'); + await receiver!.click('#download'); const { download } = await started; expect(download.suggestedFilename()).toBe('payload.bin'); expect(await hash(await download.path())).toBe(await hash(source)); @@ -406,6 +436,7 @@ for (const name of Object.keys(BROWSERS)) { const started = downloadStarts(receiver!); await sender!.click('#confirm'); await receiver!.click('#confirm'); + await receiver!.click('#download'); const { download } = await started; expect(await hash(await download.path())).toBe(await hash(source)); await expect(sender!.locator('#log')).toContainText('delivered'); @@ -434,6 +465,7 @@ for (const name of Object.keys(BROWSERS)) { const started = downloadStarts(receiver!); await sender!.click('#confirm'); await receiver!.click('#confirm'); + await receiver!.click('#download'); const { download } = await started; expect(await hash(await download.path())).toBe(await hash(source)); await expect(sender!.locator('#log')).toContainText('delivered'); @@ -491,6 +523,7 @@ for (const name of Object.keys(BROWSERS)) { ['sender', 'receiver'].map((profile) => BROWSERS[name]!.launchPersistentContext(test.info().outputPath(profile), { downloadsPath: test.info().outputPath('downloads'), + permissions: PERMISSIONS[name], }), ), ); diff --git a/src/App.vue b/src/App.vue index f5381c5..145f4cc 100644 --- a/src/App.vue +++ b/src/App.vue @@ -10,6 +10,8 @@ import { settings as coreSettings, type Check, type Events, + type Incoming, + type ReceiveEvents, } from './main'; const SETTINGS_KEY = 'hellcon:settings'; @@ -87,7 +89,8 @@ const check = ref(); const confirmed = ref(false); const log = ref([]); -const events: Events = { +// Shared by both sides, each adds its own progress. +const events: Omit = { log: (message) => log.value.push(`${new Date().toISOString().slice(11, 23)} ${message}`), check: (result) => { check.value = result; @@ -95,12 +98,47 @@ const events: Events = { }, }; +// Of the file in transfer, which on the sender isn't necessarily the one picked now. Undefined until +// a transfer starts. +type Progress = { read: number; size: number }; + +const sent = ref(); +const sendEvents: Events = { ...events, progress: (read, size) => (sent.value = { read, size }) }; + +const incoming = ref(); +const received = ref(); +const receiveEvents: ReceiveEvents = { + ...events, + progress: (read, size) => (received.value = { read, size }), + incoming: (file) => { + incoming.value = file; + received.value = undefined; + }, +}; + +const percent = ({ read, size }: Progress) => (size ? Math.floor((read / size) * 100) : 100); + function onConfirm() { confirmed.value = true; check.value?.confirm(); } -let acceptAnswer: ((answer: string) => Promise) | undefined; +// The text copied last, its button says so for a moment. +const copied = ref(''); +let copiedTimer: number | undefined; + +async function onCopy(text: string) { + try { + await navigator.clipboard.writeText(text); + } catch { + return events.log('copy failed, the browser blocked the clipboard'); + } + copied.value = text; + clearTimeout(copiedTimer); + copiedTimer = window.setTimeout(() => (copied.value = ''), 2000); +} + +let acceptAnswer:((answer: string) => Promise) | undefined; function onPick(event: Event) { file.value = (event.target as HTMLInputElement).files?.[0]; @@ -124,7 +162,8 @@ function formatSize(bytes: number) { } async function onCreateOffer() { - const result = await createOffer(file.value!, events); + sent.value = undefined; + const result = await createOffer(file.value!, sendEvents); offer.value = result.offer; acceptAnswer = result.accept; } @@ -134,15 +173,16 @@ function onAcceptAnswer() { } async function onCreateAnswer() { - answer.value = await answerOffer(offerInput.value, events); + answer.value = await answerOffer(offerInput.value, receiveEvents); } async function onShare() { - sendCode.value = await sendViaTracker(file.value!, events); + sent.value = undefined; + sendCode.value = await sendViaTracker(file.value!, sendEvents); } function onReceive() { - receiveViaTracker(receiveCode.value, events); + receiveViaTracker(receiveCode.value, receiveEvents); } @@ -189,46 +229,83 @@ function onReceive() { - -
-

Manual

- - -
+

+ + {{ percent(sent) }} % +

Tracker

-

Code: {{ sendCode }}

+
+ +
+ Manual +
+ + +
+

Receive

- -
-

Manual

- - -