Release 1.0.2: take a file picked again, refuse a dropped folder

Chromium fires no change event when the picker returns the file its input
already holds. The page kept the old one then, though the file may have
changed on disk, which makes the old one unreadable, or another file was
dropped in between, which the input never saw. The input is now emptied as
soon as its file is taken, so every pick fires. Its title names the file
taken, the browser's own tooltip would say no file is chosen.

A dropped folder came as a file of 0 B that can't be read and failed only
once the receiver downloaded it. It is refused with a note now, the file
picked before stays.

Both are covered for Chromium, whose protocol is the only one that reaches
the real picker and a drag from the file manager.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Fable 5.2 committed 2026-11-13 13:00:35 +01:00
1 parent 1b2696b6b4
commit 3c47047ddd
4 files changed
+105 -5

No files matched your search

+74 -1
View File
@@ -2,7 +2,7 @@ import { createCipheriv, createDecipheriv, createHash, hkdfSync, pbkdf2Sync, ran
import { createSocket, type Socket } from 'node:dgram';
import { once } from 'node:events';
import { createReadStream } from 'node:fs';
import { open as openFile, readFile, writeFile } from 'node:fs/promises';
import { mkdir, open as openFile, readFile, writeFile } from 'node:fs/promises';
import type { AddressInfo } from 'node:net';
import { networkInterfaces } from 'node:os';
import {
@@ -576,6 +576,79 @@ test('build: chromium sends to firefox from the production build under a path',
}
});
// The file picker and a drag from the file manager, the way the browser itself delivers them.
// setInputFiles fires a change event of its own whatever was picked, and no page can make up a
// dropped folder. Only Chromium's protocol reaches both, and it is the one that fires no change
// event for a file its input already holds.
async function fileManager(page: Page) {
const client = await page.context().newCDPSession(page);
const zone = page.locator('label:has(> [type=file])');
return {
zone,
async pick(path: string) {
const { result } = await client.send('Runtime.evaluate', {
expression: "document.querySelector('input[type=file]')",
});
await client.send('DOM.setFileInputFiles', { files: [path], objectId: result.objectId });
},
async drop(path: string) {
const box = (await zone.boundingBox())!;
const at = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
const data = { items: [], files: [path], dragOperationsMask: 1 };
for (const type of ['dragEnter', 'dragOver', 'drop'] as const) {
await client.send('Input.dispatchDragEvent', { type, ...at, data });
}
},
};
}
// Chromium fires no change event when the picker returns the file its input holds. The page has to
// take it anyway: the file may have changed on disk, what the page held of it can't be read anymore
// then, or the user dropped another one in between, which the input never saw.
test('pick: chromium takes a file picked again, changed on disk or after a drop', async () => {
const sender = await open('chromium');
const { zone, pick, drop } = await fileManager(sender);
const source = await payload(randomBytes(1000), 'picked.bin');
await pick(source);
await expect(zone).toHaveText('picked.bin 1000 B');
await writeFile(source, randomBytes(1536));
await pick(source);
await expect(zone).toHaveText('picked.bin 1.5 KB');
await drop(await payload(randomBytes(2048), 'dropped.bin'));
await expect(zone).toHaveText('dropped.bin 2.0 KB');
// The input stays empty, its tooltip would say no file is chosen.
await expect(zone.locator('[type=file]')).toHaveAttribute('title', 'dropped.bin');
await pick(source);
await expect(zone).toHaveText('picked.bin 1.5 KB');
});
// A folder comes as a file that can't be read. Unnoticed, it would fail only once the receiver
// downloads it.
test('pick: chromium refuses a dropped folder and keeps the file it had', async () => {
const sender = await open('chromium');
const { zone, pick, drop } = await fileManager(sender);
const folder = test.info().outputPath('holiday');
await mkdir(folder);
const refused = sender.locator('#not-a-file');
await drop(folder);
await expect(refused).toHaveText('holiday is a folder, only a single file can be sent');
await expect(zone).toContainText('Drop a file or click to choose');
await expect(sender.locator('#share')).toBeDisabled();
// A file ends the note, whether picked or dropped.
await pick(await payload(randomBytes(1000), 'picked.bin'));
await expect(zone).toHaveText('picked.bin 1000 B');
await drop(folder);
await expect(refused).toBeVisible();
await expect(zone).toContainText('picked.bin 1000 B');
await expect(sender.locator('#share')).toBeEnabled();
await drop(await payload(randomBytes(2048), 'dropped.bin'));
await expect(zone).toHaveText('dropped.bin 2.0 KB');
});
for (const name of Object.keys(BROWSERS)) {
test(`servers: ${name} skips invalid lines, a reload brings back the defaults`, async () => {
const page = await open(name);
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "hellcon",
"private": true,
"version": "1.0.1",
"version": "1.0.2",
"license": "MIT",
"type": "module",
"scripts": {
+24 -3
View File
@@ -242,15 +242,35 @@ async function attempt<T>(signal: AbortSignal, step: Promise<T>) {
}
}
// The name of a dropped folder, until a file takes its place.
const folder = ref('');
function take(picked: File) {
file.value = picked;
folder.value = '';
}
// The input is emptied right away: Chromium fires no change event for the file its input already
// holds, though that file may have changed on disk, or another one was dropped since, which the
// input never saw. An event without a file has nothing to take. Empty, the input's own tooltip says no
// file is chosen, so its title names the one taken.
function onPick(event: Event) {
file.value = (event.target as HTMLInputElement).files?.[0];
const input = event.target as HTMLInputElement;
const picked = input.files?.[0];
input.value = '';
if (picked) take(picked);
}
const dragging = ref(false);
// A folder comes as a file too, one that can't be read: refused here, it would fail only once the
// receiver downloads it. The file picked before stays.
function onDrop(event: DragEvent) {
dragging.value = false;
file.value = event.dataTransfer?.files[0] ?? file.value;
const dropped = event.dataTransfer?.files[0];
const entry = [...(event.dataTransfer?.items ?? [])].find((item) => item.kind === 'file')?.webkitGetAsEntry();
if (entry?.isDirectory) folder.value = entry.name;
else if (dropped) take(dropped);
}
function formatSize(bytes: number) {
@@ -432,12 +452,13 @@ document.addEventListener('visibilitychange', holdWakeLock);
@dragleave="dragging = false"
@drop.prevent="onDrop"
>
<input type="file" @change="onPick" />
<input type="file" :title="file?.name" @change="onPick" />
<template v-if="file">
<strong>{{ file.name }}</strong>
{{ formatSize(file.size) }}
</template>
<template v-else>Drop a file or click to choose</template>
<span v-if="folder" id="not-a-file" role="alert">{{ folder }} is a folder, only a single file can be sent</span>
</label>
<div v-if="side === 'send' && phase" :data-phase="phase">
<p v-if="shared !== file" id="send-file">{{ shared!.name }}</p>
+6
View File
@@ -411,6 +411,12 @@ label strong,
overflow-wrap: anywhere;
}
/* A dropped folder, refused */
#not-a-file {
color: var(--color-secondary-text);
overflow-wrap: anywhere;
}
/* How the session is doing, below the file on its side. Until the transfer starts a pill passes
through the bar, blue, or pink while it tries again after a failure. Then the transfer's progress,
green once done, muted once stopped, pink for an error. The line below says what. */