Start from scratch

This commit is contained in:
Schluffe committed 2026-10-21 20:57:44 +02:00
1 parent 9f3f21884f
commit 24307eb96a
61 files changed
+906 -8959

No files matched your search

-70
View File
@@ -1,70 +0,0 @@
/* Only handles single-use download URLs within this directory. No file uploads or cache. */
const downloads = new Map();
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', event => event.waitUntil(self.clients.claim()));
self.addEventListener('message', event => {
const { type, id, name, size, stream } = event.data || {};
const port = event.ports[0];
if (type !== 'download' || !port || !/^[a-f0-9]{32}$/.test(id) ||
typeof name !== 'string' || name.length > 255 || /[\r\n/\\]/.test(name) ||
!Number.isSafeInteger(size) || size < 0 ||
!(stream instanceof ReadableStream) || downloads.has(id)) return;
const reader = stream.getReader();
let controller, finished = false, received = 0;
const finish = error => {
if (finished) return;
finished = true;
clearTimeout(timer);
downloads.delete(id);
if (error) {
controller?.error(error);
void reader.cancel(error.message).catch(() => {});
}
port.postMessage({ type: error ? 'error' : 'complete', message: error?.message });
port.close();
};
const timer = setTimeout(() => finish(new Error('Download did not start in time.')), 60000);
const body = new ReadableStream({
start(value) { controller = value; },
async pull() {
try {
const { done, value } = await reader.read();
if (finished) return;
if (done) {
if (received !== size) throw new Error('Incomplete download.');
controller.close(); finish();
} else {
if (!(value instanceof Uint8Array) || value.byteLength > 16 * 1024 || received + value.byteLength > size) throw new Error('Invalid download data.');
received += value.byteLength;
controller.enqueue(value);
}
} catch (error) { finish(error); }
},
cancel() { finish(new Error('Download canceled in the browser.')); },
}, { highWaterMark: 0 });
port.onmessage = event => {
if (event.data?.type === 'abort') finish(new Error('Transfer canceled.'));
};
downloads.set(id, { body, name, size, timer, port });
port.postMessage({ type: 'ready', url: new URL(`file-${id}`, self.registration.scope).href });
});
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
const base = new URL(self.registration.scope);
if (url.origin !== base.origin || !url.pathname.startsWith(base.pathname + 'file-')) return;
const id = url.pathname.slice((base.pathname + 'file-').length);
const download = downloads.get(id);
if (event.request.method !== 'GET' || !download) {
event.respondWith(new Response('Download expired.', { status: 404 })); return;
}
downloads.delete(id);
clearTimeout(download.timer);
download.port.postMessage({ type: 'started' });
event.respondWith(new Response(download.body, { headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename*=UTF-8''${encodeURIComponent(download.name.toWellFormed()).replace(/['()*]/g, char => '%' + char.charCodeAt(0).toString(16))}`,
'Content-Length': String(download.size),
'Cache-Control': 'no-store',
'X-Content-Type-Options': 'nosniff',
} }));
});