Add hosted service-worker streaming downloads

This commit is contained in:
Schluffe committed 2026-10-16 13:49:17 +02:00
1 parent a53022d717
commit 7856725c72
8 files changed
+344 -17

No files matched your search

+17 -7
View File
@@ -7,6 +7,7 @@ import NetworkSettings from './NetworkSettings.vue';
import { acceptAnswerCode, createAnswerCode, createOfferCode, createPeerConnection, fetchTurnOptions, iceConfiguration, getFingerprints, MAX_SIGNAL_LENGTH, STUN_PROVIDERS } from '../lib/webrtc.ts';
import { FILE_LIMIT, MEMORY_LIMIT, Transfer, validateMetadata } from '../lib/transfer.ts';
import { ConnectionDiagnostics } from '../lib/connection-diagnostics.ts';
import { createDownloadDestination, supportsStreamingDownload } from '../lib/download.ts';
type Step = 'idle' | 'gathering' | 'offer-ready' | 'answer-ready' | TransferState;
interface Session {
@@ -40,6 +41,8 @@ const defaultStun = location.protocol === 'file:' ? '' : import.meta.env.VITE_ST
const settings = ref<NetworkSettingsModel>({ stunChoice: defaultStun ? 'custom' : 'pagequit', customStun: defaultStun, allowTurn: false, managedTurn: !!iceEndpoint, turnUrl: '', username: '', credential: '' });
const managedCredentials = ref<IceOptions | null>(null);
const canSaveToDisk = typeof window.showSaveFilePicker === 'function';
const canStreamDownload = supportsStreamingDownload();
const streamedDownload = ref(false);
let active: Session | null = null;
let copyTimer: ReturnType<typeof setTimeout> | undefined;
const busy = computed(() => !['idle', 'closed', 'done'].includes(step.value));
@@ -53,7 +56,7 @@ const status = computed(() => ({
'choose-storage': 'Choose how to receive this file.',
sending: 'Sending…', receiving: 'Receiving…', saving: 'Saving file…',
'awaiting-receipt': 'Waiting for the receiver to confirm the complete file…',
done: props.sending ? 'The receiver confirmed the complete file.' : (downloadUrl.value ? 'File received. Save it below.' : 'File saved to disk.'),
done: props.sending ? 'The receiver confirmed the complete file.' : (downloadUrl.value ? 'File received. Save it below.' : streamedDownload.value ? 'File handed to your browser. Check Downloads for the final save result.' : 'File saved to disk.'),
closed: props.sending
? 'Create a new code, then ask the receiver for a new answer. Previous codes cannot reopen a closed connection.'
: 'Ask the sender for a new code and paste it above. Previous codes cannot reopen a closed connection.',
@@ -83,7 +86,7 @@ function reset(keepNetwork = false) {
inputCode.value = outputCode.value = error.value = '';
fps.value = incoming.value = null;
verified.value = copied.value = choosing.value = false;
progress.value = 0; step.value = 'idle';
progress.value = 0; step.value = 'idle'; streamedDownload.value = false;
}
function fail(session: Session, err: unknown) {
if (active !== session) return;
@@ -216,19 +219,24 @@ function verify() {
session.timer = setTimeout(() => { if (active === session && !session.transfer) fail(session, new Error('Connection timed out.')); }, 60000);
session.transfer?.verify();
}
async function acceptFile(toDisk: boolean) {
async function acceptFile(destination: 'disk' | 'memory' | 'download') {
const session = active;
if (!session?.transfer || !incoming.value || choosing.value || step.value !== 'choose-storage') return;
choosing.value = true; error.value = '';
let writable: FileDestination | undefined;
try {
if (toDisk) {
if (destination === 'download') {
writable = await createDownloadDestination(incoming.value, session.controller.signal);
if (active !== session) { await writable.abort(); return; }
} else if (destination === 'disk') {
if (!window.showSaveFilePicker) throw new Error('Direct-to-disk saving is unavailable.');
const handle = await window.showSaveFilePicker({ suggestedName: incoming.value.name });
if (active !== session) return;
writable = await handle.createWritable();
if (active !== session) { await writable.abort(); return; }
}
streamedDownload.value = destination === 'download';
session.diagnostics.record(`storage ${destination}`);
session.transfer.accept(writable);
} catch (err) {
if (writable) await writable.abort().catch(() => {});
@@ -292,9 +300,11 @@ onBeforeUnmount(() => { dispose(); revokeDownload(); clearTimeout(copyTimer); })
<div v-if="incoming && step === 'choose-storage'" class="incoming">
<p>{{ incoming.name }} — {{ formatBytes(incoming.size) }}</p>
<p class="status">Only accept files you expect. Encryption does not make their contents safe to open.</p>
<button v-if="canSaveToDisk" class="primary" :disabled="choosing" @click="acceptFile(true)">Choose destination & receive to disk</button>
<button :disabled="choosing || incoming.size > MEMORY_LIMIT" @click="acceptFile(false)">Receive in memory (up to 64 MiB)</button>
<p v-if="incoming.size > MEMORY_LIMIT && !canSaveToDisk" class="error">This browser cannot save directly to disk. Use a browser with file-system saving support or send a smaller file.</p>
<button v-if="canStreamDownload" class="primary" :disabled="choosing" @click="acceptFile('download')">Receive as download (up to 64 GiB)</button>
<p v-if="canStreamDownload" class="status">Allow the browser download and keep this page open until the transfer finishes. Check Downloads for the final save result.</p>
<button v-if="canSaveToDisk" class="primary" :disabled="choosing" @click="acceptFile('disk')">Choose destination & receive to disk</button>
<button :disabled="choosing || incoming.size > MEMORY_LIMIT" @click="acceptFile('memory')">Receive in memory (up to 64 MiB)</button>
<p v-if="incoming.size > MEMORY_LIMIT && !canSaveToDisk && !canStreamDownload" class="error">This browser cannot save directly to disk. Use a browser with file-system saving support or send a smaller file.</p>
</div>
<div v-if="progress > 0 || step === 'done'" class="progress" role="progressbar" :aria-valuenow="progress" aria-valuemin="0" aria-valuemax="100" aria-label="Transfer progress"><div :style="{ width: progress + '%' }"></div></div>
<p class="status" role="status">{{ status }}</p>